-
Notifications
You must be signed in to change notification settings - Fork 1
/
CosineSimilarity.py
169 lines (127 loc) · 5.13 KB
/
CosineSimilarity.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
import string
import IBM1_EM
import Utils
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk.data
import DutchtoEnglish
nltk.download('stopwords')
def calcCosineSim(language) :
if(language==1) :
tokenizer = nltk.data.load('tokenizers/punkt/dutch.pickle')
with open("output.txt") as f:
output = f.readlines()
with open("actual.txt") as g:
actual = g.readlines()
# tokenization
output_lines = DutchtoEnglish.sentence_tokenizer(output)
actual_lines = DutchtoEnglish.sentence_tokenizer(actual)
output_sentences = list()
actual_sentences = list()
for line in output_lines :
l = tokenizer.tokenize(line)
for sen in l :
output_sentences.append(sen)
for line in actual_lines :
l = tokenizer.tokenize(line)
for sen in l :
actual_sentences.append(sen)
no_of_sen = len(output_sentences)
average = 0.0
# sw contains the list of stopwords
sw = stopwords.words('dutch')
for index in range(no_of_sen) :
l1 =[];l2 =[]
X_list = word_tokenize(output_sentences[index].lower())
Y_list = word_tokenize(actual_sentences[index].lower())
# remove stop words from string
X_set = {w for w in X_list if not w in sw}
Y_set = {w for w in Y_list if not w in sw}
X_set.discard('.')
X_set.discard('(')
X_set.discard(')')
X_set.discard('"')
X_set.discard(',')
X_set.discard('-')
X_set.discard('\'')
Y_set.discard('.')
Y_set.discard('(')
Y_set.discard(')')
Y_set.discard('"')
Y_set.discard(',')
Y_set.discard('-')
Y_set.discard('\'')
# form a set containing keywords of both strings
rvector = X_set.union(Y_set)
for w in rvector:
if w in X_set: l1.append(1) # create a vector
else: l1.append(0)
if w in Y_set: l2.append(1)
else: l2.append(0)
c = 0
# cosine formula
for i in range(len(rvector)):
c+= l1[i]*l2[i]
cosine = c / float((sum(l1)*sum(l2))**0.5)
average += cosine/no_of_sen
print("\ncosine =",cosine)
print("\nfinal cosine similarity: ",average)
elif(language==2) :
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
with open("output.txt") as f:
output = f.readlines()
with open("actual.txt") as g:
actual = g.readlines()
# tokenization
output_lines = DutchtoEnglish.sentence_tokenizer(output)
actual_lines = DutchtoEnglish.sentence_tokenizer(actual)
output_sentences = list()
actual_sentences = list()
for line in output_lines :
l = tokenizer.tokenize(line)
for sen in l :
output_sentences.append(sen)
for line in actual_lines :
l = tokenizer.tokenize(line)
for sen in l :
actual_sentences.append(sen)
no_of_sen = len(actual_sentences)
average = 0.0
# sw contains the list of stopwords
sw = stopwords.words('english')
for index in range(no_of_sen) :
l1 =[];l2 =[]
X_list = word_tokenize(output_sentences[index].lower())
Y_list = word_tokenize(actual_sentences[index].lower())
# remove stop words from string
X_set = {w for w in X_list if not w in sw}
Y_set = {w for w in Y_list if not w in sw}
X_set.discard('.')
X_set.discard('(')
X_set.discard(')')
X_set.discard('"')
X_set.discard(',')
X_set.discard('-')
X_set.discard('\'')
Y_set.discard('.')
Y_set.discard('(')
Y_set.discard(')')
Y_set.discard('"')
Y_set.discard(',')
Y_set.discard('-')
Y_set.discard('\'')
# form a set containing keywords of both strings
rvector = X_set.union(Y_set)
for w in rvector:
if w in X_set: l1.append(1) # create a vector
else: l1.append(0)
if w in Y_set: l2.append(1)
else: l2.append(0)
c = 0
# cosine formula
for i in range(len(rvector)):
c+= l1[i]*l2[i]
cosine = c / float((sum(l1)*sum(l2))**0.5)
average += cosine/no_of_sen
print("\ncosine=",cosine)
print("\nfinal cosine similarity: ",average)