Module: String::LiquidMetal
- Defined in:
- lib/string_scorer/scorers/liquidmetal.rb
Overview
Algorithm from github.com/rmm5t/liquidmetal/blob/master/liquidmetal.js
Constant Summary collapse
- SCORE_NO_MATCH =
0.0
- SCORE_MATCH =
1.0
- SCORE_TRAILING =
0.8
- SCORE_TRAILING_BUT_STARTED =
0.9
- SCORE_BUFFER =
0.85
Class Method Summary collapse
- .fill_array(array, value, from = 0, to = array.size) ⇒ Object
- .new_word?(string, index) ⇒ Boolean
- .score(string, abbreviation) ⇒ Object
- .score_array(string, abbreviation) ⇒ Object
- .uppercase?(string, index) ⇒ Boolean
Class Method Details
.fill_array(array, value, from = 0, to = array.size) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/string_scorer/scorers/liquidmetal.rb', line 66 def fill_array(array, value, from=0, to=array.size) from = 0 if from < 0 to = 0 if to < 0 size = 1 + (to - from) array[from..to] = Array.new(size, value) array end |
.new_word?(string, index) ⇒ Boolean
56 57 58 59 |
# File 'lib/string_scorer/scorers/liquidmetal.rb', line 56 def new_word?(string, index) c = string[index - 1] (index == 0 || c==32 || c == 9) end |
.score(string, abbreviation) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/string_scorer/scorers/liquidmetal.rb', line 15 def score(string, abbreviation) # Short circuits return SCORE_TRAILING if abbreviation.length == 0 return SCORE_NO_MATCH if abbreviation.length > string.length return SCORE_MATCH if abbreviation == string scores = score_array(string, abbreviation); scores.inject(0) {|v,m| v + m} / scores.size end |
.score_array(string, abbreviation) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/string_scorer/scorers/liquidmetal.rb', line 25 def score_array(string, abbreviation) scores = Array.new(string.length) lower = string.downcase chars = abbreviation.downcase.split("") last_index = -1 started = false chars.each do |c| index = lower.index(c, last_index + 1) return fill_array(scores, SCORE_NO_MATCH) if index.nil? started = true if index == 0 if new_word?(string, index) scores[index-1] = 1; fill_array(scores, SCORE_BUFFER, last_index + 1, index - 1) elsif uppercase?(string, index) fill_array(scores, SCORE_BUFFER, last_index + 1, index) else fill_array(scores, SCORE_NO_MATCH, last_index + 1, index) end scores[index] = SCORE_MATCH last_index = index end trailing_score = (started ? SCORE_TRAILING_BUT_STARTED : SCORE_TRAILING) fill_array(scores, trailing_score, last_index + 1) scores end |
.uppercase?(string, index) ⇒ Boolean
61 62 63 64 |
# File 'lib/string_scorer/scorers/liquidmetal.rb', line 61 def uppercase?(string, index) c = string[index] (65..90).include?(c) end |