Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def main():
logger = logging.getLogger()
confidence_threshold = .5
nlu = NaturalLanguageUnderstander(confidence_threshold, logger)
# Register the NLU messages we want
nlu.register_message(YesNoMessage)
nlu.register_message(SearchMessage)
nlu.register_message(SystemMessage)
fil = open('INGREDIENTS.txt', 'r')
# Skip comments and empty lines
input_messages = (l.strip() for l in fil if not re.search('^\s*($|#)', l))
for line in islice(input_messages, 0, LIMIT):
messages = nlu.parse_input(line)
print line
for message in messages:
print message.__class__
print pprint.pformat(message.frame)
print
# use the NLG yet; it uses a special 'echo' message that passes strings
# through the NLG to the user.
# If we didn't understand the user:
if not parsed_input:
return ContentPlanMessage("unknown")
# For now, take first (highest confidence) message.
# If the user is searching for recipes:
if isinstance(parsed_input[0], SearchMessage):
return self._handle_search_message(parsed_input[0])
# Handle system messages like 'restart' and 'exit'
elif any(isinstance(i, SystemMessage) for i in parsed_input):
for message in parsed_input:
if isinstance(message, SystemMessage):
sys_message = message
break
if sys_message.frame['action'] == 'restart':
self._go_to_start_state()
return ContentPlanMessage("echo",
message="Okay, let's start over.")
elif sys_message.frame['action'] == 'exit':
return ContentPlanMessage("echo",
message="TODO: EXIT HERE")
# If the user answers 'yes', show them the recipes:
elif isinstance(parsed_input[0], YesNoMessage) and self.search_results:
if parsed_input[0].getDecision():
return ContentPlanMessage("show_recipe",
recipe=self.search_results[0])
else:
# users to search for recipes and choose whether or not they want to
# display the titles of the recipes in the search results. It doesn't
# use the NLG yet; it uses a special 'echo' message that passes strings
# through the NLG to the user.
# If we didn't understand the user:
if not parsed_input:
return ContentPlanMessage("unknown")
# For now, take first (highest confidence) message.
# If the user is searching for recipes:
if isinstance(parsed_input[0], SearchMessage):
return self._handle_search_message(parsed_input[0])
# Handle system messages like 'restart' and 'exit'
elif any(isinstance(i, SystemMessage) for i in parsed_input):
for message in parsed_input:
if isinstance(message, SystemMessage):
sys_message = message
break
if sys_message.frame['action'] == 'restart':
self._go_to_start_state()
return ContentPlanMessage("echo",
message="Okay, let's start over.")
elif sys_message.frame['action'] == 'exit':
return ContentPlanMessage("echo",
message="TODO: EXIT HERE")
# If the user answers 'yes', show them the recipes:
elif isinstance(parsed_input[0], YesNoMessage) and self.search_results:
if parsed_input[0].getDecision():
return ContentPlanMessage("show_recipe",