Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Fix scoring to favor exact substring matches
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonwamboldt committed Apr 25, 2015
1 parent 12eaa82 commit c91837a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions spec/filter-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ rootPath = (segments...) ->

describe "filtering", ->
it "returns an array of the most accurate results", ->
candidates = ['Gruntfile','filter', 'bile', null, '', undefined]
expect(filter(candidates, 'file')).toEqual ['filter', 'Gruntfile']
candidates = ['Gruntfile', 'filter', 'bile', null, '', undefined]
expect(filter(candidates, 'file')).toEqual ['Gruntfile', 'filter']

describe "when the maxResults option is set", ->
it "limits the results to the result size", ->
candidates = ['Gruntfile', 'filter', 'bile']
expect(bestMatch(candidates, 'file')).toBe 'filter'
expect(bestMatch(candidates, 'file')).toBe 'Gruntfile'

describe "when the entries contains slashes", ->
it "weighs basename matches higher", ->
Expand Down
21 changes: 20 additions & 1 deletion src/scorer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ exports.score = (string, query) ->
# Return a perfect score if the file name itself matches the query.
return 1 if queryIsLastPathSegment(string, query)

# Return a near perfect score if the query is a substring match
baseScore = 0
substrIndex = string.indexOf(query)
substrIndexi = string.toLowerCase().indexOf(query.toLowerCase())

if substrIndex >= 0
baseScore = 0.80
baseScore = 0.95 if substrIndex == 0
baseScore = 0.90 if string[substrIndex - 1] == PathSeparator
else if substrIndexi >= 0
baseScore = 0.78
baseScore = 0.93 if substrIndexi == 0
baseScore = 0.88 if string[substrIndexi - 1] == PathSeparator

totalCharacterScore = 0
queryLength = query.length
stringLength = string.length
Expand Down Expand Up @@ -82,7 +96,12 @@ exports.score = (string, query) ->
totalCharacterScore += characterScore

queryScore = totalCharacterScore / queryLength
((queryScore * (queryLength / stringLength)) + queryScore) / 2
queryScore = ((queryScore * (queryLength / stringLength)) + queryScore) / 2

if baseScore != 0
queryScore = baseScore + (queryScore * 0.05)

queryScore

queryIsLastPathSegment = (string, query) ->
if string[string.length - query.length - 1] is PathSeparator
Expand Down

0 comments on commit c91837a

Please sign in to comment.