Skip to content

Commit 6aff78c

Browse files
committed
1.3.4 - now search results are sorted by similarity of given keyword
1 parent 2c4a220 commit 6aff78c

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

jquery.Thailand.js/src/jquery.Thailand.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,8 @@ $.Thailand = function (options) {
8484
});
8585
return expanded;
8686
},
87-
similar_text = function (first, second) {
88-
// Calculates the similarity between two strings
89-
// discuss at: http://phpjs.org/functions/similar_text
90-
91-
if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
92-
return 0;
93-
}
94-
87+
similar_text = function (first, second, percentage) {
88+
9589
first += '';
9690
second += '';
9791

@@ -102,8 +96,6 @@ $.Thailand = function (options) {
10296
secondLength = second.length,
10397
p, q, l, sum;
10498

105-
max = 0;
106-
10799
for (p = 0; p < firstLength; p++) {
108100
for (q = 0; q < secondLength; q++) {
109101
for (l = 0;
@@ -120,15 +112,28 @@ $.Thailand = function (options) {
120112

121113
if (sum) {
122114
if (pos1 && pos2) {
123-
sum += this.similar_text(first.substr(0, pos2), second.substr(0, pos2));
115+
sum += similar_text(first.substr(0, pos2), second.substr(0, pos2), false);
124116
}
125117

126118
if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
127-
sum += this.similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max));
119+
sum += similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max), false);
120+
}
121+
}
122+
123+
if(percentage === false){
124+
return sum;
125+
}else{
126+
if(first === second){
127+
return 100;
128+
}else{
129+
if(firstLength > secondLength){
130+
return Math.floor(sum/firstLength*100);
131+
}else{
132+
return Math.floor(sum/secondLength*100);
133+
}
128134
}
129135
}
130136

131-
return sum;
132137
}
133138

134139
// get zip binary
@@ -266,8 +271,15 @@ $.Thailand = function (options) {
266271
}
267272
return isUnique;
268273
}).map(function(self){ // give a likely score, will use to sort data later
269-
self.likely = [similar_text(str, self.d),similar_text(str, self.a),similar_text(str, self.p),similar_text(str, self.z),].sort().pop();
270-
})).select('*').orderBy('likely').fetch();
274+
self.likely = [
275+
similar_text(str, self.d)*5,
276+
similar_text(str, self.a.replace(/^/,''))*3,
277+
similar_text(str, self.p),
278+
similar_text(str, self.z)
279+
].sort(function(a,b){return a-b}).pop();
280+
281+
return self;
282+
})).select('*').orderBy('likely desc').fetch();
271283
} catch (e) {}
272284

273285
callback(possibles);

0 commit comments

Comments
 (0)