-
Notifications
You must be signed in to change notification settings - Fork 1
/
semantic-similarity.py
55 lines (43 loc) · 1.87 KB
/
semantic-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
from flair_cosine_similarity import flair_semantic
from elmo_cosine_similarity import elmo_semantic
from spacy_cosine_similarity import spacy_semantic
from common import similarity_test
from bert_similarity import bert_semantic
import pandas as pd
flair_similarity = []
elmo_similarity = []
bert_similarity = []
spacy_similarity = []
flair = flair_semantic()
elmo = elmo_semantic()
bert = bert_semantic()
spacy = spacy_semantic()
common = similarity_test()
data = pd.read_csv('data/sick_dev.csv')
sent_1_list = list(data.sent_1)
data = common.normalize(data, ['sim'])
sim_list = list(data.sim)
sent_2_list = list(data.sent_2)
min_list1 = sent_1_list[:10]
min_list2 = sent_2_list[:10]
min_list3 = sim_list[:10]
for text1, text2, text3 in zip(sent_1_list, sent_2_list, sim_list):
f_similarity = flair.predict_similarity(text1, text2)
e_similarity = elmo.predict_similarity(text1, text2)
b_similarity = bert.predict_similiarity(text1, text2)
s_similarity = spacy.predict_similarity(text1, text2)
flair_similarity.append(f_similarity)
elmo_similarity.append(e_similarity)
bert_similarity.append(b_similarity)
spacy_similarity.append(s_similarity)
print (' elmo: '+ str(e_similarity) + '|' + ' flair: ' + str(f_similarity) + '|' + ' bert: ' + str(b_similarity) + '|' + ' Spacy: ' +'[[' +str(s_similarity)+']]'+ '|' + ' human predicted: ' +'[[' +str(text3)+']]')
print("======================================================================================")
series1 = pd.Series('flair_similarity')
series2 = pd.Series('elmo_similarity')
series3 = pd.Series('bert_similarity')
series4 = pd.Series('spacy_similarity')
data['flair_similarity'] = series1.values
data['elmo_similarity'] = series2.values
data['bert_similarity'] = series3.values
data['spacy_similarity'] = series4.values
data.tocsv('result/result.csv')