Skip to content

Instantly share code, notes, and snippets.

@VishalTaj
Created February 21, 2021 15:01
Show Gist options
  • Save VishalTaj/fee8af2babc5bef7a7fdc153e55d0e95 to your computer and use it in GitHub Desktop.
Save VishalTaj/fee8af2babc5bef7a7fdc153e55d0e95 to your computer and use it in GitHub Desktop.
Rulebased chat bot.
import re
keywords = {
'greet': ['.*hello.*', '.*hi.*', '.*howdy.*', '.*hullo.*', '.*how-do-you-do.*'],
'track': ['.*shipment.*', '.*track.*', '.*estimated.*delivery.*'],
'tracking_fullfilment': ['.*myorder:*.'],
'stock': ['.*stock.*', '.*out-of-stock.*'],
'stock_fullfilment': ['.*product_stock:.*']
}
patterns = {}
for intent, keys in keywords.items():
patterns[intent]=re.compile('|'.join(keys))
responses={
'greet':'Hello! How can I help you?',
'track': 'Please type your tracking no. eg: myorder:143567',
'tracking_fullfilment': 'your order is processing',
'stock': 'Please type product id. eg: product_stock:123434',
'stock_fullfilment': 'This product is in stock',
'default': 'I dont quite understand. Could you repeat that?'
}
def match_intent(message):
print(message)
matched_intent = None
for intent,pattern in patterns.items():
if re.search(pattern,message):
matched_intent=intent
return matched_intent
def respond(message):
intent=match_intent(message)
key='default'
if intent in responses:
key=intent
return responses[key]
def send_message(message):
return respond(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment