Skip to content

Commit

Permalink
Add event flags
Browse files Browse the repository at this point in the history
  • Loading branch information
loristns committed Jun 12, 2018
1 parent 121d559 commit afcfdb4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
17 changes: 14 additions & 3 deletions examples/fake_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Set up an extractor to retrieve place names in queries.
places_extractor = CRFExtractor({
"For eg. at Suite 101, Johnshon Avenue in London.": ('Suite', '101', 'Johnshon', 'Avenue,', 'London'),
"For eg. at Suite 101, johnshon avenue in London.": ('Suite', '101', 'johnshon', 'avenue,', 'London'),
"The soldiers clashed at Gettysburg.": ('Gettysburg',),
"The treaty was signed at Versailles.": ('Versailles',),
"Give me the forecast in New York !": ('New', 'York'),
Expand Down Expand Up @@ -32,7 +32,7 @@ def smalltalk(raw, context):
An intent to answer to greetings and appreciations.
Answer the same with an exclamation mark (!).
"""
return raw + '!', context
return raw + ' !', context


@bot.intent([
Expand All @@ -50,12 +50,23 @@ def weather(raw, context):

if context['place']:
answer = "In {}, it will be {}".format(context['place'], random.choice(['sunny', 'cloudy', 'rainy']))
context.event_flag = 'should_continue'
return answer, context
else:
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..."
del context['continue']
return answer, context
else:
return bot.prompt("Do you want to continue ?", key='continue', callback=should_continue, context=context)


bot.train()

while True:
print(bot.predict(text=input('> ')))
print('\n'.join(bot.predict(text=input('> '))))
28 changes: 23 additions & 5 deletions kadot/bot_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,25 @@ def __init__(self, name: str):
self.age = 0
self.data = {}
self.data_track = {} # Indicate if data are expired
self.event_flag = None
self.intent_flag = None

def __setitem__(self, key, value):
if value or key not in self.data.keys(): # No empty data in the context
if value: # No empty data in the context
self.data[key] = value
self.data_track[key] = self.age

def __delitem__(self, key):
del self.data_track[key], self.data[key]

def __getitem__(self, item):
if self.data_track[item] + 2 < self.age: # If data is too old
self.data[item] = ''
try:
if self.data_track[item] + 3 < self.age: # If data is too old
del self.data[item], self.data_track[item]

return self.data[item]
return self.data[item]
except KeyError:
return None

def step(self):
self.age += 1
Expand Down Expand Up @@ -93,6 +97,9 @@ def wrapper(intent_function):

return wrapper

def hidden_intent(self):
return self.intent([], entities=[])

def prompt(self,
message: Any,
key: str,
Expand Down Expand Up @@ -157,8 +164,19 @@ def predict(self, text: str, conversation: Optional[Any] = None):
for entity_name in self.intents[best_intent].entities:
context[entity_name] = ' '.join(self.entities[entity_name].predict(text)[0])

output = []
intent_output, context = self.intents[best_intent].run(text, context)
output.append(intent_output)
context.step()
output, context = self.intents[best_intent].run(text, context)

while context.event_flag:
intent = context.event_flag
context.event = None
event_output, context = self.intents[intent].run(text, context)
context.step()

output.append(event_output)

self.contexts[conversation] = context

return output

0 comments on commit afcfdb4

Please sign in to comment.