taylor-bot/DictionaryGenerator.rb

35 lines
859 B
Ruby
Raw Permalink Normal View History

class DictionaryGenerator
attr_reader :dictionary, :depth
def initialize( depth )
@dictionary = {}
@capitalized_words = []
@depth = depth
@split_words = /(\.\s+)|(\.$)|([?!])|[\s]+/
@split_sentence = /(?<=[.!?])\s+/
end
def add_word_to_dict( keyword, followedby )
@dictionary[keyword] ||= []
@dictionary[keyword] << followedby
end
def insert_text( text )
input_sentences = text.split @split_sentence
if input_sentences.empty? then raise "insert_text called without input" end
unless input_sentences[-1].strip[-1].match /[.!?]/ then
input_sentences[-1] = input_sentences[-1].strip + '.'
end
input_sentences.each do |sentence|
sentence.split(@split_words).each_cons(@depth + 1) do |words|
self.add_word_to_dict words[0..-2], words[-1]
end
end
end
end