Skip to content

Commit

Permalink
Merge pull request #105 from gunthercox/system_adapters
Browse files Browse the repository at this point in the history
Added system adapter for case when database is empty
  • Loading branch information
gunthercox committed Jan 3, 2016
2 parents 733d143 + d30d565 commit e67c140
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 13 deletions.
3 changes: 3 additions & 0 deletions chatterbot/adapters/adaptation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def __init__(self, **kwargs):
self.logic = MultiLogicAdapter(**kwargs)
self.logic.set_context(self)

# Add required system adapter
self.add_adapter("chatterbot.adapters.logic.NoKnowledgeAdapter")

def add_adapter(self, adapter, **kwargs):
NewAdapter = import_module(adapter)

Expand Down
1 change: 1 addition & 0 deletions chatterbot/adapters/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .closest_match import ClosestMatchAdapter
from .closest_meaning import ClosestMeaningAdapter
from .multi_adapter import MultiLogicAdapter
from .no_knowledge_adapter import NoKnowledgeAdapter
8 changes: 8 additions & 0 deletions chatterbot/adapters/logic/base_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def get(self, input_statement, statement_list=None):
"""
raise AdapterNotImplementedError()

def can_process(self, statement):
"""
Override the can_process method to check if the
storage context is available and there is at least
one statement in the database.
"""
return self.has_storage_context and self.context.storage.count()

def process(self, input_statement):

# Select the closest match to the input statement
Expand Down
9 changes: 9 additions & 0 deletions chatterbot/adapters/logic/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class LogicAdapter(Adapter):
that all logic adapters should implement.
"""

def can_process(self, statement):
"""
A preliminary check that is called to determine if a
logic adapter can process a given statement. By default,
this method returns true but it can be overridden in
child classes as needed.
"""
return True

def process(self, statement):
"""
Method that takes an input statement and returns
Expand Down
9 changes: 5 additions & 4 deletions chatterbot/adapters/logic/multi_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ def process(self, statement):
max_confidence = -1

for adapter in self.adapters:
confidence, output = adapter.process(statement)
if confidence > max_confidence:
result = output
max_confidence = confidence
if adapter.can_process(statement):
confidence, output = adapter.process(statement)
if confidence > max_confidence:
result = output
max_confidence = confidence

return max_confidence, result

Expand Down
23 changes: 23 additions & 0 deletions chatterbot/adapters/logic/no_knowledge_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from .logic import LogicAdapter


class NoKnowledgeAdapter(LogicAdapter):
"""
This is a system adapter that is automatically added
to the list of logic adapters durring initialization.
This adapter is placed at the beginning of the list
to be given the highest priority.
"""

def process(self, statement):
"""
If there are no known responses in the database,
then a confidence of 1 should be returned with
the input statement.
Otherwise, a confidence of 0 should be returned.
"""

if self.context.storage.count():
return 0, statement

return 1, statement
8 changes: 0 additions & 8 deletions chatterbot/chatterbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ def get_response(self, input_text):
if not plugin_response is False:
return self.io.process_response(Statement(plugin_response))

# If no responses exist, return the input statement
if not self.storage.count():
self.storage.update(input_statement)
self.recent_statements.append(input_statement)

# Process the response output with the IO adapter
return self.io.process_response(input_statement)

# Select a response to the input statement
confidence, response = self.logic.process(input_statement)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_adaptation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ def test_add_storage_adapter(self):
self.assertEqual(len(self.adaptation.storage_adapters), 1)

def test_add_logic_adapter(self):
count_before = len(self.adaptation.logic.adapters)

self.adaptation.add_adapter(
"chatterbot.adapters.logic.ClosestMatchAdapter"
)
self.assertEqual(len(self.adaptation.logic.adapters), 1)
self.assertEqual(len(self.adaptation.logic.adapters), count_before + 1)

def test_add_io_adapter(self):
self.adaptation.add_adapter(
Expand Down

0 comments on commit e67c140

Please sign in to comment.