Class: Herbalist::Tag
- Inherits:
-
Object
- Object
- Herbalist::Tag
- Defined in:
- lib/herbalist/herbalist.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.scan(tokens) ⇒ Object
scan the given tokens and tag any matches.
-
.scan_for_numbers(token) ⇒ Object
check the token to see if if it is a number then tag it.
-
.scan_for_units(token) ⇒ Object
check the token and see if it is a type of unit that Alchemist can handle then tag the token with that unit type.
Instance Method Summary collapse
-
#initialize(type, value) ⇒ Tag
constructor
A new instance of Tag.
- #to_s ⇒ Object
Constructor Details
#initialize(type, value) ⇒ Tag
Returns a new instance of Tag.
104 105 106 107 |
# File 'lib/herbalist/herbalist.rb', line 104 def initialize(type, value) @type = type @value = value end |
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
102 103 104 |
# File 'lib/herbalist/herbalist.rb', line 102 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
102 103 104 |
# File 'lib/herbalist/herbalist.rb', line 102 def value @value end |
Class Method Details
.scan(tokens) ⇒ Object
scan the given tokens and tag any matches
110 111 112 113 114 115 116 |
# File 'lib/herbalist/herbalist.rb', line 110 def self.scan(tokens) tokens.each do |token| if t = self.scan_for_numbers(token) then token.tag(t) end if t = self.scan_for_units(token) then token.tag(t) end end tokens end |
.scan_for_numbers(token) ⇒ Object
check the token to see if if it is a number then tag it
120 121 122 123 124 125 |
# File 'lib/herbalist/herbalist.rb', line 120 def self.scan_for_numbers(token) if token.word =~ /(^|\W)(\d*\.\d+)($|\W)/ || token.word =~ /(^|\W)(\d+)($|\W)/ return Tag.new(:number, $2.to_f) end return nil end |
.scan_for_units(token) ⇒ Object
check the token and see if it is a type of unit that Alchemist can handle then tag the token with that unit type
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/herbalist/herbalist.rb', line 129 def self.scan_for_units(token) return nil if token.get_tag(:number) # all units POSSIBLE_UNITS.each do |unit| if token.word.length<=2 # special matching for short forms ex. Mi return Tag.new(:unit, unit) if token.word == unit.to_s elsif token.word =~ /(^|\W)#{unit.to_s}($|\W)/i return Tag.new(:unit, unit) end end # try si units with prefixes (kilo, deca etc) Alchemist.library.unit_prefixes.each do |prefix, value| if token.word =~ /^#{prefix.to_s}.+/i Alchemist.library.si_units.each do |unit| if unit.to_s=~/#{token.word.gsub(/^#{prefix.to_s}/i,'')}$/i return Tag.new(:unit, "#{prefix}#{unit}") end end end end return nil end |
Instance Method Details
#to_s ⇒ Object
154 155 156 |
# File 'lib/herbalist/herbalist.rb', line 154 def to_s "#{type}-#{value}" end |