Class: Markov::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/markov/parser.rb', line 6

def initialize source, options={}
  @source = source
end

Instance Attribute Details

#sourceObject (readonly)

Returns the value of attribute source.



4
5
6
# File 'lib/markov/parser.rb', line 4

def source
  @source
end

Instance Method Details

#groups(chunk_size, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/markov/parser.rb', line 19

def groups chunk_size, options={}
  text = options[:tagged] ? tagged_text : raw_text
  words = text.split
  (chunk_size - 1).times { |r,a| words.unshift("\"\"") }

  words.each_cons(chunk_size).to_a.inject([]){ |r,a| 
    r << { 
      prefix: a[0..(chunk_size - 2)],
      suffix: a.last 
    } 
  }
end

#raw_textObject



10
11
12
# File 'lib/markov/parser.rb', line 10

def raw_text
  @raw_text ||= File.read(@source).scrub!
end

#tagged_textObject



14
15
16
17
# File 'lib/markov/parser.rb', line 14

def tagged_text
  tagger = EngTagger.new
  @tagged_text ||= tagger.get_readable(raw_text)
end