Module: Herbalist

Defined in:
lib/herbalist.rb,
lib/herbalist/version.rb,
lib/herbalist/herbalist.rb

Defined Under Namespace

Classes: Tag, Token

Constant Summary collapse

VERSION =
"0.2.0"
POSSIBLE_UNITS =

collect up all the possible unit types that Alchemist can handle

Alchemist.library.categories.map{|c| Alchemist.library.unit_names c}.flatten.uniq
MULTIWORD_UNITS =
POSSIBLE_UNITS.collect{|u| u.to_s}.grep(/_/)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.debugObject

Returns the value of attribute debug.



19
20
21
# File 'lib/herbalist.rb', line 19

def debug
  @debug
end

Class Method Details

.parse(text) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/herbalist/herbalist.rb', line 7

def parse(text)
  text = text.dup
  puts "TEXT: #{text}" if Herbalist.debug
  @tokens = self.tokenize(text).select { |token| token.tagged? }
  puts "TOKENS: #{@tokens}" if Herbalist.debug
  
  return nil unless @tokens.length>1
  
  # at the moment all we handle is a number followed by a unit
  last_number = nil
  result = nil
  @tokens.each do |token|
    if last_number && (unit=token.get_tag(:unit))
      result = last_number.send(unit.value)
      break
    elsif (num=token.get_tag(:number))
      last_number = num.value
    end
  end
  
  return result
end

.tokenize(text) ⇒ Object



30
31
32
33
34
35
# File 'lib/herbalist/herbalist.rb', line 30

def tokenize(text)
  # cleanup the string before tokenizing
  text = normalize(text)
  @tokens = text.split(' ').collect { |word| Token.new(word) }
  @tokens = Tag.scan(@tokens)
end