Skip to content

Commit

Permalink
Adding option input for the Bot Engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
loristns committed Jun 14, 2018
1 parent db6cff5 commit e4f29e4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
30 changes: 27 additions & 3 deletions examples/fake_bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from kadot.bot_engine import Agent
from kadot.classifiers import FuzzyClassifier
from kadot.models import CRFExtractor
import random

Expand All @@ -15,6 +16,21 @@
crf_filename='.crf_city_extractor_city'
)

yes_no = FuzzyClassifier({
"yes": 'y',
"yep": 'y',
"of course": 'y',
"affirmative": 'y',
"ok": 'y',
"continue": 'y',
"nope": 'n',
"no": 'n',
"nay": 'n',
"negative": 'n',
"end": 'n',
}
)

bot = Agent()
bot.add_entity('place', places_extractor)

Expand Down Expand Up @@ -53,17 +69,25 @@ def weather(raw, context):
context.event_flag = 'should_continue'
return answer, context
else:
return bot.prompt("In which city ?", key='place', callback=weather, context=context)
return bot.prompt("In which city ?", key='place',
callback=weather, context=context)


@bot.hidden_intent()
def should_continue(raw, context):
if context['continue']:
answer = "For now, I can't understand if you want to continue or not..."
if context['continue'] == 'y':
answer = "Ok, fine."
else: # context['continue'] == 'n'
answer = "Ok bye bye"

del context['continue']
return answer, context

else:
return bot.prompt("Do you want to continue ?", key='continue', callback=should_continue, context=context)
return bot.option("Do you want to continue ?", key='continue',
classifier=yes_no, callback=should_continue,
context=context)


bot.train()
Expand Down
30 changes: 30 additions & 0 deletions kadot/bot_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ def _prompt(raw, ctx):

return message, context

def option(self,
message: Any,
key: str,
classifier,
callback: Callable[[str, Context], Any],
context: Context):

def _option(raw, ctx):
"""
An intent to retrieve the user's input and put it's
classification in the context.
"""

best_class, best_proba = '', 0

for e_class, e_proba in classifier.predict(raw).items():
if e_proba >= best_proba:
best_class, best_proba = e_class, e_proba

ctx[key] = best_class

return callback(raw, ctx)

self.intents['_option'] = Intent(name='_option', func=_option)
context.intent_flag = '_option'

return message, context

def _get_training_dataset(self):
training_dataset = {}

Expand Down Expand Up @@ -172,6 +200,8 @@ def predict(self, text: str, conversation: Optional[Any] = None):
while context.event_flag:
intent = context.event_flag
context.event_flag = None

logger.info("Event flag for {}.".format(intent))
event_output, context = self.intents[intent].run(text, context)
context.step()

Expand Down

0 comments on commit e4f29e4

Please sign in to comment.