Class: Money::Currency::Heuristics::Analyzer
- Inherits:
-
Object
- Object
- Money::Currency::Heuristics::Analyzer
- Defined in:
- lib/money/currency/heuristics.rb
Instance Attribute Summary collapse
-
#currencies ⇒ Object
Returns the value of attribute currencies.
-
#search_tree ⇒ Object
readonly
Returns the value of attribute search_tree.
-
#str ⇒ Object
Returns the value of attribute str.
-
#words ⇒ Object
readonly
Returns the value of attribute words.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(str, search_tree) ⇒ Analyzer
constructor
A new instance of Analyzer.
- #prepare_reply ⇒ Object
- #process ⇒ Object
- #search_by_iso_code ⇒ Object
- #search_by_name ⇒ Object
- #search_by_symbol ⇒ Object
Constructor Details
#initialize(str, search_tree) ⇒ Analyzer
Returns a new instance of Analyzer.
75 76 77 78 79 |
# File 'lib/money/currency/heuristics.rb', line 75 def initialize str, search_tree @str = (str||'').dup @search_tree = search_tree @currencies = [] end |
Instance Attribute Details
#currencies ⇒ Object
Returns the value of attribute currencies.
73 74 75 |
# File 'lib/money/currency/heuristics.rb', line 73 def currencies @currencies end |
#search_tree ⇒ Object (readonly)
Returns the value of attribute search_tree.
72 73 74 |
# File 'lib/money/currency/heuristics.rb', line 72 def search_tree @search_tree end |
#str ⇒ Object
Returns the value of attribute str.
73 74 75 |
# File 'lib/money/currency/heuristics.rb', line 73 def str @str end |
#words ⇒ Object (readonly)
Returns the value of attribute words.
72 73 74 |
# File 'lib/money/currency/heuristics.rb', line 72 def words @words end |
Instance Method Details
#format ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/money/currency/heuristics.rb', line 92 def format str.gsub!(/[\r\n\t]/,'') str.gsub!(/[0-9][\.,:0-9]*[0-9]/,'') str.gsub!(/[0-9]/, '') str.downcase! @words = str.unaccent.split @words.each {|word| word.chomp!('.'); word.chomp!(',') } end |
#prepare_reply ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/money/currency/heuristics.rb', line 142 def prepare_reply codes = currencies.map do |currency| currency[:iso_code] end codes.uniq! codes.sort! codes end |
#process ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/money/currency/heuristics.rb', line 81 def process format return [] if str.empty? search_by_symbol search_by_iso_code search_by_name prepare_reply end |
#search_by_iso_code ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/money/currency/heuristics.rb', line 109 def search_by_iso_code words.each do |word| if found = search_tree[:by_iso_code][word] currencies.concat(found) end end end |
#search_by_name ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/money/currency/heuristics.rb', line 117 def search_by_name # remember, the search tree by name is a construct of branches and leaf! # We need to try every combination of words within the sentence, so we # end up with a x^2 equation, which should be fine as most names are either # one or two words, and this is multiplied with the words of given sentence search_words = words.dup while search_words.length > 0 root = search_tree[:by_name] search_words.each do |word| if root = root[word] if root[:value] currencies.concat(root[:value]) end else break end end search_words.delete_at(0) end end |
#search_by_symbol ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/money/currency/heuristics.rb', line 101 def search_by_symbol words.each do |word| if found = search_tree[:by_symbol][word] currencies.concat(found) end end end |