-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
137 lines (118 loc) · 4.09 KB
/
parse.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
import voice
import re
import GoogleCSEGet
import os
# Command Functions
def next_slide(line):
return -1
def back_slide(line):
return -2
def go_to_slide(line):
index = line.find('go to')
words = line.split()
if 'slide number' in line: # account for 'go to slide number x'
try:
num = int(words[words.index('number')+1])
except:
num = text2int(words[words.index('number')+1])
else:
try:
num = int(words[words.index('slide')+1]) # 'go to slide x'
except:
num = 0
# print(line)
# print(num)
return num
def go_to_image(line, d):
index = line.find('go to')
words = line.split()
word = words[words.index('slide')+3]
return d.get(word, 0)
def get_url(line):
words = line.split()
relevant_words = words[words.index('show')+2:]
return GoogleCSEGet.get("%20".join(relevant_words))
def get_title(line, d): # return slide number with this title
words = line.split()
words = words[words.index('titled')+1:]
for key in d.keys():
if " ".join(words).lower().rstrip() in ' '.join(key.split()[0:10]).lower().rstrip(): # query only the first 10 words
return d.get(key, 0)
return 0
def search(line, d): # return first instance of slide with this text
words = line.split()
words = words[words.index('search')+2:]
for key in d.keys():
if " ".join(words).lower() in key.lower():
return d.get(key,0)
return 0
def zoom(line, d): # given any picture, find its filepath
words = line.split()
if 'picture' in words:
word = words[words.index('picture')+3]
elif 'into' in words:
word = words[words.index('into')+2]
else:
word = words[words.index('zoom')+4]
n = d.get(word,1)
os.system('cp parser/slide'+str(n)+'/*.jpg viewer/assets/'+word+'.jpg')
return '/assets/'+word+'.jpg'
# Returns an integer from a string
def text2int(textnum, numwords={}):
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
scales = ["hundred", "thousand", "million", "billion", "trillion"]
numwords["and"] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
raise Exception("Illegal word: " + word)
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
trigger = 'slide'
trigger2 = 'show'
trigger3 = 'search'
trigger4 = 'zoom'
# regular expressions
keywords = {'next': next_slide,
'forward': next_slide,
'last': back_slide,
'previous': back_slide,
'back a slide': back_slide,
'go.*to slide.*\d': go_to_slide,
'go to.*slide with the \w': go_to_image,
'go to.*slide with a \w': go_to_image,
'show me \w+':get_url,
'go to the slide titled \w+':get_title,
'search for \w+':search,
'zoom in.*the':zoom}
# Returns parsed voice command
def parse(d1, d2):
line = voice.send_words()
if line:
line = line.replace('fly', 'slide')
line = line.replace('flight', 'slide')
line = line.replace('line', 'slide')
print(line)
if line:
for word in keywords.keys():
if re.search(word, line) and (trigger in line or trigger2 in line or trigger3 in line or trigger4 in line):
if word == 'go to.*slide with the \w' or word == 'zoom in.*the' or word == 'go to.*slide with a \w':
return keywords[word](line,d1)
elif word == 'go to the slide titled \w+' or word == 'search for \w+':
return keywords[word](line,d2)
else:
return keywords[word](line)
return 0