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
13index = # position of the query in the string being matched13index = # position of the query in the string being matched
14lastIndex = # last position of the query in the string being matched14lastIndex = # last position of the query in the string being matched
15match = # regexp match data15match = # regexp match data
16matchIndex =
17matchLength =
16score = # score for the current match18score = # score for the current match
17separators = # counter19separators = # counter
18i = null # cursor20i = null # cursor
@@ -74,16 +76,27 @@ return Math.max 1, score · 55 unmodified lines
74`function fuzzyMatch() {`76`function fuzzyMatch() {`
75return if valueLength <= queryLength or value.indexOf(query) >= 077return if valueLength <= queryLength or value.indexOf(query) >= 0
76return unless match = fuzzyRegexp.exec(value)78return unless match = fuzzyRegexp.exec(value)
79matchIndex = match.index
80matchLength = match[0].length
81score = scoreFuzzyMatch()
82if 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())
86else
87 return score
88`}`
77 89
90`function scoreFuzzyMatch() {`
78# When the match is at the beginning of the string or preceded by a dot.91# When the match is at the beginning of the string or preceded by a dot.
79if match.index is 0 or value.charAt(match.index - 1) is SEPARATOR92if matchIndex is 0 or value.charAt(matchIndex - 1) is SEPARATOR
80 return Math.max 66, 100 - match[0].length93 return Math.max 66, 100 - matchLength
81# When the match is at the end of the string.94# When the match is at the end of the string.
82else if match.index + match[0].length is valueLength95else if matchIndex + matchLength is valueLength
83 return Math.max 33, 67 - match[0].length96 return Math.max 33, 67 - matchLength
84# When the match is in the middle of the string.97# When the match is in the middle of the string.
85else98else
86 return Math.max 1, 34 - match[0].length99 return Math.max 1, 34 - matchLength
87`}`100`}`
88 101
89#102#