-
Notifications
You must be signed in to change notification settings - Fork 39
/
commands.py
370 lines (320 loc) · 11.3 KB
/
commands.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import numpy as np
import nltk
import nlpy
import en
import psw
import gapi
import player
songPlayer = player.Player()
player.init()
import subprocess
from grooveshark import Client
songClient = Client()
songClient.init()
from sets import Set
def matchWord(tokens, words):
s = Set(tokens).intersection(Set(words))
if len(s)>0:
return [(w,1.0,w) for w in s]
else:
from nltk.metrics import distance
import operator
result = []
for token in Set(tokens):
vals = {w : (1.0-float(distance.edit_distance(token, w))/float(max(len(token),len(w)))) for w in words}
sortedvals = sorted(vals.iteritems(), key=operator.itemgetter(1),reverse=True)
result.append( (token, sortedvals[0][1], sortedvals[0][0]) )
return sorted(result, key=lambda tup: tup[1], reverse=True)
def matchMeaning(tokens, words):
s = Set(tokens).intersection(Set(words))
if len(s)>0:
return [(w,1.0) for w in s]
else:
from nltk.metrics import distance
import operator
tokens = Set(tokens)
words = Set(words)
extendedWords = {}
for w in words:
extwords = []
extwords.extend([w])
extwords.extend(nlpy.autoWordToNoun(w))
extwords.extend(nlpy.autoWordToVerb(w))
extendedWords[w] = Set(extwords)
result = []
for token in tokens:
extwords = []
extwords.extend([token])
extwords.extend(nlpy.autoWordToNoun(token))
extwords.extend(nlpy.autoWordToVerb(token))
count = sum([1 for w in words if len(extendedWords[w].intersection(Set(extwords)))>0])
result.append((token, float(count)/float(len(words))))
return sorted(result, key=lambda tup: tup[1], reverse=True)
stopwords = nltk.corpus.stopwords.words('english')
def scoreExactPhrase(tokens, phrase):
score = np.mean([p[1] for p in phrase])
remaining = Set(tokens).difference(Set([p[0] for p in phrase]))
global stopwords
remaining = remaining.difference(Set(w for w in remaining if w in stopwords))
return score*(1-float(len(remaining))/float(len(Set(tokens))))
def scoreQuery(tokens, phrase):
score = np.mean([p[1] for p in phrase])
remaining = Set(tokens).difference(Set([p[0] for p in phrase]))
#global stopwords
#remaining = remaining.difference(Set(w for w in remaining if w in stopwords))
return (score, [w for w in tokens if w in remaining])
def changeLanguage(tokens, languages):
phrase = []
phrase.append( matchMeaning(tokens,['switch','change','exchange','modify'])[0] )
phrase.append( matchWord(tokens,['language'])[0] )
lang = matchWord(tokens,languages)[0]
phrase.append( lang )
score = scoreExactPhrase(tokens, phrase)
return (score, lang[2] )
def openProgram(tokens, programs):
phrase = []
phrase.append( matchMeaning(tokens,['execute','start','open','run'])[0] )
prog = matchWord(tokens,programs)[0]
phrase.append( prog )
score = scoreExactPhrase(tokens, phrase)
return (score, prog[2] )
def closeProgram(tokens, programs):
phrase = []
phrase.append( matchMeaning(tokens,['finish','close','terminate','exit','quit'])[0] )
prog = matchWord(tokens,programs)[0]
phrase.append( prog )
score = scoreExactPhrase(tokens, phrase)
return (score, prog[2] )
def whatTime(tokens):
phrase = []
phrase.append( matchWord(tokens,['how','what','tell'])[0] )
time = matchWord(tokens,['time','hours'])[0]
phrase.append( time )
score = scoreExactPhrase(tokens, phrase)
return (score, time[0] )
def whatDay(tokens):
phrase = []
phrase.append( matchWord(tokens,['what'])[0] )
date = matchWord(tokens,['day','date'])[0]
phrase.append( date )
phrase.append( matchWord(tokens,['today','it'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, date[0] )
def playSong(tokens):
phrase = []
phrase.append( matchWord(tokens,['play'])[0] )
phrase.append( matchWord(tokens,['song','music'])[0] )
phrase.append( matchWord(tokens,['by','from'])[0] )
score1 = scoreQuery(tokens, phrase)
phrase = []
phrase.append( matchWord(tokens,['play'])[0] )
phrase.append( matchWord(tokens,['song','music'])[0] )
score2 = scoreQuery(tokens, phrase)
if score1[0]>=score2[0]:
return (score1[0], score1[1], 'artist')
else:
return (score2[0], score2[1], 'song')
def pauseSong(tokens):
phrase = []
phrase.append( matchMeaning(tokens,['pause', 'stop', 'finish', 'close'])[0] )
phrase.append( matchWord(tokens,['song','music'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, 'pause')
def nextSong(tokens):
phrase = []
phrase.append( matchWord(tokens,['next', 'change'])[0] )
phrase.append( matchWord(tokens,['song','music'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, 'next')
def previousSong(tokens):
phrase = []
phrase.append( matchWord(tokens,['previous'])[0] )
phrase.append( matchWord(tokens,['song','music'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, 'previous')
def wakeUp(tokens):
phrase = []
phrase.append( matchMeaning(tokens,['wake','awake'])[0] )
phrase.append( matchWord(tokens,['up','awake'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, 'awake')
def goSleep(tokens):
phrase = []
phrase.append( matchMeaning(tokens,['sleep'])[0] )
phrase.append( matchWord(tokens,['go','sleep'])[0] )
score = scoreExactPhrase(tokens, phrase)
return (score, 'sleep')
def whatIs(tokens):
phrase = []
phrase.append( matchWord(tokens,['who','what','which'])[0] )
phrase.append( matchMeaning(tokens,['are','is'])[0] )
score = scoreQuery(tokens, phrase)
return score
def doChangeLanguage(text, score, speech, translator):
lang = score[1]
print lang
speech.lang = gapi.languages[lang]
translator.to_lang = gapi.languages[lang]
psw.play(speech.getAudio(translator.translate('language changed to '+lang)))
def doOpenProgram(text, score, speech, translator):
openAppList = {'editor': 'gedit', 'browser':'google-chrome', 'console':'gnome-terminal', 'calculator':'gnome-calculator'}
prog = score[1]
psw.play(speech.getAudio(translator.translate('I will open the '+prog)))
from os import system
system('nohup '+openAppList[prog]+' &')
def doCloseProgram(text, score, speech, translator):
closeAppList = {'editor': 'gedit', 'browser':'chrome', 'console':'gnome-terminal', 'calculator':'gnome-calculator'}
prog = score[1]
psw.play(speech.getAudio(translator.translate('I will close the '+prog)))
from os import system
system('killall '+closeAppList[prog])
def doWhatTime(text, score, speech, translator):
import time
ts = time.time()
import datetime
st = datetime.datetime.fromtimestamp(ts).strftime('It is %-H hours and %-M minutes')
psw.play(speech.getAudio(translator.translate(st)))
def doWhatDay(text, score, speech, translator):
import time
ts = time.time()
import datetime
st = datetime.datetime.fromtimestamp(ts).strftime('Today is %A, %-d. %B %Y')
psw.play(speech.getAudio(translator.translate(st)))
def doPlaySong(text, score, speech, translator):
if score[2]=='song':
if len(score[1])>0:
songs = list(songClient.search(' '.join(score[1])))
#song = songs[0]
print 'Found',len(songs),'songs'
#subprocess.call(['cvlc', song.stream.url])
#subprocess.call(['mplayer', song.stream.url])
songPlayer.play(songs)
else:
songPlayer.play()
elif score[2]=='artist':
songs = list(list(songClient.search(' '.join(score[1]),Client.ARTISTS))[0].songs)
#n = int(np.random.rand()*len(songs))
#song = songs[n]
print 'Found',len(songs),'songs'
songPlayer.play(songs)
#print 'Playing song',n
#print str(song), song.duration
#subprocess.call(['cvlc',song.stream.url])
#subprocess.call(['mplayer',song.stream.url])
#player.set_state(gst.STATE_NULL)
#player.set_property('uri',song.stream.url)
#player.set_state(gst.STATE_PLAYING)
def doPauseSong(text, score, speech, translator):
songPlayer.pause()
def doNextSong(text, score, speech, translator):
songPlayer.next()
def doPreviousSong(text, score, speech, translator):
songPlayer.previous()
isAwaken = True
def doWakeUp(text, score, speech, translator):
global isAwaken
isAwaken = True
psw.play(speech.getAudio(translator.translate('Hi, I am back')))
def doGoSleep(text, score, speech, translator):
global isAwaken
isAwaken = False
psw.play(speech.getAudio(translator.translate('I am going to sleep')))
def clearText(text,left, right):
ctext = ''
idx = 0
for c in unicode(text):
if c==left:
idx += 1
elif c==right:
idx -= 1
elif idx==0:
ctext += c
return ctext
def doWhatIs(text, score, speech, translator):
query = ' '.join(score[1]).strip()
if query=='you':
psw.play(speech.getAudio(translator.translate('My name is Nati, a Natural interface.')))
else:
import wikipedia
titles = wikipedia.search(query)
#print titles
#score = matchWord([query], titles)
#print score
#print wikipedia.summary(score[0][2])
text = wikipedia.summary(titles[0])
#print text
print translator.translate(text)
text = clearText(text, '(',')')
text = clearText(text, '[',']')
import re
text = re.sub(re.compile(u"/[^/]+/"), " ", text)
text = text.replace(',',' ')
phrase = text.split('.')[0]
print '*',phrase
psw.play(speech.getAudio(translator.translate(phrase)))
def execute(text, speech):
translator = gapi.Translator('en-uk', speech.lang)
global isAwaken
from os import system
import sys
openAppList = {'editor': 'gedit', 'browser':'google-chrome', 'console':'gnome-terminal', 'calculator':'gnome-calculator'}
closeAppList = {'editor': 'gedit', 'browser':'chrome', 'console':'gnome-terminal', 'calculator':'gnome-calculator'}
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+')
tokens = tokenizer.tokenize(text)
if isAwaken:
cmds = ['goSleep','changeLanguage','openProgram','closeProgram','whatTime','whatDay','playSong','pauseSong','nextSong','previousSong','whatIs']
cmd = {}
cmd['changeLanguage'] = changeLanguage(tokens, gapi.languages.keys())
cmd['openProgram'] = openProgram(tokens, openAppList.keys())
cmd['closeProgram'] = closeProgram(tokens, closeAppList.keys())
cmd['whatTime'] = whatTime(tokens)
cmd['whatDay'] = whatDay(tokens)
cmd['playSong'] = playSong(tokens)
cmd['pauseSong'] = pauseSong(tokens)
cmd['nextSong'] = nextSong(tokens)
cmd['previousSong'] = previousSong(tokens)
cmd['goSleep'] = goSleep(tokens)
cmd['whatIs'] = whatIs(tokens)
maxScore = 0
maxCmd = None
for c in cmds:
s = cmd[c]
if s[0]>maxScore:
maxScore = s[0]
maxCmd = c
print 'do command:',maxCmd,maxScore
if maxScore>0.7:
if maxCmd=='changeLanguage':
doChangeLanguage(text, cmd[maxCmd], speech, translator)
elif maxCmd=='openProgram':
doOpenProgram(text, cmd[maxCmd], speech, translator)
elif maxCmd=='closeProgram':
doCloseProgram(text, cmd[maxCmd], speech, translator)
elif maxCmd=='whatTime':
doWhatTime(text, cmd[maxCmd], speech, translator)
elif maxCmd=='whatDay':
doWhatDay(text, cmd[maxCmd], speech, translator)
elif maxCmd=='playSong':
doPlaySong(text, cmd[maxCmd], speech, translator)
elif maxCmd=='pauseSong':
doPauseSong(text, cmd[maxCmd], speech, translator)
elif maxCmd=='nextSong':
doNextSong(text, cmd[maxCmd], speech, translator)
elif maxCmd=='previousSong':
doPreviousSong(text, cmd[maxCmd], speech, translator)
elif maxCmd=='goSleep':
doGoSleep(text, cmd[maxCmd], speech, translator)
elif maxCmd=='whatIs':
doWhatIs(text, cmd[maxCmd], speech, translator)
else:
print 'command not known:',text
else:
print 'command not known:',text
else:
score = wakeUp(tokens)
print 'do command: wakeUp',score[0]
if score[0]>0.7:
doWakeUp(text, score, speech, translator)
else:
print 'command not known:',text