Class: Wapiti::Token

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/wapiti/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#labelObject

Returns the value of attribute label.



7
8
9
# File 'lib/wapiti/token.rb', line 7

def label
  @label
end

#observationsObject

Returns the value of attribute observations.



7
8
9
# File 'lib/wapiti/token.rb', line 7

def observations
  @observations
end

#scoreObject

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

Returns:

  • (Boolean)


27
28
29
# File 'lib/wapiti/token.rb', line 27

def empty?
  value.nil? || value.strip.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/wapiti/token.rb', line 47

def eql?(other)
  hash == other.hash
end

#hashObject



39
40
41
# File 'lib/wapiti/token.rb', line 39

def hash
  [value.to_s, label.to_s].hash
end

#inspectObject



70
71
72
# File 'lib/wapiti/token.rb', line 70

def inspect
  %Q{#<Wapiti::Token "#{to_s tagged: true}">}
end

#label?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/wapiti/token.rb', line 31

def label?
  !(label.nil? || label.empty?)
end

#observations?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/wapiti/token.rb', line 23

def observations?
  !(observations.nil? || observations.empty?)
end

#score?Boolean

Returns:

  • (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 expanded && 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