-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
76 lines (67 loc) · 1.62 KB
/
metrics.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
# ====================================================
# @Time : 13/9/20 4:19 PM
# @Author : Xiao Junbin
# @Email : junbin@comp.nus.edu.sg
# @File : metrics.py
# ====================================================
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet
import numpy as np
def wup(word1, word2, alpha):
"""
calculate the wup similarity
:param word1:
:param word2:
:param alpha:
:return:
"""
# print(word1, word2)
if word1 == word2:
return 1.0
w1 = wordnet.synsets(word1)
w1_len = len(w1)
if w1_len == 0: return 0.0
w2 = wordnet.synsets(word2)
w2_len = len(w2)
if w2_len == 0: return 0.0
#match the first
word_sim = w1[0].wup_similarity(w2[0])
if word_sim is None:
word_sim = 0.0
if word_sim < alpha:
word_sim = 0.1*word_sim
return word_sim
def wups(words1, words2, alpha):
"""
:param pred:
:param truth:
:param alpha:
:return:
"""
sim = 1.0
flag = False
for w1 in words1:
max_sim = 0
for w2 in words2:
word_sim = wup(w1, w2, alpha)
if word_sim > max_sim:
max_sim = word_sim
if max_sim == 0: continue
sim *= max_sim
flag = True
if not flag:
sim = 0.0
return sim
def get_wups(pred, truth, alpha):
"""
calculate the wups score
:param pred:
:param truth:
:return:
"""
pred = word_tokenize(pred)
truth = word_tokenize(truth)
item1 = wups(pred, truth, alpha)
item2 = wups(truth, pred, alpha)
value = min(item1, item2)
return value