Class: CRFPP::Template

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Filelike, Enumerable
Defined in:
lib/crfpp/template.rb

Instance Attribute Summary collapse

Attributes included from Filelike

#path

Instance Method Summary collapse

Methods included from Filelike

#read, #write

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

#sentencesObject (readonly)

Returns the value of attribute sentences.



9
10
11
# File 'lib/crfpp/template.rb', line 9

def sentences
  @sentences
end

Instance Method Details

#clearObject



33
34
35
36
# File 'lib/crfpp/template.rb', line 33

def clear
  @sentences = [[]]
  self
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/crfpp/template.rb', line 52

def empty?
  [@sentences].flatten(2).compact.empty?
end

#new_sentenceObject



56
57
58
59
# File 'lib/crfpp/template.rb', line 56

def new_sentence
  @sentences << []
  self
end

#openObject



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_sObject



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