wowool-common 3.0.2.dev19__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 +289 -0
- wowool/analysis_parser.py +102 -0
- wowool/annotation/__init__.py +4 -0
- wowool/annotation/annotation.py +156 -0
- wowool/annotation/concept.py +473 -0
- wowool/annotation/paragraph.py +166 -0
- wowool/annotation/sentence.py +262 -0
- wowool/annotation/token.py +266 -0
- wowool/apps/info.py +97 -0
- wowool/common/pipeline/__init__.py +1 -0
- wowool/common/pipeline/objects.py +45 -0
- wowool/common/utilities.py +13 -0
- wowool/config.py +67 -0
- wowool/diagnostic.py +250 -0
- wowool/document/__init__.py +2 -0
- wowool/document/apps/chunks.py +22 -0
- wowool/document/apps/themes.py +11 -0
- wowool/document/apps/topics.py +11 -0
- wowool/document/document.py +399 -0
- wowool/document/factory.py +176 -0
- wowool/error.py +2 -0
- wowool/io/console/__init__.py +1 -0
- wowool/io/console/argument_parser.py +29 -0
- wowool/io/console/console.py +102 -0
- wowool/io/provider/__init__.py +4 -0
- wowool/io/provider/docx.py +26 -0
- wowool/io/provider/factory.py +173 -0
- wowool/io/provider/file.py +85 -0
- wowool/io/provider/html.py +42 -0
- wowool/io/provider/html_v2.py +420 -0
- wowool/io/provider/input_provider.py +38 -0
- wowool/io/provider/pdf.py +320 -0
- wowool/io/provider/pdf_no_layout.py +48 -0
- wowool/io/provider/rtf.py +14 -0
- wowool/io/provider/str.py +17 -0
- wowool/string/__init__.py +153 -0
- wowool/tool/woc/create.py +42 -0
- wowool/utility/__init__.py +16 -0
- wowool/utility/apps/decorators.py +66 -0
- wowool/utility/apps/yml_include.py +30 -0
- wowool/utility/csv/__init__.py +26 -0
- wowool/utility/default_arguments.py +44 -0
- wowool/utility/diagnostics.py +24 -0
- wowool/utility/iterators/__init__.py +59 -0
- wowool/utility/method_registry.py +20 -0
- wowool/utility/numbers.py +42 -0
- wowool/utility/path/__init__.py +1 -0
- wowool/utility/path/path.py +11 -0
- wowool/utility/text/cleanup.py +82 -0
- wowool/visitor.py +48 -0
- wowool/wow.py +424 -0
- wowool_common-3.0.2.dev19.dist-info/METADATA +10 -0
- wowool_common-3.0.2.dev19.dist-info/RECORD +55 -0
- wowool_common-3.0.2.dev19.dist-info/WHEEL +5 -0
- wowool_common-3.0.2.dev19.dist-info/top_level.txt +1 -0
wowool/analysis.py
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from io import StringIO
|
|
4
|
+
from wowool.annotation.sentence import Sentence
|
|
5
|
+
from wowool.annotation.concept import Concept
|
|
6
|
+
from typing import Union
|
|
7
|
+
from wowool.io.provider import InputProvider
|
|
8
|
+
|
|
9
|
+
APP_ID = "wowool_analysis"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _filter_pass_thru(concept: Concept) -> bool:
|
|
13
|
+
return True
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _filter_pass_thru_concept(concept: Concept) -> bool:
|
|
17
|
+
return concept.uri != "Sentence"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _content_access(fnc):
|
|
21
|
+
@wraps(fnc)
|
|
22
|
+
def wrapper(self, *args, **kwargs):
|
|
23
|
+
if self._sentences is None and self._json_data is None:
|
|
24
|
+
self._json_data = self._cpp.to_json()
|
|
25
|
+
current_cpp = self._cpp
|
|
26
|
+
self.__dict__.update(Analysis.parse(self._json_data, self.id).__dict__)
|
|
27
|
+
self._cpp = current_cpp
|
|
28
|
+
return fnc(self, *args, **kwargs)
|
|
29
|
+
|
|
30
|
+
wrapper.__doc__ = fnc.__doc__
|
|
31
|
+
return wrapper
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class Analysis:
|
|
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:`Concept <wowool.annotation.concept.Concept>` 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
|
+
"""
|
|
38
|
+
|
|
39
|
+
# required buy some common decorators.
|
|
40
|
+
ID = APP_ID
|
|
41
|
+
|
|
42
|
+
@staticmethod
|
|
43
|
+
def parse(json_data: Union[dict, str], id=None):
|
|
44
|
+
"""
|
|
45
|
+
Create an :class:`Analysis` instance from JSON (``dict``) data such as that returned from the Portal API.
|
|
46
|
+
|
|
47
|
+
.. code-block:: python
|
|
48
|
+
|
|
49
|
+
analysis = Analysis.parse(json_raw)
|
|
50
|
+
|
|
51
|
+
:param json_data: Raw JSON representation of the NLP analysis results
|
|
52
|
+
:type json_data: ``dict``
|
|
53
|
+
|
|
54
|
+
:rtype: :class:`Analysis`
|
|
55
|
+
"""
|
|
56
|
+
from wowool.analysis_parser import parse_document
|
|
57
|
+
|
|
58
|
+
return parse_document(json_data, id)
|
|
59
|
+
|
|
60
|
+
def __init__(
|
|
61
|
+
self,
|
|
62
|
+
cpp_document,
|
|
63
|
+
document_id=None,
|
|
64
|
+
language=None,
|
|
65
|
+
sentences=None,
|
|
66
|
+
json_data=None,
|
|
67
|
+
metadata=None,
|
|
68
|
+
):
|
|
69
|
+
super(Analysis, self).__init__()
|
|
70
|
+
self._cpp = cpp_document
|
|
71
|
+
self._language = language
|
|
72
|
+
self._sentences = sentences
|
|
73
|
+
self._json_data = json_data
|
|
74
|
+
self._metadata = metadata
|
|
75
|
+
self._text = None
|
|
76
|
+
|
|
77
|
+
if document_id != None:
|
|
78
|
+
self.id = document_id
|
|
79
|
+
elif self._cpp:
|
|
80
|
+
self.id = self._cpp.id()
|
|
81
|
+
else:
|
|
82
|
+
self.id = "none"
|
|
83
|
+
|
|
84
|
+
@_content_access
|
|
85
|
+
def __iter__(self):
|
|
86
|
+
"""
|
|
87
|
+
An :class:`Analysis` instance is iterable, yielding :class:`Sentence <wowool.annotation.sentence.Sentence>` objects. For example:
|
|
88
|
+
|
|
89
|
+
.. code-block:: python
|
|
90
|
+
|
|
91
|
+
document = analyzer("some text")
|
|
92
|
+
for sentence in document: print(annotation)
|
|
93
|
+
|
|
94
|
+
Refer to the :class:`Sentence <wowool.annotation.sentence.Sentence>` documentation for more information on further iteration.
|
|
95
|
+
|
|
96
|
+
:rtype: :class:`Sentence <wowool.annotation.sentence.Sentence>`
|
|
97
|
+
"""
|
|
98
|
+
assert self._sentences != None
|
|
99
|
+
return iter(self._sentences)
|
|
100
|
+
|
|
101
|
+
@_content_access
|
|
102
|
+
def __getitem__(self, index: int) -> Sentence:
|
|
103
|
+
"""
|
|
104
|
+
:param index: Sentence index
|
|
105
|
+
:type index: ``int``
|
|
106
|
+
|
|
107
|
+
:return: The sentence at the given index or ``None`` if the index is out of range
|
|
108
|
+
:rtype: :class:`Sentence <wowool.annotation.sentence.Sentence>`
|
|
109
|
+
|
|
110
|
+
.. code-block:: python
|
|
111
|
+
|
|
112
|
+
document = analyzer("This is the first sentence.")
|
|
113
|
+
first_sentence = document.analysis[0]
|
|
114
|
+
"""
|
|
115
|
+
assert self._sentences
|
|
116
|
+
if index < len(self._sentences) and index >= 0:
|
|
117
|
+
return self._sentences[index]
|
|
118
|
+
else:
|
|
119
|
+
raise IndexError("Sentence index out of range")
|
|
120
|
+
|
|
121
|
+
@_content_access
|
|
122
|
+
def __len__(self):
|
|
123
|
+
"""
|
|
124
|
+
:return: The number of sentences in the processed document
|
|
125
|
+
:rtype: ``int``
|
|
126
|
+
"""
|
|
127
|
+
assert self._sentences != None
|
|
128
|
+
return len(self._sentences)
|
|
129
|
+
|
|
130
|
+
@_content_access
|
|
131
|
+
def __repr__(self):
|
|
132
|
+
assert self._sentences is not None
|
|
133
|
+
with StringIO() as output:
|
|
134
|
+
for sentence in self._sentences:
|
|
135
|
+
output.write(repr(sentence))
|
|
136
|
+
return output.getvalue()
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def language(self):
|
|
140
|
+
"""
|
|
141
|
+
:return: The identified language of the processed document
|
|
142
|
+
:rtype: ``str``
|
|
143
|
+
"""
|
|
144
|
+
if self._cpp:
|
|
145
|
+
return self._cpp.language()
|
|
146
|
+
else:
|
|
147
|
+
return self._language
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
@_content_access
|
|
151
|
+
def sentences(self):
|
|
152
|
+
"""
|
|
153
|
+
:return: The sentences present in the analysis
|
|
154
|
+
:rtype: A ``list`` of :class:`Sentence <wowool.annotation.sentence.Sentence>` objects
|
|
155
|
+
"""
|
|
156
|
+
return self._sentences
|
|
157
|
+
|
|
158
|
+
@_content_access
|
|
159
|
+
def rich(self):
|
|
160
|
+
"""
|
|
161
|
+
:return: A rich string representation of the processed document object
|
|
162
|
+
:rtype: ``str``
|
|
163
|
+
"""
|
|
164
|
+
assert self._sentences
|
|
165
|
+
with StringIO() as output:
|
|
166
|
+
for sentence in self._sentences:
|
|
167
|
+
output.write(sentence.rich())
|
|
168
|
+
return output.getvalue()
|
|
169
|
+
|
|
170
|
+
@_content_access
|
|
171
|
+
def to_json(self):
|
|
172
|
+
"""
|
|
173
|
+
:return: A dictionary representing a JSON object of the processed document
|
|
174
|
+
:rtype: ``dict``
|
|
175
|
+
"""
|
|
176
|
+
if isinstance(self._json_data, str):
|
|
177
|
+
return json.loads(self._json_data)
|
|
178
|
+
elif isinstance(self._json_data, dict):
|
|
179
|
+
return self._json_data
|
|
180
|
+
|
|
181
|
+
@_content_access
|
|
182
|
+
def to_json_data(self) -> str:
|
|
183
|
+
"""
|
|
184
|
+
:return: A dictionary representing a JSON object of the processed document
|
|
185
|
+
:rtype: ``dict``
|
|
186
|
+
"""
|
|
187
|
+
if isinstance(self._json_data, str):
|
|
188
|
+
return self._json_data
|
|
189
|
+
elif isinstance(self._json_data, dict):
|
|
190
|
+
return json.dumps(self._json_data)
|
|
191
|
+
|
|
192
|
+
return ""
|
|
193
|
+
|
|
194
|
+
@property
|
|
195
|
+
@_content_access
|
|
196
|
+
def entities(self):
|
|
197
|
+
from wowool.annotation import Concept
|
|
198
|
+
|
|
199
|
+
yield from Concept.iter(self)
|
|
200
|
+
|
|
201
|
+
@_content_access
|
|
202
|
+
def concepts(self, filter=_filter_pass_thru_concept):
|
|
203
|
+
"""
|
|
204
|
+
Access the concepts in the analysis
|
|
205
|
+
|
|
206
|
+
:param filter: Optional filter to select or discard concepts
|
|
207
|
+
:type filter: Functor accepting a :class:`Concept <wowool.annotation.Concept>` and returning a ``bool``
|
|
208
|
+
|
|
209
|
+
:return: A generator expression yielding the concepts in the processed document
|
|
210
|
+
:rtype: :class:`Concepts <wowool.annotation.Concept>`
|
|
211
|
+
"""
|
|
212
|
+
from wowool.annotation import Concept
|
|
213
|
+
|
|
214
|
+
for concept in Concept.iter(self, filter):
|
|
215
|
+
yield concept
|
|
216
|
+
|
|
217
|
+
@property
|
|
218
|
+
@_content_access
|
|
219
|
+
def tokens(self):
|
|
220
|
+
"""
|
|
221
|
+
Access the tokens in the analysis
|
|
222
|
+
|
|
223
|
+
:return: A generator expression yielding the concepts in the processed document
|
|
224
|
+
:rtype: :class:`Concepts <wowool.annotation.token.Token>`
|
|
225
|
+
"""
|
|
226
|
+
from wowool.annotation import Token
|
|
227
|
+
|
|
228
|
+
yield from Token.iter(self)
|
|
229
|
+
|
|
230
|
+
@property
|
|
231
|
+
@_content_access
|
|
232
|
+
def annotations(self):
|
|
233
|
+
"""
|
|
234
|
+
Access the annotations in the analysis
|
|
235
|
+
|
|
236
|
+
:return: A generator expression yielding the annotations in the processed document
|
|
237
|
+
:rtype: :class:`Concepts <wowool.annotation.annotation.Annotation>`
|
|
238
|
+
"""
|
|
239
|
+
from wowool.annotation import Annotation
|
|
240
|
+
|
|
241
|
+
yield from Annotation.iter(self)
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
@_content_access
|
|
245
|
+
def text(self):
|
|
246
|
+
"""
|
|
247
|
+
:return: A string representation
|
|
248
|
+
:rtype: ``str``
|
|
249
|
+
"""
|
|
250
|
+
if self._text is None:
|
|
251
|
+
assert self._sentences
|
|
252
|
+
with StringIO() as output:
|
|
253
|
+
for sentence in self._sentences:
|
|
254
|
+
output.write(sentence.text)
|
|
255
|
+
self._text = output.getvalue()
|
|
256
|
+
return self._text
|
|
257
|
+
|
|
258
|
+
def reset(self):
|
|
259
|
+
"""
|
|
260
|
+
Reset the object, clearing all underlying data
|
|
261
|
+
|
|
262
|
+
:rtype: :class:`Analysis`
|
|
263
|
+
"""
|
|
264
|
+
self._sentences = None
|
|
265
|
+
self._json_data = None
|
|
266
|
+
return self
|
|
267
|
+
|
|
268
|
+
@_content_access
|
|
269
|
+
def internal_annotations(self):
|
|
270
|
+
"""
|
|
271
|
+
:return: A dictionary representing a JSON object of the processed document
|
|
272
|
+
:rtype: ``dict``
|
|
273
|
+
"""
|
|
274
|
+
assert self._cpp
|
|
275
|
+
return json.loads(self._cpp.internal_annotations())
|
|
276
|
+
|
|
277
|
+
@property
|
|
278
|
+
def metadata(self):
|
|
279
|
+
return json.loads(self._cpp.metadata())
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class AnalysisInputProvider(InputProvider):
|
|
283
|
+
def __init__(self, analysis: Analysis, id: str) -> None:
|
|
284
|
+
super(AnalysisInputProvider, self).__init__(id)
|
|
285
|
+
self.analysis = analysis
|
|
286
|
+
|
|
287
|
+
@property
|
|
288
|
+
def text(self) -> str:
|
|
289
|
+
return self.analysis.text
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from wowool.annotation.concept import Concept
|
|
3
|
+
from wowool.annotation.sentence import Sentence
|
|
4
|
+
from wowool.annotation.token import Token, MorphData
|
|
5
|
+
from wowool.analysis import Analysis
|
|
6
|
+
from wowool.error import Error
|
|
7
|
+
from enum import IntEnum
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _add_morphology(tkz, mdl, jo):
|
|
11
|
+
for json_md in jo:
|
|
12
|
+
md = MorphData()
|
|
13
|
+
if len(json_md) > 0:
|
|
14
|
+
md.stem = json_md[0]
|
|
15
|
+
if len(json_md) > 1:
|
|
16
|
+
md.pos = json_md[1]
|
|
17
|
+
pidx = 2
|
|
18
|
+
while pidx < len(json_md):
|
|
19
|
+
if isinstance(json_md[pidx], list):
|
|
20
|
+
md.morphology = []
|
|
21
|
+
_add_morphology(tkz, md.morphology, json_md[pidx])
|
|
22
|
+
elif isinstance(json_md[pidx], str):
|
|
23
|
+
tkz.properties.add(json_md[pidx])
|
|
24
|
+
pidx += 1
|
|
25
|
+
mdl.append(md)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class AnnotationType(IntEnum):
|
|
29
|
+
SENTENCE = 1
|
|
30
|
+
CONCEPT = 2
|
|
31
|
+
TOKEN = 3
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _parse_sentence(json_sentence):
|
|
35
|
+
sentence = Sentence(json_sentence[1], json_sentence[2])
|
|
36
|
+
if len(json_sentence) > 4:
|
|
37
|
+
sentence.attributes = json_sentence[4]
|
|
38
|
+
|
|
39
|
+
for annotation in json_sentence[3]:
|
|
40
|
+
if annotation[0] == AnnotationType.TOKEN: # Token
|
|
41
|
+
token = Token(annotation[1], annotation[2], annotation[3])
|
|
42
|
+
token.properties = set(annotation[4])
|
|
43
|
+
token.morphology = []
|
|
44
|
+
_add_morphology(token, token.morphology, annotation[5])
|
|
45
|
+
token._annotation_idx = len(sentence.annotations)
|
|
46
|
+
sentence.annotations.append(token)
|
|
47
|
+
elif annotation[0] == AnnotationType.CONCEPT: # Concept
|
|
48
|
+
concept = Concept(annotation[1], annotation[2], annotation[3])
|
|
49
|
+
concept.set_attributes(annotation[4] if len(annotation) > 4 else {})
|
|
50
|
+
concept.set_sentence_annotations_index(sentence.annotations, len(sentence.annotations))
|
|
51
|
+
sentence.annotations.append(concept)
|
|
52
|
+
else:
|
|
53
|
+
raise Error(f"Annotation type = {annotation} not supported.")
|
|
54
|
+
return sentence
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def parse_document(json_str_or_obj, doc_id=None, cpp_document=None):
|
|
58
|
+
"""
|
|
59
|
+
Parse a json string into a Analysis
|
|
60
|
+
|
|
61
|
+
:param json_str: a json string returned from the server.
|
|
62
|
+
:type json_str: string
|
|
63
|
+
:returns: a Analysis
|
|
64
|
+
|
|
65
|
+
.. code-block:: python
|
|
66
|
+
|
|
67
|
+
from wowool.document_parser import parse_document
|
|
68
|
+
document = parse_document(json_str)
|
|
69
|
+
for sentence in document:
|
|
70
|
+
for annotation in sent.annotations:
|
|
71
|
+
if annotation.is_token: pass
|
|
72
|
+
if annotation.is_concept: pass
|
|
73
|
+
|
|
74
|
+
:raises: ValueError, Exception
|
|
75
|
+
"""
|
|
76
|
+
json_obj = json_str_or_obj if isinstance(json_str_or_obj, dict) else json.loads(json_str_or_obj)
|
|
77
|
+
if "error" in json_obj:
|
|
78
|
+
raise ValueError(json_obj["error"])
|
|
79
|
+
if "exception" in json_obj:
|
|
80
|
+
raise Exception(json_obj["exception"])
|
|
81
|
+
jo_sentences = None
|
|
82
|
+
|
|
83
|
+
wowool_out = json_obj
|
|
84
|
+
if "document" in json_obj:
|
|
85
|
+
if "sentences" in wowool_out["document"]:
|
|
86
|
+
jo_sentences = wowool_out["document"]["sentences"]
|
|
87
|
+
elif "sentences" in wowool_out:
|
|
88
|
+
jo_sentences = wowool_out["sentences"]
|
|
89
|
+
elif "sentences" in wowool_out:
|
|
90
|
+
jo_sentences = wowool_out["sentences"]
|
|
91
|
+
|
|
92
|
+
if jo_sentences == None:
|
|
93
|
+
raise Exception("Invalid json format. Missing document object.")
|
|
94
|
+
|
|
95
|
+
language = wowool_out["language"] if "language" in wowool_out else "english"
|
|
96
|
+
if not doc_id:
|
|
97
|
+
doc_id = wowool_out["id"] if "id" in wowool_out else id(json_str_or_obj)
|
|
98
|
+
|
|
99
|
+
metadata = wowool_out["metadata"] if "metadata" in wowool_out else {}
|
|
100
|
+
|
|
101
|
+
sentences = [_parse_sentence(json_sentence) for json_sentence in jo_sentences]
|
|
102
|
+
return Analysis(cpp_document, doc_id, language, sentences, json_str_or_obj, metadata)
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
from typing import Iterator, Self
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class BasicAnnotation:
|
|
5
|
+
"""
|
|
6
|
+
:class:`BasicAnnotation` is the base class for all annotations
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
__slott__ = ["begin_offset", "end_offset"]
|
|
10
|
+
|
|
11
|
+
def __init__(self, begin_offset: int, end_offset: int):
|
|
12
|
+
"""
|
|
13
|
+
Initialize an :class:`BasicAnnotation` instance
|
|
14
|
+
|
|
15
|
+
:param begin_offset: Begin offset of the annotation
|
|
16
|
+
:type begin_offset: ``int``
|
|
17
|
+
:param end_offset: End offset of the annotation
|
|
18
|
+
:type end_offset: ``int``
|
|
19
|
+
|
|
20
|
+
:rtype: :class:`BasicAnnotation`
|
|
21
|
+
"""
|
|
22
|
+
self._begin_offset: int = begin_offset
|
|
23
|
+
self._end_offset: int = end_offset
|
|
24
|
+
|
|
25
|
+
def __repr__(self):
|
|
26
|
+
return "({:>3},{:>3})".format(self.begin_offset, self.end_offset)
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def begin_offset(self) -> int:
|
|
30
|
+
"""
|
|
31
|
+
:return: The begin offset of the annotation
|
|
32
|
+
:type: ``int``
|
|
33
|
+
"""
|
|
34
|
+
return self._begin_offset
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def end_offset(self) -> int:
|
|
38
|
+
"""
|
|
39
|
+
:return: The end offset of the annotation
|
|
40
|
+
:type: ``int``
|
|
41
|
+
"""
|
|
42
|
+
return self._end_offset
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def is_concept(self) -> bool:
|
|
46
|
+
"""
|
|
47
|
+
:return: Whether the annotation is a :class:`Concept`
|
|
48
|
+
:rtype: ``bool``
|
|
49
|
+
"""
|
|
50
|
+
from wowool.annotation.concept import Concept
|
|
51
|
+
|
|
52
|
+
return isinstance(self, Concept)
|
|
53
|
+
|
|
54
|
+
@property
|
|
55
|
+
def is_sentence(self) -> bool:
|
|
56
|
+
"""
|
|
57
|
+
:return: Whether the annotation is a :class:`Sentence`
|
|
58
|
+
:rtype: ``bool``
|
|
59
|
+
"""
|
|
60
|
+
from wowool.annotation.sentence import Sentence
|
|
61
|
+
|
|
62
|
+
return isinstance(self, Sentence)
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def is_token(self) -> bool:
|
|
66
|
+
"""
|
|
67
|
+
:return: Whether the annotation is a :class:`Token`
|
|
68
|
+
:rtype: ``bool``
|
|
69
|
+
"""
|
|
70
|
+
from wowool.annotation.token import Token
|
|
71
|
+
|
|
72
|
+
return isinstance(self, Token)
|
|
73
|
+
|
|
74
|
+
@property
|
|
75
|
+
def is_paragraph(self) -> bool:
|
|
76
|
+
"""
|
|
77
|
+
:return: Whether the annotation is a :class:`Paragraph`
|
|
78
|
+
:rtype: ``bool``
|
|
79
|
+
"""
|
|
80
|
+
from wowool.annotation.paragraph import Paragraph
|
|
81
|
+
|
|
82
|
+
return isinstance(self, Paragraph)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class Annotation(BasicAnnotation):
|
|
86
|
+
"""
|
|
87
|
+
:class:`Annotation` is the base class for all annotations
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
__slott__ = ["begin_offset", "end_offset"]
|
|
91
|
+
|
|
92
|
+
def __init__(self, begin_offset: int, end_offset: int):
|
|
93
|
+
"""
|
|
94
|
+
Initialize an :class:`Annotation` instance
|
|
95
|
+
|
|
96
|
+
:param begin_offset: Begin offset of the annotation
|
|
97
|
+
:type begin_offset: ``int``
|
|
98
|
+
:param end_offset: End offset of the annotation
|
|
99
|
+
:type end_offset: ``int``
|
|
100
|
+
|
|
101
|
+
:rtype: :class:`Annotation`
|
|
102
|
+
"""
|
|
103
|
+
super(Annotation, self).__init__(begin_offset, end_offset)
|
|
104
|
+
self._annotation_idx = None
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def index(self) -> int:
|
|
108
|
+
assert self._annotation_idx != None
|
|
109
|
+
return self._annotation_idx
|
|
110
|
+
|
|
111
|
+
@staticmethod
|
|
112
|
+
def _document_iter(doc) -> Iterator[Self]:
|
|
113
|
+
for sentence in doc:
|
|
114
|
+
for annotation in sentence:
|
|
115
|
+
yield annotation
|
|
116
|
+
|
|
117
|
+
@staticmethod
|
|
118
|
+
def _sentence_iter(sentence) -> Iterator[Self]:
|
|
119
|
+
for annotation in sentence:
|
|
120
|
+
yield annotation
|
|
121
|
+
|
|
122
|
+
@staticmethod
|
|
123
|
+
def iter(object) -> Iterator[Self]:
|
|
124
|
+
"""
|
|
125
|
+
Iterate over the concepts in a document, an analysis, a sentence or a concept. For example:
|
|
126
|
+
|
|
127
|
+
.. code-block:: python
|
|
128
|
+
|
|
129
|
+
document = analyzer("Hello from Antwerp, said John Smith.")
|
|
130
|
+
for concept in Concept.iter(document, lambda concept : concept.uri == "NP"):
|
|
131
|
+
print(concept)
|
|
132
|
+
|
|
133
|
+
:param object: Object to iterate
|
|
134
|
+
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`Concept <wowool.annotation.concept.Concept>`
|
|
135
|
+
: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:`Concept` and returning a ``bool``
|
|
137
|
+
|
|
138
|
+
:return: A generator expression yielding concepts
|
|
139
|
+
:rtype: :class:`Concept <wowool.annotation.concept.Concept>`
|
|
140
|
+
"""
|
|
141
|
+
from wowool.document.document import Document
|
|
142
|
+
from wowool.analysis import Analysis
|
|
143
|
+
from wowool.annotation import Sentence
|
|
144
|
+
|
|
145
|
+
if isinstance(object, Document) and object.analysis:
|
|
146
|
+
yield from Annotation._document_iter(object.analysis)
|
|
147
|
+
elif isinstance(object, Analysis):
|
|
148
|
+
yield from Annotation._document_iter(object)
|
|
149
|
+
elif isinstance(object, Sentence):
|
|
150
|
+
yield from Annotation._sentence_iter(object)
|
|
151
|
+
else:
|
|
152
|
+
raise TypeError(f"Expected Document, Analysis, Sentence, but got '{type(object)}'")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class ErrorAnnotationNotFound(ValueError):
|
|
156
|
+
pass
|