-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistbetwords.py
60 lines (47 loc) · 1.28 KB
/
distbetwords.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
import math
import string
def addword(dict,word) :
if not word in dict :
dict[word] = []
def finddistance(word,index,list) :
try :
next = list.index(word,index+1)
# print "next element at: ",next
return next-index
except :
return 0
def adddistance(distance,dict,word) :
l = dict[word]
if distance != 0 :
l.append(distance)
def disBetWords(text):
file = text
list = file.split()
# print "list is: ",list
dict = {}
i = 0
while i<len(list) :
# print "for the word :",word
index = i
word = list[i]
# print "index is: ",index
addword(dict,word)
# print "after adding to dict:",dict
distance = finddistance(word,index,list)
# print "distance bw words: ",distance
adddistance(distance,dict,word)
# print "after loop dict: ",dict
i = i+1
#print dict
rms_list = []
for key, value in dict.items():
sm = 0.0
for x in value:
sm = sm + math.pow(x, 2)
if len(value) != 0:
rms = math.sqrt(sm/len(value))
if len(value) == 0:
rms = 0
rms_list.append(rms)
rms_dict = {word: rms for word, rms in zip(dict.keys(), rms_list)}
return rms_dict