Class: NormalizeCountry::Scanner
- Inherits:
-
Object
- Object
- NormalizeCountry::Scanner
- Defined in:
- lib/normalize_country/scanner.rb
Instance Method Summary collapse
- #convert(text) ⇒ Object
-
#initialize(options = nil) ⇒ Scanner
constructor
A new instance of Scanner.
Constructor Details
#initialize(options = nil) ⇒ Scanner
Returns a new instance of Scanner.
29 30 31 32 33 34 |
# File 'lib/normalize_country/scanner.rb', line 29 def initialize( = nil) ||= {} @to = [:to] || NormalizeCountry.to @table = lookup_table([:from] || NormalizeCountry.formats) # need aliases!!! end |
Instance Method Details
#convert(text) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/normalize_country/scanner.rb', line 36 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 |