Skip to content

Commit

Permalink
Moved engram selector to io adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed May 18, 2015
1 parent e55ae50 commit ffebc96
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 116 deletions.
21 changes: 0 additions & 21 deletions chatterbot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
from .chatterbot import ChatBot


def Terminal():
import sys

bot = ChatBot("Terminal",
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
logic_adapter="chatterbot.adapters.logic.EngramAdapter",
io_adapter="chatterbot.adapters.io.TerminalAdapter",
database="database.db", logging=True)

user_input = "Type something to begin..."

print(user_input)

while True:
try:
bot.get_response(user_input)

except (KeyboardInterrupt, EOFError, SystemExit):
break


class TalkWithCleverbot(ChatBot):

def __init__(self, name="ChatterBot", adapter="chatterbot.adapters.JsonDatabaseAdapter", database="database.db", logging=True):
Expand Down
9 changes: 1 addition & 8 deletions chatterbot/adapters/io/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@

class TerminalAdapter(object):

def get_output(self, input_value):
# 'raw_input' is just 'input' in python3
if sys.version_info[0] < 3:
user_input = str(raw_input())
else:
user_input = input()
def get_response(self, input_value):

bot_input = chatbot.get_response(user_input)
print(bot_input)

return bot_input
2 changes: 2 additions & 0 deletions chatterbot/adapters/logic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .logic import LogicAdapter
from .engram import EngramAdapter
69 changes: 69 additions & 0 deletions chatterbot/adapters/logic/engram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from .logic import LogicAdapter


class EngramAdapter(LogicAdapter):

def __init__(self, database):
"""
Constructor for the Engram object takes a statement that
exists within the database, and the database object.
"""

self.database = database

def get_occurrence_count(self, key):
"""
Return the number of times a statement occurs in the database
"""

statement = self.database.find(key)

if "occurrence" in statement:
return statement["occurrence"]

# If the number of occurences has not been set then return 1
return 1

def responces_in_database(self, statement, entry):
"""
Returns true if a given statement is in the database.
Otherwise, returns false
"""

statement = self.database.find(statement)

# Check if the statement has responses in the database
if "in_response_to" in statement and entry in statement["in_response_to"]:
return True

return False

def get(self, input_statement):
"""
Returns a statement in response to the closest matching statement in
the database. For each match, the statement with the greatest number
of occurrence will be returned.
"""

if not input_statement in self.database.keys():
raise Exception("A matching statement must exist in the database")

# Initialize the matching responce with the first statement in the database
matching_response = self.database.keys()[0]
occurrence_count = self.get_occurrence_count(matching_response)

for statement in self.database.keys():

if self.responces_in_database(statement, input_statement):

statement_occurrence_count = self.get_occurrence_count(statement)

# Keep the more common statement
if statement_occurrence_count >= occurrence_count:
matching_response = statement
occurrence_count = statement_occurrence_count

#TODO? If the two statements occure equaly in frequency, should we keep one at random

# Choose the most common selection of matching response
return {matching_response: self.database.find(matching_response)}
9 changes: 9 additions & 0 deletions chatterbot/adapters/logic/logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class LogicAdapterNotImplementedError(NotImplementedError):
def __init__(self, message="This method must be overridden in a subclass method."):
self.message = message


class LogicAdapter(object):

def get_response(self):
raise DatabaseAdapterNotImplementedError()
67 changes: 0 additions & 67 deletions chatterbot/algorithms/engram.py
Original file line number Diff line number Diff line change
@@ -1,67 +0,0 @@
class Engram(object):

def __init__(self, statement, database):
"""
Constructor for the Engram object takes a statement that
exists within the database, and the database object.
"""

if not statement in database.keys():
raise Exception("A matching statement must exist in the database")

self.statement = statement
self.database = database

def get_occurrence_count(self, key):
"""
Return the number of times a statement occurs in the database
"""

statement = self.database.find(key)

if "occurrence" in statement:
return statement["occurrence"]

# If the number of occurences has not been set then return 1
return 1

def responces_in_database(self, statement):
"""
Returns true if a given statement is in the database.
Otherwise, returns false
"""

statement = self.database.find(statement)

# Check if the statement has responses in the database
if "in_response_to" in statement and self.statement in statement["in_response_to"]:
return True

return False

def get(self):
"""
Returns a statement in response to the closest matching statement in
the database. For each match, the statement with the greatest number
of occurrence will be returned.
"""

# Initialize the matching responce with the first statement in the database
matching_response = self.database.keys()[0]
occurrence_count = self.get_occurrence_count(matching_response)

for statement in self.database.keys():

if self.responces_in_database(statement):

statement_occurrence_count = self.get_occurrence_count(statement)

# Keep the more common statement
if statement_occurrence_count >= occurrence_count:
matching_response = statement
occurrence_count = statement_occurrence_count

#TODO? If the two statements occure equaly in frequency, should we keep one at random

# Choose the most common selection of matching response
return {matching_response: self.database.find(matching_response)}
12 changes: 8 additions & 4 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ def __init__(self, name,
self.name = name
self.log = logging

# TODO: Change database to storage
StorageAdapter = self.import_adapter(storage_adapter)
self.database = StorageAdapter(database)

LogicAdapter = self.import_adapter(logic_adapter)
self.logic = LogicAdapter(self.database)

IOAdapter = self.import_adapter(io_adapter)
self.io = IOAdapter()

Expand Down Expand Up @@ -123,7 +127,6 @@ def get_response_data(self, user_name, input_text):
* user: The user's statement meta data
* bot: The bot's statement meta data
"""
from .algorithms.engram import Engram
from .matching import closest

if input_text:
Expand All @@ -133,8 +136,8 @@ def get_response_data(self, user_name, input_text):
# If the input is blank, return a random statement
closest_statement = self.database.get_random()

response_statement = Engram(closest_statement, self.database)
self.recent_statements.append(response_statement.get())
response_statement = self.logic.get(closest_statement)
self.recent_statements.append(response_statement)

statement_text = list(self.get_last_statement().keys())[0]

Expand All @@ -153,6 +156,7 @@ def get_response_data(self, user_name, input_text):

def get_response(self, input_text, user_name="user"):
"""
Return only the bot's response text from the input
Return only the bot's response text from the input.
"""

return self.get_response_data(user_name, input_text)["bot"]
2 changes: 1 addition & 1 deletion database.db
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Why?": {"date": "2015-04-16-09-01-59", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "I'm doing great.": {"date": "2015-04-16-08-41-17", "in_response_to": ["Sure, any time."], "occurrence": 1, "name": "user"}, "What time is it?": {"date": "2015-04-16-08-53-17", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "How great?": {"date": "2015-04-16-09-01-49", "in_response_to": ["Cool"], "name": "user", "occurrence": 1}, "No": {"date": "2015-04-16-08-45-24", "in_response_to": ["Can I ask you a question?", "I'm doing great."], "occurrence": 2, "name": "user"}, "What is your name?": {"date": "2015-04-16-08-43-26", "in_response_to": ["Sure, any time."], "occurrence": 1, "name": "user"}, "Sure, I'm looking for something.": {"date": "2015-04-16-08-56-18", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "That is good to hear": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["I'm great."]}, "Sure.": {"date": "2015-04-16-08-45-07", "in_response_to": ["Can I ask you a question?"], "occurrence": 3, "name": "user"}, "Can I help you?": {"date": "2015-04-16-08-55-32", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "Good day": {"date": "2015-04-16-08-51-52", "occurrence": 1, "name": "user", "in_response_to": ["Thank you."]}, "Sure": {"date": "2015-04-16-08-46-04", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "You're welcome.": {"date": "2015-04-16-08-58-00", "in_response_to": ["Sure, any time."], "occurrence": 1, "name": "user"}, "You are welcome.": {"date": "2015-04-16-08-52-03", "in_response_to": ["Thank you.", "Can I ask you a question?", "Sure, any time."], "occurrence": 4, "name": "user"}, "Great?": {"date": "2015-04-16-08-45-48", "in_response_to": ["No"], "name": "user", "occurrence": 1}, "Hello!": {"date": "2015-04-16-08-53-45", "in_response_to": ["Hi there!"], "name": "user", "occurrence": 1}, "Thanks.": {"date": "2015-04-16-08-52-39", "occurrence": 2, "in_response_to": ["You are welcome.", "I'm doing great."], "name": "user"}, "Thats nice to hear.": {"date": "2015-04-16-08-45-40", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "Yeah, its pretty awesome.": {"date": "2015-04-16-08-55-58", "in_response_to": ["Can I help you with anything?"], "name": "user", "occurrence": 1}, "I like cats.": {"date": "2015-04-16-08-57-19", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "What time works for you?": {"date": "2015-04-16-08-41-30", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "Cool": {"date": "2015-04-16-08-55-39", "occurrence": 1, "name": "user", "in_response_to": ["Great?"]}, "Thats good to hear.": {"date": "2015-04-16-08-41-52", "in_response_to": ["Thank you."], "occurrence": 1, "name": "user"}, "Thats good to hear. What have you been up to?": {"date": "2015-04-16-08-54-07", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "Awesome.": {"date": "2015-04-16-08-53-08", "occurrence": 1, "name": "user", "in_response_to": ["Sure, any time."]}, "Woah! Where are you going?": {"date": "2015-04-16-08-55-14", "in_response_to": ["Sure, any time."], "name": "user", "occurrence": 1}, "What do you like to eat?": {"date": "2015-04-16-08-52-23", "in_response_to": ["Sure, any time."], "occurrence": 1, "name": "user"}, "Sure, any time.": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["You are welcome.", "Your welcome."]}, "Do you like cats?": {"date": "2015-04-16-08-57-26", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "Hi there!": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["Hello"]}, "Hello": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": []}, "Type something to begin...": {"date": "2015-05-15-11-48-40", "in_response_to": [{"Why?": {"date": "2015-04-16-09-01-59", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}}], "name": "user", "occurrence": 1297}, "Great is a good way to be doing.": {"date": "2015-04-16-08-53-00", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "Can I ask you a question?": {"date": "2015-04-16-08-44-57", "in_response_to": ["You are welcome.", "Can I ask you a question?"], "occurrence": 3, "name": "user"}, "Can I help you with anything?": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["Yeah"]}, "Oh, too bad.": {"date": "2015-04-16-08-45-56", "occurrence": 1, "name": "user", "in_response_to": ["Thank you."]}, "It depends what the question is?": {"date": "2015-04-16-08-45-22", "occurrence": 1, "in_response_to": ["Can I ask you a question?"], "name": "user"}, "How are you doing?": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["Hi there!"]}, "Awesome": {"date": "2015-04-16-09-01-53", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "I missed you.": {"date": "2015-04-16-08-53-53", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "Yeah": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["Sure, any time."]}, "Thank you.": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["That is good to hear"]}, "Hi!": {"date": "2015-04-16-08-57-42", "occurrence": 2, "in_response_to": ["How are you doing?", "I'm doing great."], "name": "user"}, "hi": {"date": "2015-04-16-09-01-43", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "Your welcome.": {"date": "2015-04-16-09-01-36", "occurrence": 1, "in_response_to": ["Thank you."]}, "Good morning!": {"date": "2015-04-16-08-57-52", "occurrence": 1, "in_response_to": ["Thank you."], "name": "user"}, "I'm great.": {"date": "2015-04-16-09-01-36", "occurrence": 10, "in_response_to": ["How are you doing?"]}}
{"Why?": {"date": "2015-04-16-09-01-59", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "I'm doing great.": {"date": "2015-04-16-08-41-17", "in_response_to": ["Sure, any time."], "name": "user", "occurrence": 1}, "What time is it?": {"date": "2015-04-16-08-53-17", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "How great?": {"date": "2015-04-16-09-01-49", "occurrence": 1, "in_response_to": ["Cool"], "name": "user"}, "No": {"date": "2015-04-16-08-45-24", "in_response_to": ["Can I ask you a question?", "I'm doing great."], "name": "user", "occurrence": 2}, "Yeah": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["Sure, any time."]}, "Sure, I'm looking for something.": {"date": "2015-04-16-08-56-18", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "Why what?": {"date": "2015-05-17-20-27-13", "in_response_to": [{"Why?": {"date": "2015-04-16-09-01-59", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}}], "name": "user", "occurrence": 2}, "That is good to hear": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["I'm great."]}, "Sure.": {"date": "2015-04-16-08-45-07", "in_response_to": ["Can I ask you a question?"], "name": "user", "occurrence": 3}, "Can I help you?": {"date": "2015-04-16-08-55-32", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "Good day": {"date": "2015-04-16-08-51-52", "in_response_to": ["Thank you."], "occurrence": 1, "name": "user"}, "Sure": {"date": "2015-04-16-08-46-04", "in_response_to": ["I'm doing great."], "name": "user", "occurrence": 1}, "You're welcome.": {"date": "2015-04-16-08-58-00", "in_response_to": ["Sure, any time."], "name": "user", "occurrence": 1}, "You are welcome.": {"date": "2015-04-16-08-52-03", "in_response_to": ["Thank you.", "Can I ask you a question?", "Sure, any time."], "name": "user", "occurrence": 4}, "Great?": {"date": "2015-04-16-08-45-48", "occurrence": 1, "in_response_to": ["No"], "name": "user"}, "Thats good to hear. What have you been up to?": {"date": "2015-04-16-08-54-07", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "Hello!": {"date": "2015-04-16-08-53-45", "occurrence": 1, "in_response_to": ["Hi there!"], "name": "user"}, "Thanks.": {"date": "2015-04-16-08-52-39", "occurrence": 2, "name": "user", "in_response_to": ["You are welcome.", "I'm doing great."]}, "Yeah, its pretty awesome.": {"date": "2015-04-16-08-55-58", "occurrence": 1, "in_response_to": ["Can I help you with anything?"], "name": "user"}, "I like cats.": {"date": "2015-04-16-08-57-19", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "What time works for you?": {"date": "2015-04-16-08-41-30", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}, "Cool": {"date": "2015-04-16-08-55-39", "in_response_to": ["Great?"], "occurrence": 1, "name": "user"}, "Thats good to hear.": {"date": "2015-04-16-08-41-52", "in_response_to": ["Thank you."], "name": "user", "occurrence": 1}, "Thats nice to hear.": {"date": "2015-04-16-08-45-40", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "Awesome.": {"date": "2015-04-16-08-53-08", "in_response_to": ["Sure, any time."], "occurrence": 1, "name": "user"}, "Woah! Where are you going?": {"date": "2015-04-16-08-55-14", "occurrence": 1, "in_response_to": ["Sure, any time."], "name": "user"}, "What do you like to eat?": {"date": "2015-04-16-08-52-23", "in_response_to": ["Sure, any time."], "name": "user", "occurrence": 1}, "Can I help you with anything?": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["Yeah"]}, "Do you like cats?": {"date": "2015-04-16-08-57-26", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "Hi there!": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["Hello"]}, "Hello": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": []}, "Type something to begin...": {"date": "2015-05-17-19-43-02", "occurrence": 3540, "in_response_to": [{"Why?": {"date": "2015-04-16-09-01-59", "occurrence": 1, "name": "user", "in_response_to": ["I'm doing great."]}}], "name": "user"}, "Great is a good way to be doing.": {"date": "2015-04-16-08-53-00", "occurrence": 1, "in_response_to": ["I'm doing great."], "name": "user"}, "Can I ask you a question?": {"date": "2015-04-16-08-44-57", "in_response_to": ["You are welcome.", "Can I ask you a question?"], "name": "user", "occurrence": 3}, "Sure, any time.": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["You are welcome.", "Your welcome."]}, "Oh, too bad.": {"date": "2015-04-16-08-45-56", "in_response_to": ["Thank you."], "occurrence": 1, "name": "user"}, "It depends what the question is?": {"date": "2015-04-16-08-45-22", "occurrence": 1, "name": "user", "in_response_to": ["Can I ask you a question?"]}, "How are you doing?": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["Hi there!"]}, "Awesome": {"date": "2015-04-16-09-01-53", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "I missed you.": {"date": "2015-04-16-08-53-53", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}, "What is your name?": {"date": "2015-04-16-08-43-26", "in_response_to": ["Sure, any time."], "name": "user", "occurrence": 1}, "Thank you.": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["That is good to hear"]}, "Hi!": {"date": "2015-04-16-08-57-42", "occurrence": 2, "name": "user", "in_response_to": ["How are you doing?", "I'm doing great."]}, "hi": {"date": "2015-05-17-20-27-05", "in_response_to": ["I'm doing great.", {"Why?": {"date": "2015-04-16-09-01-59", "in_response_to": ["I'm doing great."], "occurrence": 1, "name": "user"}}], "occurrence": 3, "name": "user"}, "Your welcome.": {"date": "2015-05-17-20-27-03", "occurrence": 10, "in_response_to": ["Thank you."]}, "Good morning!": {"date": "2015-04-16-08-57-52", "occurrence": 1, "name": "user", "in_response_to": ["Thank you."]}, "I'm great.": {"date": "2015-05-17-20-27-03", "occurrence": 19, "in_response_to": ["How are you doing?"]}}

0 comments on commit ffebc96

Please sign in to comment.