diff --git a/chatterbot/adapters/input/variable_input_type_adapter.py b/chatterbot/adapters/input/variable_input_type_adapter.py index ca6867dac..5461ba527 100644 --- a/chatterbot/adapters/input/variable_input_type_adapter.py +++ b/chatterbot/adapters/input/variable_input_type_adapter.py @@ -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) @@ -41,15 +39,15 @@ 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"]) @@ -57,11 +55,13 @@ def process_input(self, statement): 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) diff --git a/chatterbot/adapters/logic/logic_adapter.py b/chatterbot/adapters/logic/logic_adapter.py index 2fdeb7218..86736033a 100644 --- a/chatterbot/adapters/logic/logic_adapter.py +++ b/chatterbot/adapters/logic/logic_adapter.py @@ -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) diff --git a/chatterbot/adapters/output/output_format_adapter.py b/chatterbot/adapters/output/output_format_adapter.py index 274a856b9..aea31d723 100644 --- a/chatterbot/adapters/output/output_format_adapter.py +++ b/chatterbot/adapters/output/output_format_adapter.py @@ -2,19 +2,18 @@ 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 @@ -22,21 +21,23 @@ def __init__(self, *args, **kwargs): ) 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) diff --git a/chatterbot/adapters/storage/storage_adapter.py b/chatterbot/adapters/storage/storage_adapter.py index 0c9e53d63..3db4fd45f 100644 --- a/chatterbot/adapters/storage/storage_adapter.py +++ b/chatterbot/adapters/storage/storage_adapter.py @@ -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) diff --git a/chatterbot/chatterbot.py b/chatterbot/chatterbot.py index e6b7be80f..b4f1a18af 100644 --- a/chatterbot/chatterbot.py +++ b/chatterbot/chatterbot.py @@ -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)