Skip to content

Commit

Permalink
Add info level logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Aug 21, 2016
1 parent 2b07fdc commit 96d9aef
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
6 changes: 5 additions & 1 deletion chatterbot/adapters/adapter.py
@@ -1,9 +1,13 @@
import logging


class Adapter(object):
"""
An abstract superclass for all adapters
A superclass for all adapter classes.
"""

def __init__(self, **kwargs):
self.logger = kwargs.get('logger', logging.getLogger(__name__))
self.context = None

def set_context(self, context):
Expand Down
14 changes: 14 additions & 0 deletions chatterbot/adapters/logic/base_match.py
Expand Up @@ -43,6 +43,9 @@ def process(self, input_statement):

# Select the closest match to the input statement
confidence, closest_match = self.get(input_statement)
self.logger.info(u'Using "{}" as a close match to "{}"'.format(
input_statement.text, closest_match.text
))

# Save any updates made to the statement by the logic adapter
self.context.storage.update(closest_match)
Expand All @@ -53,9 +56,20 @@ def process(self, input_statement):
)

if response_list:
self.logger.info(
u'Breaking tie between {} optimal responses.'.format(
len(response_list)
)
)
response = self.break_tie(response_list, self.tie_breaking_method)
self.logger.info(u'Tie broken. Using "{}"'.format(response.text))
else:
response = self.context.storage.get_random()
self.logger.info(
u'No response to "{}" found. Using a random response.'.format(
closest_match.text
)
)

# Set confidence to zero if a random response is selected
confidence = 0
Expand Down
6 changes: 5 additions & 1 deletion chatterbot/adapters/logic/closest_match.py
Expand Up @@ -5,7 +5,7 @@

class ClosestMatchAdapter(BaseMatchAdapter):
"""
The ClosestMatchAdapter creates a response by
The ClosestMatchAdapter logic adapter creates a response by
using fuzzywuzzy's process class to extract the most similar
response to the input. This adapter selects a response to an
input statement by selecting the closest known matching
Expand All @@ -23,6 +23,10 @@ def get(self, input_statement):
if not statement_list:
if self.has_storage_context:
# Use a randomly picked statement
self.logger.info(
u'No statements have known responses. ' +
u'Choosing a random response to return.'
)
return 0, self.context.storage.get_random()
else:
raise self.EmptyDatasetException()
Expand Down
5 changes: 4 additions & 1 deletion chatterbot/adapters/logic/closest_meaning.py
@@ -1,5 +1,4 @@
from .base_match import BaseMatchAdapter

from chatterbot.utils.pos_tagger import POSTagger
from chatterbot.utils.stop_words import StopWordsManager
from chatterbot.utils.word_net import Wordnet
Expand Down Expand Up @@ -85,6 +84,10 @@ def get(self, input_statement):
if not statement_list:
if self.has_storage_context:
# Use a randomly picked statement
self.logger.info(
u'No statements have known responses. ' +
u'Choosing a random response to return.'
)
return 0, self.context.storage.get_random()
else:
raise self.EmptyDatasetException()
Expand Down
14 changes: 14 additions & 0 deletions chatterbot/adapters/logic/mixins.py
@@ -1,3 +1,6 @@
import logging


class TieBreaking(object):
"""
TieBreaking determines which response should be used in the event
Expand All @@ -22,9 +25,12 @@ def get_most_frequent_response(self, input_statement, response_list):
"""
Returns the statement with the greatest number of occurrences.
"""
logger = logging.getLogger(__name__)
matching_response = None
occurrence_count = -1

logger.info(u'Selecting response with greatest number of occurrences.')

for statement in response_list:
count = statement.get_response_count(input_statement)

Expand All @@ -40,11 +46,19 @@ def get_first_response(self, response_list):
"""
Return the first statement in the response list.
"""
logger = logging.getLogger(__name__)
logger.info(u'Selecting first response from list of {} options.'.format(
len(response_list)
))
return response_list[0]

def get_random_response(self, response_list):
"""
Choose a random response from the selection.
"""
from random import choice
logger = logging.getLogger(__name__)
logger.info(u'Selecting a response from list of {} options.'.format(
len(response_list)
))
return choice(response_list)
11 changes: 11 additions & 0 deletions chatterbot/chatterbot.py
Expand Up @@ -6,6 +6,7 @@
from .trainers import Trainer
from .utils.queues import ResponseQueue
from .utils.module_loading import import_module
import logging


class ChatBot(object):
Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(self, name, **kwargs):
self.output.set_context(self)

self.trainer = Trainer(self.storage)
self.logger = kwargs.get('logger', logging.getLogger(__name__))

def add_adapter(self, adapter, **kwargs):
self.validate_adapter_class(adapter, LogicAdapter)
Expand Down Expand Up @@ -132,21 +134,30 @@ def get_response(self, input_item):
Return the bot's response based on the input.
"""
input_statement = self.input.process_input(input_item)
self.logger.info(u'Recieved input statement: {}'.format(input_statement.text))

existing_statement = self.storage.find(input_statement.text)

if existing_statement:
self.logger.info(u'{} is a known statement'.format(input_statement.text))
input_statement = existing_statement
else:
self.logger.info(u'{} is not a known statement'.format(input_statement.text))

# Select a response to the input statement
confidence, response = self.logic.process(input_statement)
self.logger.info(u'Selecting "{}" as response with a confidence of {}'.format(response.text, confidence))

previous_statement = self.get_last_response_statement()

if previous_statement:
input_statement.add_response(
Response(previous_statement.text)
)
self.logger.info(u'Adding the previous statement "{}" as response to {}'.format(
previous_statement.text,
input_statement.text
))

# Update the database after selecting a response
self.storage.update(input_statement)
Expand Down
4 changes: 4 additions & 0 deletions examples/terminal_example.py
@@ -1,6 +1,10 @@
from chatterbot import ChatBot
import logging


# Uncomment the following line to enable verbose logging
# logging.basicConfig(level=logging.INFO)

# Create a new instance of a ChatBot
bot = ChatBot("Terminal",
storage_adapter="chatterbot.adapters.storage.JsonFileStorageAdapter",
Expand Down
4 changes: 4 additions & 0 deletions examples/terminal_mongo_example.py
@@ -1,6 +1,10 @@
from chatterbot import ChatBot
import logging


# Uncomment the following line to enable verbose logging
# logging.basicConfig(level=logging.INFO)

# Create a new ChatBot instance
bot = ChatBot("Terminal",
storage_adapter="chatterbot.adapters.storage.MongoDatabaseAdapter",
Expand Down

0 comments on commit 96d9aef

Please sign in to comment.