Class: CRFPP::Template
- Inherits:
-
Object
- Object
- CRFPP::Template
- Extended by:
- Forwardable
- Includes:
- Filelike, Enumerable
- Defined in:
- lib/crfpp/template.rb
Instance Attribute Summary collapse
-
#sentences ⇒ Object
readonly
Returns the value of attribute sentences.
Attributes included from Filelike
Instance Method Summary collapse
- #clear ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(path = nil) ⇒ Template
constructor
A new instance of Template.
- #new_sentence ⇒ Object
- #open ⇒ Object
- #push(feature) ⇒ Object (also: #<<)
- #to_s ⇒ Object
Methods included from Filelike
Constructor Details
#initialize(path = nil) ⇒ Template
Returns a new instance of Template.
13 14 15 16 |
# File 'lib/crfpp/template.rb', line 13 def initialize(path = nil) @path = path open end |
Instance Attribute Details
#sentences ⇒ Object (readonly)
Returns the value of attribute sentences.
9 10 11 |
# File 'lib/crfpp/template.rb', line 9 def sentences @sentences end |
Instance Method Details
#clear ⇒ Object
33 34 35 36 |
# File 'lib/crfpp/template.rb', line 33 def clear @sentences = [[]] self end |
#empty? ⇒ Boolean
52 53 54 |
# File 'lib/crfpp/template.rb', line 52 def empty? [@sentences].flatten(2).compact.empty? end |
#new_sentence ⇒ Object
56 57 58 59 |
# File 'lib/crfpp/template.rb', line 56 def new_sentence @sentences << [] self end |
#open ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/crfpp/template.rb', line 18 def open clear read.lines.each do |line| line.chomp! if line.strip.empty? new_sentence else push Feature.parse(line) end end self end |
#push(feature) ⇒ Object Also known as: <<
45 46 47 48 |
# File 'lib/crfpp/template.rb', line 45 def push(feature) @sentences.last << feature self end |
#to_s ⇒ Object
38 39 40 41 42 43 |
# File 'lib/crfpp/template.rb', line 38 def to_s return '' if empty? i = -1 map { |s| s.map { |f| f.respond_to?(:identified?) && !f.identified? ? f.to_s(i += 1) : f }.join("\n") }.zip([]).flatten.join("\n") end |