Skip to content

Commit

Permalink
#338 update
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Jun 17, 2022
1 parent 593ea3d commit 76adaef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions arekit/common/news/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class DocumentEntity(Entity):

def __init__(self, value, e_type, id_in_doc, group_index):
""" id_in_doc: Id, utilized witin the internal services
"""
super(DocumentEntity, self).__init__(value=value,
e_type=e_type,
group_index=group_index)
Expand Down
35 changes: 31 additions & 4 deletions arekit/common/news/parsed/providers/base.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
from arekit.common.entities.base import Entity
from arekit.common.news.entity import DocumentEntity
from arekit.common.news.parsed.base import ParsedNews


class BaseParsedNewsServiceProvider(object):

def __init__(self):
def __init__(self, entity_index_func):
""" Outside enity indexing function
entity_index_func: provides id for a given entity, i.e.
func(entity) -> int (id)
"""
assert(callable(entity_index_func))
self._doc_entities = None
self.__entity_map = {}
self.__entity_index_func = entity_index_func

@property
def Name(self):
raise NotImplementedError()

def init_parsed_news(self, parsed_news):
assert(isinstance(parsed_news, ParsedNews))
self._doc_entities = [DocumentEntity(id_in_doc=doc_id, value=entity.Value,
e_type=entity.Type, group_index=entity.GroupIndex)
for doc_id, entity in enumerate(parsed_news.iter_entities())]

self._doc_entities = []
self.__entity_map.clear()

for index, entity in enumerate(parsed_news.iter_entities()):

doc_entity = DocumentEntity(id_in_doc=index,
value=entity.Value,
e_type=entity.Type,
group_index=entity.GroupIndex)

self._doc_entities.append(doc_entity)
self.__entity_map[self.__entity_index_func(entity)] = doc_entity

def get_document_entity(self, entity):
""" Maps entity to the related one with DocumentEntity type
"""
assert(isinstance(entity, Entity))
return self.__entity_map[self.__entity_index_func(entity)]

def contains_entity(self, entity):
return self.__entity_index_func(entity) in self.__entity_map

0 comments on commit 76adaef

Please sign in to comment.