-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo_check.py
180 lines (160 loc) · 4.53 KB
/
echo_check.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
import sys
from nltk.corpus import cmudict
arpabet_dict = cmudict.dict()
# MADE by ILY, aka Maxwell Dulin on 5/25/2018
"""
Strips all of the numbers, which are stresses on the phones.
Args:
phones(list of strings): a list of a strings, that are the phones.
Returns:
new_list(list of strings): a list of strings, which correspond to phones.
"""
def strip_numbers_from_list(phones):
new_list = list()
for elt in phones:
new_list.append(''.join([i for i in elt if not i.isdigit()]))
return new_list
"""
Compares a list of words for alike-ness
Args:
test(list of a lists of strings): the phrase that is being checked for similarity in the system.
cur(list of a lists of strings): the phrase that the system is checked from.
Returns:
(bool): false if the phones are similar, true otherwise
"""
def compare_phones(test,cur):
if(len(test) != len(cur)):
return False
for phrases in range(len(test)):
if(not compare_phones_per_word(test[phrases],cur[phrases])):
return False
return True
"""
Compares a list of words for alike-ness
Args:
test(list of strings): the word that is being checked for similarity in the system.
cur(list of strings): the word that the system is checked from.
Returns:
(bool): false if the phones are similar, true otherwise
"""
def compare_phones_per_word(test,cur):
if(len(test) != len(cur)):
return False
amount = 0
for spot in range(len(test)-1):
if(test[spot] != cur[spot]):
if(amount == 1 or not is_similar(cur[spot],test[spot])):
return False
amount+=1
return True
"""
Convers a full phrase from a word into Arpabet(group of phones)
Args:
phrase(string): a word being converted
Returns:
phone_lst(list of strings): Puts each phone(or unit of sound) as an index in the list.
"""
def convert_phrase(phrase):
phone_lst = list()
phrase = phrase.split()
for word in phrase:
try:
skill_prounc = arpabet_dict[word][0]
skill_prounc = strip_numbers_from_list(skill_prounc)
phone_lst.append(skill_prounc)
except:
print word
raise ValueError("""
The word does not exist in the CMU pronounciations dictionary. SORRY :(
""")
return phone_lst
"""
Compares two words.
Args:
phrase1(string): a phrase
phrase2(string): a phrase
Returns:
(bool): true if the given word is similar, false otherwise.
"""
def valid_skill_compare(phrase1,phrase2):
phrase_in_phones1 = convert_phrase(phrase1)
#print phrase_in_phones1
phrase_in_phones2 = convert_phrase(phrase2)
#print phrase_in_phones2
return compare_phones(phrase_in_phones1,phrase_in_phones2)
"""
Checks a list of words against a given phrase to check
Args:
phrase1(string): the phrase being checked.
Returns:
(bool) true if not similar, false otherwise
"""
def valid_skill(phrase1, skills = ["not not", "dead in the water", "capital one"]):
for skill in skills:
if(valid_skill_compare(skill, phrase1)):
print "Found a malicious intent with '" + phrase1 + "' skill"
return False
print "Valid name!"
return True
"""
Checks for how similar the phones are. This is referenced by the dictionary below.
Args:
phone1(string): a phone
phone2(string): a phone
Returns:
(bool): Returns true if phones are similar, false otherwise
"""
def is_similar(phone1,phone2):
similar_dict = {
'AA':['AO'],
'AE':[],
'AH':[],
'AO':['AA'],
'AW':[],
'AY':['EH'],
'B' :['P','V'],
'CH':['G','JH'],
'D' :['T'],
'DH':['TH'],
'EH':['AY'],
'ER':[],
'EY':[],
'F' :[],
'G' :['CH','JH'],
'HH':[],
'IH':[],
'IY':[],
'JH':['CH','G'],
'K' :[],
'L' :[],
'M' :['N','NG'],
'N' :['M','NG'],
'NG':['N','M'],
'OW':[],
'OY':[],
'P' :['B','V'],
'R' :[],
'S' :['Z'],
'SH':['ZH'],
'T' :['D'],
'TH':['DH'],
'UH':[],
'UW':[],
'V' :['B','P'],
'W' :[],
'Y' :[],
'Z' :['C'],
'ZH':['SH']
}
return phone2 in similar_dict[phone1] or phone1 in similar_dict[phone2]
def main():
if(len(sys.argv) == 1):
print "Testing on skiil capital won..."
print "Testing against current skills 'capital one', 'steph curry' and 'help'"
print
print valid_skill("capital won", skills = ["capital one", "help","steph curry"])
else:
skill = sys.argv[1]
list_of_skills = sys.argv[2].split(",")
valid_skill(skill, skills = list_of_skills)
main()