Created
February 21, 2021 15:01
-
-
Save VishalTaj/fee8af2babc5bef7a7fdc153e55d0e95 to your computer and use it in GitHub Desktop.
Rulebased chat bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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