-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanic.py
More file actions
executable file
·197 lines (181 loc) · 8.5 KB
/
panic.py
File metadata and controls
executable file
·197 lines (181 loc) · 8.5 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
# -*- coding: utf-8 -*-
degree = u'\N{DEGREE SIGN}'
import pywapi
import difflib
import re
import datetime
import random
import time
datetoday = datetime.date.today()
timetoday = datetime.datetime.today().time()
datestr = datetoday.strftime("%d/%m/%Y")
timestr = timetoday.strftime("%H:%M")
weather = pywapi.get_weather_from_weather_com('UKXX0037')
try:
weatherStr = 'The weather in Colchester is ' + weather['current_conditions']['text'].lower() + ' and ' + weather['current_conditions']['temperature'] + degree + 'C'
except:
weatherStr = 'Cannot get weather data at this time. Try looking outside!'
logo = """\
.. .777777777777$$$.. . . .
. . ...777::777777777$$$$+.. .
. . ....77....7777777$$$$$$.... .. .
. .......777,:777777$$$$$$$$.......
........7777777777$$$$$$$$$....... . .
.................~$$$$$$$$$........
...77777777777777$$$$$$$$$$$$.======,..
.7777777777777$$$$$$$$$$$$$$$.========
I777777777777$$$$$$$$$$$$$$$$.========~
7777777777$7$$$$$$$$$$$$$$$$$.=========. ----------Welcome to PAnIC!!!!---------
77777777777$$$$$$$$$$$$$$$$$..=======++. Python Artificial Intelligence Computer
77777777$$$$$$$$$$$$$$$$$....=====+=+++.
7777777$$$....====================+++++.
777777$$$..======================++++++.
777$$$$$$.====================+++++++++.
77$$$$$$?.====================++++++++,
$$$$$$$?.=================+++++++++++
I$$$$$?.================++++++++++.. .
........=========,................. .
........=============++++++....... .
.... .....============++.,+++..... . .
. ....==========+++....++.... . .
. ..=========+++++.,+++.. .. ..
. . .======+++++++++. . .. ..
. .. .. .+++.. .. .
"""
directions = """\
1. Head southeast on Park Road toward Boundary Road
2. Turn left onto Boundary Road
3. Turn left onto Colchester Road/B1028
4. Slight left onto St. Andrew's Avenue (A133)
Go through 1 roundabout
5. At the roundabout, take the 1st exit and stay on St. Andrew's Avenue (A133)
Continue to follow A133
Go through 5 roundabouts
6. At the roundabout, take the 1st exit onto North Station Road
7. At the roundabout, take the 1st exit onto Middleborough
8. Continue onto North Hill
Destination will be on the right
"""
Database = [
['your name', 'My name is PAnIC, Python Artificial Intelligence Computer.'],
['my name', 'I am not sure, you have not told me yet. What is your name?'],
['old you', 'I do not have a birthday. I am not living and thus I was never born. However I was created by Geraint White, Charlie Callow and Henry Plumb on 4th July 2013.'],
['you ok', 'I am very well thank you. How are you?', 'Not too bad, yourself?', 'I\'m fine, how about you?', 'Me? I\'m great, you?'],
['you live', 'I exist everywhere and therefore have no one location.'],
['old I', 'I am not sure, you have not told me yet. How old are you?'],
['hello', 'Good day earthling!', 'Greetings', 'Howdy!', 'Hi there!'],
['date', 'The date is ' + datestr],
['time', 'The time is ' + timestr],
['weather', weatherStr],
['meaning of life', 'The answer to life, the universe and everything is 42'],
['your opinion', 'I am a neutral party and therefore I like to keep my opinion to myself.', 'Always enjoyable.', 'Personally I\'m not interested at all.'],
['favourite food', 'Raspberry Pie'],
['you look like', 'Lots of symbols and characters'],
['where am i', 'You are currently at the University of Essex'],
['favourite colour', 'My favourite colour is green, the colour of circuit boards'],
['direction Colchester Sixth Form', directions],
['tell joke', "Three statisticians go out hunting together. After a while they spot a solitary rabbit. The first statistician takes aim and overshoots. The second aims and undershoots. The third shouts out: We got him!", "There are 10 types of people in the world: those who understand binary, and those who don't", "Bad command or file name! Go stand in the corner", "Does fuzzy logic tickle?", "Helpdesk : Sir, you need to add 10GB space to your HD , Customer : Could you please tell where I can download that?", "Sorry, the password you tried is already being used by Dorthy, please try something else.", "root:> Sorry, you entered the wrong password, the correct password is 'a_49qwXk'", "Man is the best computer we can put aboard a spacecraft...and the only one that can be mass produced with unskilled labour"],
['thank you', 'You\'re welcome']
]
badFeel = ['sad', 'bad', 'terrible', 'awful', 'shit', 'crap', 'dreadful', 'distgusting', 'shite', 'rubbish', 'hate', 'dilike', 'digust', 'fuck']
goodFeel = ['good', 'great', 'superb', 'excellent', 'brilliant', 'wonderful', 'terrific', 'fantastic']
badResponse = ['Aww, poor you!', 'That\'s not good is it?', 'Hope you feel better soon!']
goodResponse = ['Glad to hear that', 'That\'s great', 'Excellent', 'Wonderful']
def matchword(phrase1,phrase2):
return difflib.SequenceMatcher(None, phrase1, phrase2).ratio()
def phrasefilter(phrase):
phrase = phrase.replace('hi', 'hello')
phrase = phrase.replace('hey', 'hello')
phrase = re.sub('[^A-Za-z0-9\s]+', '', phrase.lower())
noise_words_set = ['of', 'the', 'at', 'for', 'in', 'and', 'is', 'from', 'are', 'our', 'it', 'its', 'was', 'when', 'how', 'what', 'like', 'whats', 'now', 'panic']
return ' '.join(w for w in phrase.split() if w.lower() not in noise_words_set)
def getResponse(array):
return random.choice(array[1:])
class panicBot():
"""create new chatbot instance"""
def __init__(self):
print logo
self.prevResponse = ''
self.user = {
'name': '',
'age': '',
'location': ''
}
def takeInput(self, message):
out = ''
if self.prevResponse in Database[3]:
response = phrasefilter(message)
responseType = 1
for word in response.split(' '):
if word in badFeel:
responseType = 0
elif word in goodFeel:
responseType = 1
if responseType:
output = random.choice(goodResponse)
else:
output = random.choice(badResponse)
out += '\n' + output + '\n'
self.prevResponse = ''
elif self.prevResponse == (Database[0][1] + ' What is your name?') or self.prevResponse == Database[1][1]:
name = message
self.user['name'] = name
print '\n' + random.choice(['Hello ' + name, 'Hi ' + name, 'Howdy ' + name]) + '\n'
self.prevResponse = ''
elif self.prevResponse == (Database[2][1] + ' How old are you?') or self.prevResponse == Database[5][1]:
age = message
self.user['age'] = age
out += '\n' + 'Awesome, I\'ll remember that.' + '\n'
self.prevResponse = ''
elif self.prevResponse == (Database[4][1] + ' Where do you live?'):
location = message
self.user['location'] = location
out += '\n' + 'I\'m sure it\'s lovely there.' + '\n'
self.prevResponse = ''
else:
var = phrasefilter(message)
vals = []
for i in range(len(Database)):
vals.append(matchword(var, Database[i][0]))
maxVal = [0,0]
for i in range(len(vals)):
if vals[i] > maxVal[0]:
maxVal[0] = vals[i]
maxVal[1] = i
if maxVal[0] > 0.55:
if maxVal[1] == 0:
if self.user['name'] == '':
self.prevResponse = Database[maxVal[1]][1] + ' What is your name?'
else:
self.prevResponse = Database[maxVal[1]][1]
elif maxVal[1] == 1:
if self.user['name'] == '':
self.prevResponse = Database[maxVal[1]][1]
else:
self.prevResponse = 'Your name is ' + self.user['name']
elif maxVal[1] == 2:
if self.user['age'] == '':
self.prevResponse = Database[maxVal[1]][1] + ' How old are you?'
else:
self.prevResponse = Database[maxVal[1]][1]
elif maxVal[1] == 4:
if self.user['name'] == '':
self.prevResponse = Database[maxVal[1]][1] + ' Where do you live?'
else:
self.prevResponse = Database[maxVal[1]][1]
elif maxVal[1] == 5:
if self.user['age'] == '':
self.prevResponse = Database[maxVal[1]][1]
else:
self.prevResponse = 'You are ' + self.user['age'] + ' years old'
elif maxVal[1] == 16:
out += '\nFinding directions ...\n'
self.prevResponse = directions
time.sleep(2)
else:
self.prevResponse = getResponse(Database[maxVal[1]])
else:
self.prevResponse = random.choice(['Sorry, I don\'t understand', 'I don\'t know what you mean', 'Sorry, I don\'t get that', 'You\'ve lost me there'])
out += '\n' + self.prevResponse + '\n'
return out