Class: Acme::Smileage::Utils::Matcher
- Inherits:
-
Object
- Object
- Acme::Smileage::Utils::Matcher
- Defined in:
- lib/acme/smileage/utils/matcher.rb
Instance Attribute Summary collapse
-
#max_score ⇒ Object
Returns the value of attribute max_score.
Instance Method Summary collapse
- #index_by_amatch(str, targets) ⇒ Object
-
#initialize(str) ⇒ Matcher
constructor
A new instance of Matcher.
- #match(targets) ⇒ Object
- #normalize(str) ⇒ Object
Constructor Details
#initialize(str) ⇒ Matcher
Returns a new instance of Matcher.
12 13 14 15 |
# File 'lib/acme/smileage/utils/matcher.rb', line 12 def initialize(str) @str = str @max_score = 5 end |
Instance Attribute Details
#max_score ⇒ Object
Returns the value of attribute max_score.
10 11 12 |
# File 'lib/acme/smileage/utils/matcher.rb', line 10 def max_score @max_score end |
Instance Method Details
#index_by_amatch(str, targets) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/acme/smileage/utils/matcher.rb', line 50 def index_by_amatch(str, targets) matcher = Amatch::Sellers.new(str) if r = targets.map.with_index {|e,i| [i, matcher.match(e)] }.sort_by {|e| e[1] }[0] return r[0] if r[1] < self.max_score # score があまりにも大きかったら無視 end end |
#match(targets) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/acme/smileage/utils/matcher.rb', line 17 def match(targets) # 完全一致検索 if r = targets.find {|e| e == @str } return r end # 正規化 nstr = normalize(@str) ntargets = targets.map {|e| normalize(e) } # 大文字小文字を無視して完全一致検索 if i = ntargets.index {|e| e == nstr } return targets[i] end # 大文字小文字を無視して前方一致検索 (最低でも 2 文字は必要) if nstr.length > 1 and i = ntargets.index {|e| e.start_with?(nstr) } return targets[i] end # 編集距離であいまい検索 if i = index_by_amatch(@str, targets) return targets[i] end # 大文字小文字を無視して編集距離であいまい検索 if i = index_by_amatch(nstr, ntargets) return targets[i] end nil end |
#normalize(str) ⇒ Object
57 58 59 60 |
# File 'lib/acme/smileage/utils/matcher.rb', line 57 def normalize(str) return nil unless str Moji.hira_to_kata(Moji.normalize_zen_han(str)).downcase.gsub(/\s+/, "") end |