public
Created

Evented Sentence Parser

  • Download Gist
sentence_parsing.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 
class SentenceParser
include Eventually
def initialize(document)
@document = document
end
def parse!
lines = document.split(/\r?\n/)
lines.each do |line|
emit(:line, line)
words = line.split(/\s+/)
words.each do |word|
emit(:word, word)
end
end
end
end
 
document = %Q{
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus dapibus elit et ligula vestibulum porttitor. Vestibulum tristique suscipit sem eu cursus. Aenean sit amet ligula elit. Morbi venenatis scelerisque viverra. Cras at nisl quis libero rutrum accumsan.
 
Aenean et nisl felis, nec convallis erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus nec purus nunc, sit amet ornare purus. Vestibulum laoreet mattis sem non malesuada. Nunc vitae lectus neque. Duis sit amet velit non nulla facilisis sodales.
 
Aenean ultrices sapien ac enim lacinia euismod eleifend pulvinar urna. Nulla leo metus, viverra non lacinia at, posuere at leo. Nullam dictum venenatis tristique. Fusce pellentesque felis vitae libero gravida at interdum est lacinia. Nam rhoncus, diam at gravida dictum, odio velit rutrum erat, vitae laoreet nisl tortor at magna.
}
 
parser = SentenceParser.new(document)
parser.on(:line) do |line|
puts 'Found line with %d characters' % line.length
puts 'Line = %s' % line
end
parser.on(:word) do |word|
puts 'Found word = %d' % word
end
parser.parse!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.