From 0bcad9357ad0a774ce2190f1e990dcdd17762c2a Mon Sep 17 00:00:00 2001 From: Niklas Baumstark Date: Sat, 21 Jan 2012 13:36:03 +0100 Subject: improve relevancy calculation --- lib/completion.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/completion.js b/lib/completion.js index d6a901ae..d0c798c8 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -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. */ -- cgit v1.2.3