This repository was archived by the owner on Jul 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
89 lines (78 loc) · 2.58 KB
/
utils.js
File metadata and controls
89 lines (78 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function getBiggerSubstrEqual(s1,s2) {
//assert (s1.length >1 && s2.length > 1);
var bigger, smaller;
if(s1.length > s2.length) {
bigger = s1;
smaller = s2;
} else {
bigger = s2;
smaller = s1;
}
for(var len = smaller.length; len >= 1; len --) {//TODO
for( var i = 0; i <= smaller.length - len; i++) {
var strToSearch = smaller.substr(i, len);
var pos = bigger.search(strToSearch); //pode ser também indexOf;
if(pos >= 0) {
return {
equalPart: strToSearch,
prefixBigger: bigger.substr(0, pos),
suffixBigger: bigger.substr(pos + strToSearch.length),
prefixSmaller: smaller.substr(0, i),
suffixSmaller: smaller.substr(i + strToSearch.length)
}
}
}
}
return null;
}
function compareStrings(str1, str2) {
var biggerSubstrComp = getBiggerSubstrEqual(str1,str2);
if(biggerSubstrComp == null) {
return 0;
}
var result = biggerSubstrComp.equalPart.length;
/*TODO ver quando chegar a 1 caractere*/
result += compareStrings(biggerSubstrComp.prefixBigger, biggerSubstrComp.prefixSmaller);
result += compareStrings(biggerSubstrComp.suffixBigger, biggerSubstrComp.suffixSmaller);
return result;
}
/*compare searching substrings matchs. 1 for totally equal, zero for totally different*/
function rateSimilarityStr(str1,str2) {
return (compareStrings(str1,str2)*2)/(str1.length + str2.length);
}
function simplifyName(name) {
return name.toUpperCase().trim()
.replace(/[ÁÀÃÂÄ]/g, 'A')
.replace(/[ÉÈÊË]/g, 'E')
.replace(/[ÍÌÎÏ]/g, 'I')
.replace(/[ÓÒÕÔÖ]/g, 'O')
.replace(/[ÚÙÛÜ]/g, 'U')
.replace(/[Ç]/g, 'C')
.replace(/[-]/g, ' ')
//.replace(/[Z]/g, 'S') /* FUNCIONA PRA MAIORIA DOS CASOS, MAS É MAIS AGRESSIVO. Pega casos que se troca luiS por luiZ por ex*/
.replace(/[`]/g, "'");
}
function findBestMatch(name, arr) {
if(arr.length == 0 ) {
return null;
}
var bestMatch = {
str: arr[0],
rate: rateSimilarityStr(name, arr[0])
};
for(var i = 1; i < arr.length; i++) {
var rate = rateSimilarityStr(name, arr[i]);
if(rate > bestMatch.rate) {
bestMatch = {
str: arr[i],
rate: rate
}
}
}
return bestMatch;
}
module.exports = {
rateSimilarityStr: rateSimilarityStr,
simplifyName: simplifyName,
findBestMatch: findBestMatch
}