Class: Wapiti::Sequence
- Inherits:
-
Object
- Object
- Wapiti::Sequence
- Extended by:
- Forwardable
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/wapiti/sequence.rb
Instance Attribute Summary collapse
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #each(&block) ⇒ Object (also: #each_token)
- #each_segment(spacer: ' ') ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(tokens = []) ⇒ Sequence
constructor
A new instance of Sequence.
- #inspect ⇒ Object
- #label?(label) ⇒ Boolean
- #labels ⇒ Object
- #tagged? ⇒ Boolean
- #to_a(**opts) ⇒ Object
- #to_h(symbolize_keys: false, **opts) ⇒ Object
- #to_s(delimiter: "\n", **opts) ⇒ Object
- #to_sentence(delimiter: ' ') ⇒ Object
- #to_xml(xml = Builder::XmlMarkup.new) ⇒ Object
- #token_length ⇒ Object
Constructor Details
#initialize(tokens = []) ⇒ Sequence
Returns a new instance of Sequence.
21 22 23 |
# File 'lib/wapiti/sequence.rb', line 21 def initialize(tokens = []) @tokens = tokens end |
Instance Attribute Details
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
10 11 12 |
# File 'lib/wapiti/sequence.rb', line 10 def tokens @tokens end |
Class Method Details
Instance Method Details
#<=>(other) ⇒ Object
33 34 35 |
# File 'lib/wapiti/sequence.rb', line 33 def <=>(other) Sequence === other ? tokens <=> other.tokens : nil end |
#each(&block) ⇒ Object Also known as: each_token
53 54 55 56 57 58 59 60 |
# File 'lib/wapiti/sequence.rb', line 53 def each(&block) if block_given? tokens.each(&block) self else to_enum end end |
#each_segment(spacer: ' ') ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/wapiti/sequence.rb', line 64 def each_segment(spacer: ' ') if block_given? lbl, sgm = nil, [] each do |tk| if lbl != tk.label yield [lbl, sgm.join(spacer)] unless sgm.empty? lbl, sgm = tk.label, [tk.value] else sgm << tk.value end end yield [lbl, sgm.join(spacer)] unless sgm.empty? self else to_enum :each_segment end end |
#eql?(other) ⇒ Boolean
29 30 31 |
# File 'lib/wapiti/sequence.rb', line 29 def eql?(other) hash == other.hash end |
#hash ⇒ Object
25 26 27 |
# File 'lib/wapiti/sequence.rb', line 25 def hash tokens.hash end |
#inspect ⇒ Object
114 115 116 |
# File 'lib/wapiti/sequence.rb', line 114 def inspect "#<Wapiti::Sequence tokens={#{size}}>" end |
#label?(label) ⇒ Boolean
45 46 47 |
# File 'lib/wapiti/sequence.rb', line 45 def label?(label) tokens.any? { |tk| tk.label == label } end |
#labels ⇒ Object
49 50 51 |
# File 'lib/wapiti/sequence.rb', line 49 def labels tokens.map(&:label).uniq end |
#tagged? ⇒ Boolean
37 38 39 |
# File 'lib/wapiti/sequence.rb', line 37 def tagged? tokens.all?(&:label?) end |
#to_a(**opts) ⇒ Object
85 86 87 |
# File 'lib/wapiti/sequence.rb', line 85 def to_a(**opts) tokens.map { |tk| tk.to_s(**opts) } end |
#to_h(symbolize_keys: false, **opts) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/wapiti/sequence.rb', line 97 def to_h(symbolize_keys: false, **opts) each_segment(**opts).reduce({}) do |h, (label, segment)| label = label.intern if symbolize_keys h[label] = [] unless h.key? label h[label] << segment h end end |
#to_s(delimiter: "\n", **opts) ⇒ Object
89 90 91 |
# File 'lib/wapiti/sequence.rb', line 89 def to_s(delimiter: "\n", **opts) tokens.map { |tk| tk.to_s(**opts) }.join(delimiter) end |
#to_sentence(delimiter: ' ') ⇒ Object
93 94 95 |
# File 'lib/wapiti/sequence.rb', line 93 def to_sentence(delimiter: ' ') to_s(delimiter: delimiter, expanded: false, tagged: false) end |
#to_xml(xml = Builder::XmlMarkup.new) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/wapiti/sequence.rb', line 106 def to_xml(xml = Builder::XmlMarkup.new) xml.sequence do |sq| each_segment do |(label, segment)| sq.tag! label, segment end end end |
#token_length ⇒ Object
41 42 43 |
# File 'lib/wapiti/sequence.rb', line 41 def token_length tokens.reduce(0) { |len, tk| len + tk.value.length } end |