-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved engram selector to io adapters.
- Loading branch information
1 parent
e55ae50
commit ffebc96
Showing
10 changed files
with
93 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .logic import LogicAdapter | ||
from .engram import EngramAdapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"]}} |
Oops, something went wrong.