Skip to content

Commit

Permalink
Plugin update
Browse files Browse the repository at this point in the history
- Updated implementation, making it more dynamic
- Expansion for future plugins is easy now
- Cleaned up evaluate_mathematically() tests
  • Loading branch information
DarkmatterVale committed Nov 24, 2015
1 parent d77669a commit fee0369
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions chatterbot/adapters/plugins/__init__.py
@@ -1,2 +1,3 @@
from .plugin import PluginAdapter
from .evaluate_mathematically import EvaluateMathematically
from .plugin_chooser import PluginChooser
18 changes: 16 additions & 2 deletions chatterbot/adapters/plugins/evaluate_mathematically.py
Expand Up @@ -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.
Expand All @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions chatterbot/adapters/plugins/plugin.py
Expand Up @@ -12,3 +12,6 @@ def __init__(self, **kwargs):

def process(self, text):
raise AdapterNotImplementedError()

def should_answer(self, text):
raise AdapterNotImplementedError()
28 changes: 28 additions & 0 deletions 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
13 changes: 7 additions & 6 deletions chatterbot/chatterbot.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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():
Expand Down
3 changes: 0 additions & 3 deletions tests/test_chatbot_output.py
Expand Up @@ -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):
Expand Down

0 comments on commit fee0369

Please sign in to comment.