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
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
from io import StringIO
|
|
2
|
+
from wowool.annotation.annotation import BasicAnnotation
|
|
3
|
+
from wowool.annotation.concept import Concept
|
|
4
|
+
from typing import Callable, Iterator, Self
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def _filter_pass_thru_concept(concept: Concept) -> bool:
|
|
8
|
+
return concept.uri != "Sentence"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _filter_pass_thru(sentence) -> bool:
|
|
12
|
+
return True
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Sentence(BasicAnnotation):
|
|
16
|
+
"""
|
|
17
|
+
:class:`Sentence` is a class that contains all the :class:`Tokens <wowool.annotation.token.Token>` and :class:`Concepts <wowool.annotation.concept.Concept>` of a sentence
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
FilterType = Callable[[Self], bool]
|
|
21
|
+
|
|
22
|
+
def __init__(self, begin_offset: int, end_offset: int) -> None:
|
|
23
|
+
"""
|
|
24
|
+
Initialize a :class:`Sentence` instance
|
|
25
|
+
|
|
26
|
+
:param begin_offset: Begin offset of the sentence
|
|
27
|
+
:type begin_offset: ``int``
|
|
28
|
+
:param end_offset: End offset of the sentence
|
|
29
|
+
:type end_offset: ``int``
|
|
30
|
+
|
|
31
|
+
:return: An initialized sentence
|
|
32
|
+
:rtype: :class:`Sentence`
|
|
33
|
+
"""
|
|
34
|
+
super(Sentence, self).__init__(begin_offset, end_offset)
|
|
35
|
+
self.annotations = []
|
|
36
|
+
self.text_ = None
|
|
37
|
+
self.attributes = {}
|
|
38
|
+
self.tokens_ = None
|
|
39
|
+
|
|
40
|
+
def rich(self) -> str:
|
|
41
|
+
"""
|
|
42
|
+
:return: The rich string representation of the sentence
|
|
43
|
+
:rtype: ``str``
|
|
44
|
+
"""
|
|
45
|
+
output = StringIO()
|
|
46
|
+
output.write("S:" + BasicAnnotation.__repr__(self))
|
|
47
|
+
if self.is_header:
|
|
48
|
+
output.write(" @( header='true' )")
|
|
49
|
+
output.write("\n")
|
|
50
|
+
for annotation in self.annotations:
|
|
51
|
+
output.write(" ")
|
|
52
|
+
output.write(annotation.rich())
|
|
53
|
+
output.write("\n")
|
|
54
|
+
contents = output.getvalue()
|
|
55
|
+
output.close()
|
|
56
|
+
return contents
|
|
57
|
+
|
|
58
|
+
def __repr__(self) -> str:
|
|
59
|
+
output = StringIO()
|
|
60
|
+
output.write("S:" + BasicAnnotation.__repr__(self))
|
|
61
|
+
if self.is_header:
|
|
62
|
+
output.write(" @( header='true' )")
|
|
63
|
+
output.write("\n")
|
|
64
|
+
for annotation in self.annotations:
|
|
65
|
+
output.write(" ")
|
|
66
|
+
output.write(str(annotation))
|
|
67
|
+
output.write("\n")
|
|
68
|
+
contents = output.getvalue()
|
|
69
|
+
output.close()
|
|
70
|
+
return contents
|
|
71
|
+
|
|
72
|
+
def __iter__(self):
|
|
73
|
+
"""
|
|
74
|
+
A :class:`Sentence` instance is iterable, yielding :class:`Annotation <wowool.annotation.annotation.Annotation>` objects. For example:
|
|
75
|
+
|
|
76
|
+
.. code-block:: python
|
|
77
|
+
|
|
78
|
+
for annotation in sentence:
|
|
79
|
+
print(annotation)
|
|
80
|
+
|
|
81
|
+
:rtype: :class:`Annotation <wowool.annotation.annotation.Annotation>`
|
|
82
|
+
"""
|
|
83
|
+
return iter(self.annotations)
|
|
84
|
+
|
|
85
|
+
def __len__(self) -> int:
|
|
86
|
+
return len(self.annotations)
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def tokens(self):
|
|
90
|
+
if self.tokens_ is None:
|
|
91
|
+
self.tokens_ = [a for a in self.annotations if a.is_token]
|
|
92
|
+
return self.tokens_
|
|
93
|
+
|
|
94
|
+
@property
|
|
95
|
+
def entities(self):
|
|
96
|
+
yield from Concept.iter(self)
|
|
97
|
+
|
|
98
|
+
def __getattr__(self, uri: str):
|
|
99
|
+
"""
|
|
100
|
+
Find the first instance of the concept with the given URI in the sentence
|
|
101
|
+
|
|
102
|
+
For example:
|
|
103
|
+
|
|
104
|
+
.. code-block:: python
|
|
105
|
+
|
|
106
|
+
# Find the first person in the current sentence
|
|
107
|
+
person = sentence.Person
|
|
108
|
+
|
|
109
|
+
:param uri: URI, or name, of the concept
|
|
110
|
+
:type uri: ``str``
|
|
111
|
+
|
|
112
|
+
:returns: The first matching instance of the given concept
|
|
113
|
+
:type: :class:`Concept <wowool.annotation.concept.Concept>`
|
|
114
|
+
|
|
115
|
+
"""
|
|
116
|
+
return self.find_first(uri)
|
|
117
|
+
|
|
118
|
+
def find_first(self, uri: str):
|
|
119
|
+
"""
|
|
120
|
+
Find the first instance of the concept with the given URI in the sentence
|
|
121
|
+
|
|
122
|
+
For example:
|
|
123
|
+
|
|
124
|
+
.. code-block:: python
|
|
125
|
+
|
|
126
|
+
# Find a child concept in the current sentence
|
|
127
|
+
person = concept.find_first('Person'))
|
|
128
|
+
|
|
129
|
+
:param uri: URI, or name, of the concept
|
|
130
|
+
:type uri: ``str``
|
|
131
|
+
|
|
132
|
+
:returns: The first matching instance of the given concept
|
|
133
|
+
:type: :class:`Concept <wowool.annotation.concept.Concept>`
|
|
134
|
+
"""
|
|
135
|
+
concept = Concept(self.begin_offset, self.end_offset, "Sentence")
|
|
136
|
+
concept._sentence_annotations = self.annotations
|
|
137
|
+
concept._annotation_idx = 0
|
|
138
|
+
return concept.find_first(uri)
|
|
139
|
+
|
|
140
|
+
def find(self, uri: str):
|
|
141
|
+
"""
|
|
142
|
+
Find all instances of the concept the with the given URI in the sentence
|
|
143
|
+
|
|
144
|
+
For example:
|
|
145
|
+
|
|
146
|
+
.. code-block:: python
|
|
147
|
+
|
|
148
|
+
# List all persons in the sentence
|
|
149
|
+
for person in concept.find('Person')):
|
|
150
|
+
print(person)
|
|
151
|
+
|
|
152
|
+
:param uri: URI, or name, of the concept
|
|
153
|
+
:type uri: ``str``
|
|
154
|
+
|
|
155
|
+
:returns: The matching instances of the given concept
|
|
156
|
+
:rtype: A ``list`` of :class:`Concept`
|
|
157
|
+
"""
|
|
158
|
+
concept = Concept(self.begin_offset, self.end_offset, "Sentence")
|
|
159
|
+
concept._sentence_annotations = self.annotations
|
|
160
|
+
concept._annotation_idx = 0
|
|
161
|
+
return concept.find(uri)
|
|
162
|
+
|
|
163
|
+
def __getitem__(self, value):
|
|
164
|
+
return self.annotations[value] if value < len(self.annotations) else None
|
|
165
|
+
|
|
166
|
+
@property
|
|
167
|
+
def text(self):
|
|
168
|
+
"""
|
|
169
|
+
:return: A string representation of the sentence
|
|
170
|
+
:rtype: ``str``
|
|
171
|
+
"""
|
|
172
|
+
retval = ""
|
|
173
|
+
prev_tk = None
|
|
174
|
+
for tk in [a for a in self.annotations if a.is_token]:
|
|
175
|
+
if prev_tk:
|
|
176
|
+
if prev_tk.end_offset != tk.begin_offset:
|
|
177
|
+
retval += " "
|
|
178
|
+
retval += tk.literal
|
|
179
|
+
prev_tk = tk
|
|
180
|
+
return retval.strip()
|
|
181
|
+
|
|
182
|
+
@property
|
|
183
|
+
def stems(self):
|
|
184
|
+
"""
|
|
185
|
+
:return: A string representation of the sentence with the stems
|
|
186
|
+
:rtype: ``str``
|
|
187
|
+
"""
|
|
188
|
+
if self.text_ is None:
|
|
189
|
+
retval = ""
|
|
190
|
+
prev_tk = None
|
|
191
|
+
for tk in [a for a in self.annotations if a.is_token]:
|
|
192
|
+
if prev_tk:
|
|
193
|
+
if prev_tk.end_offset != tk.begin_offset:
|
|
194
|
+
retval += " "
|
|
195
|
+
retval += tk.stem
|
|
196
|
+
prev_tk = tk
|
|
197
|
+
self.text_ = retval.strip()
|
|
198
|
+
return self.text_
|
|
199
|
+
|
|
200
|
+
@property
|
|
201
|
+
def is_header(self) -> bool:
|
|
202
|
+
"""
|
|
203
|
+
:return: True if the sentence is a header
|
|
204
|
+
:rtype: ``bool``
|
|
205
|
+
"""
|
|
206
|
+
return self.attributes.get("header", []) == ["true"]
|
|
207
|
+
|
|
208
|
+
def concepts(self, filter=_filter_pass_thru_concept):
|
|
209
|
+
"""
|
|
210
|
+
Access the concepts in the analysis of the document
|
|
211
|
+
|
|
212
|
+
:param filter: Optional filter to select or discard concepts
|
|
213
|
+
:type filter: Functor accepting a :class:`Concept <wowool.annotation.concept.Concept>` and returning a ``bool``
|
|
214
|
+
|
|
215
|
+
:return: A generator expression yielding the concepts in the processed document
|
|
216
|
+
:rtype: :class:`Concepts <wowool.annotation.concept.Concept>`
|
|
217
|
+
"""
|
|
218
|
+
yield from Concept.iter(self, filter)
|
|
219
|
+
|
|
220
|
+
@staticmethod
|
|
221
|
+
def _document_iter(doc, filter: FilterType = _filter_pass_thru) -> Iterator[Concept]:
|
|
222
|
+
for sentence in doc:
|
|
223
|
+
for annotation in sentence:
|
|
224
|
+
if annotation.is_concept and filter(annotation):
|
|
225
|
+
yield annotation
|
|
226
|
+
|
|
227
|
+
@staticmethod
|
|
228
|
+
def _paragraph_iter(paragraph, filter: FilterType = _filter_pass_thru) -> Iterator[Concept]:
|
|
229
|
+
for sentence in paragraph.sentences:
|
|
230
|
+
yield sentence
|
|
231
|
+
|
|
232
|
+
@staticmethod
|
|
233
|
+
def iter(object, filter: FilterType = _filter_pass_thru, align: bool = True) -> Iterator[Self]:
|
|
234
|
+
"""
|
|
235
|
+
Iterate over the concepts in a document, an analysis, a sentence or a concept. For example:
|
|
236
|
+
|
|
237
|
+
.. code-block:: python
|
|
238
|
+
|
|
239
|
+
document = analyzer("Hello from Antwerp, said John Smith.")
|
|
240
|
+
for concept in Sentence.iter(document, lambda concept : concept.uri == "NP"):
|
|
241
|
+
print(concept)
|
|
242
|
+
|
|
243
|
+
:param object: Object to iterate
|
|
244
|
+
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`Sentence <wowool.annotation.concept.Sentence>`
|
|
245
|
+
:param filter: Predicate function to filter the concepts, callable that accepts a concept and returns True if the concept is considered a match
|
|
246
|
+
:type filter: Callable accepting a :class:`Sentence` and returning a ``bool``
|
|
247
|
+
|
|
248
|
+
:return: A generator expression yielding concepts
|
|
249
|
+
:rtype: :class:`Sentence <wowool.annotation.concept.Sentence>`
|
|
250
|
+
"""
|
|
251
|
+
from wowool.document.document import Document
|
|
252
|
+
from wowool.analysis import Analysis
|
|
253
|
+
from wowool.annotation.paragraph import Paragraph
|
|
254
|
+
|
|
255
|
+
if isinstance(object, Document) and object.analysis:
|
|
256
|
+
yield from Sentence._document_iter(object.analysis, filter)
|
|
257
|
+
elif isinstance(object, Analysis):
|
|
258
|
+
yield from Sentence._document_iter(object, filter)
|
|
259
|
+
elif isinstance(object, Paragraph):
|
|
260
|
+
yield from Sentence._paragraph_iter(object, filter)
|
|
261
|
+
else:
|
|
262
|
+
raise TypeError(f"Expected Document, Analysis, Sentence, or Concept but got '{type(object)}'")
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from wowool.annotation.annotation import Annotation, ErrorAnnotationNotFound
|
|
3
|
+
from typing import List, MutableSet, Union, cast, Generator
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
from typing import NewType
|
|
7
|
+
|
|
8
|
+
Concept = NewType("Concept", Annotation)
|
|
9
|
+
TokenType = NewType("TokenType", Annotation)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Token(Annotation):
|
|
13
|
+
"""
|
|
14
|
+
:class:`Token` is a class that contains all the information of a token
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
__slots__ = ["literal", "morphology", "properties"]
|
|
18
|
+
|
|
19
|
+
def __init__(self, begin_offset: int, end_offset: int, literal: str):
|
|
20
|
+
"""
|
|
21
|
+
Initialize a :class:`Token` instance
|
|
22
|
+
|
|
23
|
+
:param begin_offset: Begin offset of the token
|
|
24
|
+
:type begin_offset: ``int``
|
|
25
|
+
:param end_offset: End offset of the token
|
|
26
|
+
:type end_offset: ``int``
|
|
27
|
+
:param literal: Literal string from the input document
|
|
28
|
+
:type literal: ``str``
|
|
29
|
+
|
|
30
|
+
:return: An initialized token
|
|
31
|
+
:rtype: :class:`Token`
|
|
32
|
+
"""
|
|
33
|
+
super(Token, self).__init__(begin_offset, end_offset)
|
|
34
|
+
self.literal: str = literal
|
|
35
|
+
self.morphology: List[MorphData] = []
|
|
36
|
+
self.properties: MutableSet[str] = set()
|
|
37
|
+
|
|
38
|
+
# :param properties: the properties of the token.
|
|
39
|
+
# :type properties: set()
|
|
40
|
+
# :param morphology: the morphologic data information of the token.
|
|
41
|
+
# :type morphology: MorphData
|
|
42
|
+
|
|
43
|
+
def rich(self):
|
|
44
|
+
"""
|
|
45
|
+
:return: The rich string representation of the token
|
|
46
|
+
:rtype: ``str``
|
|
47
|
+
"""
|
|
48
|
+
retval = "<literal>T</literal>:" + Annotation.__repr__(self) + ": <literal>" + self.literal + "</literal>"
|
|
49
|
+
if self.properties:
|
|
50
|
+
retval += ",{"
|
|
51
|
+
for idx, prop in enumerate(sorted(self.properties)):
|
|
52
|
+
if idx != 0:
|
|
53
|
+
retval += ", "
|
|
54
|
+
retval += "+" + prop
|
|
55
|
+
retval += "}"
|
|
56
|
+
|
|
57
|
+
if self.morphology:
|
|
58
|
+
retval += ","
|
|
59
|
+
for morph_info in self.morphology:
|
|
60
|
+
retval += morph_info.rich()
|
|
61
|
+
|
|
62
|
+
return retval
|
|
63
|
+
|
|
64
|
+
def __repr__(self):
|
|
65
|
+
retval = "T:" + Annotation.__repr__(self) + ": " + self.literal
|
|
66
|
+
if self.properties:
|
|
67
|
+
retval += ",{"
|
|
68
|
+
for idx, prop in enumerate(sorted(self.properties)):
|
|
69
|
+
if idx != 0:
|
|
70
|
+
retval += ", "
|
|
71
|
+
retval += "+" + prop
|
|
72
|
+
retval += "}"
|
|
73
|
+
|
|
74
|
+
if self.morphology:
|
|
75
|
+
retval += ","
|
|
76
|
+
for morph_info in self.morphology:
|
|
77
|
+
retval += str(morph_info)
|
|
78
|
+
|
|
79
|
+
return retval
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def stem(self) -> str:
|
|
83
|
+
"""
|
|
84
|
+
:return: The first stem of the token or an empty string if absent
|
|
85
|
+
:type: ``str``
|
|
86
|
+
"""
|
|
87
|
+
for morph_info in self.morphology:
|
|
88
|
+
return morph_info.stem
|
|
89
|
+
return ""
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def pos(self) -> str:
|
|
93
|
+
"""
|
|
94
|
+
:return: The first part-of-speech of the token or an empty string if absent
|
|
95
|
+
:type: ``str``
|
|
96
|
+
"""
|
|
97
|
+
for morph_info in self.morphology:
|
|
98
|
+
return morph_info.pos
|
|
99
|
+
return ""
|
|
100
|
+
|
|
101
|
+
def has_property(self, prop: str) -> bool:
|
|
102
|
+
"""
|
|
103
|
+
:param prop: Property name. For example ``"NF"``
|
|
104
|
+
:type prop: ``str``
|
|
105
|
+
|
|
106
|
+
:return: Whether a given property is set on the token
|
|
107
|
+
:rtype: ``bool``
|
|
108
|
+
"""
|
|
109
|
+
return prop in self.properties
|
|
110
|
+
|
|
111
|
+
def has_pos(self, pos: str) -> bool:
|
|
112
|
+
"""
|
|
113
|
+
:param pos: Part-of-speech. For example ``"Nn"``
|
|
114
|
+
:type pos: ``str``
|
|
115
|
+
|
|
116
|
+
:return: Whether a given part-of-speech is set on the token
|
|
117
|
+
:rtype: ``bool``
|
|
118
|
+
"""
|
|
119
|
+
for morph_info in self.morphology:
|
|
120
|
+
if morph_info.pos.startswith(pos):
|
|
121
|
+
return True
|
|
122
|
+
return False
|
|
123
|
+
|
|
124
|
+
def get_morphology(self, pos: str):
|
|
125
|
+
"""
|
|
126
|
+
:param pos: Part-of-speech. For example ``"Nn"``
|
|
127
|
+
:type pos: ``str``
|
|
128
|
+
|
|
129
|
+
:return: Whether a given part-of-speech is set on the token
|
|
130
|
+
:rtype: ``bool``
|
|
131
|
+
"""
|
|
132
|
+
for morph_info in self.morphology:
|
|
133
|
+
if morph_info.pos.startswith(pos):
|
|
134
|
+
return morph_info
|
|
135
|
+
|
|
136
|
+
def __len__(self) -> int:
|
|
137
|
+
"""
|
|
138
|
+
:returns: The length of the token in the input document in bytes. This takes into account unicode characters
|
|
139
|
+
:type: ``int``
|
|
140
|
+
"""
|
|
141
|
+
return self.end_offset - self.begin_offset
|
|
142
|
+
|
|
143
|
+
def __bool__(self) -> bool:
|
|
144
|
+
return self._begin_offset != -1
|
|
145
|
+
|
|
146
|
+
@staticmethod
|
|
147
|
+
def _document_iter(document) -> Generator[Token, None, None]:
|
|
148
|
+
for sentence in document:
|
|
149
|
+
for annotation in sentence:
|
|
150
|
+
if annotation.is_token:
|
|
151
|
+
yield cast(Token, annotation)
|
|
152
|
+
|
|
153
|
+
@staticmethod
|
|
154
|
+
def _concept_iter(concept) -> Generator[Token, None, None]:
|
|
155
|
+
for annotation in concept.annotations:
|
|
156
|
+
if annotation.is_token:
|
|
157
|
+
yield cast(Token, annotation)
|
|
158
|
+
|
|
159
|
+
@staticmethod
|
|
160
|
+
def iter(object) -> Generator[Token, None, None]:
|
|
161
|
+
"""
|
|
162
|
+
Iterate over the concepts in a document, an analysis, a sentence or a concept. For example:
|
|
163
|
+
|
|
164
|
+
.. code-block:: python
|
|
165
|
+
|
|
166
|
+
document = analyzer("Hello from Antwerp, said John Smith.")
|
|
167
|
+
for token in Token.iter(document):
|
|
168
|
+
print(token)
|
|
169
|
+
|
|
170
|
+
:param object: Object to iterate
|
|
171
|
+
:type object: :class:`Analysis <wowool.analysis.Analysis>`, :class:`Sentence <wowool.annotation.sentence.Sentence>` or :class:`Concept <wowool.annotation.concept.Concept>`
|
|
172
|
+
|
|
173
|
+
:return: A generator expression yielding tokens
|
|
174
|
+
:rtype: :class:`Token <wowool.annotation.token.Token>`
|
|
175
|
+
"""
|
|
176
|
+
from wowool.document.document import Document
|
|
177
|
+
from wowool.analysis import Analysis
|
|
178
|
+
from wowool.annotation import Sentence, Concept
|
|
179
|
+
|
|
180
|
+
if isinstance(object, Document) and object.analysis:
|
|
181
|
+
yield from Token._document_iter(object.analysis)
|
|
182
|
+
elif isinstance(object, Analysis):
|
|
183
|
+
yield from Token._document_iter(object)
|
|
184
|
+
elif isinstance(object, Sentence):
|
|
185
|
+
for annotation in object:
|
|
186
|
+
if isinstance(annotation, Token):
|
|
187
|
+
yield cast(Token, annotation)
|
|
188
|
+
elif isinstance(object, Concept):
|
|
189
|
+
yield from Token._concept_iter(object)
|
|
190
|
+
else:
|
|
191
|
+
raise TypeError(f"Expected Document, Analysis, Sentence, or Concept but got '{type(object)}'")
|
|
192
|
+
|
|
193
|
+
@staticmethod
|
|
194
|
+
def next(sentence, object) -> Token:
|
|
195
|
+
"""returns the next Token"""
|
|
196
|
+
index = object.index + 1
|
|
197
|
+
sentence_length = len(sentence)
|
|
198
|
+
while index < sentence_length:
|
|
199
|
+
if sentence[index].is_token:
|
|
200
|
+
return cast(Token, sentence[index])
|
|
201
|
+
else:
|
|
202
|
+
index += 1
|
|
203
|
+
raise ErrorAnnotationNotFound
|
|
204
|
+
|
|
205
|
+
@staticmethod
|
|
206
|
+
def prev(sentence, object: Union[Concept, Token]) -> Token:
|
|
207
|
+
"""returns the prev Token"""
|
|
208
|
+
index = object.index - 1
|
|
209
|
+
while index >= 0:
|
|
210
|
+
if sentence[index].is_token:
|
|
211
|
+
return cast(Token, sentence[index])
|
|
212
|
+
else:
|
|
213
|
+
index -= 1
|
|
214
|
+
raise ErrorAnnotationNotFound
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
class MorphData:
|
|
218
|
+
"""
|
|
219
|
+
:class:`MorphData` is a class that contains the morphological data. For example:
|
|
220
|
+
|
|
221
|
+
.. code-block:: python
|
|
222
|
+
|
|
223
|
+
for md in token.morphology:
|
|
224
|
+
print( md.pos , md.stem )
|
|
225
|
+
|
|
226
|
+
:param pos: Part-of-speech
|
|
227
|
+
:type pos: ``str``
|
|
228
|
+
:param stem: Stem
|
|
229
|
+
:type stem: ``str``
|
|
230
|
+
"""
|
|
231
|
+
|
|
232
|
+
def __init__(self):
|
|
233
|
+
self.pos: str = "None"
|
|
234
|
+
self.stem: str = ""
|
|
235
|
+
|
|
236
|
+
def rich(self) -> str:
|
|
237
|
+
"""
|
|
238
|
+
:return: The rich string representation of the morphological data
|
|
239
|
+
:rtype: ``str``
|
|
240
|
+
"""
|
|
241
|
+
retval = "['<stem>" + self.stem + "</stem>':" + self.pos + "]"
|
|
242
|
+
if hasattr(self, "morphology"):
|
|
243
|
+
self.morphology: List[MorphData]
|
|
244
|
+
for md in self.morphology:
|
|
245
|
+
retval += md.rich()
|
|
246
|
+
return retval
|
|
247
|
+
|
|
248
|
+
def __repr__(self) -> str:
|
|
249
|
+
retval = f"[{self.stem}:{self.pos}"
|
|
250
|
+
if hasattr(self, "morphology"):
|
|
251
|
+
retval += ",["
|
|
252
|
+
self.morphology: List[MorphData]
|
|
253
|
+
for md in self.morphology:
|
|
254
|
+
retval += str(md)
|
|
255
|
+
retval += "]"
|
|
256
|
+
retval += "]"
|
|
257
|
+
return retval
|
|
258
|
+
|
|
259
|
+
def __setstate__(self, d):
|
|
260
|
+
self.__dict__.update(d)
|
|
261
|
+
|
|
262
|
+
def __getstate__(self):
|
|
263
|
+
return self.__dict__
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
TokenNone = Token(-1, -1, "")
|
wowool/apps/info.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from logging import getLogger
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
import importlib.util
|
|
4
|
+
|
|
5
|
+
import wowool.apps as applications
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
logger = getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def to_app_name(folder_name) -> str:
|
|
12
|
+
return folder_name.replace("_", " ").title().replace(" ", "")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def load_info():
|
|
16
|
+
info_ = {}
|
|
17
|
+
for dir_name in applications.__path__:
|
|
18
|
+
for app in [fn for fn in Path(dir_name).glob("*") if fn.is_dir() and fn.name[0] != "_"]:
|
|
19
|
+
try:
|
|
20
|
+
config_fn = app / "config.py"
|
|
21
|
+
folder_name = config_fn.parent.name
|
|
22
|
+
info_name = folder_name.replace("_", "-")
|
|
23
|
+
if not config_fn.is_file():
|
|
24
|
+
logger.warning(f"Missing Application config file {config_fn}!")
|
|
25
|
+
continue
|
|
26
|
+
|
|
27
|
+
module_name = f"wowool.apps.{folder_name}"
|
|
28
|
+
spec = importlib.util.spec_from_file_location(module_name, str(config_fn))
|
|
29
|
+
# creates a new module based on spec
|
|
30
|
+
assert spec
|
|
31
|
+
app_module = importlib.util.module_from_spec(spec)
|
|
32
|
+
|
|
33
|
+
# executes the module in its own namespace
|
|
34
|
+
# when a module is imported or reloaded.
|
|
35
|
+
assert spec.loader
|
|
36
|
+
spec.loader.exec_module(app_module)
|
|
37
|
+
|
|
38
|
+
config = {**app_module.CONFIG}
|
|
39
|
+
config["uid"] = folder_name
|
|
40
|
+
if "name" not in config:
|
|
41
|
+
config["name"] = f"{folder_name.replace('_',' ').title()}"
|
|
42
|
+
|
|
43
|
+
if "app_id" not in config:
|
|
44
|
+
config["app_id"] = f"wowool_{folder_name}"
|
|
45
|
+
if "class" not in config:
|
|
46
|
+
config["class"] = to_app_name(folder_name)
|
|
47
|
+
if "module" not in config:
|
|
48
|
+
config["module"] = module_name
|
|
49
|
+
|
|
50
|
+
info_[info_name] = config
|
|
51
|
+
# logger.debug(f"APPS_INFO:, {info_name},{config}")
|
|
52
|
+
except Exception as ex:
|
|
53
|
+
logger.exception(ex)
|
|
54
|
+
return info_
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
info = load_info()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def load_aliases(app_info_list):
|
|
61
|
+
aliases_ = {}
|
|
62
|
+
try:
|
|
63
|
+
# check for missing apps.
|
|
64
|
+
for app in app_info_list:
|
|
65
|
+
if app not in aliases_:
|
|
66
|
+
aliases_[app] = app_info_list[app]
|
|
67
|
+
if "aliases" in app_info_list[app]:
|
|
68
|
+
for alias in app_info_list[app]["aliases"]:
|
|
69
|
+
if alias not in aliases_:
|
|
70
|
+
aliases_[alias] = app_info_list[app]
|
|
71
|
+
if "module" in app_info_list[app] and app_info_list[app]["module"].startswith("wowool."):
|
|
72
|
+
alias_name = f"wowool/{alias}"
|
|
73
|
+
aliases_[alias_name] = app_info_list[app]
|
|
74
|
+
|
|
75
|
+
if "module" in app_info_list[app] and app_info_list[app]["module"].startswith("wowool."):
|
|
76
|
+
alias_name = f"wowool/{app}"
|
|
77
|
+
aliases_[alias_name] = app_info_list[app]
|
|
78
|
+
|
|
79
|
+
except Exception as ex:
|
|
80
|
+
logger.exception(ex)
|
|
81
|
+
return aliases_
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
aliases = load_aliases(info)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def is_app_released(app_config: dict) -> bool:
|
|
88
|
+
if not "release" in app_config:
|
|
89
|
+
return True
|
|
90
|
+
return app_config["release"]
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def reload():
|
|
94
|
+
global info
|
|
95
|
+
global aliases
|
|
96
|
+
info = load_info()
|
|
97
|
+
aliases = load_aliases(info)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from wowool.common.pipeline.objects import UUID, createUUID, ComponentInfo # noqa: F401
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@dataclass
|
|
6
|
+
class UUID:
|
|
7
|
+
name: str
|
|
8
|
+
options: dict[str, Any] = field(default_factory=dict)
|
|
9
|
+
|
|
10
|
+
def to_json(self):
|
|
11
|
+
return {"name": self.name, "options": self.options}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def createUUID(uid: str | UUID | dict) -> UUID:
|
|
15
|
+
if isinstance(uid, str):
|
|
16
|
+
return UUID(name=uid)
|
|
17
|
+
elif isinstance(uid, UUID):
|
|
18
|
+
return uid
|
|
19
|
+
elif isinstance(uid, dict):
|
|
20
|
+
if "options" not in uid:
|
|
21
|
+
uid["options"] = {}
|
|
22
|
+
return UUID(name=uid["name"], options=uid["options"])
|
|
23
|
+
else:
|
|
24
|
+
raise ValueError(f"Invalid UID: {uid}")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class ComponentInfo:
|
|
29
|
+
options: dict[str, Any]
|
|
30
|
+
type: str
|
|
31
|
+
name: str
|
|
32
|
+
uid: str
|
|
33
|
+
filename: str | None = None
|
|
34
|
+
app: dict[str, str] | None = None
|
|
35
|
+
original: str | None = None
|
|
36
|
+
|
|
37
|
+
def to_json(self):
|
|
38
|
+
retval = {"type": self.type, "uid": self.uid}
|
|
39
|
+
if self.options:
|
|
40
|
+
retval["options"] = self.options
|
|
41
|
+
if self.filename:
|
|
42
|
+
retval["filename"] = self.filename
|
|
43
|
+
if self.app:
|
|
44
|
+
retval["app"] = self.app
|
|
45
|
+
return retval
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def strip_version(component_name: str) -> str:
|
|
5
|
+
return re.sub("""(.*)@(?:dev\\d*|[0-9\\.]+[\\d]+)(\\.(?:language|dom))?""", "\\1\\2", component_name)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_lxware_version():
|
|
9
|
+
from os import environ
|
|
10
|
+
|
|
11
|
+
if "WOWOOL_LXWARE_VERSION" in environ:
|
|
12
|
+
return environ["WOWOOL_LXWARE_VERSION"]
|
|
13
|
+
return ""
|