History

Improve search fuzzy matching

Thibaut committed 11 years ago

Commit a6774171 parent cca1b371 file · +18 −5Browse files

Unified · Split

assets/javascripts/app/searcher.coffee+18−5
@@ -13,6 +13,8 @@ fuzzyRegexp = # query fuzzy regexp · 12 unmodified lines
1313 index = # position of the query in the string being matched
1414 lastIndex = # last position of the query in the string being matched
1515 match = # regexp match data
16+matchIndex =
17+matchLength =
1618 score = # score for the current match
1719 separators = # counter
1820 i = null # cursor
@@ -74,16 +76,27 @@ return Math.max 1, score · 55 unmodified lines
7476 `function fuzzyMatch() {`
7577 return if valueLength <= queryLength or value.indexOf(query) >= 0
7678 return unless match = fuzzyRegexp.exec(value)
79+matchIndex = match.index
80+matchLength = match[0].length
81+score = scoreFuzzyMatch()
82+if match = fuzzyRegexp.exec(value.slice(i = value.lastIndexOf(SEPARATOR) + 1))
83+ matchIndex = i + match.index
84+ matchLength = match[0].length
85+ return Math.max(score, scoreFuzzyMatch())
86+else
87+ return score
88+`}`
7789
90+`function scoreFuzzyMatch() {`
7891 # When the match is at the beginning of the string or preceded by a dot.
79−if match.index is 0 or value.charAt(match.index - 1) is SEPARATOR
80− return Math.max 66, 100 - match[0].length
92+if matchIndex is 0 or value.charAt(matchIndex - 1) is SEPARATOR
93+ return Math.max 66, 100 - matchLength
8194 # When the match is at the end of the string.
82−else if match.index + match[0].length is valueLength
83− return Math.max 33, 67 - match[0].length
95+else if matchIndex + matchLength is valueLength
96+ return Math.max 33, 67 - matchLength
8497 # When the match is in the middle of the string.
8598 else
86− return Math.max 1, 34 - match[0].length
99+ return Math.max 1, 34 - matchLength
87100 `}`
88101
89102 #