Class: Wapiti::Token
Instance Attribute Summary collapse
-
#label ⇒ Object
Returns the value of attribute label.
-
#observations ⇒ Object
Returns the value of attribute observations.
-
#score ⇒ Object
Returns the value of attribute score.
-
#value(encode: false) ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #empty? ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(value = '', label: '', observations: [], score: nil) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #label? ⇒ Boolean
- #observations? ⇒ Boolean
- #score? ⇒ Boolean
- #to_a(expanded: true, tagged: true, encode: false) ⇒ Object
- #to_s(spacer: ' ', **opts) ⇒ Object
Constructor Details
#initialize(value = '', label: '', observations: [], score: nil) ⇒ Token
Returns a new instance of Token.
19 20 21 |
# File 'lib/wapiti/token.rb', line 19 def initialize(value = '', label: '', observations: [], score: nil) @value, @label, @observations, @score = value, label, observations, score end |
Instance Attribute Details
#label ⇒ Object
Returns the value of attribute label.
7 8 9 |
# File 'lib/wapiti/token.rb', line 7 def label @label end |
#observations ⇒ Object
Returns the value of attribute observations.
7 8 9 |
# File 'lib/wapiti/token.rb', line 7 def observations @observations end |
#score ⇒ Object
Returns the value of attribute score.
7 8 9 |
# File 'lib/wapiti/token.rb', line 7 def score @score end |
#value(encode: false) ⇒ Object
Returns the value of attribute value.
7 8 9 |
# File 'lib/wapiti/token.rb', line 7 def value @value end |
Class Method Details
.parse(string, spacer: /\s+/, tagged: false, **opts) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/wapiti/token.rb', line 10 def parse(string, spacer: /\s+/, tagged: false, **opts) value, *observations = string.split(spacer) new(value, label: (tagged ? observations.pop : nil).to_s, observations: observations ) end |
Instance Method Details
#<=>(other) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/wapiti/token.rb', line 51 def <=>(other) if other.is_a?(Token) [value.to_s, label.to_s] <=> [other.value.to_s, other.label.to_s] else nil end end |
#empty? ⇒ Boolean
27 28 29 |
# File 'lib/wapiti/token.rb', line 27 def empty? value.nil? || value.strip.empty? end |
#eql?(other) ⇒ Boolean
47 48 49 |
# File 'lib/wapiti/token.rb', line 47 def eql?(other) hash == other.hash end |
#hash ⇒ Object
39 40 41 |
# File 'lib/wapiti/token.rb', line 39 def hash [value.to_s, label.to_s].hash end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/wapiti/token.rb', line 70 def inspect %Q{#<Wapiti::Token "#{to_s tagged: true}">} end |
#label? ⇒ Boolean
31 32 33 |
# File 'lib/wapiti/token.rb', line 31 def label? !(label.nil? || label.empty?) end |
#observations? ⇒ Boolean
23 24 25 |
# File 'lib/wapiti/token.rb', line 23 def observations? !(observations.nil? || observations.empty?) end |
#score? ⇒ Boolean
35 36 37 |
# File 'lib/wapiti/token.rb', line 35 def score? !score.nil? end |
#to_a(expanded: true, tagged: true, encode: false) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/wapiti/token.rb', line 63 def to_a(expanded: true, tagged: true, encode: false) a = [value(encode: encode)] a.concat observations if && observations? a << label if tagged && label? a end |
#to_s(spacer: ' ', **opts) ⇒ Object
59 60 61 |
# File 'lib/wapiti/token.rb', line 59 def to_s(spacer: ' ', **opts) to_a(**opts).join(spacer) end |