diff options
| author | Jez Ng | 2012-11-03 10:12:48 -0700 |
|---|---|---|
| committer | Jez Ng | 2012-11-03 10:12:48 -0700 |
| commit | c5744b9ac228d2943a1ba77cc3e8fc91aeab85a7 (patch) | |
| tree | 0c46c2213c0978141049f3fc481742c86f746364 | |
| parent | c492f651970437b9f3a9c90d424db31f19225d73 (diff) | |
| parent | d9e0ba4f0a06b5f5543a4398434399497573336d (diff) | |
| download | vimium-c5744b9ac228d2943a1ba77cc3e8fc91aeab85a7.tar.bz2 | |
Merge pull request #702 from smblott-github/relevancy-fix
Improve consistency and fix bug in relevancy calculation.
| -rw-r--r-- | background_scripts/completion.coffee | 16 | ||||
| -rw-r--r-- | tests/unit_tests/completion_test.coffee | 19 |
2 files changed, 28 insertions, 7 deletions
diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 79f31073..cf946b06 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -249,12 +249,16 @@ class MultiCompleter # Utilities which help us compute a relevancy score for a given item. RankingUtils = - # Whether the given URL or title match any one of the query terms. This is used to prune out irrelevant - # suggestions before we try to rank them. - matches: (queryTerms, url, title) -> + # Whether the given things (usually URLs or titles) match any one of the query terms. + # This is used to prune out irrelevant suggestions before we try to rank them, and for calculating word relevancy. + # Every term must match at least one thing. + matches: (queryTerms, things...) -> for term in queryTerms regexp = RegexpCache.get(term) - return false unless title.match(regexp) || url.match(regexp) + matchedTerm = false + for thing in things + matchedTerm = matchedTerm || (thing?.match && thing.match regexp) + return false unless matchedTerm true # Returns a number between [0, 1] indicating how often the query terms appear in the url and title. @@ -264,8 +268,8 @@ RankingUtils = titleScore = 0.0 for term in queryTerms queryLength += term.length - urlScore += 1 if url.indexOf(term) >= 0 - titleScore += 1 if title && title.indexOf(term) >= 0 + urlScore += 1 if RankingUtils.matches [term], url + titleScore += 1 if RankingUtils.matches [term], title urlScore = urlScore / queryTerms.length urlScore = urlScore * RankingUtils.normalizeDifference(queryLength, url.length) if title diff --git a/tests/unit_tests/completion_test.coffee b/tests/unit_tests/completion_test.coffee index f3ed4e61..32b62a28 100644 --- a/tests/unit_tests/completion_test.coffee +++ b/tests/unit_tests/completion_test.coffee @@ -154,8 +154,25 @@ context "suggestions", context "RankingUtils", should "do a case insensitive match", -> - assert.isTrue RankingUtils.matches(["maRiO"], "MARIO", "MARIo") + assert.isTrue RankingUtils.matches(["aRi"], "MARIO", "MARio") + should "do a case insensitive match on full term", -> + assert.isTrue RankingUtils.matches(["MaRiO"], "MARIO", "MARio") + + should "do a case insensitive match on more than just two terms", -> + assert.isTrue RankingUtils.matches(["aRi"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio") + + should "do case insensitive word relevancy (matching)", -> + assert.isTrue RankingUtils.wordRelevancy(["aRi"], "MARIO", "MARio") > 0.0 + + should "do case insensitive word relevancy (not matching)", -> + assert.isTrue RankingUtils.wordRelevancy(["DOES_NOT_MATCH"], "MARIO", "MARio") == 0.0 + + should "every term must match at least one thing (matching)", -> + assert.isTrue RankingUtils.matches(["cat", "dog"], "catapult", "hound dog") + + should "every term must match at least one thing (not matching)", -> + assert.isTrue not RankingUtils.matches(["cat", "dog", "wolf"], "catapult", "hound dog") # A convenience wrapper around completer.filter() so it can be called synchronously in tests. filterCompleter = (completer, queryTerms) -> |
