class MarkovChainChatBot
A chat bot utilizing MarkovChain
.
Public Class Methods
from(data, answer_limit = 1000)
click to toggle source
data
is a map. It may be empty, in this case a brand new MarkovChainChatBot
is created. data
becomes owned by the returned MarkovChainChatBot
.
answer_limit
is maximum size of the result of answer()
.
# File lib/markov_chain_chat_bot.rb, line 19 def self.from(data, answer_limit = 1000) new(data, answer_limit) end
Public Instance Methods
answer(question)
click to toggle source
question
is String
.
It returns String
.
# File lib/markov_chain_chat_bot.rb, line 51 def answer(question) answer = "" previous_token = nil catch :out_of_limit do for token in @markov_chain.predict() break if token.tkn_is_a? EndOfMessage or token.nil? delimiter = if (previous_token.tkn_is_a? Word and token.tkn_is_a? Word) then " " else "" end answer.append_limited(delimiter + token.tkn_value, @answer_limit) previous_token = token end end return answer end
data()
click to toggle source
data
passed to MarkovChainChatBot.from()
.
# File lib/markov_chain_chat_bot.rb, line 32 def data @markov_chain.data end
learn(message)
click to toggle source
message
is String
.
It returns this (modified) MarkovChainChatBot
.
# File lib/markov_chain_chat_bot.rb, line 41 def learn(message) @markov_chain.append!(tokenize(message)).append!([EndOfMessage.new]) return self end