Skip to content

Commit

Permalink
Merge b29939c into 63a11eb
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Sep 19, 2018
2 parents 63a11eb + b29939c commit 34dec93
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 38 deletions.
8 changes: 2 additions & 6 deletions chatterbot/chatterbot.py
Expand Up @@ -90,19 +90,15 @@ def initialize(self):
"""
self.logic.initialize()

def get_response(self, input_item, conversation='default'):
def get_response(self, input_item):
"""
Return the bot's response based on the input.
:param input_item: An input value.
:param conversation: A string of characters unique to the conversation.
:returns: A response to the input.
:rtype: Statement
"""
input_statement = self.input.process_input(
input_item,
conversation
)
input_statement = self.input.process_input(input_item)

# Preprocess the input statement
for preprocessor in self.preprocessors:
Expand Down
5 changes: 2 additions & 3 deletions chatterbot/input/gitter.py
Expand Up @@ -147,7 +147,7 @@ def remove_mentions(self, text):

return text_without_mentions

def process_input(self, statement, conversation):
def process_input(self, statement):
new_message = False

while not new_message:
Expand All @@ -158,9 +158,8 @@ def process_input(self, statement, conversation):
sleep(self.sleep_time)

text = self.remove_mentions(data['text'])
statement = Statement(text)

return statement
return Statement(text)

class HTTPStatusException(Exception):
"""
Expand Down
4 changes: 2 additions & 2 deletions chatterbot/input/hipchat.py
Expand Up @@ -76,14 +76,14 @@ def get_most_recent_message(self, room_id_or_name):
return None
return items[-1]

def process_input(self, statement, conversation):
def process_input(self, statement):
"""
Process input from the HipChat room.
"""
new_message = False

conversation = self.chatbot.storage.filter(
text=conversation,
conversation=statement.conversation,
order_by=['id']
)

Expand Down
2 changes: 1 addition & 1 deletion chatterbot/input/input_adapter.py
Expand Up @@ -7,7 +7,7 @@ class InputAdapter(Adapter):
interface that all input adapters should implement.
"""

def process_input(self, statement, conversation):
def process_input(self, statement):
"""
Returns a statement object based on the input source.
"""
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/input/mailgun.py
Expand Up @@ -48,7 +48,7 @@ def get_message(self, url):
auth=('api', self.api_key)
)

def process_input(self, statement, conversation):
def process_input(self, statement):
urls = self.get_stored_email_urls()
url = list(urls)[0]

Expand Down
2 changes: 1 addition & 1 deletion chatterbot/input/microsoft.py
Expand Up @@ -88,7 +88,7 @@ def get_most_recent_message(self):
return data['messages'][last_msg - 1]
return None

def process_input(self, statement, conversation):
def process_input(self, statement):
new_message = False
data = None
while not new_message:
Expand Down
2 changes: 1 addition & 1 deletion chatterbot/input/terminal.py
Expand Up @@ -8,7 +8,7 @@ class TerminalAdapter(InputAdapter):
communicate through the terminal.
"""

def process_input(self, *args, **kwargs):
def process_input(self, *args):
"""
Read the user's input from the terminal.
"""
Expand Down
10 changes: 6 additions & 4 deletions chatterbot/input/variable_input_type_adapter.py
Expand Up @@ -25,22 +25,24 @@ def detect_type(self, statement):
)
)

def process_input(self, statement, conversation):
def process_input(self, statement):
DEFAULT_CONVERSATION = 'default'

input_type = self.detect_type(statement)

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

if not statement.conversation:
statement.conversation = conversation
statement.conversation = DEFAULT_CONVERSATION

return statement

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

# Convert input dictionary into a statement object
Expand All @@ -50,7 +52,7 @@ def process_input(self, statement, conversation):
del input_json['text']

if 'conversation' not in input_json:
input_json['conversation'] = conversation
input_json['conversation'] = DEFAULT_CONVERSATION

return Statement(text, **input_json)

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Expand Up @@ -194,5 +194,5 @@

# Configuration for intersphinx
intersphinx_mapping = {
'python': ('https://docs.python.org/3.4', None)
'python': ('https://docs.python.org/3', None)
}
6 changes: 2 additions & 4 deletions examples/django_app/example_app/views.py
Expand Up @@ -32,10 +32,8 @@ def post(self, request, *args, **kwargs):
]
}, status=400)

response = self.chatterbot.get_response(
input_data,
input_data.get('conversation', 'default')
)
response = self.chatterbot.get_response(input_data)

response_data = response.serialize()

return JsonResponse(response_data, status=200)
Expand Down
Expand Up @@ -10,6 +10,7 @@ def test_filter_selection(self):
"""
Test that repetitive responses are filtered out of the results.
"""
from chatterbot.conversation import Statement
from chatterbot.filters import RepetitiveResponseFilter
from chatterbot.trainers import ListTrainer

Expand All @@ -29,8 +30,9 @@ def test_filter_selection(self):
'How are you?'
])

first_response = self.chatbot.get_response('Hello', conversation='training')
second_response = self.chatbot.get_response('Hello', conversation='training')
statement = Statement(text='Hello', conversation='training')
first_response = self.chatbot.get_response(statement)
second_response = self.chatbot.get_response(statement)

self.assertEqual('I am good.', first_response.text)
self.assertEqual('Hi', second_response.text)
2 changes: 1 addition & 1 deletion tests/input_adapter_tests/test_gitter_input_adapter.py
Expand Up @@ -120,5 +120,5 @@ def test_remove_mentions(self):

def test_process_input(self):
statement = Statement('Hello')
data = self.adapter.process_input(statement, 'test')
data = self.adapter.process_input(statement)
self.assertEqual('Hello', data)
5 changes: 1 addition & 4 deletions tests/input_adapter_tests/test_input_adapter.py
Expand Up @@ -16,7 +16,4 @@ def setUp(self):

def test_process_response(self):
with self.assertRaises(InputAdapter.AdapterMethodNotImplementedError):
self.adapter.process_input(
'test statement',
'test conversation'
)
self.adapter.process_input('test statement')
2 changes: 1 addition & 1 deletion tests/input_adapter_tests/test_microsoft_input_adapter.py
Expand Up @@ -124,5 +124,5 @@ def test_start_conversation(self):

def test_process_input(self):
statement = Statement('Hi! What is your name?')
data = self.adapter.process_input(statement, conversation='test')
data = self.adapter.process_input(statement)
self.assertEqual('Hi! What is your name?', data)
9 changes: 4 additions & 5 deletions tests/input_adapter_tests/test_variable_input_type_adapter.py
Expand Up @@ -7,31 +7,30 @@ class VariableInputTypeAdapterTests(TestCase):

def setUp(self):
self.adapter = VariableInputTypeAdapter()
self.conversation = 'test'

def test_statement_returned_dict(self):
data = {
'text': 'Robot ipsum datus scan amet.',
'in_response_to': []
}
response = self.adapter.process_input(data, self.conversation)
response = self.adapter.process_input(data)

self.assertEqual(response.text, data['text'])

def test_statement_returned_text(self):
text = 'The test statement to process is here.'
response = self.adapter.process_input(text, self.conversation)
response = self.adapter.process_input(text)

self.assertEqual(response.text, text)

def test_statement_returned_object(self):
statement = Statement('The test statement to process is here.')
response = self.adapter.process_input(statement, self.conversation)
response = self.adapter.process_input(statement)

self.assertEqual(response.text, statement.text)

def test_invalid_input_type(self):
data = ['A list', 'of text', 'is an', 'invalid input type.']

with self.assertRaises(VariableInputTypeAdapter.UnrecognizedInputFormatException):
self.adapter.process_input(data, self.conversation)
self.adapter.process_input(data)
Expand Up @@ -15,6 +15,6 @@ def test_variable_type_input_adapter(self):

statement = Statement(text='_')

result = adapter.process_input(statement, 'test')
result = adapter.process_input(statement)

self.assertEqual(result.text, '_')

0 comments on commit 34dec93

Please sign in to comment.