Class: CRFPP::Data

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

Overview

A Data object represents test or training data.

Instance Attribute Summary collapse

Attributes included from Filelike

#path

Instance Method Summary collapse

Methods included from Filelike

#read, #write

Constructor Details

#initialize(path = nil) ⇒ Data

Returns a new instance of Data.



17
18
19
20
# File 'lib/crfpp/data.rb', line 17

def initialize(path = nil)
  @path = path
  open
end

Instance Attribute Details

#sentencesObject (readonly)

Returns the value of attribute sentences.



13
14
15
# File 'lib/crfpp/data.rb', line 13

def sentences
  @sentences
end

Instance Method Details

#clearObject



37
38
39
40
# File 'lib/crfpp/data.rb', line 37

def clear
  @sentences = [[]]
  self
end

#empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/crfpp/data.rb', line 53

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

#new_sentenceObject



57
58
59
60
# File 'lib/crfpp/data.rb', line 57

def new_sentence
  @sentences << []
  self
end

#openObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/crfpp/data.rb', line 22

def open
  clear
  
  read.lines.each do |line|
    line.chomp!
    if line.strip.empty?
      new_sentence 
    else
      push Token.parse(line)
    end
  end
  
  self
end

#push(feature) ⇒ Object Also known as: <<



46
47
48
49
# File 'lib/crfpp/data.rb', line 46

def push(feature)
  @sentences.last << feature
  self
end

#to_sObject



42
43
44
# File 'lib/crfpp/data.rb', line 42

def to_s
  empty? ? '' : zip([]).flatten.join("\n")
end