-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNLPDiff.py
127 lines (110 loc) · 4.13 KB
/
NLPDiff.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
#!/usr/bin/env python3
# -*- coding: utf-8, vim: expandtab:ts=4 -*-
from NLPInstance import *
class NLPDiff():
"""
* This class defines the identity of an edge with respect to the diff operation.
"""
class EdgeIdentity():
@property
def From(self):
return self._From
@From.setter
def From(self, value):
self._From = value
@property
def To(self):
return self._To
@To.setter
def To(self, value):
self._To = value
@property
def type(self):
return self._type
@type.setter
def type(self, value):
self._type = value
@property
def label(self):
return self._label
@label.setter
def label(self, value):
self._label = value
def __init__(self, edge):
self.edge = edge
self._From = edge.From.index
self._To = edge.To.index
self._type = edge.type
self._label = edge.label
def __eq__(self, other):
if other is None or type(self) != type(other):
return False
if self._From != other.From:
return False
if self._To != other.To:
return False
if self._label is not None:
if self._label != other.label:
return False
else:
if other.label is not None:
return False
if self._type is not None:
if self._type != other.type:
return False
else:
if other.type is not None:
return False
return True
def __hash__(self):
result = int(self._From)
result = 31*result + int(self._To)
if self._type is not None:
result = 31*result + hash(self._type)
if self._label is not None:
result = 31*result + hash(self._label)
return result
"""
* Calculates the difference between two NLP instances in terms of their edges.
*
* @param goldInstance the gold instance
* @param guessInstance the (system) guess instance.
* @return An NLPInstance with Matches, False Negatives and False Positives of the difference.
"""
def diff(self, goldInstance=NLPInstance, guessInstance=NLPInstance):
diff = NLPInstance()
diff.renderType = goldInstance.renderType
for splitPoint in tuple(goldInstance.splitPoints):
diff.splitPoints.append(splitPoint)
diff.addTokens(goldInstance.tokens)
goldIdentities = set()
goldIdentities.update(self.createIdentities(goldInstance.getEdges()))
guessIdentities = set()
guessIdentities.update(self.createIdentities(guessInstance.getEdges()))
fn = set()
fn = goldIdentities - guessIdentities
fp = set()
fp = guessIdentities - goldIdentities
matches = set()
matches = goldIdentities & guessIdentities
for edgeid in fn:
edge = edgeid.edge
Type = edge.type +":FN"
diff.addEdge(edge=Edge(From=edge.From, To=edge.To, label=edge.label, note=edge.note, Type=Type,
renderType=edge.renderType, description=edge.description))
for edgeid in fp:
edge = edgeid.edge
Type = edge.type +":FP"
diff.addEdge(edge=Edge(From=edge.From, To=edge.To, label=edge.label, note=edge.note, Type=Type,
renderType=edge.renderType, description=edge.description))
for edgeid in matches:
edge = edgeid.edge
Type = edge.type +":Match"
diff.addEdge(edge=Edge(From=edge.From, To=edge.To, label=edge.label, note=edge.note, Type=Type,
renderType=edge.renderType, description=edge.description))
return diff
def createIdentities(self, edges):
result = set()
for edge in edges:
result.add(NLPDiff.EdgeIdentity(edge))
return result