Skip to content

Commit

Permalink
1.3.4 - now search results are sorted by similarity of given keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
earthchie committed Apr 16, 2017
1 parent 2c4a220 commit 6aff78c
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions jquery.Thailand.js/src/jquery.Thailand.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,8 @@ $.Thailand = function (options) {
});
return expanded;
},
similar_text = function (first, second) {
// Calculates the similarity between two strings
// discuss at: http://phpjs.org/functions/similar_text

if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
return 0;
}

similar_text = function (first, second, percentage) {

first += '';
second += '';

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

max = 0;

for (p = 0; p < firstLength; p++) {
for (q = 0; q < secondLength; q++) {
for (l = 0;
Expand All @@ -120,15 +112,28 @@ $.Thailand = function (options) {

if (sum) {
if (pos1 && pos2) {
sum += this.similar_text(first.substr(0, pos2), second.substr(0, pos2));
sum += similar_text(first.substr(0, pos2), second.substr(0, pos2), false);
}

if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
sum += this.similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max));
sum += similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max), false);
}
}

if(percentage === false){
return sum;
}else{
if(first === second){
return 100;
}else{
if(firstLength > secondLength){
return Math.floor(sum/firstLength*100);
}else{
return Math.floor(sum/secondLength*100);
}
}
}

return sum;
}

// get zip binary
Expand Down Expand Up @@ -266,8 +271,15 @@ $.Thailand = function (options) {
}
return isUnique;
}).map(function(self){ // give a likely score, will use to sort data later
self.likely = [similar_text(str, self.d),similar_text(str, self.a),similar_text(str, self.p),similar_text(str, self.z),].sort().pop();
})).select('*').orderBy('likely').fetch();
self.likely = [
similar_text(str, self.d)*5,
similar_text(str, self.a.replace(/^/,''))*3,
similar_text(str, self.p),
similar_text(str, self.z)
].sort(function(a,b){return a-b}).pop();

return self;
})).select('*').orderBy('likely desc').fetch();
} catch (e) {}

callback(possibles);
Expand Down

0 comments on commit 6aff78c

Please sign in to comment.