Skip to content

Commit

Permalink
Simplify list trainer code.
Browse files Browse the repository at this point in the history
  • Loading branch information
gunthercox committed Aug 13, 2016
1 parent fe1bcfa commit 2f7ea84
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions chatterbot/training/trainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Trainer(object):

def __init__(self, storage, **kwargs):
self.kwargs = kwargs
self.storage = storage
self.corpus = Corpus()

Expand All @@ -15,23 +14,27 @@ def train(self):

class ListTrainer(Trainer):

def get_or_create(self, statement_text):
"""
Return a statement if it exists.
Create and return the statement if it does not exist.
"""
statement = self.storage.find(statement_text)

if not statement:
statement = Statement(statement_text)

return statement

def train(self, conversation):
statement_history = []

for text in conversation:
statement = self.storage.find(text)
statement = self.get_or_create(text)

# Create the statement if a match was not found
if not statement:
statement = Statement(text)

previous_statement = None
if statement_history:
previous_statement = statement_history[-1]

if previous_statement:
statement.add_response(
Response(previous_statement.text)
Response(statement_history[-1].text)
)

statement_history.append(statement)
Expand Down

0 comments on commit 2f7ea84

Please sign in to comment.