|
1
|
1 |
|
class NamedEntityMerging: |
|
2
|
|
|
""" |
|
3
|
|
|
Perform several merging on the dependency tree according to the named |
|
4
|
|
|
entity tags. |
|
5
|
|
|
""" |
|
6
|
1 |
|
def __init__(self, tree): |
|
7
|
1 |
|
self.tree = tree |
|
8
|
|
|
|
|
9
|
1 |
|
@classmethod |
|
10
|
|
|
def _mergeChildParent(cls, tree): |
|
11
|
|
|
""" |
|
12
|
|
|
Merge all nodes n1, n2 such that: |
|
13
|
|
|
* n1 is parent of n2 |
|
14
|
|
|
* n1 and n2 have a same namedEntityTag |
|
15
|
|
|
Don't merge if the 2 words are linked by a conjonction |
|
16
|
|
|
""" |
|
17
|
1 |
|
for child in tree.child: |
|
18
|
1 |
|
cls._mergeChildParent(child) |
|
19
|
1 |
|
if tree.namedEntityTag == 'undef': |
|
20
|
1 |
|
return |
|
21
|
1 |
|
sameTagChild = set() |
|
22
|
1 |
|
for child in tree.child: |
|
23
|
1 |
|
if child.namedEntityTag == tree.namedEntityTag and not child.dependency.startswith('conj'): |
|
24
|
1 |
|
sameTagChild.add(child) |
|
25
|
1 |
|
for child in sameTagChild: |
|
26
|
1 |
|
tree.merge(child, True) |
|
27
|
|
|
|
|
28
|
1 |
|
@classmethod |
|
29
|
|
|
def _mergeSibling(cls, tree): |
|
30
|
|
|
""" |
|
31
|
|
|
Merge all nodes n1, n2 such that: |
|
32
|
|
|
* n1 and n2 have a same parent |
|
33
|
|
|
* n1 and n2 have a same namedEntityTag (except conj) |
|
34
|
|
|
* n1 and n2 have a same dependency |
|
35
|
|
|
""" |
|
36
|
1 |
|
for child in tree.child: |
|
37
|
1 |
|
cls._mergeSibling(child) |
|
38
|
1 |
|
tagToNodes = {} |
|
39
|
1 |
|
for child in tree.child: |
|
40
|
1 |
|
if child.namedEntityTag == 'undef' or child.dependency.startswith('conj'): |
|
41
|
1 |
|
continue |
|
42
|
1 |
|
try: |
|
43
|
1 |
|
tagToNodes[child.namedEntityTag+child.dependency].add(child) |
|
44
|
1 |
|
except KeyError: |
|
45
|
1 |
|
tagToNodes[child.namedEntityTag+child.dependency] = set([child]) |
|
46
|
1 |
|
for sameTag in tagToNodes.values(): |
|
47
|
1 |
|
x = sameTag.pop() |
|
48
|
1 |
|
for other in sameTag: |
|
49
|
1 |
|
x.merge(other, True) |
|
50
|
|
|
|
|
51
|
1 |
|
def merge(self): |
|
52
|
|
|
""" |
|
53
|
|
|
Perform the different merges. |
|
54
|
|
|
""" |
|
55
|
1 |
|
self._mergeChildParent(self.tree) |
|
56
|
1 |
|
self._mergeSibling(self.tree) |
|
57
|
|
|
|
|
58
|
1 |
|
class PrepositionMerging: |
|
59
|
|
|
""" |
|
60
|
|
|
Perform several merging on the dependency tree according to the preposition |
|
61
|
|
|
dependencies. |
|
62
|
|
|
""" |
|
63
|
1 |
|
prepositionSet = ['in', 'for', 'to', 'with', 'about', 'at', 'of', 'on', 'from', 'between', 'against'] |
|
64
|
|
|
|
|
65
|
1 |
|
@staticmethod |
|
66
|
|
|
def getPreposition(dep): |
|
67
|
|
|
""" |
|
68
|
|
|
Return the preposition associated to a prep dependency. |
|
69
|
|
|
""" |
|
70
|
1 |
|
return ' '.join(dep.split('_')[1:]) |
|
71
|
|
|
|
|
72
|
1 |
|
def __init__(self, tree): |
|
73
|
1 |
|
self.tree = tree |
|
74
|
|
|
|
|
75
|
1 |
|
@classmethod |
|
76
|
|
|
def _mergeNode(cls, tree): |
|
77
|
|
|
""" |
|
78
|
|
|
Merge x -> y into 'x y' if y is a preposition |
|
79
|
|
|
""" |
|
80
|
1 |
|
for child in tree.child: |
|
81
|
1 |
|
cls._mergeNode(child) |
|
82
|
1 |
|
if child.getWords() in cls.prepositionSet: |
|
83
|
1 |
|
tree.merge(child, True) |
|
84
|
|
|
|
|
85
|
1 |
|
@classmethod |
|
86
|
|
|
def _mergeEdge(cls, tree): |
|
87
|
|
|
""" |
|
88
|
|
|
Replace a -prep_x-> b by 'a x' -prep-> b if a is a verb, a -prep-> b otherwise |
|
89
|
|
|
Replace a -agent-> b by 'a by' -agent-> b |
|
90
|
|
|
""" |
|
91
|
1 |
|
for child in tree.child: |
|
92
|
1 |
|
cls._mergeEdge(child) |
|
93
|
1 |
|
if child.dependency.startswith('prep'): # prep_x or prepc_x |
|
94
|
1 |
|
preposition = cls.getPreposition(child.dependency) # type of the prep (of, in, ...) |
|
95
|
1 |
|
if tree.isVerb(): |
|
96
|
1 |
|
tree.appendWord(preposition) |
|
97
|
1 |
|
child.dependency = 'prep' |
|
98
|
1 |
|
if child.dependency == 'agent': |
|
99
|
1 |
|
assert tree.isVerb() |
|
100
|
1 |
|
tree.appendWord('by') |
|
101
|
|
|
|
|
102
|
1 |
|
def merge(self): |
|
103
|
|
|
""" |
|
104
|
|
|
Perform the different merges. |
|
105
|
|
|
""" |
|
106
|
1 |
|
self._mergeNode(self.tree) |
|
107
|
|
|
self._mergeEdge(self.tree) |
|
108
|
|
|
|