-
Notifications
You must be signed in to change notification settings - Fork 0
/
travelbot.py
111 lines (103 loc) · 2.85 KB
/
travelbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""TravelBot.ipynb"""
from nltk.chat.util import Chat, reflections
#Pairs is a list of patterns and responses.
pairs = [
[
r"(.*)my name is (.*)",
["Hello %2, How are you today ?",]
],
[
r"(.*) your name ?",
["My name is TravelBot.",]
],
[
r"how are you (.*) ?",
["I'm doing very well", "i am great !"]
],
[
r"sorry (.*)",
["Its alright","Its OK, never mind that",]
],
[
r"i'm (.*) (good|well|okay|ok)",
["Nice to hear that","Alright, great !",]
],
[
r"(hi|hey|hello|hola|holla)(.*)",
["Hello", "Hey there",]
],
[
r"what (.*) want ?",
["Make me an offer I can't refuse",]
],
[
r"(.*)created(.*)",
["Yash Menaria created me using Python's NLTK library ","top secret ;)",]
],
[
r"(.*)travel(.*)Sehore|sehore to bhopal|Bhopal(.*)",
["You can travel from Sehore to Bhopal by bus or train. The distance is approximately 40 kilometers."]
],
[
r"(.*)travel(.*)Sehore|sehore to indore|Indore(.*)",
["You can travel from Sehore to Indore by bus or train. The distance is approximately 170 kilometers."]
],
[
r"(.*)travel(.*)Sehore|sehore to ujjain|Ujjain(.*)",
["You can travel from Sehore to Ujjain by bus or train. The distance is approximately 200 kilometers."]
],
[
r"(.*)help(.*)",
["I can help you with travel information.",]
],
[
r"(.*)thank|Thank you | Thanks",
["welcome It's my duty."]
],
[
r"(.*) your name ?",
["My name is TravelBot, but you can just call me bot and I'm a travel assistant.",]
],
[
r"(.*) (location|city) ?",
['I am a virtual assistant, so I exist everywhere!',]
],
[
r"quit",
["Bye for now. Safe travels!","It was nice helping you. Have a great day!"]
],
[
r"(.*)",
["Sorry, I am a travel assistant. I can only help you with travel-related queries."]
],
]
reflections = {
"am": "are",
"was": "were",
"i": "you",
"i'd": "you would",
"i've": "you have",
"i'll": "you will",
"my": "your",
"are": "am",
"you've": "I have",
"you'll": "I will",
"your": "my",
"yours": "mine",
"you": "me",
"me": "you"
}
import random
import re
def travel_response(user_input):
for pattern, responses in pairs:
if re.match(pattern, user_input):
return random.choice(responses)
# Example usage:
user_input = "Can you help me with traveling from Sehore to Bhopal?"
response = travel_response(user_input)
print(response)
print("Hi, I'm TravelBot and If you like to ask \nPlease type lowercase English language to start a conversation. Type quit to leave ")
chat = Chat(pairs, reflections)
chat.converse()