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/config.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Union
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Config:
|
|
6
|
+
"""
|
|
7
|
+
General configuration object.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(self):
|
|
11
|
+
|
|
12
|
+
self._languages = [
|
|
13
|
+
{"name": "english", "code": "en"},
|
|
14
|
+
{"name": "french", "code": "fr"},
|
|
15
|
+
{"name": "dutch", "code": "nl"},
|
|
16
|
+
{"name": "german", "code": "de"},
|
|
17
|
+
{"name": "portuguese", "code": "po"},
|
|
18
|
+
{"name": "swedish", "code": "sv"},
|
|
19
|
+
{"name": "spanish", "code": "es"},
|
|
20
|
+
{"name": "danish", "code": "dk"},
|
|
21
|
+
{"name": "italian", "code": "it"},
|
|
22
|
+
{"name": "norwegian", "code": "no"},
|
|
23
|
+
{"name": "russian", "code": "ru"},
|
|
24
|
+
]
|
|
25
|
+
self._language_to_code, self._code_to_language = self._generate_language_maps()
|
|
26
|
+
|
|
27
|
+
def _generate_language_maps(self):
|
|
28
|
+
self.l2c = {}
|
|
29
|
+
self.c2l = {}
|
|
30
|
+
for language_info in self._languages:
|
|
31
|
+
self.l2c[language_info["name"]] = language_info["code"]
|
|
32
|
+
self.c2l[language_info["code"]] = language_info["name"]
|
|
33
|
+
return self.l2c, self.c2l
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def languages(self):
|
|
37
|
+
""":return: a (``list``) with the available languages"""
|
|
38
|
+
return self._languages
|
|
39
|
+
|
|
40
|
+
def get_language(self, code: str) -> str:
|
|
41
|
+
"""
|
|
42
|
+
:param str code: get the language for a give code.
|
|
43
|
+
:return: (``str``) : ex english for the code en
|
|
44
|
+
"""
|
|
45
|
+
_code = code.lower()
|
|
46
|
+
retval = self._code_to_language[_code] if _code in self._code_to_language else ""
|
|
47
|
+
if not retval:
|
|
48
|
+
return code if code in self._language_to_code else ""
|
|
49
|
+
return retval
|
|
50
|
+
|
|
51
|
+
def get_language_code(self, language: str) -> Union[str, None]:
|
|
52
|
+
"""
|
|
53
|
+
:param str code: get the code for a give language.
|
|
54
|
+
:return: (``str``) : ex en for the language english
|
|
55
|
+
"""
|
|
56
|
+
_language = language.lower()
|
|
57
|
+
retval = self._language_to_code[_language] if _language in self._language_to_code else None
|
|
58
|
+
if not retval:
|
|
59
|
+
return language if language in self._code_to_language else None
|
|
60
|
+
return retval
|
|
61
|
+
|
|
62
|
+
def __str__(self):
|
|
63
|
+
info = {"languages": self.languages}
|
|
64
|
+
return json.dumps(info)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
config = Config()
|
wowool/diagnostic.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
from enum import IntEnum
|
|
2
|
+
import logging
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DiagnosticType(IntEnum):
|
|
7
|
+
"""
|
|
8
|
+
:class:`DiagnosticType` is an enumeration type that holds the diagnostic types supported by default. The included levels map to the levels provided by the logging library
|
|
9
|
+
|
|
10
|
+
.. note:: This enumeration type can be extended or replaced with any other integer type
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
Debug = logging.DEBUG
|
|
14
|
+
Info = logging.INFO
|
|
15
|
+
Warning = logging.WARNING
|
|
16
|
+
Error = logging.ERROR
|
|
17
|
+
Critical = logging.CRITICAL
|
|
18
|
+
Notset = logging.NOTSET
|
|
19
|
+
|
|
20
|
+
@classmethod
|
|
21
|
+
def names(cls) -> typing.List[str]:
|
|
22
|
+
"""
|
|
23
|
+
:return: The diagnostic names, sorted by level value (``int``)
|
|
24
|
+
:rtype: A ``list`` of ``str``
|
|
25
|
+
"""
|
|
26
|
+
names = [name for name in dir(DiagnosticType) if not name.startswith("_") and name[0].isupper()]
|
|
27
|
+
return sorted(names, key=lambda name: int(getattr(DiagnosticType, name)))
|
|
28
|
+
|
|
29
|
+
@classmethod
|
|
30
|
+
def all(cls):
|
|
31
|
+
"""
|
|
32
|
+
:return: All type enumerations
|
|
33
|
+
:rtype: A ``list`` of ``int``
|
|
34
|
+
"""
|
|
35
|
+
return [getattr(DiagnosticType, name) for name in cls.names()]
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def count(cls) -> int:
|
|
39
|
+
"""
|
|
40
|
+
:return: The number of types
|
|
41
|
+
:rtype: ``int``
|
|
42
|
+
"""
|
|
43
|
+
return len(cls.names())
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
_levelToName = {
|
|
47
|
+
logging.CRITICAL: "[red bold]CRITICAL[/red bold]",
|
|
48
|
+
logging.ERROR: "[red]ERROR[/red]",
|
|
49
|
+
logging.WARNING: "[yellow]WARNING[/yellow]",
|
|
50
|
+
logging.INFO: "INFO",
|
|
51
|
+
logging.DEBUG: "DEBUG",
|
|
52
|
+
logging.NOTSET: "NOTSET",
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _getLevelName(level):
|
|
57
|
+
if level in _levelToName:
|
|
58
|
+
return _levelToName[level]
|
|
59
|
+
return ""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Diagnostic:
|
|
63
|
+
"""
|
|
64
|
+
:class:`Diagnostic` is a class that holds all relevant diagnostical information
|
|
65
|
+
|
|
66
|
+
.. note:: The ``type`` parameter can be any integer type. For convenience sake, :class:`DiagnosticType` is used by default, but you can provide your own typing scheme
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def from_json(json: dict):
|
|
71
|
+
return Diagnostic(
|
|
72
|
+
id=json["id"],
|
|
73
|
+
message=json["message"],
|
|
74
|
+
type=json["type"],
|
|
75
|
+
line=json["line"] if "line" in json else None,
|
|
76
|
+
offset=json["offset"] if "offset" in json else None,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def __init__(
|
|
80
|
+
self,
|
|
81
|
+
id: typing.Union[str, int],
|
|
82
|
+
message: str,
|
|
83
|
+
type: typing.SupportsInt,
|
|
84
|
+
line: typing.Union[int, None] = None,
|
|
85
|
+
offset: typing.Union[int, None] = None,
|
|
86
|
+
):
|
|
87
|
+
"""
|
|
88
|
+
Initialize a :class:`Diagnostic` instance
|
|
89
|
+
|
|
90
|
+
:param id: Unique identifier
|
|
91
|
+
:type id: ``str`` or ``int``
|
|
92
|
+
:param message: Message
|
|
93
|
+
:type message: ``str``
|
|
94
|
+
:param line: Line number (optional)
|
|
95
|
+
:type line: ``int``
|
|
96
|
+
:param offset: Offset (optional)
|
|
97
|
+
:type offset: ``int``
|
|
98
|
+
"""
|
|
99
|
+
super(Diagnostic, self).__init__()
|
|
100
|
+
self.id = id
|
|
101
|
+
self.message = message
|
|
102
|
+
self.type = type
|
|
103
|
+
self.line = line
|
|
104
|
+
self.offset = offset
|
|
105
|
+
|
|
106
|
+
def to_json(self):
|
|
107
|
+
"""
|
|
108
|
+
:return: A dictionary representing a JSON object of the diagnostic
|
|
109
|
+
:rtype: ``dict``
|
|
110
|
+
"""
|
|
111
|
+
obj = dict(id=self.id, message=self.message, type=self.type)
|
|
112
|
+
if self.line is not None:
|
|
113
|
+
obj["line"] = self.line
|
|
114
|
+
if self.offset is not None:
|
|
115
|
+
obj["offset"] = self.offset
|
|
116
|
+
return obj
|
|
117
|
+
|
|
118
|
+
def to_exception(self):
|
|
119
|
+
return DiagnosticException(self)
|
|
120
|
+
|
|
121
|
+
def rich(self):
|
|
122
|
+
"""
|
|
123
|
+
:return: The rich string representation of the token
|
|
124
|
+
:rtype: ``str``
|
|
125
|
+
"""
|
|
126
|
+
if self.line:
|
|
127
|
+
return f"<default>{self.id}</default>:{self.line}:{_getLevelName(self.type)}:{self.message}"
|
|
128
|
+
else:
|
|
129
|
+
return f"<default>{self.id}</default>:{_getLevelName(self.type)}:{self.message}"
|
|
130
|
+
|
|
131
|
+
def __eq__(self, other):
|
|
132
|
+
return self.to_json() == other.to_json()
|
|
133
|
+
|
|
134
|
+
def __str__(self):
|
|
135
|
+
return f"<Diagnostic: id={self.id}, type={self.type}, message={self.message}>"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class DiagnosticException(Exception):
|
|
139
|
+
def __init__(self, diagnostic: Diagnostic):
|
|
140
|
+
super(DiagnosticException, self).__init__(self, diagnostic.message)
|
|
141
|
+
self.diagnostic = diagnostic
|
|
142
|
+
|
|
143
|
+
def __str__(self):
|
|
144
|
+
return self.diagnostic.message
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class Diagnostics:
|
|
148
|
+
"""
|
|
149
|
+
:class:`Diagnostics` is a convenience class that provides some commonly used functionality and acts as a facade
|
|
150
|
+
"""
|
|
151
|
+
|
|
152
|
+
@staticmethod
|
|
153
|
+
def from_json(json: list):
|
|
154
|
+
items = [Diagnostic.from_json(item) for item in json]
|
|
155
|
+
return Diagnostics(items=items)
|
|
156
|
+
|
|
157
|
+
def __init__(self, items: typing.List[Diagnostic] = None):
|
|
158
|
+
"""
|
|
159
|
+
Initialize a :class:`Diagnostics` instance
|
|
160
|
+
|
|
161
|
+
:params items: Diagnostics. Defaults to an empty list
|
|
162
|
+
:type items: A ``list`` of :class:`Diagnostic`
|
|
163
|
+
"""
|
|
164
|
+
super(Diagnostics, self).__init__()
|
|
165
|
+
self.items = items if items else []
|
|
166
|
+
|
|
167
|
+
def add(self, diagnostic: Diagnostic):
|
|
168
|
+
"""
|
|
169
|
+
Add a diagnostic
|
|
170
|
+
|
|
171
|
+
:param diagnostic: Diagnostic
|
|
172
|
+
:type diagnostic: :class:`Diagnostic`
|
|
173
|
+
"""
|
|
174
|
+
self.items.append(diagnostic)
|
|
175
|
+
|
|
176
|
+
def extend(self, diagnostics):
|
|
177
|
+
"""
|
|
178
|
+
Extend with given diagnostics
|
|
179
|
+
|
|
180
|
+
:param diagnostic: Diagnostics
|
|
181
|
+
:type diagnostic: :class:`Diagnostics`
|
|
182
|
+
"""
|
|
183
|
+
for diagnostic in diagnostics:
|
|
184
|
+
self.add(diagnostic)
|
|
185
|
+
|
|
186
|
+
def filter(self, type: typing.SupportsInt):
|
|
187
|
+
"""
|
|
188
|
+
Filter on a given diagnostic type
|
|
189
|
+
|
|
190
|
+
:param type: Diagnostic type
|
|
191
|
+
:type type: :class:`DiagnosticType` or ``int``
|
|
192
|
+
|
|
193
|
+
:return: A generator expression yielding diagnostics of the matching type
|
|
194
|
+
:rtype: :class:`Diagnostic`
|
|
195
|
+
"""
|
|
196
|
+
if type == DiagnosticType.Notset:
|
|
197
|
+
return self.items
|
|
198
|
+
return filter(lambda item: type == item.type, self.items)
|
|
199
|
+
|
|
200
|
+
def has(self, type: typing.SupportsInt) -> bool:
|
|
201
|
+
"""
|
|
202
|
+
:return: Whether a diagnostic with the given type is present
|
|
203
|
+
:rtype: ``bool``
|
|
204
|
+
"""
|
|
205
|
+
for _ in self.filter(type):
|
|
206
|
+
return True
|
|
207
|
+
return False
|
|
208
|
+
|
|
209
|
+
def __len__(self) -> int:
|
|
210
|
+
"""
|
|
211
|
+
:return: The number of diagnostics
|
|
212
|
+
:rtype: ``int``
|
|
213
|
+
"""
|
|
214
|
+
return len(self.items)
|
|
215
|
+
|
|
216
|
+
def __iter__(self):
|
|
217
|
+
return iter(self.items)
|
|
218
|
+
|
|
219
|
+
def raise_when(self, type: typing.SupportsInt):
|
|
220
|
+
"""
|
|
221
|
+
Raises an exception when a diagnostic exceeds the level of the given type
|
|
222
|
+
|
|
223
|
+
:raises DiagnosticException: Only in presence of a diagnostic level that exceeds the given type
|
|
224
|
+
"""
|
|
225
|
+
for diagnostic in self.items:
|
|
226
|
+
if int(diagnostic.type) >= int(type):
|
|
227
|
+
exception = diagnostic.to_exception()
|
|
228
|
+
assert isinstance(exception, DiagnosticException), f"Diagnostic did not return an exception"
|
|
229
|
+
raise exception
|
|
230
|
+
|
|
231
|
+
def is_greater_or_equal_than(self, type: typing.SupportsInt):
|
|
232
|
+
"""
|
|
233
|
+
:returns: Whether a diagnostic is greater or equal than the given diagnostic type
|
|
234
|
+
:rtype: ``bool``
|
|
235
|
+
"""
|
|
236
|
+
for diagnostic in self.items:
|
|
237
|
+
if int(diagnostic.type) >= int(type):
|
|
238
|
+
return True
|
|
239
|
+
|
|
240
|
+
def to_json(self):
|
|
241
|
+
return [diagnostic.to_json() for diagnostic in self.items]
|
|
242
|
+
|
|
243
|
+
def __eq__(self, other):
|
|
244
|
+
return self.to_json() == other.to_json()
|
|
245
|
+
|
|
246
|
+
def __str__(self):
|
|
247
|
+
return f"<Diagnostics: {len(self)} items>"
|
|
248
|
+
|
|
249
|
+
def __getitem__(self, index):
|
|
250
|
+
return self.items[index]
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class Chunk:
|
|
8
|
+
sentences: list
|
|
9
|
+
begin_offset: int
|
|
10
|
+
end_offset: int
|
|
11
|
+
outline: list | None
|
|
12
|
+
topics: list[Topic] | None
|
|
13
|
+
themes: list[Theme] | None
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def convert_chunks(jo_chunks: list):
|
|
17
|
+
chunks = []
|
|
18
|
+
for c in jo_chunks["chunks"]:
|
|
19
|
+
chunk = Chunk(c["sc"], c.get("bo"), c.get("eo"), c.get("outline"), convert_topics(c.get("topics")), convert_themes(c.get("themes")))
|
|
20
|
+
chunks.append(chunk)
|
|
21
|
+
|
|
22
|
+
return chunks
|