irc-bots/taylor_bot/DictionaryGenerator.rb

37 lines
924 B
Ruby
Raw Normal View History

2017-12-14 22:47:33 +00:00
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|
words = words.collect { |s| s.gsub(/["()]/, "") }
self.add_word_to_dict words[0..-2], words[-1]
end
end
end
end