Parent

Money::Currency::Heuristics::Analyzer

Attributes

currencies[RW]
search_tree[R]
str[RW]
words[R]

Public Class Methods

new(str, search_tree) click to toggle source
# File lib/money/currency/heuristics.rb, line 74
def initialize str, search_tree
  @str = (str||'').dup
  @search_tree = search_tree
  @currencies = []
end

Public Instance Methods

format() click to toggle source
# File lib/money/currency/heuristics.rb, line 91
def format
  str.gsub!(/[\r\n\t]/,'')
  str.gsub!(/[0-9][\.,:0-9]*[0-9]/,'')
  str.gsub!(/[0-9]/, '')
  str.downcase!
  @words = str.split
  @words.each {|word| word.chomp!('.'); word.chomp!(',') }
end
prepare_reply() click to toggle source
# File lib/money/currency/heuristics.rb, line 141
def prepare_reply
  codes = currencies.map do |currency|
    currency[:iso_code]
  end
  codes.uniq!
  codes.sort!
  codes
end
process() click to toggle source
# File lib/money/currency/heuristics.rb, line 80
def process
  format
  return [] if str.empty?

  search_by_symbol
  search_by_iso_code
  search_by_name

  prepare_reply
end
search_by_iso_code() click to toggle source
# File lib/money/currency/heuristics.rb, line 108
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() click to toggle source
# File lib/money/currency/heuristics.rb, line 116
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() click to toggle source
# File lib/money/currency/heuristics.rb, line 100
def search_by_symbol
  words.each do |word|
    if found = search_tree[:by_symbol][word]
      currencies.concat(found)
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.