Class: Markov::Parser
- Inherits:
-
Object
- Object
- Markov::Parser
- Defined in:
- lib/markov/parser.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #groups(chunk_size, options = {}) ⇒ Object
-
#initialize(source, options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #raw_text ⇒ Object
- #tagged_text ⇒ Object
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, ={} @source = source end |
Instance Attribute Details
#source ⇒ Object (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, ={} text = [: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_text ⇒ Object
10 11 12 |
# File 'lib/markov/parser.rb', line 10 def raw_text @raw_text ||= File.read(@source).scrub! end |
#tagged_text ⇒ Object
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 |