-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranking.py
executable file
·147 lines (130 loc) · 4.24 KB
/
ranking.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
import redis
import math
import utils
rds=redis.Redis()
############# Disambiguate second time ##########
def disambiguate_mentions_deep(entity_mentions):
current=0
for em in entity_mentions:
if em.sys_link and not em.anchor_mention:
lastN=utils.getLastN(entity_mentions, current)
coherence=compute_coherence(em.sys_link.subject, lastN)
#maxScore=coherence
if em.total_pr>0:
maxScore=coherence*em.sys_link.ss_score*(em.sys_link.lotus_score/em.total_lotus)*(em.sys_link.pr_score/em.total_pr)
else:
maxScore=coherence*em.sys_link.ss_score*(em.sys_link.lotus_score/em.total_lotus)
maxCandidate=em.sys_link
print(em.mention, em.sys_link.subject, em.sys_link.ss_score, (em.sys_link.lotus_score/em.total_lotus), coherence, maxScore, em.gold_link)
for c in em.candidates:
if c.score>0.5*em.sys_link.score and c.subject!=em.sys_link.subject: # If there is any chance that this candidate will win
coherence=compute_coherence(c.subject, lastN)
#score=coherence
score=coherence*c.ss_score*(c.lotus_score/em.total_lotus)*pow(c.pr_score/em.total_pr,1) #entity_mentions[max(current-10, 0):current])
print("***************** REBUTTAL *******************")
print(c.subject, c.ss_score,(c.lotus_score/em.total_lotus), coherence, score)
if score>maxScore:
maxScore=score
maxCandidate=c
if em.sys_link!=maxCandidate:
print("########### Best candidate changed to", maxCandidate)
em.sys_link=maxCandidate
em.sys_link.score=maxScore
current+=1
return entity_mentions
############# Disambiguate first time ###########
def disambiguate_mentions(entity_mentions):
current=0
for em in entity_mentions:
em.anchor_mention=utils.setAnchorMention(em.mention, entity_mentions[:current])
#ones=0
if em.anchor_mention:
# ones+=1
try:
maxScore=em.anchor_mention.sys_link.score
except:
maxScore=1.0
maxCandidate=em.anchor_mention.sys_link
else:
maxScore=0.01
maxCandidate=None
for c in em.candidates:
#if c.ss_score<0.9:
# continue
# try:
# c.score=c.ss_score*(c.tp_score/em.total_tp)*(c.pr_score/em.total_pr)*(c.lotus_score/em.total_lotus)
# except:
if em.total_pr>0:
c.score=c.ss_score*pow(c.pr_score/em.total_pr,1)*(c.lotus_score/em.total_lotus)
else:
c.score=c.ss_score*(c.lotus_score/em.total_lotus)
# except:
# if em.total_pr==0:
# c.score=c.ss_score*math.sqrt(c.lotus_score/em.total_lotus)
# elif em.total_lotus==0:
# c.score=c.ss_score*math.sqrt(c.pr_score/em.total_pr)
#ones+=1
if c.score>maxScore:
maxScore=c.score
maxCandidate=c
em.sys_link=maxCandidate
current+=1
return entity_mentions
############# 1) PageRank ################
def getMostPopularCandidates(candidates):
candScores={}
for candidate in candidates:
try:
score=float(rds.get('pr:%s' % candidate))
candScores[candidate]=score
except:
continue
#print('ERROR: No pr for %s' % candidate)
return utils.sortAndReturnKeys(candScores)
############# 2) Time popularity ###########
def getMostTemporallyPopularCandidates(candidates, pkl):
candScores={}
for candidate in candidates:
try:
score=pkl[candidate]
candScores[candidate]=score
except:
continue
return utils.sortAndReturnKeys(candScores)
############# 2) Coherence - shortest path ###########
"""
def getMostCoherentCandidates(candidates, lastN, N):
candScores={}
for candidate in candidates:
score=computeCoherence(candidate, lastN, N)
candScores[candidate]=score
return utils.sortAndReturnKeys(candScores)
"""
def compute_coherence(candidate, lastN):
total=0.0
N=0
for entity_mention in lastN:
if entity_mention.sys_link:
total+=computeShortestPathCoherence(entity_mention.sys_link.subject, candidate)
N+=1
if N==0:
return 1.0
return total/N
def computeShortestPathCoherence(m1, m2):
if m1==m2:
return 1.0
fromCache=rds.get("%s:%s" % (m1, m2))
if fromCache:
return float(fromCache)
else:
path=utils.neo4jPath([m1, m2])
#
if path:
rds.set("%s:%s" % (m1, m2), 1/path)
rds.set("%s:%s" % (m2, m1), 1/path)
return 1.0/path
else:
rds.set("%s:%s" % (m1, m2), 0.0)
rds.set("%s:%s" % (m2, m1), 0.0)
return 0.0
############### 3) ?