Skip to content

Commit

Permalink
improve relevancy calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasb committed Apr 10, 2012
1 parent 8f126b0 commit 0bcad93
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/completion.js
Expand Up @@ -21,16 +21,23 @@ var completion = (function() {
}

/** Calculates a very simple similarity value between a :query and a :string. The current
* implementation simply returns the length of the longest prefix of :query that is found within :str.
* implementation simply returns the cumulated length of query parts that are also in the string.
*/
self.calculateRelevancy = function(query, str) {
query = self.normalize(query);
str = self.normalize(str);
for (var i = query.length; i >= 0; --i) {
if (str.indexOf(query.slice(0, i)) >= 0)
return i;
var sum = 0;
// only iterate over slices of the query starting at an offset up to 10 to save resources
for (var start = 0; start < 10; ++start) {
for (var i = query.length; i >= start; --i) {
if (str.indexOf(query.slice(start, i)) >= 0) {
sum += i - start;
break;
}

}
}
return 0;
return sum;
}

/** Trims the size of the regex cache to the configured size using a FIFO algorithm. */
Expand Down

0 comments on commit 0bcad93

Please sign in to comment.