-
Notifications
You must be signed in to change notification settings - Fork 42
/
pipeline.py
259 lines (194 loc) · 6.45 KB
/
pipeline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Pipeline for CONLL-U formatting.
"""
# pylint: disable=too-few-public-methods, unused-import, undefined-variable, too-many-nested-blocks
import pathlib
try:
from networkx import DiGraph
except ImportError: # pragma: no cover
DiGraph = None # type: ignore
print('No libraries installed. Failed to import.')
from core_utils.article.article import Article
from core_utils.pipeline import (AbstractCoNLLUAnalyzer, CoNLLUDocument, LibraryWrapper,
PipelineProtocol, StanzaDocument, TreeNode)
class CorpusManager:
"""
Work with articles and store them.
"""
def __init__(self, path_to_raw_txt_data: pathlib.Path) -> None:
"""
Initialize an instance of the CorpusManager class.
Args:
path_to_raw_txt_data (pathlib.Path): Path to raw txt data
"""
def _validate_dataset(self) -> None:
"""
Validate folder with assets.
"""
def _scan_dataset(self) -> None:
"""
Register each dataset entry.
"""
def get_articles(self) -> dict:
"""
Get storage params.
Returns:
dict: Storage params
"""
class TextProcessingPipeline(PipelineProtocol):
"""
Preprocess and morphologically annotate sentences into the CONLL-U format.
"""
def __init__(
self, corpus_manager: CorpusManager, analyzer: LibraryWrapper | None = None
) -> None:
"""
Initialize an instance of the TextProcessingPipeline class.
Args:
corpus_manager (CorpusManager): CorpusManager instance
analyzer (LibraryWrapper | None): Analyzer instance
"""
def run(self) -> None:
"""
Perform basic preprocessing and write processed text to files.
"""
class UDPipeAnalyzer(LibraryWrapper):
"""
Wrapper for udpipe library.
"""
_analyzer: AbstractCoNLLUAnalyzer
def __init__(self) -> None:
"""
Initialize an instance of the UDPipeAnalyzer class.
"""
def _bootstrap(self) -> AbstractCoNLLUAnalyzer:
"""
Load and set up the UDPipe model.
Returns:
AbstractCoNLLUAnalyzer: Analyzer instance
"""
def analyze(self, texts: list[str]) -> list[StanzaDocument | str]:
"""
Process texts into CoNLL-U formatted markup.
Args:
texts (list[str]): Collection of texts
Returns:
list[StanzaDocument | str]: List of documents
"""
def to_conllu(self, article: Article) -> None:
"""
Save content to ConLLU format.
Args:
article (Article): Article containing information to save
"""
class StanzaAnalyzer(LibraryWrapper):
"""
Wrapper for stanza library.
"""
_analyzer: AbstractCoNLLUAnalyzer
def __init__(self) -> None:
"""
Initialize an instance of the StanzaAnalyzer class.
"""
def _bootstrap(self) -> AbstractCoNLLUAnalyzer:
"""
Load and set up the Stanza model.
Returns:
AbstractCoNLLUAnalyzer: Analyzer instance
"""
def analyze(self, texts: list[str]) -> list[StanzaDocument]:
"""
Process texts into CoNLL-U formatted markup.
Args:
texts (list[str]): Collection of texts
Returns:
list[StanzaDocument]: List of documents
"""
def to_conllu(self, article: Article) -> None:
"""
Save content to ConLLU format.
Args:
article (Article): Article containing information to save
"""
def from_conllu(self, article: Article) -> CoNLLUDocument:
"""
Load ConLLU content from article stored on disk.
Args:
article (Article): Article to load
Returns:
CoNLLUDocument: Document ready for parsing
"""
class POSFrequencyPipeline:
"""
Count frequencies of each POS in articles, update meta info and produce graphic report.
"""
def __init__(self, corpus_manager: CorpusManager, analyzer: LibraryWrapper) -> None:
"""
Initialize an instance of the POSFrequencyPipeline class.
Args:
corpus_manager (CorpusManager): CorpusManager instance
analyzer (LibraryWrapper): Analyzer instance
"""
def run(self) -> None:
"""
Visualize the frequencies of each part of speech.
"""
def _count_frequencies(self, article: Article) -> dict[str, int]:
"""
Count POS frequency in Article.
Args:
article (Article): Article instance
Returns:
dict[str, int]: POS frequencies
"""
class PatternSearchPipeline(PipelineProtocol):
"""
Search for the required syntactic pattern.
"""
def __init__(
self, corpus_manager: CorpusManager, analyzer: LibraryWrapper, pos: tuple[str, ...]
) -> None:
"""
Initialize an instance of the PatternSearchPipeline class.
Args:
corpus_manager (CorpusManager): CorpusManager instance
analyzer (LibraryWrapper): Analyzer instance
pos (tuple[str, ...]): Root, Dependency, Child part of speech
"""
def _make_graphs(self, doc: CoNLLUDocument) -> list[DiGraph]:
"""
Make graphs for a document.
Args:
doc (CoNLLUDocument): Document for patterns searching
Returns:
list[DiGraph]: Graphs for the sentences in the document
"""
def _add_children(
self, graph: DiGraph, subgraph_to_graph: dict, node_id: int, tree_node: TreeNode
) -> None:
"""
Add children to TreeNode.
Args:
graph (DiGraph): Sentence graph to search for a pattern
subgraph_to_graph (dict): Matched subgraph
node_id (int): ID of root node of the match
tree_node (TreeNode): Root node of the match
"""
def _find_pattern(self, doc_graphs: list) -> dict[int, list[TreeNode]]:
"""
Search for the required pattern.
Args:
doc_graphs (list): A list of graphs for the document
Returns:
dict[int, list[TreeNode]]: A dictionary with pattern matches
"""
def run(self) -> None:
"""
Search for a pattern in documents and writes found information to JSON file.
"""
def main() -> None:
"""
Entrypoint for pipeline module.
"""
if __name__ == "__main__":
main()