Skip to content

Commit

Permalink
Update exception formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Jun 23, 2016
1 parent d31c8f8 commit 227f213
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
42 changes: 21 additions & 21 deletions chatterbot/adapters/input/variable_input_type_adapter.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
from chatterbot.adapters.input import InputAdapter
from chatterbot.conversation import Statement
import sys

PY3 = sys.version_info[0] == 3

JSON = 'json'
TEXT = 'text'
OBJECT = 'object'
VALID_FORMATS = (JSON, TEXT, OBJECT, )


class VariableInputTypeAdapter(InputAdapter):

JSON = 'json'
TEXT = 'text'
OBJECT = 'object'
VALID_FORMATS = (JSON, TEXT, OBJECT, )

def __init__(self, **kwargs):
super(VariableInputTypeAdapter, self).__init__(**kwargs)

def detect_type(self, statement):
import sys

if PY3:
if sys.version_info[0] == 3:
string_types = str
else:
string_types = basestring

if isinstance(statement, Statement):
return OBJECT
return self.OBJECT
if isinstance(statement, string_types):
return TEXT
return self.TEXT
if isinstance(statement, dict):
return JSON
return self.JSON

input_type = type(statement)

Expand All @@ -41,27 +39,29 @@ def process_input(self, statement):
input_type = self.detect_type(statement)

# Return the statement object without modification
if input_type == OBJECT:
if input_type == self.OBJECT:
return statement

# Convert the input string into a statement object
if input_type == TEXT:
if input_type == self.TEXT:
return Statement(statement)

# Convert input dictionary into a statement object
if input_type == JSON:
if input_type == self.JSON:
input_json = dict(statement)
text = input_json["text"]
del(input_json["text"])

return Statement(text, **input_json)

class UnrecognizedInputFormatException(Exception):
def __init__(self, message='The input format was not recognized.'):
super(
VariableInputTypeAdapter.UnrecognizedInputFormatException,
self
).__init__(message)
"""
Exception raised when an input format is specified that is
not in the VariableInputTypeAdapter.VALID_FORMATS variable.
"""

def __init__(self, value='The input format was not recognized.'):
self.value = value

def __str__(self):
return self.message
return repr(self.value)
6 changes: 3 additions & 3 deletions chatterbot/adapters/logic/logic_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def process(self, statement):

class EmptyDatasetException(Exception):

def __init__(self, message="An empty collection of elements was received when at least one entry was expected."):
self.message = message
def __init__(self, value="An empty set was received when at least one statement was expected."):
self.value = value

def __str__(self):
return self.message
return repr(self.value)
31 changes: 16 additions & 15 deletions chatterbot/adapters/output/output_format_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,42 @@
from chatterbot.utils.read_input import input_function


JSON = 'json'
TEXT = 'text'
OBJECT = 'object'
VALID_FORMATS = (JSON, TEXT, OBJECT, )


class OutputFormatAdapter(OutputAdapter):

JSON = 'json'
TEXT = 'text'
OBJECT = 'object'
VALID_FORMATS = (JSON, TEXT, OBJECT, )

def __init__(self, *args, **kwargs):
super(OutputFormatAdapter, self).__init__(**kwargs)
self.format = kwargs.get('output_format', 'object')

if self.format not in VALID_FORMATS:
if self.format not in self.VALID_FORMATS:
raise self.UnrecognizedOutputFormatException(
'The output type {} is not a known valid format'.format(
self.format
)
)

def process_response(self, statement):
if self.format == TEXT:
if self.format == self.TEXT:
return statement.text

if self.format == JSON:
if self.format == self.JSON:
return statement.serialize()

# Return the statement OBJECT by default
return statement

class UnrecognizedOutputFormatException(Exception):
def __init__(self, message='The input format was not recognized.'):
super(
OutputFormatAdapter.UnrecognizedOutputFormatException,
self
).__init__(message)
"""
A exception raised when the output format specified is not one of the
options listed in the OutputFormatAdapter.VALID_FORMATS variable.
"""

def __init__(self, value='The input format was not recognized.'):
self.value = value

def __str__(self):
return self.message
return repr(self.value)
6 changes: 3 additions & 3 deletions chatterbot/adapters/storage/storage_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def get_response_statements(self):

class EmptyDatabaseException(Exception):

def __init__(self, message="The database currently contains no entries. At least one entry is expected. You may need to train your chat bot to populate your database."):
self.message = message
def __init__(self, value="The database currently contains no entries. At least one entry is expected. You may need to train your chat bot to populate your database."):
self.value = value

def __str__(self):
return self.message
return repr(self.value)
12 changes: 6 additions & 6 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ def train(self):

class InvalidAdapterException(Exception):

def __init__(self, message='Recieved an unexpected adapter setting.'):
super(ChatBot.InvalidAdapterException, self).__init__(message)
def __init__(self, value='Recieved an unexpected adapter setting.'):
self.value = value

def __str__(self):
return self.message
return repr(self.value)

class TrainerInitializationException(Exception):

def __init__(self, message='The `set_trainer` method must be called before calling `train`.'):
super(ChatBot.TrainerInitializationException, self).__init__(message)
def __init__(self, value='The `set_trainer` method must be called before calling `train`.'):
self.value = value

def __str__(self):
return self.message
return repr(self.value)

0 comments on commit 227f213

Please sign in to comment.