Class: Yanbi::Corpus

Inherits:
Object
  • Object
show all
Defined in:
lib/corpus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass = WordBag) ⇒ Corpus

Returns a new instance of Corpus.



22
23
24
25
26
# File 'lib/corpus.rb', line 22

def initialize(klass=WordBag)
  @all = klass.new
  @docs = []
  @bags = []
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



20
21
22
# File 'lib/corpus.rb', line 20

def all
  @all
end

#bagsObject (readonly)

Returns the value of attribute bags.



19
20
21
# File 'lib/corpus.rb', line 19

def bags
  @bags
end

#docsObject (readonly)

Returns the value of attribute docs.



18
19
20
# File 'lib/corpus.rb', line 18

def docs
  @docs
end

Instance Method Details

#add_doc(doc, comment = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/corpus.rb', line 45

def add_doc(doc, comment=nil)
  doc.gsub! comment, '' if comment
  doc.strip!
  
  unless doc.length.zero?
    @bags << @all.class.new(doc)
    @all.add_text doc
    @docs << doc
  end
end

#add_file(docpath, delim = nil, comment = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/corpus.rb', line 32

def add_file(docpath, delim=nil, comment=nil)
  infile = File.open(docpath, 'r')
  raw = infile.read
  infile.close
  
  if delim
    docs = raw.split(delim) 
    docs.each {|d| add_doc(d, comment)} 
  else
    add_doc(raw, comment)
  end
end

#each_docObject



56
57
58
59
60
# File 'lib/corpus.rb', line 56

def each_doc
  @bags.each do |bag|
    yield bag
  end
end

#sizeObject



28
29
30
# File 'lib/corpus.rb', line 28

def size
  @docs.size
end