arekit 0.25.0__py3-none-any.whl → 0.25.2__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/context/terms_mapper.py +5 -2
- arekit/common/data/input/providers/rows/samples.py +8 -12
- arekit/common/data/input/providers/sample/cropped.py +4 -3
- arekit/common/data/input/terms_mapper.py +4 -8
- arekit/common/data/storages/base.py +4 -18
- arekit/common/docs/entities_grouping.py +5 -3
- arekit/common/docs/parsed/base.py +3 -3
- arekit/common/docs/parsed/providers/base.py +3 -5
- arekit/common/docs/parsed/providers/entity_service.py +7 -28
- arekit/common/docs/parsed/providers/opinion_pairs.py +6 -6
- arekit/common/docs/parsed/providers/text_opinion_pairs.py +4 -4
- arekit/common/docs/parsed/service.py +2 -2
- arekit/common/docs/parser.py +3 -30
- arekit/common/model/labeling/single.py +7 -3
- arekit/common/opinions/annot/algo/pair_based.py +9 -5
- arekit/common/pipeline/base.py +0 -2
- arekit/common/pipeline/batching.py +0 -3
- arekit/common/pipeline/items/base.py +1 -1
- arekit/common/utils.py +11 -8
- arekit/contrib/bert/input/providers/cropped_sample.py +2 -5
- arekit/contrib/bert/terms/mapper.py +2 -2
- arekit/contrib/prompt/sample.py +2 -6
- arekit/contrib/utils/bert/samplers.py +4 -2
- arekit/contrib/utils/data/storages/jsonl_based.py +2 -1
- 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/annot/algo_based.py +8 -5
- arekit/contrib/utils/pipelines/text_opinion/extraction.py +16 -8
- {arekit-0.25.0.dist-info → arekit-0.25.2.dist-info}/METADATA +10 -8
- {arekit-0.25.0.dist-info → arekit-0.25.2.dist-info}/RECORD +34 -115
- {arekit-0.25.0.dist-info → arekit-0.25.2.dist-info}/WHEEL +1 -1
- 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/experiment/__init__.py +0 -0
- arekit/common/experiment/api/__init__.py +0 -0
- arekit/common/experiment/api/base_samples_io.py +0 -20
- arekit/common/experiment/data_type.py +0 -17
- 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/storages/pandas_based.py +0 -123
- 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/entities/formatters/str_display.py +0 -11
- 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/entities_default.py +0 -23
- 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/__init__.py +0 -0
- 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.2.data}/data/logo.png +0 -0
- {arekit-0.25.0.dist-info → arekit-0.25.2.dist-info}/LICENSE +0 -0
- {arekit-0.25.0.dist-info → arekit-0.25.2.dist-info}/top_level.txt +0 -0
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
from collections.abc import Iterable
|
|
2
2
|
|
|
3
3
|
from arekit.common.context.token import Token
|
|
4
|
-
from arekit.common.entities.base import Entity
|
|
5
4
|
from arekit.common.frames.text_variant import TextFrameVariant
|
|
6
5
|
|
|
7
6
|
|
|
8
7
|
class TextTermsMapper(object):
|
|
9
8
|
|
|
9
|
+
def __init__(self, is_entity_func):
|
|
10
|
+
assert(callable(is_entity_func))
|
|
11
|
+
self.__is_entity_func = is_entity_func
|
|
12
|
+
|
|
10
13
|
def iter_mapped(self, terms):
|
|
11
14
|
""" Performs mapping operation of each terms in a sequence
|
|
12
15
|
"""
|
|
@@ -22,7 +25,7 @@ class TextTermsMapper(object):
|
|
|
22
25
|
m_term = self.map_token(i, term)
|
|
23
26
|
elif isinstance(term, TextFrameVariant):
|
|
24
27
|
m_term = self.map_text_frame_variant(i, term)
|
|
25
|
-
elif
|
|
28
|
+
elif self.__is_entity_func(term):
|
|
26
29
|
m_term = self.map_entity(i, term)
|
|
27
30
|
else:
|
|
28
31
|
raise Exception("Unsupported type {}".format(term))
|
|
@@ -9,13 +9,11 @@ from arekit.common.data.input.providers.label.multiple import MultipleLabelProvi
|
|
|
9
9
|
from arekit.common.data.input.providers.rows.base import BaseRowProvider
|
|
10
10
|
from arekit.common.data.input.providers.text.single import BaseSingleTextProvider
|
|
11
11
|
from arekit.common.data.rows_fmt import create_base_column_fmt
|
|
12
|
-
from arekit.common.entities.base import Entity
|
|
13
|
-
from arekit.common.labels.base import Label
|
|
14
|
-
|
|
15
|
-
from arekit.common.linkage.text_opinions import TextOpinionsLinkage
|
|
16
12
|
from arekit.common.docs.parsed.base import ParsedDocument
|
|
17
13
|
from arekit.common.docs.parsed.providers.entity_service import EntityEndType, EntityServiceProvider
|
|
18
14
|
from arekit.common.docs.parsed.term_position import TermPositionTypes
|
|
15
|
+
from arekit.common.labels.base import Label
|
|
16
|
+
from arekit.common.linkage.text_opinions import TextOpinionsLinkage
|
|
19
17
|
from arekit.common.text_opinions.base import TextOpinion
|
|
20
18
|
|
|
21
19
|
|
|
@@ -26,13 +24,15 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
26
24
|
""" Rows provider for samples storage.
|
|
27
25
|
"""
|
|
28
26
|
|
|
29
|
-
def __init__(self, label_provider, text_provider):
|
|
27
|
+
def __init__(self, is_entity_func, label_provider, text_provider):
|
|
28
|
+
assert(callable(is_entity_func))
|
|
30
29
|
assert(isinstance(label_provider, LabelProvider))
|
|
31
30
|
assert(isinstance(text_provider, BaseSingleTextProvider))
|
|
32
31
|
super(BaseSampleRowProvider, self).__init__()
|
|
33
32
|
|
|
34
33
|
self._label_provider = label_provider
|
|
35
34
|
self.__text_provider = text_provider
|
|
35
|
+
self.__is_entity_func = is_entity_func
|
|
36
36
|
self.__instances_provider = self.__create_instances_provider(label_provider)
|
|
37
37
|
self.__store_labels = None
|
|
38
38
|
self._val_fmt = create_base_column_fmt(fmt_type="writer")
|
|
@@ -65,7 +65,7 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
65
65
|
parsed_doc=parsed_doc, sentence_ind=sentence_ind, s_ind=s_ind, t_ind=t_ind)
|
|
66
66
|
|
|
67
67
|
# Entity indices from the related context.
|
|
68
|
-
entities = list(filter(
|
|
68
|
+
entities = list(filter(self.__is_entity_func, sentence_terms))
|
|
69
69
|
|
|
70
70
|
# Values mapping.
|
|
71
71
|
vm = {
|
|
@@ -76,7 +76,7 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
76
76
|
const.SENT_IND: sentence_ind,
|
|
77
77
|
const.ENTITY_VALUES: entities,
|
|
78
78
|
const.ENTITY_TYPES: entities,
|
|
79
|
-
const.ENTITIES: [str(i) for i, t in enumerate(sentence_terms) if
|
|
79
|
+
const.ENTITIES: [str(i) for i, t in enumerate(sentence_terms) if self.__is_entity_func(t)],
|
|
80
80
|
const.S_IND: actual_s_ind,
|
|
81
81
|
const.T_IND: actual_t_ind,
|
|
82
82
|
const.LABEL_UINT: None,
|
|
@@ -143,9 +143,6 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
143
143
|
|
|
144
144
|
def __provide_rows(self, row_dict, parsed_doc, entity_service,
|
|
145
145
|
text_opinion_linkage, index_in_linked, idle_mode):
|
|
146
|
-
"""
|
|
147
|
-
Providing Rows depending on row_id_formatter type
|
|
148
|
-
"""
|
|
149
146
|
assert(isinstance(parsed_doc, ParsedDocument))
|
|
150
147
|
assert(isinstance(row_dict, OrderedDict))
|
|
151
148
|
assert(isinstance(text_opinion_linkage, TextOpinionsLinkage))
|
|
@@ -153,7 +150,6 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
153
150
|
etalon_label = self.__instances_provider.provide_label(text_opinion_linkage)
|
|
154
151
|
for instance in self.__instances_provider.iter_instances(text_opinion_linkage):
|
|
155
152
|
yield self.__create_row(row=row_dict,
|
|
156
|
-
row_id=0,
|
|
157
153
|
parsed_doc=parsed_doc,
|
|
158
154
|
entity_service=entity_service,
|
|
159
155
|
text_opinions_linkage=instance,
|
|
@@ -162,7 +158,7 @@ class BaseSampleRowProvider(BaseRowProvider):
|
|
|
162
158
|
etalon_label=etalon_label,
|
|
163
159
|
idle_mode=idle_mode)
|
|
164
160
|
|
|
165
|
-
def __create_row(self, row,
|
|
161
|
+
def __create_row(self, row, parsed_doc, entity_service, text_opinions_linkage,
|
|
166
162
|
index_in_linked, etalon_label, idle_mode):
|
|
167
163
|
"""
|
|
168
164
|
Composing row in following format:
|
|
@@ -8,10 +8,11 @@ class CroppedSampleRowProvider(BaseSampleRowProvider):
|
|
|
8
8
|
attitude inside.
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
|
-
def __init__(self, crop_window_size, label_scaler,
|
|
11
|
+
def __init__(self, crop_window_size, label_scaler, **kwargs):
|
|
12
12
|
assert(isinstance(crop_window_size, int) and crop_window_size > 0)
|
|
13
|
-
super(CroppedSampleRowProvider, self).__init__(
|
|
14
|
-
|
|
13
|
+
super(CroppedSampleRowProvider, self).__init__(
|
|
14
|
+
label_provider=MultipleLabelProvider(label_scaler),
|
|
15
|
+
**kwargs)
|
|
15
16
|
self.__crop_window_size = crop_window_size
|
|
16
17
|
|
|
17
18
|
@staticmethod
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from arekit.common.context.terms_mapper import TextTermsMapper
|
|
2
2
|
from arekit.common.context.token import Token
|
|
3
|
-
from arekit.common.entities.base import Entity
|
|
4
3
|
from arekit.common.entities.str_fmt import StringEntitiesFormatter
|
|
5
4
|
from arekit.common.entities.types import OpinionEntityType
|
|
6
5
|
from arekit.common.frames.text_variant import TextFrameVariant
|
|
@@ -12,9 +11,12 @@ class OpinionContainingTextTermsMapper(TextTermsMapper):
|
|
|
12
11
|
The latter might be utilized with synonyms collection
|
|
13
12
|
"""
|
|
14
13
|
|
|
15
|
-
def __init__(self, entity_formatter):
|
|
14
|
+
def __init__(self, entity_formatter, entity_group_ind_func, **kwargs):
|
|
16
15
|
assert(isinstance(entity_formatter, StringEntitiesFormatter))
|
|
16
|
+
assert(callable(entity_group_ind_func))
|
|
17
|
+
super(OpinionContainingTextTermsMapper, self).__init__(**kwargs)
|
|
17
18
|
self.__entities_formatter = entity_formatter
|
|
19
|
+
self.__syn_group = entity_group_ind_func
|
|
18
20
|
self.__s_ind = None
|
|
19
21
|
self.__t_ind = None
|
|
20
22
|
self.__s_group = None
|
|
@@ -24,12 +26,6 @@ class OpinionContainingTextTermsMapper(TextTermsMapper):
|
|
|
24
26
|
def StringEntitiesFormatter(self):
|
|
25
27
|
return self.__entities_formatter
|
|
26
28
|
|
|
27
|
-
def __syn_group(self, entity):
|
|
28
|
-
""" Note: here we guarantee that entity has GroupIndex.
|
|
29
|
-
"""
|
|
30
|
-
assert(isinstance(entity, Entity))
|
|
31
|
-
return entity.GroupIndex if entity is not None else None
|
|
32
|
-
|
|
33
29
|
def set_s_ind(self, s_ind):
|
|
34
30
|
assert(isinstance(s_ind, int))
|
|
35
31
|
self.__s_ind = s_ind
|
|
@@ -10,6 +10,9 @@ logger = logging.getLogger(__name__)
|
|
|
10
10
|
|
|
11
11
|
class BaseRowsStorage(object):
|
|
12
12
|
|
|
13
|
+
def __init__(self, log_out=None):
|
|
14
|
+
self.__log_out = log_out
|
|
15
|
+
|
|
13
16
|
# region protected methods
|
|
14
17
|
|
|
15
18
|
def _begin_filling_row(self, row_ind):
|
|
@@ -31,27 +34,9 @@ class BaseRowsStorage(object):
|
|
|
31
34
|
def _get_rows_count(self):
|
|
32
35
|
raise NotImplemented()
|
|
33
36
|
|
|
34
|
-
def find_by_value(self, column_name, value):
|
|
35
|
-
raise NotImplemented()
|
|
36
|
-
|
|
37
|
-
def find_first_by_value(self, column_name, value):
|
|
38
|
-
raise NotImplemented()
|
|
39
|
-
|
|
40
|
-
def iter_column_values(self, column_name, dtype=None):
|
|
41
|
-
raise NotImplemented()
|
|
42
|
-
|
|
43
|
-
def get_row(self, row_index):
|
|
44
|
-
raise NotImplemented()
|
|
45
|
-
|
|
46
|
-
def get_cell(self, row_index, column_name):
|
|
47
|
-
raise NotImplemented()
|
|
48
|
-
|
|
49
37
|
def init_empty(self, columns_provider):
|
|
50
38
|
raise NotImplemented()
|
|
51
39
|
|
|
52
|
-
def iter_shuffled(self):
|
|
53
|
-
raise NotImplemented()
|
|
54
|
-
|
|
55
40
|
def iter_column_names(self):
|
|
56
41
|
raise NotImplemented()
|
|
57
42
|
|
|
@@ -81,6 +66,7 @@ class BaseRowsStorage(object):
|
|
|
81
66
|
condition_func=lambda item: not isinstance(item[1], MetaEmptyLinkedDataWrapper),
|
|
82
67
|
postfix_func=postfix_func,
|
|
83
68
|
desc="{fmt}".format(fmt=desc),
|
|
69
|
+
file=self.__log_out,
|
|
84
70
|
total=rows_count)
|
|
85
71
|
|
|
86
72
|
for row_index, item in enumerate(pbar_it):
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
from arekit.common.entities.base import Entity
|
|
2
1
|
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
class EntitiesGroupingPipelineItem(BasePipelineItem):
|
|
6
5
|
|
|
7
|
-
def __init__(self, value_to_group_id_func, **kwargs):
|
|
6
|
+
def __init__(self, value_to_group_id_func, is_entity_func, **kwargs):
|
|
8
7
|
assert(callable(value_to_group_id_func))
|
|
8
|
+
assert(callable(is_entity_func))
|
|
9
9
|
super(EntitiesGroupingPipelineItem, self).__init__(**kwargs)
|
|
10
|
+
|
|
10
11
|
self.__value_to_group_id_func = value_to_group_id_func
|
|
12
|
+
self.__is_entity_func = is_entity_func
|
|
11
13
|
|
|
12
14
|
def apply_core(self, input_data, pipeline_ctx):
|
|
13
15
|
assert(isinstance(input_data, list))
|
|
14
16
|
|
|
15
|
-
for entity in filter(lambda term:
|
|
17
|
+
for entity in filter(lambda term: self.__is_entity_func(term), input_data):
|
|
16
18
|
group_index = self.__value_to_group_id_func(entity.Value)
|
|
17
19
|
entity.set_group_index(group_index)
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
from collections.abc import Iterable
|
|
2
2
|
|
|
3
|
-
from arekit.common.entities.base import Entity
|
|
4
3
|
from arekit.common.text.enums import TermFormat
|
|
5
4
|
from arekit.common.text.parsed import BaseParsedText
|
|
6
5
|
|
|
@@ -73,8 +72,9 @@ class ParsedDocument(object):
|
|
|
73
72
|
assert(isinstance(s_ind, int))
|
|
74
73
|
return self.__parsed_sentences[s_ind]
|
|
75
74
|
|
|
76
|
-
def iter_entities(self):
|
|
77
|
-
|
|
75
|
+
def iter_entities(self, is_entity_func):
|
|
76
|
+
assert(callable(is_entity_func))
|
|
77
|
+
for entity in self.__iter_all_raw_terms(term_only=True, filter_func=is_entity_func):
|
|
78
78
|
yield entity
|
|
79
79
|
|
|
80
80
|
def iter_terms(self, filter_func=None, term_only=True):
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
from arekit.common.entities.base import Entity
|
|
2
1
|
from arekit.common.docs.entity import DocumentEntity
|
|
3
2
|
from arekit.common.docs.parsed.base import ParsedDocument
|
|
4
3
|
|
|
@@ -6,7 +5,7 @@ from arekit.common.docs.parsed.base import ParsedDocument
|
|
|
6
5
|
class BaseParsedDocumentServiceProvider(object):
|
|
7
6
|
|
|
8
7
|
def __init__(self, entity_index_func=None):
|
|
9
|
-
""" Outside
|
|
8
|
+
""" Outside entity indexing function
|
|
10
9
|
entity_index_func: provides id for a given entity, i.e.
|
|
11
10
|
func(entity) -> int (id)
|
|
12
11
|
"""
|
|
@@ -19,7 +18,7 @@ class BaseParsedDocumentServiceProvider(object):
|
|
|
19
18
|
def Name(self):
|
|
20
19
|
raise NotImplementedError()
|
|
21
20
|
|
|
22
|
-
def init_parsed_doc(self, parsed_doc):
|
|
21
|
+
def init_parsed_doc(self, parsed_doc, is_entity_func):
|
|
23
22
|
assert(isinstance(parsed_doc, ParsedDocument))
|
|
24
23
|
|
|
25
24
|
def __iter_childs_and_root_node(entity):
|
|
@@ -37,7 +36,7 @@ class BaseParsedDocumentServiceProvider(object):
|
|
|
37
36
|
self.__entity_map.clear()
|
|
38
37
|
|
|
39
38
|
current_id = 0
|
|
40
|
-
for _, entity in enumerate(parsed_doc.iter_entities()):
|
|
39
|
+
for _, entity in enumerate(parsed_doc.iter_entities(is_entity_func=is_entity_func)):
|
|
41
40
|
|
|
42
41
|
child_doc_entities = []
|
|
43
42
|
for tree_entity, is_child in __iter_childs_and_root_node(entity):
|
|
@@ -61,7 +60,6 @@ class BaseParsedDocumentServiceProvider(object):
|
|
|
61
60
|
def get_document_entity(self, entity):
|
|
62
61
|
""" Maps entity to the related one with DocumentEntity type
|
|
63
62
|
"""
|
|
64
|
-
assert(isinstance(entity, Entity))
|
|
65
63
|
return self.__entity_map[self.__entity_index_func(entity)]
|
|
66
64
|
|
|
67
65
|
def contains_entity(self, entity):
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
2
|
|
|
3
|
-
from arekit.common.entities.base import Entity
|
|
4
3
|
from arekit.common.docs.entity import DocumentEntity
|
|
5
|
-
from arekit.common.docs.parsed.base import ParsedDocument
|
|
6
4
|
from arekit.common.docs.parsed.providers.base import BaseParsedDocumentServiceProvider
|
|
7
5
|
from arekit.common.docs.parsed.term_position import TermPositionTypes, TermPosition
|
|
8
6
|
from arekit.common.text_opinions.base import TextOpinion
|
|
@@ -41,9 +39,8 @@ class EntityServiceProvider(BaseParsedDocumentServiceProvider):
|
|
|
41
39
|
|
|
42
40
|
NAME = "entity-service-provider"
|
|
43
41
|
|
|
44
|
-
def __init__(self,
|
|
45
|
-
|
|
46
|
-
super(EntityServiceProvider, self).__init__(entity_index_func=entity_index_func)
|
|
42
|
+
def __init__(self, **kwargs):
|
|
43
|
+
super(EntityServiceProvider, self).__init__(**kwargs)
|
|
47
44
|
# Initialize API.
|
|
48
45
|
self.__iter_raw_terms_func = None
|
|
49
46
|
# Initialize entity positions.
|
|
@@ -53,24 +50,16 @@ class EntityServiceProvider(BaseParsedDocumentServiceProvider):
|
|
|
53
50
|
def Name(self):
|
|
54
51
|
return self.NAME
|
|
55
52
|
|
|
56
|
-
def init_parsed_doc(self, parsed_doc):
|
|
57
|
-
super(EntityServiceProvider, self).init_parsed_doc(parsed_doc)
|
|
58
|
-
assert(isinstance(parsed_doc, ParsedDocument))
|
|
53
|
+
def init_parsed_doc(self, parsed_doc, is_entity_func):
|
|
54
|
+
super(EntityServiceProvider, self).init_parsed_doc(parsed_doc=parsed_doc, is_entity_func=is_entity_func)
|
|
59
55
|
self.__iter_raw_terms_func = lambda: parsed_doc.iter_terms(filter_func=None, term_only=False)
|
|
60
|
-
self.__entity_positions = self.__calculate_entity_positions()
|
|
61
|
-
|
|
62
|
-
# region public 'extract' methods
|
|
63
|
-
|
|
64
|
-
def extract_entity_value(self, text_opinion, end_type):
|
|
65
|
-
return self.__extract_entity_value(text_opinion=text_opinion, end_type=end_type)
|
|
56
|
+
self.__entity_positions = self.__calculate_entity_positions(is_entity_func=is_entity_func)
|
|
66
57
|
|
|
67
58
|
def extract_entity_position(self, text_opinion, end_type, position_type=None):
|
|
68
59
|
return self.__get_entity_position(text_opinion=text_opinion,
|
|
69
60
|
end_type=end_type,
|
|
70
61
|
position_type=position_type)
|
|
71
62
|
|
|
72
|
-
# endregion
|
|
73
|
-
|
|
74
63
|
# region public 'calculate' methods
|
|
75
64
|
|
|
76
65
|
@staticmethod
|
|
@@ -112,20 +101,10 @@ class EntityServiceProvider(BaseParsedDocumentServiceProvider):
|
|
|
112
101
|
|
|
113
102
|
return e_pos.get_index(position_type)
|
|
114
103
|
|
|
115
|
-
def get_entity_value(self, id_in_document):
|
|
116
|
-
entity = self._doc_entities[id_in_document]
|
|
117
|
-
assert(isinstance(entity, Entity))
|
|
118
|
-
return entity.Value
|
|
119
|
-
|
|
120
104
|
# endregion
|
|
121
105
|
|
|
122
106
|
# region private methods
|
|
123
107
|
|
|
124
|
-
def __extract_entity_value(self, text_opinion, end_type):
|
|
125
|
-
assert(isinstance(text_opinion, TextOpinion))
|
|
126
|
-
end_id = self.__get_end_id(text_opinion=text_opinion, end_type=end_type)
|
|
127
|
-
return self.get_entity_value(end_id)
|
|
128
|
-
|
|
129
108
|
def __get_entity_position(self, text_opinion, end_type, position_type=None):
|
|
130
109
|
assert(isinstance(text_opinion, TextOpinion))
|
|
131
110
|
end_id = self.__get_end_id(text_opinion=text_opinion, end_type=end_type)
|
|
@@ -147,7 +126,7 @@ class EntityServiceProvider(BaseParsedDocumentServiceProvider):
|
|
|
147
126
|
assert(end_type == EntityEndType.Source or end_type == EntityEndType.Target)
|
|
148
127
|
return text_opinion.SourceId if end_type == EntityEndType.Source else text_opinion.TargetId
|
|
149
128
|
|
|
150
|
-
def __calculate_entity_positions(self):
|
|
129
|
+
def __calculate_entity_positions(self, is_entity_func):
|
|
151
130
|
""" Note: here we consider the same order as in self._entities.
|
|
152
131
|
"""
|
|
153
132
|
t_ind_in_doc = -1
|
|
@@ -157,7 +136,7 @@ class EntityServiceProvider(BaseParsedDocumentServiceProvider):
|
|
|
157
136
|
|
|
158
137
|
t_ind_in_doc += 1
|
|
159
138
|
|
|
160
|
-
if not
|
|
139
|
+
if not is_entity_func(term):
|
|
161
140
|
continue
|
|
162
141
|
|
|
163
142
|
# We consider that entities within a single tree has the same positions.
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
from arekit.common.entities.base import Entity
|
|
2
1
|
from arekit.common.docs.parsed.providers.base_pairs import BasePairProvider
|
|
3
2
|
from arekit.common.opinions.base import Opinion
|
|
4
3
|
|
|
@@ -7,14 +6,15 @@ class OpinionPairsProvider(BasePairProvider):
|
|
|
7
6
|
|
|
8
7
|
NAME = "opinion-pairs-provider"
|
|
9
8
|
|
|
9
|
+
def __init__(self, entity_value_func, **kwargs):
|
|
10
|
+
super(OpinionPairsProvider, self).__init__(**kwargs)
|
|
11
|
+
self.__entity_value_func = entity_value_func
|
|
12
|
+
|
|
10
13
|
@property
|
|
11
14
|
def Name(self):
|
|
12
15
|
return self.NAME
|
|
13
16
|
|
|
14
17
|
def _create_pair(self, source_entity, target_entity, label):
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return Opinion(source_value=source_entity.Value,
|
|
19
|
-
target_value=target_entity.Value,
|
|
18
|
+
return Opinion(source_value=self.__entity_value_func(source_entity),
|
|
19
|
+
target_value=self.__entity_value_func(target_entity),
|
|
20
20
|
label=label)
|
|
@@ -16,8 +16,8 @@ class TextOpinionPairsProvider(BasePairProvider):
|
|
|
16
16
|
|
|
17
17
|
NAME = "text-opinion-pairs-provider"
|
|
18
18
|
|
|
19
|
-
def __init__(self, value_to_group_id_func):
|
|
20
|
-
super(TextOpinionPairsProvider, self).__init__()
|
|
19
|
+
def __init__(self, value_to_group_id_func, **kwargs):
|
|
20
|
+
super(TextOpinionPairsProvider, self).__init__(**kwargs)
|
|
21
21
|
self.__value_to_group_id_func = value_to_group_id_func
|
|
22
22
|
self.__doc_id = None
|
|
23
23
|
self.__entities_collection = None
|
|
@@ -36,8 +36,8 @@ class TextOpinionPairsProvider(BasePairProvider):
|
|
|
36
36
|
label=label,
|
|
37
37
|
text_opinion_id=None)
|
|
38
38
|
|
|
39
|
-
def init_parsed_doc(self, parsed_doc):
|
|
40
|
-
super(TextOpinionPairsProvider, self).init_parsed_doc(parsed_doc)
|
|
39
|
+
def init_parsed_doc(self, parsed_doc, is_entity_func):
|
|
40
|
+
super(TextOpinionPairsProvider, self).init_parsed_doc(parsed_doc=parsed_doc, is_entity_func=is_entity_func)
|
|
41
41
|
self.__doc_id = parsed_doc.RelatedDocID
|
|
42
42
|
self.__entities_collection = EntityCollection(
|
|
43
43
|
entities=list(self._doc_entities),
|
|
@@ -6,7 +6,7 @@ class ParsedDocumentService(object):
|
|
|
6
6
|
""" Represents a collection of providers, combined with the parsed doc.
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
|
-
def __init__(self, parsed_doc, providers):
|
|
9
|
+
def __init__(self, parsed_doc, providers, is_entity_func):
|
|
10
10
|
assert(isinstance(parsed_doc, ParsedDocument))
|
|
11
11
|
assert(isinstance(providers, list))
|
|
12
12
|
self.__parsed_doc = parsed_doc
|
|
@@ -20,7 +20,7 @@ class ParsedDocumentService(object):
|
|
|
20
20
|
self.__providers[provider.Name] = provider
|
|
21
21
|
|
|
22
22
|
# Post initialize with the related parsed doc.
|
|
23
|
-
provider.init_parsed_doc(self.__parsed_doc)
|
|
23
|
+
provider.init_parsed_doc(self.__parsed_doc, is_entity_func=is_entity_func)
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
@property
|
arekit/common/docs/parser.py
CHANGED
|
@@ -1,42 +1,14 @@
|
|
|
1
|
-
from tqdm import tqdm
|
|
2
1
|
from arekit.common.docs.base import Document
|
|
3
2
|
from arekit.common.docs.parsed.base import ParsedDocument
|
|
4
|
-
from arekit.common.pipeline.base import BasePipelineLauncher
|
|
5
3
|
from arekit.common.pipeline.batching import BatchingPipelineLauncher
|
|
6
4
|
from arekit.common.pipeline.context import PipelineContext
|
|
7
5
|
from arekit.common.pipeline.utils import BatchIterator
|
|
8
6
|
from arekit.common.text.parsed import BaseParsedText
|
|
7
|
+
from arekit.common.utils import progress_bar_defined
|
|
9
8
|
|
|
10
9
|
|
|
11
10
|
class DocumentParsers(object):
|
|
12
11
|
|
|
13
|
-
@staticmethod
|
|
14
|
-
def parse(doc, pipeline_items, parent_ppl_ctx=None, src_key="input", show_progress=False):
|
|
15
|
-
""" This document parser is based on single text parts (sentences)
|
|
16
|
-
that passes sequentially through the pipeline of transformations.
|
|
17
|
-
"""
|
|
18
|
-
assert(isinstance(doc, Document))
|
|
19
|
-
assert(isinstance(pipeline_items, list))
|
|
20
|
-
assert(isinstance(parent_ppl_ctx, PipelineContext) or parent_ppl_ctx is None)
|
|
21
|
-
|
|
22
|
-
parsed_sentences = []
|
|
23
|
-
|
|
24
|
-
data_it = range(doc.SentencesCount)
|
|
25
|
-
progress_it = tqdm(data_it, disable=not show_progress)
|
|
26
|
-
|
|
27
|
-
for sent_ind in progress_it:
|
|
28
|
-
|
|
29
|
-
# Composing the context from a single sentence.
|
|
30
|
-
ctx = PipelineContext({src_key: doc.get_sentence(sent_ind)}, parent_ctx=parent_ppl_ctx)
|
|
31
|
-
|
|
32
|
-
# Apply all the operations.
|
|
33
|
-
BasePipelineLauncher.run(pipeline=pipeline_items, pipeline_ctx=ctx, src_key=src_key)
|
|
34
|
-
|
|
35
|
-
# Collecting the result.
|
|
36
|
-
parsed_sentences.append(BaseParsedText(terms=ctx.provide("result")))
|
|
37
|
-
|
|
38
|
-
return ParsedDocument(doc_id=doc.ID, parsed_sentences=parsed_sentences)
|
|
39
|
-
|
|
40
12
|
@staticmethod
|
|
41
13
|
def parse_batch(doc, pipeline_items, batch_size, parent_ppl_ctx=None, src_key="input", show_progress=False):
|
|
42
14
|
""" This document parser is based on batch of sentences.
|
|
@@ -49,7 +21,8 @@ class DocumentParsers(object):
|
|
|
49
21
|
parsed_sentences = []
|
|
50
22
|
|
|
51
23
|
data_it = BatchIterator(data_iter=iter(range(doc.SentencesCount)), batch_size=batch_size)
|
|
52
|
-
progress_it =
|
|
24
|
+
progress_it = progress_bar_defined(data_it, total=round(doc.SentencesCount / batch_size),
|
|
25
|
+
disable=not show_progress)
|
|
53
26
|
|
|
54
27
|
for batch in progress_it:
|
|
55
28
|
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
|
|
3
1
|
from arekit.common.model.labeling.base import LabelsHelper
|
|
4
2
|
from arekit.common.model.labeling.modes import LabelCalculationMode
|
|
5
3
|
|
|
6
4
|
|
|
7
5
|
class SingleLabelsHelper(LabelsHelper):
|
|
8
6
|
|
|
7
|
+
@staticmethod
|
|
8
|
+
def __sign(x):
|
|
9
|
+
if x == 0:
|
|
10
|
+
return 0
|
|
11
|
+
return -1 if x < 0 else 1
|
|
12
|
+
|
|
9
13
|
def aggregate_labels(self, labels_list, label_calc_mode):
|
|
10
14
|
assert(isinstance(labels_list, list))
|
|
11
15
|
assert(isinstance(label_calc_mode, LabelCalculationMode))
|
|
@@ -18,7 +22,7 @@ class SingleLabelsHelper(LabelsHelper):
|
|
|
18
22
|
if label_calc_mode == LabelCalculationMode.AVERAGE:
|
|
19
23
|
int_labels = [self._label_scaler.label_to_int(label)
|
|
20
24
|
for label in labels_list]
|
|
21
|
-
label = self._label_scaler.int_to_label(
|
|
25
|
+
label = self._label_scaler.int_to_label(SingleLabelsHelper.__sign(sum(int_labels)))
|
|
22
26
|
|
|
23
27
|
return label
|
|
24
28
|
|
|
@@ -16,8 +16,9 @@ class PairBasedOpinionAnnotationAlgorithm(BaseOpinionAnnotationAlgorithm):
|
|
|
16
16
|
[1] Extracting Sentiment Attitudes from Analytical Texts https://arxiv.org/pdf/1808.08932.pdf
|
|
17
17
|
"""
|
|
18
18
|
|
|
19
|
-
def __init__(self, dist_in_terms_bound, label_provider,
|
|
20
|
-
|
|
19
|
+
def __init__(self, dist_in_terms_bound, label_provider,
|
|
20
|
+
is_entity_func, entity_index_func, entity_value_func,
|
|
21
|
+
dist_in_sents=0, is_entity_ignored_func=None):
|
|
21
22
|
"""
|
|
22
23
|
dist_in_terms_bound: int
|
|
23
24
|
max allowed distance in term (less than passed value)
|
|
@@ -34,7 +35,9 @@ class PairBasedOpinionAnnotationAlgorithm(BaseOpinionAnnotationAlgorithm):
|
|
|
34
35
|
self.__dist_in_terms_bound = dist_in_terms_bound
|
|
35
36
|
self.__dist_in_sents = dist_in_sents
|
|
36
37
|
self.__is_entity_ignored_func = is_entity_ignored_func
|
|
38
|
+
self.__is_entity_func = is_entity_func
|
|
37
39
|
self.__entity_index_func = entity_index_func
|
|
40
|
+
self.__entity_value_func = entity_value_func
|
|
38
41
|
|
|
39
42
|
# region private methods
|
|
40
43
|
|
|
@@ -90,10 +93,11 @@ class PairBasedOpinionAnnotationAlgorithm(BaseOpinionAnnotationAlgorithm):
|
|
|
90
93
|
return key is not None
|
|
91
94
|
|
|
92
95
|
# Initialize providers.
|
|
93
|
-
opinions_provider = OpinionPairsProvider(entity_index_func=self.__entity_index_func
|
|
96
|
+
opinions_provider = OpinionPairsProvider(entity_index_func=self.__entity_index_func,
|
|
97
|
+
entity_value_func=self.__entity_value_func)
|
|
94
98
|
entity_service_provider = EntityServiceProvider(entity_index_func=self.__entity_index_func)
|
|
95
|
-
opinions_provider.init_parsed_doc(parsed_doc)
|
|
96
|
-
entity_service_provider.init_parsed_doc(parsed_doc)
|
|
99
|
+
opinions_provider.init_parsed_doc(parsed_doc=parsed_doc, is_entity_func=self.__is_entity_func)
|
|
100
|
+
entity_service_provider.init_parsed_doc(parsed_doc=parsed_doc, is_entity_func=self.__is_entity_func)
|
|
97
101
|
|
|
98
102
|
return opinions_provider.iter_from_all(label_provider=self.__label_provider,
|
|
99
103
|
filter_func=__filter_pair_func)
|
arekit/common/pipeline/base.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from arekit.common.pipeline.context import PipelineContext
|
|
2
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
class BasePipelineLauncher:
|
|
@@ -11,7 +10,6 @@ class BasePipelineLauncher:
|
|
|
11
10
|
assert(isinstance(src_key, str) or src_key is None)
|
|
12
11
|
|
|
13
12
|
for ind, item in enumerate(filter(lambda itm: itm is not None, pipeline)):
|
|
14
|
-
assert(isinstance(item, BasePipelineItem))
|
|
15
13
|
do_force_key = src_key is not None and ind == 0
|
|
16
14
|
input_data = item.get_source(pipeline_ctx, force_key=src_key if do_force_key else None) \
|
|
17
15
|
if has_input or ind > 0 else None
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
from arekit.common.pipeline.context import PipelineContext
|
|
2
|
-
from arekit.common.pipeline.items.base import BasePipelineItem
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
class BatchingPipelineLauncher:
|
|
@@ -11,8 +10,6 @@ class BatchingPipelineLauncher:
|
|
|
11
10
|
assert(isinstance(src_key, str) or src_key is None)
|
|
12
11
|
|
|
13
12
|
for ind, item in enumerate(filter(lambda itm: itm is not None, pipeline)):
|
|
14
|
-
assert (isinstance(item, BasePipelineItem))
|
|
15
|
-
|
|
16
13
|
# Handle the content of the batch or batch itself.
|
|
17
14
|
content = item.get_source(pipeline_ctx, call_func=False, force_key=src_key if ind == 0 else None)
|
|
18
15
|
handled_batch = [item._src_func(i) if item._src_func is not None else i for i in content]
|
|
@@ -2,7 +2,7 @@ from arekit.common.pipeline.context import PipelineContext
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
class BasePipelineItem(object):
|
|
5
|
-
""" Single pipeline item that might be
|
|
5
|
+
""" Single pipeline item that might be instantiated and embedded into pipeline.
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
def __init__(self, src_key="result", result_key="result", src_func=None):
|
arekit/common/utils.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import sys
|
|
2
1
|
import os
|
|
3
2
|
from tqdm import tqdm
|
|
4
3
|
|
|
@@ -27,14 +26,14 @@ def split_by_whitespaces(text):
|
|
|
27
26
|
return text.split()
|
|
28
27
|
|
|
29
28
|
|
|
30
|
-
def progress_bar(iterable, total, desc="", unit="it"):
|
|
29
|
+
def progress_bar(iterable, total, desc="", unit="it", file=None, disable=False):
|
|
31
30
|
if total is not None:
|
|
32
|
-
return progress_bar_defined(iterable=iterable, total=total, desc=desc, unit=unit)
|
|
31
|
+
return progress_bar_defined(iterable=iterable, total=total, desc=desc, unit=unit, file=file, disable=disable)
|
|
33
32
|
else:
|
|
34
|
-
return progress_bar_iter(iterable=iterable, desc=desc, unit=unit)
|
|
33
|
+
return progress_bar_iter(iterable=iterable, desc=desc, unit=unit, file=file, disable=disable)
|
|
35
34
|
|
|
36
35
|
|
|
37
|
-
def progress_bar_conditional(iterable, condition_func, total, postfix_func=None, desc="", unit="it"):
|
|
36
|
+
def progress_bar_conditional(iterable, condition_func, total, postfix_func=None, desc="", unit="it", file=None):
|
|
38
37
|
""" This progress-bar updates only on the
|
|
39
38
|
specific conditions during the iteration process.
|
|
40
39
|
"""
|
|
@@ -47,7 +46,7 @@ def progress_bar_conditional(iterable, condition_func, total, postfix_func=None,
|
|
|
47
46
|
yield 0
|
|
48
47
|
|
|
49
48
|
pbar_it = progress_bar(iterable=__iter_infinite_placeholder(),
|
|
50
|
-
desc=desc, unit=unit, total=total)
|
|
49
|
+
desc=desc, unit=unit, total=total, file=file)
|
|
51
50
|
element = iter(pbar_it)
|
|
52
51
|
|
|
53
52
|
# Initialize with 0.
|
|
@@ -65,7 +64,7 @@ def progress_bar_conditional(iterable, condition_func, total, postfix_func=None,
|
|
|
65
64
|
pbar_it.set_postfix(postfix_func(item))
|
|
66
65
|
|
|
67
66
|
|
|
68
|
-
def progress_bar_defined(iterable, total, miniters=200, desc="", unit="it"):
|
|
67
|
+
def progress_bar_defined(iterable, total, miniters=200, desc="", unit="it", file=None, disable=False):
|
|
69
68
|
return tqdm(iterable=iterable,
|
|
70
69
|
total=total,
|
|
71
70
|
desc=desc,
|
|
@@ -73,13 +72,17 @@ def progress_bar_defined(iterable, total, miniters=200, desc="", unit="it"):
|
|
|
73
72
|
position=0,
|
|
74
73
|
leave=True,
|
|
75
74
|
unit=unit,
|
|
75
|
+
file=file,
|
|
76
|
+
disable=disable,
|
|
76
77
|
miniters=total / miniters if total is not None else total)
|
|
77
78
|
|
|
78
79
|
|
|
79
|
-
def progress_bar_iter(iterable, desc="", unit='it'):
|
|
80
|
+
def progress_bar_iter(iterable, desc="", unit='it', file=None, disable=False):
|
|
80
81
|
return tqdm(iterable=iterable,
|
|
81
82
|
desc=desc,
|
|
82
83
|
position=0,
|
|
83
84
|
leave=True,
|
|
84
85
|
ncols=120,
|
|
86
|
+
file=file,
|
|
87
|
+
disable=disable,
|
|
85
88
|
unit=unit)
|