-
Notifications
You must be signed in to change notification settings - Fork 302
/
euclidean_distance.py
48 lines (35 loc) · 1023 Bytes
/
euclidean_distance.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
import random, math, operator
from pprint import pprint
from get_words import get_words
random.seed(1337)
"""
euclidean_dist.py
Compute euclidean distance between 5-letter words.
"""
def euclidean_distance(word1, word2):
v1 = word2vec(word1)
v2 = word2vec(word2)
return l2norm(v1,v2)
def l2norm(vec1, vec2):
radicand = [(v2-v1)*(v2-v1) for (v1,v2) in zip(vec1,vec2)]
return math.sqrt(sum(radicand))
def word2vec(word):
charvec = []
vec = []
for c in word:
charvec.append(c)
vec.append(ord(c)-ord('a'))
return vec
def print_tuple(e):
print("Distance between {0:s} and {1:s} = {2:f}".format(*e))
if __name__=="__main__":
words = get_words()
eds = []
for i in range(100):
w1 = words[random.randint(1,5757)]
w2 = words[random.randint(1,5757)]
ed = euclidean_distance(w1,w2)
eds.append((w1,w2,ed))
sorted_eds = sorted(eds, key=operator.itemgetter(2))
for e in reversed(sorted_eds):
print_tuple(e)