-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependencies.py
188 lines (140 loc) · 5.55 KB
/
dependencies.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Provides AnnotationEngines for deducing word dependencies in a text."""
import re
from abc import ABC
from re import Match as RegexMatch
from re import Pattern as RegexPattern
from typing import List, Optional
from enhanced_search.annotation import (
Annotation,
AnnotationResult,
LiteralString,
RelationshipType,
)
from enhanced_search.annotation.utils import convert_text_to_abstracted_string
class Pattern(ABC):
"""All patterns to match the annotations of a text string.
I went with classes instead of simple RegEx statements, since classes allow
for further internal manipulation of the output. A procedure that is not
necessary in all patterns, but is useful in some (e.g. in situation, where there is
negation).
To allow ambiguity in an annotation (e.g. a word is recognized both as plant and
as a location, the pattern matching has to catch cases like:
"{plant} in {plant|location}"
Hence, all patterns should apply ".*?" within the curly brackets
(i.e. "{.*?(?:taxon|plant|animal).*?}"), to consider this variability.
For testing the regex, you can use: https://regex101.com/
"""
regex_pattern: Optional[RegexPattern] = None
additional_data: dict = {}
def match(
self, text: str, annotations: List[Annotation], literals: List[LiteralString]
) -> List[dict]:
"""Checks the given text, if it fits the internal pattern.
Returns a list of dictionaries, each holding context data of a single
match within the text. If no match was found, the list is empty.
"""
if self.regex_pattern is None:
raise NotImplementedError(
"You did not provide a `regex_pattern` variable for this class!"
)
abstracted_matching_string = convert_text_to_abstracted_string(
text, annotations, literals
)
matches = self.regex_pattern.search(abstracted_matching_string)
additional_data = self.additional_data
return compile_result(matches, **additional_data)
class OnlyTaxonPattern(Pattern):
"""Matching text examples:
* "Fagus"
* "Fagus sylvatica"
"""
regex_pattern = re.compile(r"{.*?(?:taxon|plant|animal)<(?P<taxon>.*?)>}")
class SimpleTaxonLocationPattern(Pattern):
"""Matching text examples:
* "Fagus Deutschland"
* "Fagus in Deutschland"
* "Fagus in New Zealand"
"""
regex_pattern = re.compile(
r"{.*?(?:taxon|plant|animal)<(?P<taxon>.*?)>} +(?:in +)?"
r"{.*?location<(?P<location>.*?)>}"
)
class TaxonPropertyPattern(Pattern):
"""Matching text examples:
* "Pflanzen mit roten Blüten"
* "Plant with red flowers"
* "Plant red flowers"
"""
regex_pattern = re.compile(
r"{.*?(?:taxon|plant|animal)<(?P<subject>.+?)>} +(?:mit<.+?> +|with<.+?> +)?"
r"{.*?miscellaneous<(?P<object>.+?)>}"
r"(?: +{.*?miscellaneous<(?P<predicate>.+?)>})?"
)
class TaxonNumericalPropertyPattern(Pattern):
"""Matching text examples:
* "Pflanzen mit 3 Kelchblättern"
* "Plant with 3 petals"
* "Plant 3 petals"
"""
regex_pattern = re.compile(
r"{.*?(?:taxon|plant|animal)<(?P<subject>.+?)>} +(?:mit<.+?> +|with<.+?> +)?"
r"[0-9]+<(?P<object>.+?)> +"
r"{.*?miscellaneous<(?P<predicate>.+?)>}"
)
class AndConjunctionPattern(Pattern):
"""Recognizes AND-conjuncted terms."""
regex_pattern = re.compile(
r"\S+<(?P<subject>.+?)>}? (?:und<.+?>|and<.+?>) \S+<(?P<object>.+?)>"
)
additional_data = {"relationship": RelationshipType.AND}
class OrConjunctionPattern(Pattern):
"""Recognize OR-conjuncted terms."""
regex_pattern = re.compile(
r"\S+<(?P<subject>.+?)>}? (?:oder<.+?>|or<.+?>) \S+<(?P<object>.+?)>"
)
additional_data = {"relationship": RelationshipType.OR}
class PatternDependencyAnnotationEngine:
"""Inferences semantic relationships between Annotations and returns them
as Statement.
This AnnotationEngine relies on the fact that there already exists an
Annotation list. The inferencing is done RegEx patterns.
Obeys the AnnotatorEngine interface!
"""
patterns = [
TaxonNumericalPropertyPattern(),
TaxonPropertyPattern(),
AndConjunctionPattern(),
OrConjunctionPattern(),
]
def parse(self, text: str, annotation_result: AnnotationResult) -> None:
"""Searches for patterns in a given query and returns
a list of context data. If no pattern matches the query, the resulting list
is empty.
The present patterns are iterated successively and a result is returned
as soon as a pattern matches.
"""
statements = []
for pattern in self.patterns:
statements = pattern.match(
text=text,
annotations=annotation_result.named_entity_recognition,
literals=annotation_result.literals,
)
if statements:
break
annotation_result.annotation_relationships = statements
def compile_result(matches: Optional[RegexMatch], **kwargs) -> List[dict]:
"""Uses the group names of the matches to generate a dict with all URI data.
The group names are used as keys. The value of the group is that of
an Annotation ID.
"""
if matches is None:
return []
context = []
statement = {}
for name, word_id in matches.groupdict().items():
statement[name] = word_id
for name, value in kwargs.items():
statement[name] = value
context.append(statement)
return context