From fee03692121859821af921ebef27c398671264b6 Mon Sep 17 00:00:00 2001 From: Vale Date: Tue, 24 Nov 2015 13:53:31 -0500 Subject: [PATCH] Plugin update - Updated implementation, making it more dynamic - Expansion for future plugins is easy now - Cleaned up evaluate_mathematically() tests --- chatterbot/adapters/plugins/__init__.py | 1 + .../plugins/evaluate_mathematically.py | 18 ++++++++++-- chatterbot/adapters/plugins/plugin.py | 3 ++ chatterbot/adapters/plugins/plugin_chooser.py | 28 +++++++++++++++++++ chatterbot/chatterbot.py | 13 +++++---- tests/test_chatbot_output.py | 3 -- 6 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 chatterbot/adapters/plugins/plugin_chooser.py diff --git a/chatterbot/adapters/plugins/__init__.py b/chatterbot/adapters/plugins/__init__.py index bc4c4e41c..885b7faf6 100644 --- a/chatterbot/adapters/plugins/__init__.py +++ b/chatterbot/adapters/plugins/__init__.py @@ -1,2 +1,3 @@ from .plugin import PluginAdapter from .evaluate_mathematically import EvaluateMathematically +from .plugin_chooser import PluginChooser diff --git a/chatterbot/adapters/plugins/evaluate_mathematically.py b/chatterbot/adapters/plugins/evaluate_mathematically.py index 57b1c1fc9..40ed7a349 100644 --- a/chatterbot/adapters/plugins/evaluate_mathematically.py +++ b/chatterbot/adapters/plugins/evaluate_mathematically.py @@ -4,6 +4,20 @@ class EvaluateMathematically(PluginAdapter): + def should_answer(self, input_text): + """ + Determines whether it is appropriate for this plugin + to respond to the user input. + """ + + response = self.process( input_text ) + + if response == False: + return False + else: + return True + + def process(self, input_text): """ Takes a statement string. @@ -18,9 +32,9 @@ def process(self, input_text): try: string += '= ' + str( eval( string ) )#self.evaluate( string ) ) - return string, True + return string except: - return string, False + return False def simplify_chunks(self, input_text): diff --git a/chatterbot/adapters/plugins/plugin.py b/chatterbot/adapters/plugins/plugin.py index f938e832e..556d2b4b6 100644 --- a/chatterbot/adapters/plugins/plugin.py +++ b/chatterbot/adapters/plugins/plugin.py @@ -12,3 +12,6 @@ def __init__(self, **kwargs): def process(self, text): raise AdapterNotImplementedError() + + def should_answer(self, text): + raise AdapterNotImplementedError() diff --git a/chatterbot/adapters/plugins/plugin_chooser.py b/chatterbot/adapters/plugins/plugin_chooser.py new file mode 100644 index 000000000..9cbbda51f --- /dev/null +++ b/chatterbot/adapters/plugins/plugin_chooser.py @@ -0,0 +1,28 @@ +from evaluate_mathematically import EvaluateMathematically + +class PluginChooser(): + + def __init__( self, **kwargs ): + """ + Initializes all plugins & initial variables. + """ + + self.plugins = [ + EvaluateMathematically(**kwargs) + ] + + + def choose( self, input_statement ): + """ + Used to determine whether a plugin should be used + to "answer" or reply to the user input. + """ + + # Testing each plugin to determine whether it should be used to answer user input + for plugin in self.plugins: + should_use = plugin.should_answer( input_statement.text ) + + if should_use: + return plugin.process( input_statement.text ) + + return False diff --git a/chatterbot/chatterbot.py b/chatterbot/chatterbot.py index 4c566d801..a8db8a296 100644 --- a/chatterbot/chatterbot.py +++ b/chatterbot/chatterbot.py @@ -19,8 +19,8 @@ def __init__(self, name, **kwargs): "chatterbot.adapters.io.TerminalAdapter" ) - MathematicalPlugin = import_module("chatterbot.adapters.plugins.EvaluateMathematically") - self.math_plugin = MathematicalPlugin(**kwargs) + PluginChooser = import_module("chatterbot.adapters.plugins.PluginChooser") + self.plugin_chooser = PluginChooser(**kwargs) StorageAdapter = import_module(storage_adapter) self.storage = StorageAdapter(**kwargs) @@ -84,11 +84,12 @@ def get_response(self, input_text): input_statement = Statement(input_text) # Applying plugin logic to see whether the chatbot should respond in this way - math_response, is_response = self.math_plugin.process( input_statement.text ) + plugin_response = self.plugin_chooser.choose( input_statement ) - # If the question was a mathematical question, use the answer as a response (and do not update the database) - if is_response: - return math_response + if plugin_response == False: + pass + else: + return plugin_response # If no responses exist, return the input statement if not self.storage.count(): diff --git a/tests/test_chatbot_output.py b/tests/test_chatbot_output.py index 6d11e7b17..74b25ebff 100644 --- a/tests/test_chatbot_output.py +++ b/tests/test_chatbot_output.py @@ -193,9 +193,6 @@ def test_evaluate_mathematically(self): self.assertEqual(third_response, "( 100 + ( ( 1000 * ( 2 ) ) ) ) = 2100") self.assertEqual(fourth_response, "( 4 + ( 100 + ( ( 100 * ( 2 ) ) ) ) ) = 304") self.assertEqual(fifth_response, "( 100 + 400 ) = 500") - # The following assert statements break the Travis-Ci build because division is handled differently in Python 3.x than Python 2.7 - #self.assertEqual(sixth_response, "( 100 / 100 ) = 1") - #self.assertEqual(seventh_response, "( 1000 + 200 + 4 ) / ( 100 ) = 12") class ChatterBotStorageIntegrationTests(UntrainedChatBotTestCase):