Skip to content

Commit

Permalink
Merge pull request #224 from gunthercox/issue_203
Browse files Browse the repository at this point in the history
Correct issue with statement response serialization
  • Loading branch information
gunthercox authored Aug 9, 2016
2 parents 154757c + d3abef8 commit 9578a35
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
6 changes: 4 additions & 2 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .adapters.logic import LogicAdapter, MultiLogicAdapter
from .adapters.input import InputAdapter
from .adapters.output import OutputAdapter
from .conversation import Statement
from .conversation import Statement, Response
from .utils.queues import ResponseQueue
from .utils.module_loading import import_module

Expand Down Expand Up @@ -143,7 +143,9 @@ def get_response(self, input_item):
previous_statement = self.get_last_response_statement()

if previous_statement:
input_statement.add_response(previous_statement)
input_statement.add_response(
Response(previous_statement.text)
)

# Update the database after selecting a response
self.storage.update(input_statement)
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/conversation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .statement import Statement
from .statement import Response
from .response import Response
31 changes: 31 additions & 0 deletions chatterbot/conversation/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Response(object):
"""
A response represents an entity which response to a statement.
"""

def __init__(self, text, **kwargs):
self.text = text
self.occurrence = kwargs.get("occurrence", 1)

def __str__(self):
return self.text

def __repr__(self):
return "<Response text:%s>" % (self.text)

def __eq__(self, other):
if not other:
return False

if isinstance(other, Response):
return self.text == other.text

return self.text == other

def serialize(self):
data = {}

data["text"] = self.text
data["occurrence"] = self.occurrence

return data
46 changes: 16 additions & 30 deletions chatterbot/conversation/statement.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .response import Response


class Statement(object):
"""
A statement represents a single spoken entity, sentence or
Expand Down Expand Up @@ -41,6 +44,14 @@ def add_response(self, response):
"""
Add the response to the list if it does not already exist.
"""
if not isinstance(response, Response):
raise Statement.InvalidTypeException(
'A {} was recieved when a {} instance was expected'.format(
type(response),
type(Response(''))
)
)

updated = False
for index in range(0, len(self.in_response_to)):
if response.text == self.in_response_to[index].text:
Expand Down Expand Up @@ -86,35 +97,10 @@ def serialize(self):

return data

class InvalidTypeException(Exception):

class Response(object):
"""
A response represents an entity which response to a statement.
"""

def __init__(self, text, **kwargs):
self.text = text
self.occurrence = kwargs.get("occurrence", 1)

def __str__(self):
return self.text
def __init__(self, value='Recieved an unexpected value type.'):
self.value = value

def __repr__(self):
return "<Response text:%s>" % (self.text)

def __eq__(self, other):
if not other:
return False

if isinstance(other, Response):
return self.text == other.text

return self.text == other

def serialize(self):
data = {}

data["text"] = self.text
data["occurrence"] = self.occurrence

return data
def __str__(self):
return repr(self.value)
6 changes: 4 additions & 2 deletions chatterbot/training/trainers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from chatterbot.conversation import Statement
from chatterbot.conversation import Statement, Response
from chatterbot.corpus import Corpus


Expand Down Expand Up @@ -30,7 +30,9 @@ def train(self, conversation):
previous_statement = statement_history[-1]

if previous_statement:
statement.add_response(previous_statement)
statement.add_response(
Response(previous_statement.text)
)

statement_history.append(statement)
self.storage.update(statement)
Expand Down
4 changes: 4 additions & 0 deletions tests/conversation_tests/test_statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def test_occurrence_count_incremented(self):

self.assertEqual(len(self.statement.in_response_to), 1)
self.assertEqual(self.statement.in_response_to[0].occurrence, 2)

def test_add_non_response(self):
with self.assertRaises(Statement.InvalidTypeException):
self.statement.add_response(Statement("Blah"))
4 changes: 2 additions & 2 deletions tests/storage_adapter_tests/test_jsondb_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_update_modifies_existing_statement(self):

# Update the statement value
statement.add_response(
Statement("New response")
Response("New response")
)
self.adapter.update(statement)

Expand Down Expand Up @@ -365,7 +365,7 @@ def test_update_does_not_modify_existing_statement(self):
self.adapter.read_only = True

statement.add_response(
Statement("New response")
Response("New response")
)

self.adapter.update(statement)
Expand Down

0 comments on commit 9578a35

Please sign in to comment.