Skip to content

Instantly share code, notes, and snippets.

@alexgolec
Last active January 20, 2019 06:27
Show Gist options
  • Save alexgolec/155256f50cca40d26c5af8e2285dadbf to your computer and use it in GitHub Desktop.
Save alexgolec/155256f50cca40d26c5af8e2285dadbf to your computer and use it in GitHub Desktop.
def synonym_queries(synonym_words, queries):
'''
synonym_words: iterable of pairs of strings representing synonymous words
queries: iterable of pairs of strings representing queries to be tested for
synonymous-ness
'''
synonyms = defaultdict(set)
for w1, w2 in synonym_words:
synonyms[w1].add(w2)
output = []
for q1, q2 in queries:
q1, q2 = q1.split(), q2.split()
if len(q1) != len(q2):
output.append(False)
continue
result = True
for i in range(len(q1)):
w1, w2 = q1[i], q2[i]
if w1 == w2:
continue
elif ((w1 in synonyms and w2 in synonyms[w1])
or (w2 in synonyms and w1 in synonyms[w2])):
continue
result = False
break
output.append(result)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment