Skip to content

Commit

Permalink
Merge pull request #340 from gunthercox/config
Browse files Browse the repository at this point in the history
Allow chat bot to be created from json config file
  • Loading branch information
gunthercox authored Oct 12, 2016
2 parents f59a99a + e94fe4a commit 4a47fe1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ def train(self):
"""
return self.trainer.train

@classmethod
def from_config(self, config_file_path):
import json
with open(config_file_path, 'r') as config_file:
data = json.load(config_file)

name = data.pop('name')

return ChatBot(name, **data)

class InvalidAdapterException(Exception):

def __init__(self, value='Recieved an unexpected adapter setting.'):
Expand Down
3 changes: 1 addition & 2 deletions tests/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ def get_kwargs(self):
kwargs = super(ChatBotMongoTestCase, self).get_kwargs()
kwargs['database'] = self.random_string()
kwargs['storage_adapter'] = 'chatterbot.adapters.storage.MongoDatabaseAdapter'
return kwargs

return kwargs
26 changes: 26 additions & 0 deletions tests/test_chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,29 @@ def test_response_extra_data(self):

self.assertIn("test", saved_statement.extra_data)
self.assertEqual(1, saved_statement.extra_data["test"])


class ChatBotConfigFileTestCase(ChatBotTestCase):

def setUp(self):
super(ChatBotConfigFileTestCase, self).setUp()
import json
self.config_file_path = './test-config.json'
self.data = self.get_kwargs()
self.data['name'] = 'Config Test'

with open(self.config_file_path, 'w+') as config_file:
json.dump(self.data, config_file)

def tearDown(self):
super(ChatBotConfigFileTestCase, self).tearDown()
import os

if os.path.exists(self.config_file_path):
os.remove(self.config_file_path)

def test_read_from_config_file(self):
from chatterbot import ChatBot
self.chatbot = ChatBot.from_config(self.config_file_path)

self.assertEqual(self.chatbot.name, self.data['name'])

0 comments on commit 4a47fe1

Please sign in to comment.