wowool-common 3.0.2.dev23__py3-none-any.whl → 3.1.1.dev1__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/annotation/annotation.py +3 -3
- wowool/annotation/entity.py +3 -3
- wowool/annotation/paragraph.py +5 -5
- wowool/annotation/sentence.py +3 -3
- wowool/annotation/token.py +3 -3
- wowool/document/__init__.py +1 -1
- wowool/{analysis.py → document/analysis/analysis.py} +23 -8
- wowool/{analysis_parser.py → document/analysis/analysis_parser.py} +1 -1
- wowool/document/{apps → analysis/apps}/chunks.py +2 -2
- wowool/document/analysis/document.py +388 -0
- wowool/document/defines.py +5 -0
- wowool/document/document.py +85 -366
- wowool/document/document_interface.py +48 -0
- wowool/document/factory.py +149 -53
- wowool/document/serialize.py +48 -0
- wowool/io/provider/__init__.py +1 -4
- wowool/io/provider/docx.py +23 -7
- wowool/io/provider/factory.py +15 -20
- wowool/io/provider/file.py +29 -11
- wowool/io/provider/html.py +13 -3
- wowool/io/provider/html_v2.py +26 -7
- wowool/io/provider/raw.py +39 -0
- wowool/io/provider/rtf.py +28 -6
- wowool/io/provider/str.py +30 -10
- wowool/string/__init__.py +4 -4
- wowool/utility/apps/decorators.py +13 -8
- wowool/utility/default_arguments.py +4 -13
- wowool/utility/diagnostics.py +3 -3
- wowool/utility/text/cleanup.py +1 -1
- wowool/visitor.py +2 -2
- wowool/wow.py +14 -14
- {wowool_common-3.0.2.dev23.dist-info → wowool_common-3.1.1.dev1.dist-info}/METADATA +1 -1
- wowool_common-3.1.1.dev1.dist-info/RECORD +61 -0
- wowool/io/provider/input_provider.py +0 -38
- wowool_common-3.0.2.dev23.dist-info/RECORD +0 -57
- /wowool/document/{apps → analysis/apps}/themes.py +0 -0
- /wowool/document/{apps → analysis/apps}/topics.py +0 -0
- {wowool_common-3.0.2.dev23.dist-info → wowool_common-3.1.1.dev1.dist-info}/WHEEL +0 -0
- {wowool_common-3.0.2.dev23.dist-info → wowool_common-3.1.1.dev1.dist-info}/top_level.txt +0 -0
wowool/annotation/annotation.py
CHANGED
|
@@ -147,11 +147,11 @@ class Annotation(BasicAnnotation):
|
|
|
147
147
|
:return: A generator expression yielding concepts
|
|
148
148
|
:rtype: :class:`Entity <wowool.annotation.entity.Entity>`
|
|
149
149
|
"""
|
|
150
|
-
from wowool.document.document import
|
|
151
|
-
from wowool.analysis import Analysis
|
|
150
|
+
from wowool.document.analysis.document import AnalysisDocument
|
|
151
|
+
from wowool.document.analysis.analysis import Analysis
|
|
152
152
|
from wowool.annotation import Sentence
|
|
153
153
|
|
|
154
|
-
if isinstance(object,
|
|
154
|
+
if isinstance(object, AnalysisDocument) and object.analysis:
|
|
155
155
|
yield from Annotation._document_iter(object.analysis)
|
|
156
156
|
elif isinstance(object, Analysis):
|
|
157
157
|
yield from Annotation._document_iter(object)
|
wowool/annotation/entity.py
CHANGED
|
@@ -424,11 +424,11 @@ class Entity(Annotation):
|
|
|
424
424
|
:return: A generator expression yielding concepts
|
|
425
425
|
:rtype: :class:`Entity <wowool.annotation.entity.Entity>`
|
|
426
426
|
"""
|
|
427
|
-
from wowool.document.document import
|
|
428
|
-
from wowool.analysis import Analysis
|
|
427
|
+
from wowool.document.analysis.document import AnalysisDocument
|
|
428
|
+
from wowool.document.analysis.analysis import Analysis
|
|
429
429
|
from wowool.annotation import Sentence
|
|
430
430
|
|
|
431
|
-
if isinstance(object,
|
|
431
|
+
if isinstance(object, AnalysisDocument) and object.analysis:
|
|
432
432
|
yield from Entity._document_iter(object.analysis, filter)
|
|
433
433
|
elif isinstance(object, Analysis):
|
|
434
434
|
yield from Entity._document_iter(object, filter)
|
wowool/annotation/paragraph.py
CHANGED
|
@@ -3,7 +3,7 @@ from wowool.annotation.annotation import Annotation
|
|
|
3
3
|
from wowool.annotation.sentence import Sentence
|
|
4
4
|
from wowool.annotation.token import Token
|
|
5
5
|
from typing import Callable, List, cast, Iterator, Self
|
|
6
|
-
from wowool.analysis import Analysis
|
|
6
|
+
from wowool.document.analysis.analysis import Analysis
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
class Paragraph(Annotation):
|
|
@@ -152,13 +152,13 @@ class Paragraph(Annotation):
|
|
|
152
152
|
:return: A generator expression yielding paragraphs
|
|
153
153
|
:rtype: :class:`Entity <wowool.annotation.paragraph.Paragraph>`
|
|
154
154
|
"""
|
|
155
|
-
from wowool.document.document import
|
|
156
|
-
from wowool.analysis import Analysis
|
|
155
|
+
from wowool.document.analysis.document import AnalysisDocument
|
|
156
|
+
from wowool.document.analysis.analysis import Analysis
|
|
157
157
|
|
|
158
158
|
if original_text is None:
|
|
159
|
-
original_text = object.
|
|
159
|
+
original_text = object.data
|
|
160
160
|
|
|
161
|
-
if isinstance(object,
|
|
161
|
+
if isinstance(object, AnalysisDocument) and object.analysis:
|
|
162
162
|
yield from Paragraph._document_iter(object.analysis, original_text)
|
|
163
163
|
elif isinstance(object, Analysis):
|
|
164
164
|
yield from Paragraph._document_iter(object, original_text)
|
wowool/annotation/sentence.py
CHANGED
|
@@ -248,11 +248,11 @@ class Sentence(BasicAnnotation):
|
|
|
248
248
|
:return: A generator expression yielding concepts
|
|
249
249
|
:rtype: :class:`Sentence <wowool.annotation.concept.Sentence>`
|
|
250
250
|
"""
|
|
251
|
-
from wowool.document.document import
|
|
252
|
-
from wowool.analysis import Analysis
|
|
251
|
+
from wowool.document.analysis.document import AnalysisDocument
|
|
252
|
+
from wowool.document.analysis.analysis import Analysis
|
|
253
253
|
from wowool.annotation.paragraph import Paragraph
|
|
254
254
|
|
|
255
|
-
if isinstance(object,
|
|
255
|
+
if isinstance(object, AnalysisDocument) and object.analysis:
|
|
256
256
|
yield from Sentence._document_iter(object.analysis, filter)
|
|
257
257
|
elif isinstance(object, Analysis):
|
|
258
258
|
yield from Sentence._document_iter(object, filter)
|
wowool/annotation/token.py
CHANGED
|
@@ -173,11 +173,11 @@ class Token(Annotation):
|
|
|
173
173
|
:return: A generator expression yielding tokens
|
|
174
174
|
:rtype: :class:`Token <wowool.annotation.token.Token>`
|
|
175
175
|
"""
|
|
176
|
-
from wowool.document.document import
|
|
177
|
-
from wowool.analysis import Analysis
|
|
176
|
+
from wowool.document.analysis.document import AnalysisDocument
|
|
177
|
+
from wowool.document.analysis.analysis import Analysis
|
|
178
178
|
from wowool.annotation import Sentence, Entity
|
|
179
179
|
|
|
180
|
-
if isinstance(object,
|
|
180
|
+
if isinstance(object, AnalysisDocument) and object.analysis:
|
|
181
181
|
yield from Token._document_iter(object.analysis)
|
|
182
182
|
elif isinstance(object, Analysis):
|
|
183
183
|
yield from Token._document_iter(object)
|
wowool/document/__init__.py
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
from wowool.document.document import Document
|
|
2
|
-
from wowool.document.
|
|
2
|
+
from wowool.document.document_interface import DocumentInterface
|
|
@@ -4,7 +4,7 @@ from io import StringIO
|
|
|
4
4
|
from wowool.annotation.sentence import Sentence
|
|
5
5
|
from wowool.annotation.entity import Entity
|
|
6
6
|
from typing import Union
|
|
7
|
-
from wowool.
|
|
7
|
+
from wowool.document.document_interface import DocumentInterface
|
|
8
8
|
|
|
9
9
|
APP_ID = "wowool_analysis"
|
|
10
10
|
|
|
@@ -53,7 +53,7 @@ class Analysis:
|
|
|
53
53
|
|
|
54
54
|
:rtype: :class:`Analysis`
|
|
55
55
|
"""
|
|
56
|
-
from wowool.analysis_parser import parse_document
|
|
56
|
+
from wowool.document.analysis.analysis_parser import parse_document
|
|
57
57
|
|
|
58
58
|
return parse_document(json_data, id)
|
|
59
59
|
|
|
@@ -279,11 +279,26 @@ class Analysis:
|
|
|
279
279
|
return json.loads(self._cpp.metadata())
|
|
280
280
|
|
|
281
281
|
|
|
282
|
-
class AnalysisInputProvider(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
282
|
+
class AnalysisInputProvider(DocumentInterface):
|
|
283
|
+
DATA_TYPE = "analysis/json"
|
|
284
|
+
|
|
285
|
+
def __init__(self, data: dict, id: str, metadata: dict | None = None) -> None:
|
|
286
|
+
self._id = id
|
|
287
|
+
self.analysis_json = data
|
|
288
|
+
self._metadata = metadata if metadata is not None else {}
|
|
289
|
+
|
|
290
|
+
@property
|
|
291
|
+
def id(self) -> str:
|
|
292
|
+
return self._id
|
|
293
|
+
|
|
294
|
+
@property
|
|
295
|
+
def data_type(self) -> str:
|
|
296
|
+
return self.DATA_TYPE
|
|
297
|
+
|
|
298
|
+
@property
|
|
299
|
+
def data(self) -> str:
|
|
300
|
+
return self.analysis_json
|
|
286
301
|
|
|
287
302
|
@property
|
|
288
|
-
def
|
|
289
|
-
return self.
|
|
303
|
+
def metadata(self) -> dict:
|
|
304
|
+
return self._metadata
|
|
@@ -2,7 +2,7 @@ import json
|
|
|
2
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
|
-
from wowool.analysis import Analysis
|
|
5
|
+
from wowool.document.analysis.analysis import Analysis
|
|
6
6
|
from wowool.error import Error
|
|
7
7
|
from enum import IntEnum
|
|
8
8
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
-
from wowool.document.apps.topics import convert_topics, Topic
|
|
3
|
-
from wowool.document.apps.themes import convert_themes, Theme
|
|
2
|
+
from wowool.document.analysis.apps.topics import convert_topics, Topic
|
|
3
|
+
from wowool.document.analysis.apps.themes import convert_themes, Theme
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
@dataclass
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
from io import StringIO
|
|
2
|
+
from typing import Union, Any, Generator
|
|
3
|
+
from wowool.annotation import Entity, Annotation, Token, Sentence
|
|
4
|
+
import json
|
|
5
|
+
from wowool.diagnostic import Diagnostics
|
|
6
|
+
from wowool.document.analysis.analysis import (
|
|
7
|
+
_filter_pass_thru_concept,
|
|
8
|
+
Analysis,
|
|
9
|
+
AnalysisInputProvider,
|
|
10
|
+
)
|
|
11
|
+
from wowool.document.analysis.analysis import APP_ID as APP_ID_ANALYSIS
|
|
12
|
+
from typing import cast
|
|
13
|
+
from wowool.document.analysis.apps.topics import convert_topics, Topic
|
|
14
|
+
from wowool.document.analysis.apps.themes import convert_themes, Theme
|
|
15
|
+
from wowool.document.analysis.apps.chunks import convert_chunks, Chunk
|
|
16
|
+
from wowool.document.document_interface import DocumentInterface
|
|
17
|
+
from wowool.document.document import Document
|
|
18
|
+
from wowool.document.defines import WJ_ID, WJ_DATA, WJ_DATA_TYPE
|
|
19
|
+
|
|
20
|
+
RESULTS = "results"
|
|
21
|
+
DIAGNOSTICS = "diagnostics"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def to_json_convert(obj):
|
|
25
|
+
if isinstance(obj, set):
|
|
26
|
+
return str(list(obj))
|
|
27
|
+
return obj
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
STR_MISSING_ANALYSIS = "Document has not been processed by a Language"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class AnalysisDocument(DocumentInterface):
|
|
34
|
+
"""
|
|
35
|
+
:class:`Document` is a class that stores the data related to a document. Instances of this class are returned from a Pipeline, a Language or a Domain object.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(self, document: DocumentInterface, metadata: dict | None = None):
|
|
39
|
+
"""
|
|
40
|
+
Initialize a :class:`AnalysisDocument` instance
|
|
41
|
+
"""
|
|
42
|
+
super().__init__()
|
|
43
|
+
|
|
44
|
+
self.input_document = document
|
|
45
|
+
self._metadata = metadata if metadata != None else {**self.input_document.metadata}
|
|
46
|
+
self._apps = {}
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def id(self) -> str:
|
|
50
|
+
"""
|
|
51
|
+
:return: The unique identifier of the document
|
|
52
|
+
:rtype: ``str``
|
|
53
|
+
"""
|
|
54
|
+
return self.input_document.id
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def data_type(self) -> str:
|
|
58
|
+
"""
|
|
59
|
+
:return: The data type of the document
|
|
60
|
+
:rtype: 'str | None'
|
|
61
|
+
"""
|
|
62
|
+
assert self.input_document.data_type is not None, "Document has no data type"
|
|
63
|
+
return self.input_document.data_type
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def data(self) -> Union[str, None]:
|
|
67
|
+
"""
|
|
68
|
+
:return: The text data of the document
|
|
69
|
+
:rtype: 'str | None'
|
|
70
|
+
"""
|
|
71
|
+
return self.input_document.data if self.input_document else ""
|
|
72
|
+
|
|
73
|
+
@property
|
|
74
|
+
def metadata(self) -> dict:
|
|
75
|
+
"""
|
|
76
|
+
:return: The metadata of the document
|
|
77
|
+
:rtype: ``dict``
|
|
78
|
+
"""
|
|
79
|
+
return self._metadata
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def analysis(self):
|
|
83
|
+
"""
|
|
84
|
+
:return: The :class:`Analysis <wowool.analysis.Analysis>` of the document, containing the :class:`Sentences <wowool.annotation.sentence.Sentence>`, :class:`Tokens <wowool.annotation.token.Token>` and :class:`Concepts <wowool.annotation.entity.Entity>`, or ``None`` if the document has not been processed by a Language
|
|
85
|
+
|
|
86
|
+
.. literalinclude:: ../../../../common/py/eot/wowool/init_document_analysis.py
|
|
87
|
+
|
|
88
|
+
:rtype: :class:`Analysis <wowool.analysis.Analysis>`
|
|
89
|
+
"""
|
|
90
|
+
return cast(Analysis, self.results(APP_ID_ANALYSIS))
|
|
91
|
+
|
|
92
|
+
def app_ids(self):
|
|
93
|
+
"""
|
|
94
|
+
Iterate over the application identifiers
|
|
95
|
+
|
|
96
|
+
:return: A generator expression yielding application identifiers
|
|
97
|
+
:rtype: ``str``
|
|
98
|
+
"""
|
|
99
|
+
for app_id in self._apps:
|
|
100
|
+
yield app_id
|
|
101
|
+
|
|
102
|
+
def has(self, app_id: str) -> bool:
|
|
103
|
+
return self.has_results(app_id)
|
|
104
|
+
|
|
105
|
+
def has_results(self, app_id: str) -> bool:
|
|
106
|
+
"""
|
|
107
|
+
:return: Whether the application, as identified by the given application identifier, is in the document
|
|
108
|
+
:rtype: ``bool``
|
|
109
|
+
"""
|
|
110
|
+
return app_id in self._apps
|
|
111
|
+
|
|
112
|
+
def add_results(self, app_id: str, results):
|
|
113
|
+
"""
|
|
114
|
+
Add the given application results to the document
|
|
115
|
+
|
|
116
|
+
:param app_id: Application identifier
|
|
117
|
+
:type app_id: ``str``
|
|
118
|
+
:param results: Application results
|
|
119
|
+
:type results: A JSON serializable object type
|
|
120
|
+
"""
|
|
121
|
+
if app_id in self._apps:
|
|
122
|
+
self._apps[app_id][RESULTS] = results
|
|
123
|
+
else:
|
|
124
|
+
self._apps[app_id] = {RESULTS: results}
|
|
125
|
+
return results
|
|
126
|
+
|
|
127
|
+
def results(self, app_id: str) -> Union[Any, None]:
|
|
128
|
+
"""
|
|
129
|
+
:return: The results of the given application. See the different type of :ref:`application results <apps>`
|
|
130
|
+
|
|
131
|
+
:param app_id: Application identifier
|
|
132
|
+
:type app_id: ``str``
|
|
133
|
+
:param defaults: The defaults result to create when the application identifier is not present
|
|
134
|
+
:type default: Any JSON serializable object
|
|
135
|
+
"""
|
|
136
|
+
if app_id in self._apps and RESULTS in self._apps[app_id]:
|
|
137
|
+
return self._apps[app_id][RESULTS]
|
|
138
|
+
|
|
139
|
+
def add_diagnostics(self, app_id: str, diagnostics: Diagnostics):
|
|
140
|
+
"""
|
|
141
|
+
Add the given application diagnostics to the document
|
|
142
|
+
|
|
143
|
+
:param app_id: Application identifier
|
|
144
|
+
:type app_id: ``str``
|
|
145
|
+
:param diagnostics: Application diagnostics
|
|
146
|
+
:type diagnostics: :class:`Diagnostics <wowool.diagnostic.Diagnostics>`
|
|
147
|
+
"""
|
|
148
|
+
if app_id in self._apps:
|
|
149
|
+
self._apps[app_id][DIAGNOSTICS] = diagnostics
|
|
150
|
+
else:
|
|
151
|
+
self._apps[app_id] = {DIAGNOSTICS: diagnostics}
|
|
152
|
+
|
|
153
|
+
def has_diagnostics(self, app_id: str = None) -> bool:
|
|
154
|
+
"""
|
|
155
|
+
:param app_id: Application identifier
|
|
156
|
+
:type app_id: ``str`` or ``None``
|
|
157
|
+
:return: Whether the document contains diagnostics for the given application or any diagnostics if no application identifier is provided
|
|
158
|
+
:rtype: ``bool``
|
|
159
|
+
"""
|
|
160
|
+
if app_id is None:
|
|
161
|
+
for app_id in self._apps:
|
|
162
|
+
if DIAGNOSTICS in self._apps[app_id]:
|
|
163
|
+
return True
|
|
164
|
+
return False
|
|
165
|
+
else:
|
|
166
|
+
if app_id in self._apps and DIAGNOSTICS in self._apps[app_id]:
|
|
167
|
+
return True
|
|
168
|
+
else:
|
|
169
|
+
return False
|
|
170
|
+
|
|
171
|
+
def diagnostics(self, app_id: str = None) -> Diagnostics:
|
|
172
|
+
"""
|
|
173
|
+
:param app_id: Application identifier
|
|
174
|
+
:type app_id: ``str`` or ``None``
|
|
175
|
+
|
|
176
|
+
:return: The diagnostics of the given application. See the different type of :ref:`application results <apps>`
|
|
177
|
+
:rtype: :class:`Diagnostics <wowool.diagnostic.Diagnostics>`
|
|
178
|
+
"""
|
|
179
|
+
if app_id is None:
|
|
180
|
+
diagnostics = Diagnostics()
|
|
181
|
+
for _, app_data in self._apps.items():
|
|
182
|
+
if DIAGNOSTICS in app_data:
|
|
183
|
+
diagnostics.extend(app_data[DIAGNOSTICS])
|
|
184
|
+
return diagnostics
|
|
185
|
+
else:
|
|
186
|
+
if app_id in self._apps and DIAGNOSTICS in self._apps[app_id]:
|
|
187
|
+
return self._apps[app_id][DIAGNOSTICS]
|
|
188
|
+
else:
|
|
189
|
+
raise ValueError(f"App '{app_id}' has no diagnostics")
|
|
190
|
+
|
|
191
|
+
def to_json(self) -> dict:
|
|
192
|
+
"""
|
|
193
|
+
:return: A dictionary representing a JSON object of the document
|
|
194
|
+
:rtype: ``dict``
|
|
195
|
+
"""
|
|
196
|
+
from json import JSONEncoder
|
|
197
|
+
|
|
198
|
+
class Encoder(JSONEncoder):
|
|
199
|
+
def default(self, obj):
|
|
200
|
+
if isinstance(obj, set):
|
|
201
|
+
return str(list(obj))
|
|
202
|
+
else:
|
|
203
|
+
return getattr(obj, "to_json")() if hasattr(obj, "to_json") else super().default(obj)
|
|
204
|
+
|
|
205
|
+
document = {WJ_ID: self.id, WJ_DATA_TYPE: AnalysisInputProvider.DATA_TYPE, WJ_DATA: {"apps": self._apps}}
|
|
206
|
+
return json.loads(json.dumps(document, cls=Encoder))
|
|
207
|
+
|
|
208
|
+
@staticmethod
|
|
209
|
+
def from_json(document_json: dict) -> "AnalysisDocument":
|
|
210
|
+
assert WJ_ID in document_json, "Invalid Document json format"
|
|
211
|
+
assert WJ_DATA_TYPE in document_json, "Invalid Document json format"
|
|
212
|
+
assert AnalysisInputProvider.DATA_TYPE in document_json[WJ_DATA_TYPE]
|
|
213
|
+
assert WJ_DATA in document_json, "Invalid Document json format"
|
|
214
|
+
assert "apps" in document_json[WJ_DATA]
|
|
215
|
+
|
|
216
|
+
doc = AnalysisDocument(Document.create(**document_json))
|
|
217
|
+
assert AnalysisInputProvider.DATA_TYPE == doc.data_type
|
|
218
|
+
doc._apps = doc.data["apps"]
|
|
219
|
+
|
|
220
|
+
if APP_ID_ANALYSIS in doc._apps:
|
|
221
|
+
analysis_ = doc._apps[APP_ID_ANALYSIS]
|
|
222
|
+
assert isinstance(analysis_, dict), f"Expected dict, not '{type(analysis_)}'"
|
|
223
|
+
# phforest : this should no be a assert, in case we have errors there will be no results.
|
|
224
|
+
# assert RESULTS in analysis_, f"Missing {RESULTS} in {APP_ID_ANALYSIS}"
|
|
225
|
+
if RESULTS in analysis_:
|
|
226
|
+
analysis = Analysis.parse(analysis_[RESULTS])
|
|
227
|
+
# doc.input_document = AnalysisInputProvider(json.dumps(analysis_[RESULTS]), doc.id)
|
|
228
|
+
doc.add_results(APP_ID_ANALYSIS, analysis)
|
|
229
|
+
|
|
230
|
+
for _, app_data in doc._apps.items():
|
|
231
|
+
if DIAGNOSTICS in app_data:
|
|
232
|
+
app_data[DIAGNOSTICS] = Diagnostics.from_json(app_data[DIAGNOSTICS])
|
|
233
|
+
|
|
234
|
+
return doc
|
|
235
|
+
|
|
236
|
+
def concepts(self, filter=_filter_pass_thru_concept):
|
|
237
|
+
"""
|
|
238
|
+
Access the concepts in the analysis of the document
|
|
239
|
+
|
|
240
|
+
:param filter: Optional filter to select or discard concepts
|
|
241
|
+
:type filter: Functor accepting a :class:`Entity <wowool.annotation.entity.Entity>` and returning a ``bool``
|
|
242
|
+
|
|
243
|
+
:return: A generator expression yielding the concepts in the processed document
|
|
244
|
+
:rtype: :class:`Concepts <wowool.annotation.entity.Entity>`
|
|
245
|
+
"""
|
|
246
|
+
return self.analysis.concepts(filter) if self.analysis else iter([])
|
|
247
|
+
|
|
248
|
+
def __repr__(self):
|
|
249
|
+
if self.data_type == AnalysisInputProvider.DATA_TYPE:
|
|
250
|
+
# we are using the input document
|
|
251
|
+
return f"<AnalysisDocument id={self.id} data_type={self.data_type}>"
|
|
252
|
+
sz = len(self.data) if self.data else 0
|
|
253
|
+
text = '"' + self.data[:50].strip().replace("\n", " ") + '"' if self.data else None
|
|
254
|
+
return f"""<AnalysisDocument id="{self.id}" size={sz} text={text}>"""
|
|
255
|
+
|
|
256
|
+
def __str__(self):
|
|
257
|
+
with StringIO() as output:
|
|
258
|
+
if self.analysis:
|
|
259
|
+
output.write(str(self.analysis))
|
|
260
|
+
else:
|
|
261
|
+
output.write(self.__repr__())
|
|
262
|
+
|
|
263
|
+
# print the rest of the applications.
|
|
264
|
+
for app_id, app_data in self._apps.items():
|
|
265
|
+
if app_id == APP_ID_ANALYSIS:
|
|
266
|
+
# we already have printed the self.analysis
|
|
267
|
+
continue
|
|
268
|
+
|
|
269
|
+
if RESULTS in app_data:
|
|
270
|
+
output.write(f"{app_id}, {json.dumps(app_data[RESULTS], indent=2)}\n")
|
|
271
|
+
elif DIAGNOSTICS in app_data:
|
|
272
|
+
output.write(f"{app_id}, {app_data[DIAGNOSTICS].to_json()}\n")
|
|
273
|
+
|
|
274
|
+
return output.getvalue()
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def entities(self) -> Generator[Entity, Any, None]:
|
|
278
|
+
"""
|
|
279
|
+
:return: The entities of the document
|
|
280
|
+
:rtype: ``Generator``
|
|
281
|
+
"""
|
|
282
|
+
if self.analysis is not None:
|
|
283
|
+
yield from self.analysis.entities
|
|
284
|
+
else:
|
|
285
|
+
raise ValueError(STR_MISSING_ANALYSIS)
|
|
286
|
+
|
|
287
|
+
@property
|
|
288
|
+
def tokens(self) -> Generator[Token, Any, None]:
|
|
289
|
+
"""
|
|
290
|
+
:return: The tokens of the document
|
|
291
|
+
:rtype: ``Generator``
|
|
292
|
+
"""
|
|
293
|
+
if self.analysis is not None:
|
|
294
|
+
yield from self.analysis.tokens
|
|
295
|
+
else:
|
|
296
|
+
raise ValueError(STR_MISSING_ANALYSIS)
|
|
297
|
+
|
|
298
|
+
@property
|
|
299
|
+
def annotations(self) -> Generator[Annotation, Any, None]:
|
|
300
|
+
"""
|
|
301
|
+
:return: The annotations of the document
|
|
302
|
+
:rtype: ``list``
|
|
303
|
+
"""
|
|
304
|
+
if self.analysis is not None:
|
|
305
|
+
yield from self.analysis.annotations
|
|
306
|
+
else:
|
|
307
|
+
raise ValueError(STR_MISSING_ANALYSIS)
|
|
308
|
+
|
|
309
|
+
@property
|
|
310
|
+
def sentences(self) -> Generator[Sentence, Any, None]:
|
|
311
|
+
"""
|
|
312
|
+
:return: The annotations of the document
|
|
313
|
+
:rtype: ``list``
|
|
314
|
+
"""
|
|
315
|
+
|
|
316
|
+
if self.analysis is not None:
|
|
317
|
+
yield from self.analysis.sentences
|
|
318
|
+
else:
|
|
319
|
+
raise ValueError(STR_MISSING_ANALYSIS)
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def topics(self) -> list[Topic] | list:
|
|
323
|
+
"""
|
|
324
|
+
:return: The topics of the document
|
|
325
|
+
:rtype: ``list``
|
|
326
|
+
"""
|
|
327
|
+
if self.has("wowool_topics"):
|
|
328
|
+
return convert_topics(self.results("wowool_topics"))
|
|
329
|
+
else:
|
|
330
|
+
return []
|
|
331
|
+
|
|
332
|
+
@property
|
|
333
|
+
def categories(self) -> list[Theme] | list:
|
|
334
|
+
"""
|
|
335
|
+
:return: The categories of the document
|
|
336
|
+
:rtype: ``list``
|
|
337
|
+
"""
|
|
338
|
+
if self.has("wowool_themes"):
|
|
339
|
+
return convert_themes(self.results("wowool_themes"))
|
|
340
|
+
else:
|
|
341
|
+
return []
|
|
342
|
+
|
|
343
|
+
@property
|
|
344
|
+
def chunks(self) -> list[Chunk] | list:
|
|
345
|
+
"""
|
|
346
|
+
:return: The chunks data of the document
|
|
347
|
+
:rtype: ``list``
|
|
348
|
+
"""
|
|
349
|
+
if self.has("wowool_chunks"):
|
|
350
|
+
return convert_chunks(self.results("wowool_chunks"))
|
|
351
|
+
else:
|
|
352
|
+
return []
|
|
353
|
+
|
|
354
|
+
@property
|
|
355
|
+
def themes(self) -> list[Theme] | list:
|
|
356
|
+
"""
|
|
357
|
+
:return: The categories of the document
|
|
358
|
+
:rtype: ``list``
|
|
359
|
+
"""
|
|
360
|
+
return self.categories
|
|
361
|
+
|
|
362
|
+
@property
|
|
363
|
+
def language(self):
|
|
364
|
+
"""
|
|
365
|
+
:return: The language of the document
|
|
366
|
+
:rtype: ``str``
|
|
367
|
+
"""
|
|
368
|
+
if self.has("wowool_language_identifier"):
|
|
369
|
+
return self.results("wowool_language_identifier")["language"]
|
|
370
|
+
else:
|
|
371
|
+
if self.has(APP_ID_ANALYSIS):
|
|
372
|
+
language = self.results(APP_ID_ANALYSIS).language
|
|
373
|
+
if "@" in language:
|
|
374
|
+
return language.split("@")[0]
|
|
375
|
+
return self.results(APP_ID_ANALYSIS).language
|
|
376
|
+
else:
|
|
377
|
+
return None
|
|
378
|
+
|
|
379
|
+
@staticmethod
|
|
380
|
+
def deserialize(document: dict):
|
|
381
|
+
"""
|
|
382
|
+
Deserialize a document from JSON format.
|
|
383
|
+
:param document: JSON representation of the document.
|
|
384
|
+
:return: Document object.
|
|
385
|
+
:rtype: ``Document``
|
|
386
|
+
"""
|
|
387
|
+
|
|
388
|
+
return AnalysisDocument.from_json(document)
|