class NormalizeCountry::Scanner
Public Class Methods
new(options = nil)
click to toggle source
# File lib/normalize_country/scanner.rb, line 28 def initialize(options = nil) options ||= {} @to = options[:to] || NormalizeCountry.to @table = lookup_table(options[:from] || NormalizeCountry.formats) # need aliases!!! end
Public Instance Methods
convert(text)
click to toggle source
# File lib/normalize_country/scanner.rb, line 35 def convert(text) s = Tokenizer.new(text) matches = [] stack = [] match_position = @table while !s.end? word = s.peek.downcase if !match_position[word] s.scan next end stack << s.scan alternatives = match_position[stack[-1].downcase] peek = s.peek if alternatives && peek && alternatives[peek.downcase] match_position = alternatives next end if match_position[stack[-1].downcase][:match] text = stack.join(" ") matches << { :matched => text, :converted => NormalizeCountry(text, :to => @to) } end stack.clear match_position = @table end matches end
Private Instance Methods
lookup_table(from_formats)
click to toggle source
# File lib/normalize_country/scanner.rb, line 73 def lookup_table(from_formats) table = {} NormalizeCountry::Countries.values.uniq.each_with_object(table) do |country, o| # country.name # eq all # no way to get aliases from_formats.each do |format| name = country[format] next unless name head = o parts = name.split(/[[:space:]]+/) parts.each_with_index do |word, i| # options[:case_sensitive_codes] = [x,y] # or true # options[:case_sensitive_formats] = [x,y] # options[:case_sensitive_formats] = true # alpha2, alpha3, fifa, ioc word.downcase! #unless parts.size == 1 && ABBRV_FORMATS.include?(format) # if head[word] # if i == parts.size - 1 # head[word][:match] = true # else # head = head[word] # end # else # head[word] = {} # if i == parts.size - 1 # head[word][:match] = true # else # head = head[word] # end # end head[word] = {} unless head[word] if i == parts.size - 1 head[word][:match] = true else head = head[word] end end end o end end