-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCosine_Similarity.py
131 lines (100 loc) · 4.42 KB
/
Cosine_Similarity.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
import string
import Utility
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk.data
from numpy import dot
import French_To_English
import English_To_French
import IBM_Model1_EM
from numpy.linalg import norm
nltk.download('stopwords')
def cosine(string1) :
average = 0
if (string1 == 1): #French
tokenizer = nltk.data.load('tokenizers/punkt/french.pickle')
with open("Dataset/output.txt") as k:
output = k.readlines()
with open("Dataset/actual.txt") as p:
actual = p.readlines()
output_tokenized = English_To_French.sen_tokenizer(output)
actual_tokenized = English_To_French.sen_tokenizer(actual)
output_sentence = list()
actual_sentence = list()
for line in output_tokenized :
a = tokenizer.tokenize(line)
for sentence in a :
output_sentence.append(sentence)
for line in actual_tokenized :
a = tokenizer.tokenize(line)
for sentence in a :
actual_sentence.append(sentence)
no_of_sentence = len(output_sentence)
stopwords_list = stopwords.words('french')
for index in range(no_of_sentence) :
list1 =[];
list2 =[]
A = word_tokenize(output_sentence[index].lower())
B = word_tokenize(actual_sentence[index].lower())
A_set = {words for words in A if not words in stopwords_list}
B_set = {words for words in B if not words in stopwords_list}
rvector = A_set.union(B_set)
for word in rvector:
if word in A_set: list1.append(1) # create a vector
else: list1.append(0)
if word in B_set: list2.append(1)
else: list2.append(0)
c = 0
# cosine formula
for i in range(len(rvector)):
c+= list1[i]*list2[i]
if (norm(list1)*norm(list2)) > 0:
cosine_sim = c/(norm(list1)*norm(list2))
average += cosine_sim/no_of_sentence
print("\ncosine=",cosine_sim)
print("\nfinal cosine similarity: ",average)
elif(string1 ==2) :
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
with open("Dataset/output.txt") as k:
output = k.readlines()
with open("Dataset/actual.txt") as p:
actual = p.readlines()
output_tokenized = French_To_English.sen_tokenizer(output)
actual_tokenized = French_To_English.sen_tokenizer(actual)
output_sentence = list()
actual_sentence = list()
for line in output_tokenized :
a = tokenizer.tokenize(line)
for sentence in a :
output_sentence.append(sentence)
for line in actual_tokenized :
a = tokenizer.tokenize(line)
for sentence in a :
actual_sentence.append(sentence)
no_of_sentence = len(actual_sentence)
stopwords_list = stopwords.words('english')
for index in range(no_of_sentence) :
list1 =[];
list2 =[]
try:
A = word_tokenize(output_sentence[index].lower())
B = word_tokenize(actual_sentence[index].lower())
except IndexError:
continue
A_set = {words for words in A if not words in stopwords_list}
B_set = {words for words in B if not words in stopwords_list}
rvector = A_set.union(B_set)
for word in rvector:
if word in A_set: list1.append(1) # create a vector
else: list1.append(0)
if word in B_set: list2.append(1)
else: list2.append(0)
c = 0
# cosine formula
for i in range(len(rvector)):
c+= list1[i]*list2[i]
if (norm(list1)*norm(list2)) > 0:
cosine_sim = c/(norm(list1)*norm(list2))
average += cosine_sim/no_of_sentence
print("\ncosine=",cosine_sim)
print("\nfinal cosine similarity: ",average)