-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_helpers.py
133 lines (110 loc) · 3.75 KB
/
speech_helpers.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
# Some helper functions for processing speech commands
import objects as obj_types
def correct_text(text):
"""Because Google gives English, not commands"""
text = text.lower()
text = text.replace("-", " ")
text = text.replace("aiden", "8 and")
text = text.split(" ")
conversions = [
[["one", "won"], "1"],
[["to", "too", "two"], "2"],
[["three", "free"], "3"],
[["four", "for", "ford"], "4"],
[["five"], "5"],
[["six", "stix"], "6"],
[["seven"], "7"],
[["eight", "ate", "hate"], "8"],
[["nine"], "9"],
[["ten"], "10"],
[["+"], "and"],
[["x"], "by"],
[["buy"], "by"],
[["criticize", "play"], "create a size"],
[["write"], "right"],
[["op"], "up"],
[["run"], "room"]
]
for i in range(len(text)):
for conversion in conversions:
if text[i] in conversion[0]:
text[i] = conversion[1]
return text
def either_side(text, delimiter = "and", default = [-1, -1]):
"""Take form 12 AND 15 to return [12, 15] for example"""
if delimiter in text:
pos = text.index(delimiter)
if text[pos - 1].isnumeric() and text[pos + 1].isnumeric():
return [int(text[pos - 1]), int(text[pos + 1])]
else:
return default
else:
return default
def get_after(keyword, text):
"""Returns everything after the keyword in the full text provided."""
return text[text.index(keyword) + 1:]
def get_position(text):
"""Will look for position keywords and returns the first. [-1, -1] if nonexistent."""
location = [-1, -1]
if "and" in text:
location = either_side(text, "and")
location[0] -= 1
location[1] -= 1
return location
def get_positions(text, min=0):
"""Finds all positions. Returns a list thereof."""
locations = list()
while "and" in text:
locations.append(get_position(text))
text = get_after("and", text)
while len(locations) < min:
locations.append([-1, -1])
return locations
def get_size(text):
"""Finds size parameters in text."""
size = [1, 1]
if "by" in text:
size = either_side(text, "by", [1, 1])
return size
def is_in_objects(text):
"""Will tell you the definitive key of the object_types list to use.
Or returns None if it's not in the keywords at all."""
obj_type_int = obj_types.possible.intersection(text)
if len(obj_type_int) > 0:
obj = obj_type_int.pop()
if obj in obj_types.synonyms:
obj = obj_types.synonyms[obj]
# A hard-coded exception, our favorite :(
if "cocktail" in obj_type_int:
obj = "cocktail"
return obj
else:
return None
def process_relative(text):
"""Will process relative keywords (up, down, left, right) into relative locations."""
to_location = None
relative = {"up", "down", "left", "right"}
rel_int = relative.intersection(text)
if len(rel_int) > 0:
to_location = [0, 0]
for rel in rel_int:
delta = get_after(rel, text)[0]
if delta.isnumeric():
if rel == "up":
to_location[1] -= int(delta)
elif rel == "down":
to_location[1] += int(delta)
elif rel == "left":
to_location[0] -= int(delta)
elif rel == "right":
to_location[0] += int(delta)
to_location.append("relative")
return to_location
def select_obj_type(text):
"""If not in objects, calls it "any" """
obj = is_in_objects(text)
if obj is None:
obj_type = "any"
else:
obj_type = obj
return obj_type