Skip to content

Commit

Permalink
Update now creates an entry if it does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed May 21, 2015
1 parent 9e315bb commit 4bd7cd8
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
1 change: 1 addition & 0 deletions chatterbot/adapters/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ def insert(self, key):
def update(self, key):
"""
Modifies an entry in the database.
Creates an entry if one does not exist.
"""
raise AdapterNotImplementedError()
5 changes: 5 additions & 0 deletions chatterbot/adapters/storage/jsondatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def update(self, key, **kwargs):

values = self.database.data(key=key)

# Create the statement if it doesn't exist in the database
if not values:
self.database[key] = {}
values = {}

for parameter in kwargs:
values[parameter] = kwargs.get(parameter)

Expand Down
10 changes: 2 additions & 8 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def train(self, conversation):

# Create an entry if the statement does not exist in the database
if not values:
values = self.storage.insert(statement, {})
values = {}

count = self.update_occurrence_count(values)
timestamp = self.timestamp()
Expand All @@ -98,10 +98,6 @@ def update_log(self, data):
statement = list(data.keys())[0]
values = data[statement]

# Create the statement if it doesn't exist in the database
if not self.storage.find(statement):
self.storage.insert(statement, {})

count = self.update_occurrence_count(values)
username = values["name"]
timestamp = values["date"]
Expand All @@ -110,9 +106,7 @@ def update_log(self, data):
response_list = self.update_response_list(statement, previous_statement)

# Update the database with the changes
data = self.storage.update(statement, name=username, date=timestamp, occurrence=count, in_response_to=response_list)

#print "YYYY", count, data
self.storage.update(statement, name=username, date=timestamp, occurrence=count, in_response_to=response_list)

# TODO, change user_name and input_text into a single dict
def get_response_data(self, user_name, input_text):
Expand Down
2 changes: 0 additions & 2 deletions tests/test_chatbot_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ def test_answer_close_to_known_input(self):
input_text = "What is your favourite colour?"
response = self.chatbot.get_response(input_text)

print self.chatbot.storage.database.data()

self.assertIn("Blue", response)

def test_match_has_no_response(self):
Expand Down

0 comments on commit 4bd7cd8

Please sign in to comment.