wowool-common 3.0.2.dev19__py3-none-any.whl → 3.0.2.dev23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of wowool-common might be problematic. Click here for more details.
- wowool/analysis.py +10 -10
- wowool/analysis_parser.py +3 -3
- wowool/annotation/__init__.py +2 -1
- wowool/annotation/annotation.py +16 -7
- wowool/annotation/concept.py +1 -473
- wowool/annotation/entity.py +473 -0
- wowool/annotation/paragraph.py +1 -1
- wowool/annotation/sentence.py +15 -15
- wowool/annotation/token.py +6 -6
- wowool/document/apps/themes.py +1 -1
- wowool/document/apps/topics.py +1 -1
- wowool/document/document.py +11 -10
- wowool/io/provider/pdf_old.pydnu +269 -0
- wowool/string/__init__.py +4 -4
- wowool/visitor.py +2 -2
- wowool/wow.py +2 -2
- {wowool_common-3.0.2.dev19.dist-info → wowool_common-3.0.2.dev23.dist-info}/METADATA +1 -1
- {wowool_common-3.0.2.dev19.dist-info → wowool_common-3.0.2.dev23.dist-info}/RECORD +20 -18
- {wowool_common-3.0.2.dev19.dist-info → wowool_common-3.0.2.dev23.dist-info}/WHEEL +0 -0
- {wowool_common-3.0.2.dev19.dist-info → wowool_common-3.0.2.dev23.dist-info}/top_level.txt +0 -0
wowool/analysis.py
CHANGED
|
@@ -2,18 +2,18 @@ import json
|
|
|
2
2
|
from functools import wraps
|
|
3
3
|
from io import StringIO
|
|
4
4
|
from wowool.annotation.sentence import Sentence
|
|
5
|
-
from wowool.annotation.
|
|
5
|
+
from wowool.annotation.entity import Entity
|
|
6
6
|
from typing import Union
|
|
7
7
|
from wowool.io.provider import InputProvider
|
|
8
8
|
|
|
9
9
|
APP_ID = "wowool_analysis"
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
def _filter_pass_thru(concept:
|
|
12
|
+
def _filter_pass_thru(concept: Entity) -> bool:
|
|
13
13
|
return True
|
|
14
14
|
|
|
15
15
|
|
|
16
|
-
def _filter_pass_thru_concept(concept:
|
|
16
|
+
def _filter_pass_thru_concept(concept: Entity) -> bool:
|
|
17
17
|
return concept.uri != "Sentence"
|
|
18
18
|
|
|
19
19
|
|
|
@@ -33,7 +33,7 @@ def _content_access(fnc):
|
|
|
33
33
|
|
|
34
34
|
class Analysis:
|
|
35
35
|
"""
|
|
36
|
-
:class:`Analysis` is a class that holds the results of an NLP analysis. Instances of this class can parse the NLP result and yield objects such as :class:`Sentence <wowool.annotation.sentence.Sentence>`, :class:`Token <wowool.annotation.token.Token>` and :class:`
|
|
36
|
+
:class:`Analysis` is a class that holds the results of an NLP analysis. Instances of this class can parse the NLP result and yield objects such as :class:`Sentence <wowool.annotation.sentence.Sentence>`, :class:`Token <wowool.annotation.token.Token>` and :class:`Entity <wowool.annotation.entity.Entity>` to conveniently access the data behind those annotations. Furthermore, the class also provides a static method to parse raw JSON data as returned by, for example, the Portal API.
|
|
37
37
|
"""
|
|
38
38
|
|
|
39
39
|
# required buy some common decorators.
|
|
@@ -194,9 +194,9 @@ class Analysis:
|
|
|
194
194
|
@property
|
|
195
195
|
@_content_access
|
|
196
196
|
def entities(self):
|
|
197
|
-
from wowool.annotation import
|
|
197
|
+
from wowool.annotation import Entity
|
|
198
198
|
|
|
199
|
-
yield from
|
|
199
|
+
yield from Entity.iter(self)
|
|
200
200
|
|
|
201
201
|
@_content_access
|
|
202
202
|
def concepts(self, filter=_filter_pass_thru_concept):
|
|
@@ -204,14 +204,14 @@ class Analysis:
|
|
|
204
204
|
Access the concepts in the analysis
|
|
205
205
|
|
|
206
206
|
:param filter: Optional filter to select or discard concepts
|
|
207
|
-
:type filter: Functor accepting a :class:`
|
|
207
|
+
:type filter: Functor accepting a :class:`Entity <wowool.annotation.Entity>` and returning a ``bool``
|
|
208
208
|
|
|
209
209
|
:return: A generator expression yielding the concepts in the processed document
|
|
210
|
-
:rtype: :class:`Concepts <wowool.annotation.
|
|
210
|
+
:rtype: :class:`Concepts <wowool.annotation.Entity>`
|
|
211
211
|
"""
|
|
212
|
-
from wowool.annotation import
|
|
212
|
+
from wowool.annotation import Entity
|
|
213
213
|
|
|
214
|
-
for concept in
|
|
214
|
+
for concept in Entity.iter(self, filter):
|
|
215
215
|
yield concept
|
|
216
216
|
|
|
217
217
|
@property
|
wowool/analysis_parser.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import json
|
|
2
|
-
from wowool.annotation.
|
|
2
|
+
from wowool.annotation.entity import Entity
|
|
3
3
|
from wowool.annotation.sentence import Sentence
|
|
4
4
|
from wowool.annotation.token import Token, MorphData
|
|
5
5
|
from wowool.analysis import Analysis
|
|
@@ -44,8 +44,8 @@ def _parse_sentence(json_sentence):
|
|
|
44
44
|
_add_morphology(token, token.morphology, annotation[5])
|
|
45
45
|
token._annotation_idx = len(sentence.annotations)
|
|
46
46
|
sentence.annotations.append(token)
|
|
47
|
-
elif annotation[0] == AnnotationType.CONCEPT: #
|
|
48
|
-
concept =
|
|
47
|
+
elif annotation[0] == AnnotationType.CONCEPT: # Entity
|
|
48
|
+
concept = Entity(annotation[1], annotation[2], annotation[3])
|
|
49
49
|
concept.set_attributes(annotation[4] if len(annotation) > 4 else {})
|
|
50
50
|
concept.set_sentence_annotations_index(sentence.annotations, len(sentence.annotations))
|
|
51
51
|
sentence.annotations.append(concept)
|
wowool/annotation/__init__.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from wowool.annotation.annotation import Annotation, ErrorAnnotationNotFound
|
|
2
2
|
from wowool.annotation.token import Token
|
|
3
|
-
from wowool.annotation.
|
|
3
|
+
from wowool.annotation.entity import Entity
|
|
4
4
|
from wowool.annotation.sentence import Sentence
|
|
5
|
+
from wowool.annotation.concept import Concept
|
wowool/annotation/annotation.py
CHANGED
|
@@ -44,12 +44,21 @@ class BasicAnnotation:
|
|
|
44
44
|
@property
|
|
45
45
|
def is_concept(self) -> bool:
|
|
46
46
|
"""
|
|
47
|
-
|
|
47
|
+
Deprecated, use :meth:`is_entity` instead
|
|
48
|
+
:return: Whether the annotation is a :class:`Entity`
|
|
48
49
|
:rtype: ``bool``
|
|
49
50
|
"""
|
|
50
|
-
|
|
51
|
+
return self.is_entity
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
@property
|
|
54
|
+
def is_entity(self) -> bool:
|
|
55
|
+
"""
|
|
56
|
+
:return: Whether the annotation is a :class:`Entity`
|
|
57
|
+
:rtype: ``bool``
|
|
58
|
+
"""
|
|
59
|
+
from wowool.annotation.entity import Entity
|
|
60
|
+
|
|
61
|
+
return isinstance(self, Entity)
|
|
53
62
|
|
|
54
63
|
@property
|
|
55
64
|
def is_sentence(self) -> bool:
|
|
@@ -127,16 +136,16 @@ class Annotation(BasicAnnotation):
|
|
|
127
136
|
.. code-block:: python
|
|
128
137
|
|
|
129
138
|
document = analyzer("Hello from Antwerp, said John Smith.")
|
|
130
|
-
for concept in
|
|
139
|
+
for concept in Entity.iter(document, lambda concept : concept.uri == "NP"):
|
|
131
140
|
print(concept)
|
|
132
141
|
|
|
133
142
|
:param object: Object to iterate
|
|
134
|
-
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`
|
|
143
|
+
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`Entity <wowool.annotation.entity.Entity>`
|
|
135
144
|
:param filter: Predicate function to filter the concepts, callable that accepts a concept and returns True if the concept is considered a match
|
|
136
|
-
:type filter: Callable accepting a :class:`
|
|
145
|
+
:type filter: Callable accepting a :class:`Entity` and returning a ``bool``
|
|
137
146
|
|
|
138
147
|
:return: A generator expression yielding concepts
|
|
139
|
-
:rtype: :class:`
|
|
148
|
+
:rtype: :class:`Entity <wowool.annotation.entity.Entity>`
|
|
140
149
|
"""
|
|
141
150
|
from wowool.document.document import Document
|
|
142
151
|
from wowool.analysis import Analysis
|
wowool/annotation/concept.py
CHANGED
|
@@ -1,473 +1 @@
|
|
|
1
|
-
from
|
|
2
|
-
from wowool.annotation.annotation import Annotation, ErrorAnnotationNotFound
|
|
3
|
-
from wowool.annotation.token import Token
|
|
4
|
-
from typing import Callable, Union, List, Type, cast, Generator, NewType, Iterator, Self
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def _align(idx, collection) -> int:
|
|
8
|
-
bo = collection[idx].begin_offset
|
|
9
|
-
eo = collection[idx].end_offset
|
|
10
|
-
rvidx = idx - 1
|
|
11
|
-
while rvidx >= 0:
|
|
12
|
-
if bo == collection[rvidx].begin_offset and eo == collection[rvidx].end_offset:
|
|
13
|
-
rvidx -= 1
|
|
14
|
-
else:
|
|
15
|
-
return rvidx + 1
|
|
16
|
-
return idx
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def _filter_pass_thru(concept) -> bool:
|
|
20
|
-
return True
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class Concept(Annotation):
|
|
24
|
-
"""
|
|
25
|
-
:class:`Concept` is a class that contains all the information of a concept
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
FilterType = Callable[[Self], bool]
|
|
29
|
-
|
|
30
|
-
def __init__(self, begin_offset: int, end_offset: int, uri: str):
|
|
31
|
-
"""
|
|
32
|
-
Initialize a :class:`Concept` instance
|
|
33
|
-
|
|
34
|
-
:param begin_offset: Begin offset of the concept
|
|
35
|
-
:type begin_offset: ``int``
|
|
36
|
-
:param end_offset: End offset of the concept
|
|
37
|
-
:type end_offset: ``int``
|
|
38
|
-
:param uri: URI, or name, of the concept
|
|
39
|
-
:type uri: ``str``
|
|
40
|
-
|
|
41
|
-
:return: An initialized concept
|
|
42
|
-
:rtype: :class:`Concept`
|
|
43
|
-
"""
|
|
44
|
-
self._attributes = {}
|
|
45
|
-
super(Concept, self).__init__(begin_offset, end_offset)
|
|
46
|
-
self._uri = uri
|
|
47
|
-
self._sentence_annotations = None
|
|
48
|
-
self._annotations = None
|
|
49
|
-
self._literals = None
|
|
50
|
-
self._stems = None
|
|
51
|
-
self._tokens = None
|
|
52
|
-
self._dict = None
|
|
53
|
-
self._canonical = None
|
|
54
|
-
self._text = None
|
|
55
|
-
|
|
56
|
-
@property
|
|
57
|
-
def uri(self) -> str:
|
|
58
|
-
"""
|
|
59
|
-
:return: The URI, or name, of the concept
|
|
60
|
-
:type: ``str``
|
|
61
|
-
"""
|
|
62
|
-
return self._uri
|
|
63
|
-
|
|
64
|
-
@property
|
|
65
|
-
def literal(self) -> str:
|
|
66
|
-
"""
|
|
67
|
-
:return: The literal representation of the concept
|
|
68
|
-
:rtype: ``str``
|
|
69
|
-
"""
|
|
70
|
-
return self.text
|
|
71
|
-
|
|
72
|
-
@property
|
|
73
|
-
def literals(self) -> List[str]:
|
|
74
|
-
"""
|
|
75
|
-
:return: The literals of the concept
|
|
76
|
-
:rtype: A ``list`` of ``str``
|
|
77
|
-
"""
|
|
78
|
-
if not self._literals:
|
|
79
|
-
self._literals = [token.literal for token in self.tokens]
|
|
80
|
-
return self._literals
|
|
81
|
-
|
|
82
|
-
@property
|
|
83
|
-
def stem(self) -> str:
|
|
84
|
-
"""
|
|
85
|
-
:return: The stem representation of the concept
|
|
86
|
-
:type: ``str``
|
|
87
|
-
"""
|
|
88
|
-
return " ".join(self.stems)
|
|
89
|
-
|
|
90
|
-
def has_canonical(self) -> bool:
|
|
91
|
-
if "canonical" in self._attributes:
|
|
92
|
-
return True
|
|
93
|
-
elif "icanonical" in self._attributes:
|
|
94
|
-
return True
|
|
95
|
-
return False
|
|
96
|
-
|
|
97
|
-
def _get_canonical(self) -> str:
|
|
98
|
-
if "canonical" in self._attributes:
|
|
99
|
-
return self._attributes["canonical"][0]
|
|
100
|
-
elif "icanonical" in self._attributes:
|
|
101
|
-
return self._attributes["icanonical"][0]
|
|
102
|
-
elif self._has_guesses() or self._has_props():
|
|
103
|
-
return self.literal
|
|
104
|
-
return self.stem if self.stem else self.literal
|
|
105
|
-
|
|
106
|
-
@property
|
|
107
|
-
def canonical(self) -> str:
|
|
108
|
-
"""
|
|
109
|
-
:return: The stem representation of the concept
|
|
110
|
-
:type: ``str``
|
|
111
|
-
"""
|
|
112
|
-
if self._canonical is None:
|
|
113
|
-
self._canonical = self._get_canonical()
|
|
114
|
-
return self._canonical
|
|
115
|
-
|
|
116
|
-
@property
|
|
117
|
-
def attributes(self):
|
|
118
|
-
return self._attributes
|
|
119
|
-
|
|
120
|
-
@property
|
|
121
|
-
def stems(self) -> List[str]:
|
|
122
|
-
"""
|
|
123
|
-
:return: The stems of the concept
|
|
124
|
-
:rtype: A ``list`` of ``str``
|
|
125
|
-
"""
|
|
126
|
-
if not self._stems:
|
|
127
|
-
self._stems = [token.stem for token in self.tokens]
|
|
128
|
-
return self._stems
|
|
129
|
-
|
|
130
|
-
def _get_text(self):
|
|
131
|
-
"""
|
|
132
|
-
:return: A string representation of the concept
|
|
133
|
-
:rtype: ``str``
|
|
134
|
-
"""
|
|
135
|
-
retval = ""
|
|
136
|
-
prev_tk = None
|
|
137
|
-
for tk in [a for a in self.annotations if a.is_token]:
|
|
138
|
-
if prev_tk and prev_tk.end_offset != tk.begin_offset:
|
|
139
|
-
retval += " "
|
|
140
|
-
retval += tk.literal
|
|
141
|
-
prev_tk = tk
|
|
142
|
-
return retval.strip()
|
|
143
|
-
|
|
144
|
-
@property
|
|
145
|
-
def text(self):
|
|
146
|
-
"""
|
|
147
|
-
:return: A string representation of the concept
|
|
148
|
-
:rtype: ``str``
|
|
149
|
-
"""
|
|
150
|
-
if self._text is None:
|
|
151
|
-
self._text = self._get_text()
|
|
152
|
-
return self._text
|
|
153
|
-
|
|
154
|
-
@property
|
|
155
|
-
def tokens(self) -> List[Token]:
|
|
156
|
-
"""
|
|
157
|
-
:return: The tokens of the concept
|
|
158
|
-
:rtype: A ``list`` of ``str``
|
|
159
|
-
"""
|
|
160
|
-
if not self._tokens:
|
|
161
|
-
assert self._sentence_annotations is not None
|
|
162
|
-
self._tokens = [
|
|
163
|
-
annotation
|
|
164
|
-
for annotation in self._sentence_annotations[self._annotation_idx :]
|
|
165
|
-
if annotation.is_token and annotation.begin_offset < self.end_offset
|
|
166
|
-
]
|
|
167
|
-
return self._tokens
|
|
168
|
-
|
|
169
|
-
@property
|
|
170
|
-
def annotations(self) -> List[Annotation]:
|
|
171
|
-
"""
|
|
172
|
-
:return: The tokens of the concept
|
|
173
|
-
:rtype: A ``list`` of ``str``
|
|
174
|
-
"""
|
|
175
|
-
if not self._annotations:
|
|
176
|
-
assert self._sentence_annotations is not None
|
|
177
|
-
self._annotations = [
|
|
178
|
-
annotation for annotation in self._sentence_annotations[self._annotation_idx :] if annotation.begin_offset < self.end_offset
|
|
179
|
-
]
|
|
180
|
-
return self._annotations
|
|
181
|
-
|
|
182
|
-
def set_attributes(self, new_attributes: dict):
|
|
183
|
-
self._attributes = new_attributes
|
|
184
|
-
|
|
185
|
-
def _has_guesses(self) -> bool:
|
|
186
|
-
return len([tk for tk in self.tokens if "guess" in tk.properties]) > 0
|
|
187
|
-
|
|
188
|
-
def _has_props(self) -> bool:
|
|
189
|
-
for tk in self.tokens:
|
|
190
|
-
if tk.has_pos("Prop"):
|
|
191
|
-
return True
|
|
192
|
-
return False
|
|
193
|
-
|
|
194
|
-
def __setstate__(self, d):
|
|
195
|
-
self.__dict__.update(d)
|
|
196
|
-
|
|
197
|
-
def __getstate__(self):
|
|
198
|
-
return self.__dict__
|
|
199
|
-
|
|
200
|
-
def __getattr__(self, name: str) -> Union[None, str, Concept]:
|
|
201
|
-
"""
|
|
202
|
-
Find an attribute or the first child instance of the concept with the given name. Some often used attributes are:
|
|
203
|
-
|
|
204
|
-
* ``canonical``: canonical representation (``str``) of the concept
|
|
205
|
-
* ``attributes``: attributes (``dict``) collected on the concept
|
|
206
|
-
|
|
207
|
-
For example:
|
|
208
|
-
|
|
209
|
-
.. code-block:: python
|
|
210
|
-
|
|
211
|
-
# Find a child concept in this concept
|
|
212
|
-
person = concept.Person
|
|
213
|
-
# or print the attribute 'gender'
|
|
214
|
-
print(person.gender)
|
|
215
|
-
|
|
216
|
-
:param name: Attribute name
|
|
217
|
-
:type name: ``str``
|
|
218
|
-
|
|
219
|
-
:return: The first instance of the matching concept or the requested attribute value
|
|
220
|
-
:type: :class:`Concept` or ``str``
|
|
221
|
-
"""
|
|
222
|
-
|
|
223
|
-
if name in self._attributes:
|
|
224
|
-
return ",".join(self._attributes[name])
|
|
225
|
-
concept = self.find_first(name)
|
|
226
|
-
if concept:
|
|
227
|
-
return cast(Concept, concept)
|
|
228
|
-
|
|
229
|
-
def find_first(self, uri: str) -> Union[None, Concept]:
|
|
230
|
-
"""
|
|
231
|
-
Find the first child instance of the concept with the given URI in this concept
|
|
232
|
-
|
|
233
|
-
For example:
|
|
234
|
-
|
|
235
|
-
.. code-block:: python
|
|
236
|
-
|
|
237
|
-
# Find a child concept in the current concept
|
|
238
|
-
person = concept.find_first('Person'))
|
|
239
|
-
|
|
240
|
-
:param uri: URI, or name, of the concept
|
|
241
|
-
:type uri: ``str``
|
|
242
|
-
|
|
243
|
-
:returns: The first matching child instance of the given concept
|
|
244
|
-
:rtype: :class:`Concept`
|
|
245
|
-
"""
|
|
246
|
-
try:
|
|
247
|
-
return next(self._find(uri))
|
|
248
|
-
except StopIteration:
|
|
249
|
-
return None
|
|
250
|
-
|
|
251
|
-
def find(self, uri: str) -> List[Concept]:
|
|
252
|
-
"""
|
|
253
|
-
Find all child concepts with the given URI in this concept
|
|
254
|
-
|
|
255
|
-
For example:
|
|
256
|
-
|
|
257
|
-
.. code-block:: python
|
|
258
|
-
|
|
259
|
-
# Find a child concept in the current concept
|
|
260
|
-
for person in concept.find('Person')):
|
|
261
|
-
print(person)
|
|
262
|
-
|
|
263
|
-
:param uri: URI, or name, of the concept
|
|
264
|
-
:type uri: str
|
|
265
|
-
|
|
266
|
-
:returns: The matching child instances of the given concept
|
|
267
|
-
:rtype: A ``list`` of :class:`Concept`
|
|
268
|
-
"""
|
|
269
|
-
return [concept for concept in self._find(uri)]
|
|
270
|
-
|
|
271
|
-
def _find(self, uri: str) -> Generator[Concept, None, None]:
|
|
272
|
-
assert self._sentence_annotations is not None
|
|
273
|
-
idx = _align(self._annotation_idx, self._sentence_annotations)
|
|
274
|
-
sent_len = len(self._sentence_annotations)
|
|
275
|
-
while idx < sent_len:
|
|
276
|
-
annotation = self._sentence_annotations[idx]
|
|
277
|
-
if annotation.is_concept and uri == cast(Concept, annotation).uri:
|
|
278
|
-
yield cast(Concept, annotation)
|
|
279
|
-
elif annotation.is_token:
|
|
280
|
-
if annotation.begin_offset < self.end_offset:
|
|
281
|
-
pass
|
|
282
|
-
else:
|
|
283
|
-
return
|
|
284
|
-
idx += 1
|
|
285
|
-
|
|
286
|
-
def match(self, call_able: Callable[[str], bool]) -> List[Concept]:
|
|
287
|
-
"""
|
|
288
|
-
Find all child concepts with the given URI in this concept
|
|
289
|
-
|
|
290
|
-
For example:
|
|
291
|
-
|
|
292
|
-
.. code-block:: python
|
|
293
|
-
|
|
294
|
-
# Find a child concept in the current concept
|
|
295
|
-
for person in concept.find('Person')):
|
|
296
|
-
print(person)
|
|
297
|
-
|
|
298
|
-
:param uri: URI, or name, of the concept
|
|
299
|
-
:type uri: str
|
|
300
|
-
|
|
301
|
-
:returns: The matching child instances of the given concept
|
|
302
|
-
:rtype: A ``list`` of :class:`Concept`
|
|
303
|
-
"""
|
|
304
|
-
return [concept for concept in self._match(call_able)]
|
|
305
|
-
|
|
306
|
-
def _match(self, call_able: Callable[[str], bool]) -> Generator[Concept, None, None]:
|
|
307
|
-
assert self._sentence_annotations is not None
|
|
308
|
-
idx = _align(self._annotation_idx, self._sentence_annotations)
|
|
309
|
-
sent_len = len(self._sentence_annotations)
|
|
310
|
-
while idx < sent_len:
|
|
311
|
-
annotation = self._sentence_annotations[idx]
|
|
312
|
-
if annotation.is_concept and call_able(cast(Self, annotation).uri):
|
|
313
|
-
yield cast(Concept, annotation)
|
|
314
|
-
elif annotation.is_token:
|
|
315
|
-
if annotation.begin_offset < self.end_offset:
|
|
316
|
-
pass
|
|
317
|
-
else:
|
|
318
|
-
return
|
|
319
|
-
idx += 1
|
|
320
|
-
|
|
321
|
-
def __getitem__(self, key):
|
|
322
|
-
"""
|
|
323
|
-
:return: Attribute of the concept
|
|
324
|
-
:rtype: ``str``
|
|
325
|
-
"""
|
|
326
|
-
if self._dict and key in self._dict:
|
|
327
|
-
return self._dict[key]
|
|
328
|
-
|
|
329
|
-
def rich(self) -> str:
|
|
330
|
-
"""
|
|
331
|
-
:return: A rich string representation of the concept
|
|
332
|
-
:rtype: ``str``
|
|
333
|
-
"""
|
|
334
|
-
retval = "<uri>C</uri>:" + Annotation.__repr__(self) + ": <uri>" + self.uri + "</uri>"
|
|
335
|
-
if self._attributes:
|
|
336
|
-
retval += ",@(<default>"
|
|
337
|
-
for k, vs in sorted(self._attributes.items()):
|
|
338
|
-
for v in vs:
|
|
339
|
-
retval += f"{k}='{v}' "
|
|
340
|
-
retval += "</default>)"
|
|
341
|
-
return retval
|
|
342
|
-
|
|
343
|
-
def keys(self):
|
|
344
|
-
"""
|
|
345
|
-
This function is used to convert a concept object to a dictionary
|
|
346
|
-
|
|
347
|
-
.. code-block:: python
|
|
348
|
-
|
|
349
|
-
{ **concept }
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
:return: a list of the keys of the concept.
|
|
353
|
-
:rtype: list(str)
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
"""
|
|
357
|
-
if not self._dict:
|
|
358
|
-
self._dict = self.to_json()
|
|
359
|
-
return self._dict.keys()
|
|
360
|
-
|
|
361
|
-
def __repr__(self) -> str:
|
|
362
|
-
retval = "C:" + Annotation.__repr__(self) + ": " + self.uri
|
|
363
|
-
if self._attributes:
|
|
364
|
-
retval += ",@("
|
|
365
|
-
for k, vs in sorted(self._attributes.items()):
|
|
366
|
-
for v in vs:
|
|
367
|
-
retval += f"{k}='{v}' "
|
|
368
|
-
retval += ")"
|
|
369
|
-
return retval
|
|
370
|
-
|
|
371
|
-
def to_json(self):
|
|
372
|
-
"""
|
|
373
|
-
:return: A dictionary representing a JSON object of the concept
|
|
374
|
-
:rtype: ``dict``
|
|
375
|
-
"""
|
|
376
|
-
dict_self = {"uri": self.uri, "literal": self.literal, "stem": self.stem}
|
|
377
|
-
dict_attributes = {}
|
|
378
|
-
for k, vl in self._attributes.items():
|
|
379
|
-
dict_attributes[k] = ",".join([str(v) for v in vl])
|
|
380
|
-
return {**dict_self, **dict_attributes}
|
|
381
|
-
|
|
382
|
-
@staticmethod
|
|
383
|
-
def _document_iter(doc, filter: FilterType = _filter_pass_thru) -> Iterator[Concept]:
|
|
384
|
-
for sentence in doc:
|
|
385
|
-
for annotation in sentence:
|
|
386
|
-
if annotation.is_concept and filter(annotation):
|
|
387
|
-
yield annotation
|
|
388
|
-
|
|
389
|
-
@staticmethod
|
|
390
|
-
def _sentence_iter(sentence, filter: FilterType = _filter_pass_thru) -> Iterator[Concept]:
|
|
391
|
-
for annotation in sentence:
|
|
392
|
-
if annotation.is_concept and filter(annotation):
|
|
393
|
-
yield annotation
|
|
394
|
-
|
|
395
|
-
@staticmethod
|
|
396
|
-
def _concept_iter(concept, filter: FilterType = _filter_pass_thru, align: bool = True) -> Iterator[Concept]:
|
|
397
|
-
idx = _align(concept._annotation_idx, concept._sentence_annotations) if align else concept._annotation_idx + 1
|
|
398
|
-
N = len(concept._sentence_annotations)
|
|
399
|
-
while idx < N:
|
|
400
|
-
if concept._annotation_idx != idx:
|
|
401
|
-
annotation = concept._sentence_annotations[idx]
|
|
402
|
-
if annotation.begin_offset >= concept.end_offset:
|
|
403
|
-
break
|
|
404
|
-
if annotation.is_concept and filter(annotation) and annotation.end_offset <= concept.end_offset:
|
|
405
|
-
yield cast(Concept, annotation)
|
|
406
|
-
idx += 1
|
|
407
|
-
|
|
408
|
-
@staticmethod
|
|
409
|
-
def iter(object, filter: FilterType = _filter_pass_thru, align: bool = True) -> Iterator[Concept]:
|
|
410
|
-
"""
|
|
411
|
-
Iterate over the concepts in a document, an analysis, a sentence or a concept. For example:
|
|
412
|
-
|
|
413
|
-
.. code-block:: python
|
|
414
|
-
|
|
415
|
-
document = analyzer("Hello from Antwerp, said John Smith.")
|
|
416
|
-
for concept in Concept.iter(document, lambda concept : concept.uri == "NP"):
|
|
417
|
-
print(concept)
|
|
418
|
-
|
|
419
|
-
:param object: Object to iterate
|
|
420
|
-
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`Concept <wowool.annotation.concept.Concept>`
|
|
421
|
-
:param filter: Predicate function to filter the concepts, callable that accepts a concept and returns True if the concept is considered a match
|
|
422
|
-
:type filter: Callable accepting a :class:`Concept` and returning a ``bool``
|
|
423
|
-
|
|
424
|
-
:return: A generator expression yielding concepts
|
|
425
|
-
:rtype: :class:`Concept <wowool.annotation.concept.Concept>`
|
|
426
|
-
"""
|
|
427
|
-
from wowool.document.document import Document
|
|
428
|
-
from wowool.analysis import Analysis
|
|
429
|
-
from wowool.annotation import Sentence
|
|
430
|
-
|
|
431
|
-
if isinstance(object, Document) and object.analysis:
|
|
432
|
-
yield from Concept._document_iter(object.analysis, filter)
|
|
433
|
-
elif isinstance(object, Analysis):
|
|
434
|
-
yield from Concept._document_iter(object, filter)
|
|
435
|
-
elif isinstance(object, Sentence):
|
|
436
|
-
yield from Concept._sentence_iter(object, filter)
|
|
437
|
-
elif isinstance(object, Concept):
|
|
438
|
-
yield from Concept._concept_iter(object, filter, align)
|
|
439
|
-
else:
|
|
440
|
-
raise TypeError(f"Expected Document, Analysis, Sentence, or Concept but got '{type(object)}'")
|
|
441
|
-
|
|
442
|
-
@staticmethod
|
|
443
|
-
def next(sentence: List[Annotation], object) -> Concept:
|
|
444
|
-
"""returns the next Concept"""
|
|
445
|
-
index = object.index + 1
|
|
446
|
-
sentence_length = len(sentence)
|
|
447
|
-
while index < sentence_length:
|
|
448
|
-
if sentence[index].is_concept:
|
|
449
|
-
return cast(Concept, sentence[index])
|
|
450
|
-
else:
|
|
451
|
-
index += 1
|
|
452
|
-
raise ErrorAnnotationNotFound
|
|
453
|
-
|
|
454
|
-
@staticmethod
|
|
455
|
-
def prev(sentence: List[Annotation], object) -> Concept:
|
|
456
|
-
"""returns the prev Concept"""
|
|
457
|
-
index = object.index - 1
|
|
458
|
-
while index >= 0:
|
|
459
|
-
if sentence[index].is_concept:
|
|
460
|
-
return cast(Concept, sentence[index])
|
|
461
|
-
else:
|
|
462
|
-
index -= 1
|
|
463
|
-
raise ErrorAnnotationNotFound
|
|
464
|
-
|
|
465
|
-
def set_sentence_annotations_index(self, sentence_annotations: list[Annotation], annotation_idx: int):
|
|
466
|
-
self._sentence_annotations = sentence_annotations
|
|
467
|
-
self._annotation_idx = annotation_idx
|
|
468
|
-
|
|
469
|
-
def get_attribute(self, key: str):
|
|
470
|
-
return self._attributes[key][0]
|
|
471
|
-
|
|
472
|
-
def get_attributes(self, key: str):
|
|
473
|
-
return self._attributes[key]
|
|
1
|
+
from wowool.annotation.entity import Entity as Concept
|