arekit 0.25.0__py3-none-any.whl → 0.25.1__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.
- arekit/common/data/storages/base.py +4 -15
- arekit/common/docs/parser.py +3 -30
- arekit/common/pipeline/items/base.py +1 -1
- arekit/common/utils.py +11 -8
- arekit/contrib/utils/data/storages/jsonl_based.py +2 -1
- arekit/contrib/utils/data/storages/pandas_based.py +2 -17
- arekit/contrib/utils/data/storages/row_cache.py +2 -1
- arekit/contrib/utils/data/storages/sqlite_based.py +2 -1
- arekit/contrib/utils/pipelines/text_opinion/extraction.py +5 -4
- {arekit-0.25.0.dist-info → arekit-0.25.1.dist-info}/METADATA +4 -5
- {arekit-0.25.0.dist-info → arekit-0.25.1.dist-info}/RECORD +15 -88
- arekit/common/data/input/repositories/__init__.py +0 -0
- arekit/common/data/input/repositories/base.py +0 -68
- arekit/common/data/input/repositories/sample.py +0 -22
- arekit/common/data/views/__init__.py +0 -0
- arekit/common/data/views/samples.py +0 -26
- arekit/common/service/__init__.py +0 -0
- arekit/common/service/sqlite.py +0 -36
- arekit/contrib/networks/__init__.py +0 -0
- arekit/contrib/networks/embedding.py +0 -149
- arekit/contrib/networks/embedding_io.py +0 -18
- arekit/contrib/networks/input/__init__.py +0 -0
- arekit/contrib/networks/input/const.py +0 -6
- arekit/contrib/networks/input/ctx_serialization.py +0 -28
- arekit/contrib/networks/input/embedding/__init__.py +0 -0
- arekit/contrib/networks/input/embedding/matrix.py +0 -29
- arekit/contrib/networks/input/embedding/offsets.py +0 -55
- arekit/contrib/networks/input/formatters/__init__.py +0 -0
- arekit/contrib/networks/input/formatters/pos_mapper.py +0 -22
- arekit/contrib/networks/input/providers/__init__.py +0 -0
- arekit/contrib/networks/input/providers/sample.py +0 -129
- arekit/contrib/networks/input/providers/term_connotation.py +0 -23
- arekit/contrib/networks/input/providers/text.py +0 -24
- arekit/contrib/networks/input/rows_parser.py +0 -47
- arekit/contrib/networks/input/term_types.py +0 -13
- arekit/contrib/networks/input/terms_mapping.py +0 -60
- arekit/contrib/networks/vectorizer.py +0 -6
- arekit/contrib/utils/data/readers/__init__.py +0 -0
- arekit/contrib/utils/data/readers/base.py +0 -7
- arekit/contrib/utils/data/readers/csv_pd.py +0 -38
- arekit/contrib/utils/data/readers/jsonl.py +0 -15
- arekit/contrib/utils/data/readers/sqlite.py +0 -14
- arekit/contrib/utils/data/service/__init__.py +0 -0
- arekit/contrib/utils/data/service/balance.py +0 -50
- arekit/contrib/utils/data/writers/csv_native.py +0 -63
- arekit/contrib/utils/data/writers/csv_pd.py +0 -40
- arekit/contrib/utils/data/writers/json_opennre.py +0 -132
- arekit/contrib/utils/data/writers/sqlite_native.py +0 -114
- arekit/contrib/utils/embeddings/__init__.py +0 -0
- arekit/contrib/utils/embeddings/rusvectores.py +0 -58
- arekit/contrib/utils/embeddings/tokens.py +0 -30
- arekit/contrib/utils/io_utils/embedding.py +0 -72
- arekit/contrib/utils/np_utils/__init__.py +0 -0
- arekit/contrib/utils/np_utils/embedding.py +0 -22
- arekit/contrib/utils/np_utils/npz_utils.py +0 -13
- arekit/contrib/utils/np_utils/vocab.py +0 -20
- arekit/contrib/utils/pipelines/items/sampling/__init__.py +0 -0
- arekit/contrib/utils/pipelines/items/sampling/base.py +0 -94
- arekit/contrib/utils/pipelines/items/sampling/networks.py +0 -55
- arekit/contrib/utils/pipelines/items/text/frames_lemmatized.py +0 -36
- arekit/contrib/utils/pipelines/items/text/frames_negation.py +0 -33
- arekit/contrib/utils/pipelines/items/text/tokenizer.py +0 -105
- arekit/contrib/utils/pipelines/items/text/translator.py +0 -136
- arekit/contrib/utils/processing/languages/__init__.py +0 -0
- arekit/contrib/utils/processing/languages/mods.py +0 -12
- arekit/contrib/utils/processing/languages/pos.py +0 -23
- arekit/contrib/utils/processing/languages/ru/__init__.py +0 -0
- arekit/contrib/utils/processing/languages/ru/cases.py +0 -78
- arekit/contrib/utils/processing/languages/ru/constants.py +0 -6
- arekit/contrib/utils/processing/languages/ru/mods.py +0 -13
- arekit/contrib/utils/processing/languages/ru/number.py +0 -23
- arekit/contrib/utils/processing/languages/ru/pos_service.py +0 -36
- arekit/contrib/utils/processing/lemmatization/__init__.py +0 -0
- arekit/contrib/utils/processing/lemmatization/mystem.py +0 -51
- arekit/contrib/utils/processing/pos/__init__.py +0 -0
- arekit/contrib/utils/processing/pos/base.py +0 -12
- arekit/contrib/utils/processing/pos/mystem_wrap.py +0 -134
- arekit/contrib/utils/processing/pos/russian.py +0 -10
- arekit/contrib/utils/processing/text/__init__.py +0 -0
- arekit/contrib/utils/processing/text/tokens.py +0 -127
- arekit/contrib/utils/serializer.py +0 -42
- arekit/contrib/utils/vectorizers/__init__.py +0 -0
- arekit/contrib/utils/vectorizers/bpe.py +0 -93
- arekit/contrib/utils/vectorizers/random_norm.py +0 -39
- {arekit-0.25.0.data → arekit-0.25.1.data}/data/logo.png +0 -0
- {arekit-0.25.0.dist-info → arekit-0.25.1.dist-info}/LICENSE +0 -0
- {arekit-0.25.0.dist-info → arekit-0.25.1.dist-info}/WHEEL +0 -0
- {arekit-0.25.0.dist-info → arekit-0.25.1.dist-info}/top_level.txt +0 -0
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
|
-
import numpy as np
|
|
4
|
-
|
|
5
|
-
logger = logging.getLogger(__name__)
|
|
6
|
-
logging.basicConfig(level=logging.INFO)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class VocabRepositoryUtils(object):
|
|
10
|
-
|
|
11
|
-
@staticmethod
|
|
12
|
-
def save(data, target):
|
|
13
|
-
logger.info("Saving vocabulary [size={size}]: {filepath}".format(size=len(data), filepath=target))
|
|
14
|
-
np.savetxt(target, data, fmt='%s')
|
|
15
|
-
|
|
16
|
-
@staticmethod
|
|
17
|
-
def load(source):
|
|
18
|
-
vocab = np.loadtxt(source, dtype=str, comments=None)
|
|
19
|
-
logger.info("Loading vocabulary [size={size}]: {filepath}".format(size=len(vocab), filepath=source))
|
|
20
|
-
return vocab
|
|
File without changes
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
from arekit.common.data.input.providers.rows.samples import BaseSampleRowProvider
|
|
2
|
-
from arekit.common.data.storages.base import BaseRowsStorage
|
|
3
|
-
from arekit.common.experiment.api.base_samples_io import BaseSamplesIO
|
|
4
|
-
from arekit.common.experiment.data_type import DataType
|
|
5
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
6
|
-
from arekit.contrib.utils.serializer import InputDataSerializationHelper
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class BaseSerializerPipelineItem(BasePipelineItem):
|
|
10
|
-
|
|
11
|
-
def __init__(self, rows_provider, samples_io, save_labels_func, storage, **kwargs):
|
|
12
|
-
""" sample_rows_formatter:
|
|
13
|
-
how we format input texts for a BERT model, for example:
|
|
14
|
-
- single text
|
|
15
|
-
- two sequences, separated by [SEP] token
|
|
16
|
-
|
|
17
|
-
save_labels_func: function
|
|
18
|
-
data_type -> bool
|
|
19
|
-
"""
|
|
20
|
-
assert(isinstance(rows_provider, BaseSampleRowProvider))
|
|
21
|
-
assert(isinstance(samples_io, BaseSamplesIO))
|
|
22
|
-
assert(callable(save_labels_func))
|
|
23
|
-
assert(isinstance(storage, BaseRowsStorage))
|
|
24
|
-
super(BaseSerializerPipelineItem, self).__init__(**kwargs)
|
|
25
|
-
|
|
26
|
-
self._rows_provider = rows_provider
|
|
27
|
-
self._samples_io = samples_io
|
|
28
|
-
self._save_labels_func = save_labels_func
|
|
29
|
-
self._storage = storage
|
|
30
|
-
|
|
31
|
-
def _serialize_iteration(self, data_type, pipeline, data_folding, doc_ids):
|
|
32
|
-
assert(isinstance(data_type, DataType))
|
|
33
|
-
assert(isinstance(pipeline, list))
|
|
34
|
-
assert(isinstance(data_folding, dict) or data_folding is None)
|
|
35
|
-
assert(isinstance(doc_ids, list) or doc_ids is None)
|
|
36
|
-
assert(doc_ids is not None or data_folding is not None)
|
|
37
|
-
|
|
38
|
-
repos = {
|
|
39
|
-
"sample": InputDataSerializationHelper.create_samples_repo(
|
|
40
|
-
keep_labels=self._save_labels_func(data_type),
|
|
41
|
-
rows_provider=self._rows_provider,
|
|
42
|
-
storage=self._storage),
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
writer_and_targets = {
|
|
46
|
-
"sample": (self._samples_io.Writer,
|
|
47
|
-
self._samples_io.create_target(data_type=data_type)),
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for description, repo in repos.items():
|
|
51
|
-
|
|
52
|
-
if data_folding is None:
|
|
53
|
-
# Consider only the predefined doc_ids.
|
|
54
|
-
doc_ids_iter = doc_ids
|
|
55
|
-
else:
|
|
56
|
-
# Take particular data_type.
|
|
57
|
-
doc_ids_iter = data_folding[data_type]
|
|
58
|
-
# Consider only predefined doc_ids.
|
|
59
|
-
if doc_ids is not None:
|
|
60
|
-
doc_ids_iter = set(doc_ids_iter).intersection(doc_ids)
|
|
61
|
-
|
|
62
|
-
InputDataSerializationHelper.fill_and_write(
|
|
63
|
-
repo=repo,
|
|
64
|
-
pipeline=pipeline,
|
|
65
|
-
doc_ids_iter=doc_ids_iter,
|
|
66
|
-
desc="{desc} [{data_type}]".format(desc=description, data_type=data_type),
|
|
67
|
-
writer=writer_and_targets[description][0],
|
|
68
|
-
target=writer_and_targets[description][1])
|
|
69
|
-
|
|
70
|
-
def _handle_iteration(self, data_type_pipelines, data_folding, doc_ids):
|
|
71
|
-
""" Performing data serialization for a particular iteration
|
|
72
|
-
"""
|
|
73
|
-
assert(isinstance(data_type_pipelines, dict))
|
|
74
|
-
for data_type, pipeline in data_type_pipelines.items():
|
|
75
|
-
self._serialize_iteration(data_type=data_type, pipeline=pipeline, data_folding=data_folding,
|
|
76
|
-
doc_ids=doc_ids)
|
|
77
|
-
|
|
78
|
-
def apply_core(self, input_data, pipeline_ctx):
|
|
79
|
-
"""
|
|
80
|
-
data_type_pipelines: dict of, for example:
|
|
81
|
-
{
|
|
82
|
-
DataType.Train: BasePipeline,
|
|
83
|
-
DataType.Test: BasePipeline
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
data_type_pipelines: doc_id -> parsed_doc -> annot -> opinion linkages
|
|
87
|
-
for example, function: sentiment_attitude_extraction_default_pipeline
|
|
88
|
-
doc_ids: optional
|
|
89
|
-
this parameter allows to limit amount of documents considered for sampling
|
|
90
|
-
"""
|
|
91
|
-
assert("data_type_pipelines" in pipeline_ctx)
|
|
92
|
-
self._handle_iteration(data_type_pipelines=pipeline_ctx.provide("data_type_pipelines"),
|
|
93
|
-
doc_ids=pipeline_ctx.provide_or_none("doc_ids"),
|
|
94
|
-
data_folding=pipeline_ctx.provide_or_none("data_folding"))
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
from arekit.contrib.networks.input.embedding.matrix import create_term_embedding_matrix
|
|
2
|
-
from arekit.contrib.networks.input.embedding.offsets import TermsEmbeddingOffsets
|
|
3
|
-
from arekit.contrib.networks.embedding import Embedding
|
|
4
|
-
from arekit.contrib.networks.input.providers.sample import NetworkSampleRowProvider
|
|
5
|
-
from arekit.contrib.utils.io_utils.embedding import NpEmbeddingIO
|
|
6
|
-
from arekit.contrib.utils.pipelines.items.sampling.base import BaseSerializerPipelineItem
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class NetworksInputSerializerPipelineItem(BaseSerializerPipelineItem):
|
|
10
|
-
|
|
11
|
-
def __init__(self, save_labels_func, rows_provider, samples_io, emb_io, storage, save_embedding=True, **kwargs):
|
|
12
|
-
""" This pipeline item allows to perform a data preparation for neural network models.
|
|
13
|
-
|
|
14
|
-
considering a list of the whole data_types with the related pipelines,
|
|
15
|
-
which are supported and required in a handler. It is necessary to know
|
|
16
|
-
data_types in advance as it allows to create a complete vocabulary of input terms,
|
|
17
|
-
with the related embeddings.
|
|
18
|
-
"""
|
|
19
|
-
assert(isinstance(emb_io, NpEmbeddingIO))
|
|
20
|
-
assert(isinstance(rows_provider, NetworkSampleRowProvider))
|
|
21
|
-
assert(isinstance(save_embedding, bool))
|
|
22
|
-
super(NetworksInputSerializerPipelineItem, self).__init__(
|
|
23
|
-
rows_provider=rows_provider,
|
|
24
|
-
samples_io=samples_io,
|
|
25
|
-
save_labels_func=save_labels_func,
|
|
26
|
-
storage=storage,
|
|
27
|
-
**kwargs)
|
|
28
|
-
|
|
29
|
-
self.__emb_io = emb_io
|
|
30
|
-
self.__save_embedding = save_embedding
|
|
31
|
-
|
|
32
|
-
def _handle_iteration(self, data_type_pipelines, data_folding, doc_ids):
|
|
33
|
-
""" Performing data serialization for a particular iteration
|
|
34
|
-
"""
|
|
35
|
-
assert(isinstance(data_type_pipelines, dict))
|
|
36
|
-
|
|
37
|
-
# Prepare for the present iteration.
|
|
38
|
-
self._rows_provider.clear_embedding_pairs()
|
|
39
|
-
|
|
40
|
-
super(NetworksInputSerializerPipelineItem, self)._handle_iteration(
|
|
41
|
-
data_type_pipelines=data_type_pipelines, data_folding=data_folding, doc_ids=doc_ids)
|
|
42
|
-
|
|
43
|
-
if not (self.__save_embedding and self._rows_provider.HasEmbeddingPairs):
|
|
44
|
-
return
|
|
45
|
-
|
|
46
|
-
# Save embedding information additionally.
|
|
47
|
-
term_embedding = Embedding.from_word_embedding_pairs_iter(self._rows_provider.iter_term_embedding_pairs())
|
|
48
|
-
embedding_matrix = create_term_embedding_matrix(term_embedding=term_embedding)
|
|
49
|
-
vocab = list(TermsEmbeddingOffsets.extract_vocab(words_embedding=term_embedding))
|
|
50
|
-
|
|
51
|
-
# Save embedding matrix
|
|
52
|
-
self.__emb_io.save_embedding(data=embedding_matrix)
|
|
53
|
-
self.__emb_io.save_vocab(data=vocab)
|
|
54
|
-
|
|
55
|
-
del embedding_matrix
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
from arekit.common.text.stemmer import Stemmer
|
|
2
|
-
from arekit.contrib.utils.pipelines.items.text.frames import FrameVariantsParser
|
|
3
|
-
from arekit.contrib.utils.processing.languages.ru.mods import RussianLanguageMods
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
class LemmasBasedFrameVariantsParser(FrameVariantsParser):
|
|
7
|
-
|
|
8
|
-
def __init__(self, frame_variants, stemmer, locale_mods=RussianLanguageMods, save_lemmas=False, **kwargs):
|
|
9
|
-
assert(isinstance(stemmer, Stemmer))
|
|
10
|
-
assert(isinstance(save_lemmas, bool))
|
|
11
|
-
super(LemmasBasedFrameVariantsParser, self).__init__(frame_variants=frame_variants, **kwargs)
|
|
12
|
-
|
|
13
|
-
self.__frame_variants = frame_variants
|
|
14
|
-
self.__stemmer = stemmer
|
|
15
|
-
self.__save_lemmas = save_lemmas
|
|
16
|
-
self.__max_variant_len = max([len(variant) for _, variant in frame_variants.iter_variants()])
|
|
17
|
-
self.__locale_mods = locale_mods
|
|
18
|
-
|
|
19
|
-
def __lemmatize_term(self, term):
|
|
20
|
-
# we first split onto words for lemmatization and then join all of them.
|
|
21
|
-
lemma = "".join(self.__stemmer.lemmatize_to_list(term))
|
|
22
|
-
# then we replace certain chars according to the locale restrictions.
|
|
23
|
-
return self.__locale_mods.replace_specific_word_chars(lemma)
|
|
24
|
-
|
|
25
|
-
def __provide_lemmatized_terms(self, terms):
|
|
26
|
-
"""
|
|
27
|
-
Compose a list of lemmatized versions of parsed_doc
|
|
28
|
-
PS: Might be significantly slow, depending on stemmer were used.
|
|
29
|
-
"""
|
|
30
|
-
assert(isinstance(terms, list))
|
|
31
|
-
return [self.__lemmatize_term(term) if isinstance(term, str) else term for term in terms]
|
|
32
|
-
|
|
33
|
-
def apply_core(self, input_data, pipeline_ctx):
|
|
34
|
-
lemmas = self.__provide_lemmatized_terms(input_data)
|
|
35
|
-
processed_it = self._iter_processed(terms=lemmas, origin=lemmas if self.__save_lemmas else input_data)
|
|
36
|
-
return list(processed_it)
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
from arekit.common.frames.text_variant import TextFrameVariant
|
|
2
|
-
from arekit.common.pipeline.context import PipelineContext
|
|
3
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
4
|
-
from arekit.contrib.utils.processing.languages.mods import BaseLanguageMods
|
|
5
|
-
from arekit.contrib.utils.processing.languages.ru.mods import RussianLanguageMods
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class FrameVariantsSentimentNegation(BasePipelineItem):
|
|
9
|
-
|
|
10
|
-
def __init__(self, locale_mods=RussianLanguageMods, **kwargs):
|
|
11
|
-
assert(issubclass(locale_mods, BaseLanguageMods))
|
|
12
|
-
super(FrameVariantsSentimentNegation, self).__init__(**kwargs)
|
|
13
|
-
self._locale_mods = locale_mods
|
|
14
|
-
|
|
15
|
-
@staticmethod
|
|
16
|
-
def __get_preposition(terms, index):
|
|
17
|
-
return terms[index-1] if index > 0 else None
|
|
18
|
-
|
|
19
|
-
def apply_core(self, input_data, pipeline_ctx):
|
|
20
|
-
assert(isinstance(input_data, list))
|
|
21
|
-
assert(isinstance(pipeline_ctx, PipelineContext))
|
|
22
|
-
|
|
23
|
-
for curr_ind, term in enumerate(input_data):
|
|
24
|
-
|
|
25
|
-
if not isinstance(term, TextFrameVariant):
|
|
26
|
-
continue
|
|
27
|
-
|
|
28
|
-
prep_term = self.__get_preposition(terms=input_data, index=curr_ind)
|
|
29
|
-
is_negated = self._locale_mods.is_negation_word(prep_term) if prep_term is not None else False
|
|
30
|
-
|
|
31
|
-
term.set_is_negated(is_negated)
|
|
32
|
-
|
|
33
|
-
return input_data
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
|
|
3
|
-
from arekit.common.context.token import Token
|
|
4
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
5
|
-
from arekit.common.utils import split_by_whitespaces
|
|
6
|
-
from arekit.contrib.utils.processing.text.tokens import Tokens
|
|
7
|
-
|
|
8
|
-
logger = logging.getLogger(__name__)
|
|
9
|
-
logger.setLevel(logging.INFO)
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class DefaultTextTokenizer(BasePipelineItem):
|
|
13
|
-
""" Default parser implementation.
|
|
14
|
-
"""
|
|
15
|
-
|
|
16
|
-
def __init__(self, keep_tokens=True, **kwargs):
|
|
17
|
-
super(DefaultTextTokenizer, self).__init__(**kwargs)
|
|
18
|
-
self.__keep_tokens = keep_tokens
|
|
19
|
-
|
|
20
|
-
# region protected methods
|
|
21
|
-
|
|
22
|
-
def apply_core(self, input_data, pipeline_ctx):
|
|
23
|
-
output_data = self.__process_parts(input_data)
|
|
24
|
-
if not self.__keep_tokens:
|
|
25
|
-
output_data = [word for word in output_data if not isinstance(word, Token)]
|
|
26
|
-
return output_data
|
|
27
|
-
|
|
28
|
-
# endregion
|
|
29
|
-
|
|
30
|
-
# region private static methods
|
|
31
|
-
|
|
32
|
-
def __process_parts(self, parts):
|
|
33
|
-
assert(isinstance(parts, list))
|
|
34
|
-
|
|
35
|
-
parsed = []
|
|
36
|
-
for part in parts:
|
|
37
|
-
|
|
38
|
-
if part is None:
|
|
39
|
-
continue
|
|
40
|
-
|
|
41
|
-
# Keep non str words as it is and try to parse str-based words.
|
|
42
|
-
processed = [part] if not isinstance(part, str) else \
|
|
43
|
-
self.__iter_processed_part(part=part)
|
|
44
|
-
|
|
45
|
-
parsed.extend(processed)
|
|
46
|
-
|
|
47
|
-
return parsed
|
|
48
|
-
|
|
49
|
-
def __iter_processed_part(self, part):
|
|
50
|
-
for word in split_by_whitespaces(part):
|
|
51
|
-
for term in self.__process_word(word):
|
|
52
|
-
yield term
|
|
53
|
-
|
|
54
|
-
def __process_word(self, word):
|
|
55
|
-
assert(isinstance(word, str))
|
|
56
|
-
return self.__split_tokens(word)
|
|
57
|
-
|
|
58
|
-
@staticmethod
|
|
59
|
-
def __split_tokens(term):
|
|
60
|
-
"""
|
|
61
|
-
Splitting off tokens from parsed_doc ending, i.e. for example:
|
|
62
|
-
term: "сказать,-" -> "(term: "сказать", ["COMMA_TOKEN", "DASH_TOKEN"])
|
|
63
|
-
return: (unicode or None, list)
|
|
64
|
-
modified term and list of extracted tokens.
|
|
65
|
-
"""
|
|
66
|
-
|
|
67
|
-
url = Tokens.try_create_url(term)
|
|
68
|
-
if url is not None:
|
|
69
|
-
return [url]
|
|
70
|
-
|
|
71
|
-
l = 0
|
|
72
|
-
words_and_tokens = []
|
|
73
|
-
while l < len(term):
|
|
74
|
-
|
|
75
|
-
# Token.
|
|
76
|
-
token = Tokens.try_create(term[l])
|
|
77
|
-
if token is not None:
|
|
78
|
-
if token.get_token_value() != Tokens.NEW_LINE:
|
|
79
|
-
words_and_tokens.append(token)
|
|
80
|
-
l += 1
|
|
81
|
-
|
|
82
|
-
# Number.
|
|
83
|
-
elif str.isdigit(term[l]):
|
|
84
|
-
k = l + 1
|
|
85
|
-
while k < len(term) and str.isdigit(term[k]):
|
|
86
|
-
k += 1
|
|
87
|
-
token = Tokens.try_create_number(term[l:k])
|
|
88
|
-
assert(token is not None)
|
|
89
|
-
words_and_tokens.append(token)
|
|
90
|
-
l = k
|
|
91
|
-
|
|
92
|
-
# Term.
|
|
93
|
-
else:
|
|
94
|
-
k = l + 1
|
|
95
|
-
while k < len(term):
|
|
96
|
-
token = Tokens.try_create(term[k])
|
|
97
|
-
if token is not None and token.get_token_value() != Tokens.DASH:
|
|
98
|
-
break
|
|
99
|
-
k += 1
|
|
100
|
-
words_and_tokens.append(term[l:k])
|
|
101
|
-
l = k
|
|
102
|
-
|
|
103
|
-
return words_and_tokens
|
|
104
|
-
|
|
105
|
-
# endregion
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
from arekit.common.data.input.providers.const import IDLE_MODE
|
|
2
|
-
from arekit.common.pipeline.conts import PARENT_CTX
|
|
3
|
-
from arekit.common.entities.base import Entity
|
|
4
|
-
from arekit.common.pipeline.context import PipelineContext
|
|
5
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
class MLTextTranslatorPipelineItem(BasePipelineItem):
|
|
9
|
-
""" Machine learning based translator pipeline item.
|
|
10
|
-
"""
|
|
11
|
-
|
|
12
|
-
def __init__(self, batch_translate_model, do_translate_entity=True, **kwargs):
|
|
13
|
-
""" Model, which is based on translation of the text,
|
|
14
|
-
represented as a list of words.
|
|
15
|
-
"""
|
|
16
|
-
super(MLTextTranslatorPipelineItem, self).__init__(**kwargs)
|
|
17
|
-
self.__do_translate_entity = do_translate_entity
|
|
18
|
-
self.__translate = batch_translate_model
|
|
19
|
-
|
|
20
|
-
def fast_most_accurate_approach(self, input_data, entity_placeholder_template="<entityTag={}/>"):
|
|
21
|
-
""" This approach assumes that the translation won't corrupt the original
|
|
22
|
-
meta-annotation for entities and objects mentioned in text.
|
|
23
|
-
"""
|
|
24
|
-
|
|
25
|
-
def __optionally_register(prts):
|
|
26
|
-
if len(prts) > 0:
|
|
27
|
-
content.append(" ".join(prts))
|
|
28
|
-
parts_to_join.clear()
|
|
29
|
-
|
|
30
|
-
content = []
|
|
31
|
-
origin_entities = []
|
|
32
|
-
parts_to_join = []
|
|
33
|
-
|
|
34
|
-
for part in input_data:
|
|
35
|
-
if isinstance(part, str) and part.strip():
|
|
36
|
-
parts_to_join.append(part)
|
|
37
|
-
elif isinstance(part, Entity):
|
|
38
|
-
entity_index = len(origin_entities)
|
|
39
|
-
parts_to_join.append(entity_placeholder_template.format(entity_index))
|
|
40
|
-
# Register entities information for further restoration.
|
|
41
|
-
origin_entities.append(part)
|
|
42
|
-
|
|
43
|
-
# Register original text with masked named entities.
|
|
44
|
-
__optionally_register(parts_to_join)
|
|
45
|
-
# Register all named entities in order of their appearance in text.
|
|
46
|
-
content.extend([e.Value for e in origin_entities])
|
|
47
|
-
|
|
48
|
-
# Compose text parts.
|
|
49
|
-
translated_parts = self.__translate(content)
|
|
50
|
-
|
|
51
|
-
if len(translated_parts) == 0:
|
|
52
|
-
return None
|
|
53
|
-
|
|
54
|
-
# Take the original text.
|
|
55
|
-
text = translated_parts[0]
|
|
56
|
-
for entity_index in range(len(origin_entities)):
|
|
57
|
-
if entity_placeholder_template.format(entity_index) not in text:
|
|
58
|
-
return None
|
|
59
|
-
|
|
60
|
-
# Enumerate entities.
|
|
61
|
-
from_ind = 0
|
|
62
|
-
text_parts = []
|
|
63
|
-
for entity_index, translated_value in enumerate(translated_parts[1:]):
|
|
64
|
-
entity_placeholder_instance = entity_placeholder_template.format(entity_index)
|
|
65
|
-
# Cropping text part.
|
|
66
|
-
to_ind = text.index(entity_placeholder_instance)
|
|
67
|
-
|
|
68
|
-
if self.__do_translate_entity:
|
|
69
|
-
origin_entities[entity_index].set_display_value(translated_value.strip())
|
|
70
|
-
|
|
71
|
-
# Register entities.
|
|
72
|
-
text_parts.append(text[from_ind:to_ind])
|
|
73
|
-
text_parts.append(origin_entities[entity_index])
|
|
74
|
-
# Update from index.
|
|
75
|
-
from_ind = to_ind + len(entity_placeholder_instance)
|
|
76
|
-
|
|
77
|
-
# Consider the remaining part.
|
|
78
|
-
text_parts.append(text[from_ind:])
|
|
79
|
-
return text_parts
|
|
80
|
-
|
|
81
|
-
def default_pre_part_splitting_approach(self, input_data):
|
|
82
|
-
""" This is the original strategy, based on the manually cropped named entities
|
|
83
|
-
before the actual translation call.
|
|
84
|
-
"""
|
|
85
|
-
|
|
86
|
-
def __optionally_register(prts):
|
|
87
|
-
if len(prts) > 0:
|
|
88
|
-
content.append(" ".join(prts))
|
|
89
|
-
parts_to_join.clear()
|
|
90
|
-
|
|
91
|
-
content = []
|
|
92
|
-
origin_entities = []
|
|
93
|
-
origin_entity_ind = []
|
|
94
|
-
parts_to_join = []
|
|
95
|
-
|
|
96
|
-
for _, part in enumerate(input_data):
|
|
97
|
-
if isinstance(part, str) and part.strip():
|
|
98
|
-
parts_to_join.append(part)
|
|
99
|
-
elif isinstance(part, Entity):
|
|
100
|
-
# Register first the prior parts were merged.
|
|
101
|
-
__optionally_register(parts_to_join)
|
|
102
|
-
# Register entities information for further restoration.
|
|
103
|
-
origin_entity_ind.append(len(content))
|
|
104
|
-
origin_entities.append(part)
|
|
105
|
-
content.append(part.Value)
|
|
106
|
-
|
|
107
|
-
__optionally_register(parts_to_join)
|
|
108
|
-
|
|
109
|
-
# Compose text parts.
|
|
110
|
-
translated_parts = self.__translate(content)
|
|
111
|
-
|
|
112
|
-
for entity_ind, entity_part_ind in enumerate(origin_entity_ind):
|
|
113
|
-
entity = origin_entities[entity_ind]
|
|
114
|
-
if self.__do_translate_entity:
|
|
115
|
-
entity.set_display_value(translated_parts[entity_part_ind].strip())
|
|
116
|
-
translated_parts[entity_part_ind] = entity
|
|
117
|
-
|
|
118
|
-
return translated_parts
|
|
119
|
-
|
|
120
|
-
def apply_core(self, input_data, pipeline_ctx):
|
|
121
|
-
assert(isinstance(pipeline_ctx, PipelineContext))
|
|
122
|
-
assert(isinstance(input_data, list))
|
|
123
|
-
|
|
124
|
-
# Check the pipeline state whether is an idle mode or not.
|
|
125
|
-
parent_ctx = pipeline_ctx.provide(PARENT_CTX)
|
|
126
|
-
idle_mode = parent_ctx.provide(IDLE_MODE)
|
|
127
|
-
|
|
128
|
-
# When pipeline utilized only for the assessing the expected amount
|
|
129
|
-
# of rows (common case of idle_mode), there is no need to perform
|
|
130
|
-
# translation.
|
|
131
|
-
if idle_mode:
|
|
132
|
-
return
|
|
133
|
-
|
|
134
|
-
fast_accurate = self.fast_most_accurate_approach(input_data)
|
|
135
|
-
return self.default_pre_part_splitting_approach(input_data) \
|
|
136
|
-
if fast_accurate is None else fast_accurate
|
|
File without changes
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
from enum import IntEnum
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class PartOfSpeechType(IntEnum):
|
|
5
|
-
|
|
6
|
-
NOUN = 1
|
|
7
|
-
ADV = 2
|
|
8
|
-
ADVPRO = 3
|
|
9
|
-
ANUM = 4
|
|
10
|
-
APRO = 5
|
|
11
|
-
COM = 6
|
|
12
|
-
CONJ = 7
|
|
13
|
-
INTJ = 8
|
|
14
|
-
NUM = 9
|
|
15
|
-
PART = 10
|
|
16
|
-
PR = 11
|
|
17
|
-
ADJ = 12
|
|
18
|
-
SPRO = 13
|
|
19
|
-
VERB = 14
|
|
20
|
-
|
|
21
|
-
Unknown = 15
|
|
22
|
-
|
|
23
|
-
Empty = 16
|
|
File without changes
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
from enum import Enum
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class RussianCases(Enum):
|
|
5
|
-
""" Падежи русского языка
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
""" не определено
|
|
9
|
-
"""
|
|
10
|
-
UNKN = 10
|
|
11
|
-
|
|
12
|
-
""" именительный
|
|
13
|
-
"""
|
|
14
|
-
NOM = 1
|
|
15
|
-
|
|
16
|
-
""" родительный
|
|
17
|
-
"""
|
|
18
|
-
GEN = 2
|
|
19
|
-
|
|
20
|
-
""" дательный
|
|
21
|
-
"""
|
|
22
|
-
DAT = 3
|
|
23
|
-
|
|
24
|
-
""" винительный
|
|
25
|
-
"""
|
|
26
|
-
ACC = 4
|
|
27
|
-
|
|
28
|
-
""" творительный
|
|
29
|
-
"""
|
|
30
|
-
INS = 5
|
|
31
|
-
|
|
32
|
-
""" предложный
|
|
33
|
-
"""
|
|
34
|
-
ABL = 6
|
|
35
|
-
|
|
36
|
-
""" партитив
|
|
37
|
-
"""
|
|
38
|
-
PART = 7
|
|
39
|
-
|
|
40
|
-
""" местный
|
|
41
|
-
"""
|
|
42
|
-
LOC = 8
|
|
43
|
-
|
|
44
|
-
""" звательный
|
|
45
|
-
"""
|
|
46
|
-
VOC = 9
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
class RussianCasesService(object):
|
|
50
|
-
|
|
51
|
-
__english = {
|
|
52
|
-
'nom': RussianCases.NOM,
|
|
53
|
-
'gen': RussianCases.GEN,
|
|
54
|
-
'dat': RussianCases.DAT,
|
|
55
|
-
'acc': RussianCases.ACC,
|
|
56
|
-
'ins': RussianCases.INS,
|
|
57
|
-
'abl': RussianCases.ABL,
|
|
58
|
-
'part': RussianCases.PART,
|
|
59
|
-
'loc': RussianCases.LOC,
|
|
60
|
-
'voc': RussianCases.VOC,
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
__mystem_russian = {
|
|
64
|
-
'им': RussianCases.NOM,
|
|
65
|
-
'род': RussianCases.GEN,
|
|
66
|
-
'дат': RussianCases.DAT,
|
|
67
|
-
'вин': RussianCases.ACC,
|
|
68
|
-
'твор': RussianCases.INS,
|
|
69
|
-
'пр': RussianCases.ABL,
|
|
70
|
-
'парт': RussianCases.PART,
|
|
71
|
-
'местн': RussianCases.LOC,
|
|
72
|
-
'зват': RussianCases.VOC,
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
@staticmethod
|
|
76
|
-
def iter_rus_mystem_tags():
|
|
77
|
-
for key, value in RussianCasesService.__mystem_russian.items():
|
|
78
|
-
yield key, value
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
from arekit.contrib.utils.processing.languages.mods import BaseLanguageMods
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class RussianLanguageMods(BaseLanguageMods):
|
|
5
|
-
|
|
6
|
-
@staticmethod
|
|
7
|
-
def replace_specific_word_chars(word):
|
|
8
|
-
assert(isinstance(word, str))
|
|
9
|
-
return word.replace('ё', 'e')
|
|
10
|
-
|
|
11
|
-
@staticmethod
|
|
12
|
-
def is_negation_word(word):
|
|
13
|
-
return word == 'не'
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
from enum import Enum
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class RussianNumberType(Enum):
|
|
5
|
-
|
|
6
|
-
UNKN = 3
|
|
7
|
-
|
|
8
|
-
Plural = 1
|
|
9
|
-
|
|
10
|
-
Single = 2
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class RussianNumberTypeService(object):
|
|
14
|
-
|
|
15
|
-
__russian = {
|
|
16
|
-
'ед': RussianNumberType.Single,
|
|
17
|
-
'мн': RussianNumberType.Plural
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
@staticmethod
|
|
21
|
-
def iter_rus_mystem_tags():
|
|
22
|
-
for key, value in RussianNumberTypeService.__russian.items():
|
|
23
|
-
yield key, value
|