Skip to content

Commit

Permalink
#338 -- provide text opinion conversion API
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Jun 17, 2022
1 parent e92f9f4 commit 7f91089
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions arekit/common/text_opinions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,38 @@ def __init__(self, doc_id, text_opinion_id, source_id, target_id, owner, label):
@classmethod
def create_copy(cls, other, keep_text_opinion_id=True):
assert(isinstance(other, TextOpinion))
assert(isinstance(keep_text_opinion_id, bool))
return TextOpinion.__try_create_copy_core(other=other, keep_text_opinion_id=keep_text_opinion_id)

@classmethod
def convert(cls, other, convert_func):
""" Creates a copy of `other` opinion with different id of opinion participants.
Use cases: required for BaseParsedNewsServiceProvider, when we decided to bring the outside
opinion into one which is based on DocumentEntities.
"""
assert(isinstance(other, TextOpinion))
assert(callable(convert_func))
return TextOpinion.__try_create_copy_core(other=other, convert_id_func=convert_func)

@staticmethod
def __try_create_copy_core(other, convert_id_func=lambda part_id: part_id, keep_text_opinion_id=True):
""" Tries to compose a copy by considering an optional id conversion,
and identification keeping.
convert_id:
func(id) -> id
"""
assert(callable(convert_id_func))

source_id = convert_id_func(other.SourceId)
target_id = convert_id_func(other.TargetId)

if source_id is None or target_id is None:
return None

return TextOpinion(doc_id=other.__doc_id,
text_opinion_id=other.__text_opinion_id if keep_text_opinion_id else None,
source_id=other.SourceId,
target_id=other.TargetId,
source_id=source_id,
target_id=target_id,
owner=other.Owner,
label=other.Sentiment)

Expand Down

0 comments on commit 7f91089

Please sign in to comment.