Skip to content

Commit

Permalink
Fixed #137
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Jun 30, 2021
1 parent 9f25273 commit 11f0bc5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
16 changes: 14 additions & 2 deletions common/labels/str_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class StringLabelsFormatter(object):
def __init__(self, stol):
assert(isinstance(stol, dict))
self._stol = stol
self.__supported_labels = set(self._stol.itervalues())

def __is_label_supported(self, label):
return label in self.__supported_labels

def str_to_label(self, value):
assert(isinstance(value, unicode))
Expand All @@ -18,10 +22,18 @@ def str_to_label(self, value):

def label_to_str(self, label):
assert(isinstance(label, Label))
for value, l in self._stol.iteritems():
if l == label:

if not self.__is_label_supported(label):
raise Exception("Label {label} is not supported. Supported labels: [{values}]".format(
label=label, values=self.__supported_labels))

for value, supported_label in self._stol.iteritems():
if supported_label == label:
return value

def supports_label(self, label):
return label in self.__supported_labels

def supports_value(self, value):
assert(isinstance(value, unicode))
return value in self._stol
Expand Down
52 changes: 39 additions & 13 deletions contrib/source/rusentrel/opinions/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class RuSentRelOpinionCollectionFormatter(OpinionCollectionsFormatter):
def iter_opinions_from_file(self, filepath, labels_formatter):
"""
Important: For externaly saved collections (using save_to_file method) and related usage
NOTE: We consider only those opinions which labels could be obtained by provided labels_formatter
"""
assert(isinstance(filepath, unicode))
assert(isinstance(labels_formatter, StringLabelsFormatter))
Expand All @@ -28,6 +29,9 @@ def iter_opinions_from_file(self, filepath, labels_formatter):
yield opinion

def save_to_file(self, collection, filepath, labels_formatter):
"""
NOTE: We consider only those opinions which labels could be obtained by provided labels_formatter
"""
assert(isinstance(collection, OpinionCollection))
assert(isinstance(filepath, unicode))
assert(isinstance(labels_formatter, StringLabelsFormatter))
Expand All @@ -43,20 +47,21 @@ def __opinion_key(opinion):
with io.open(filepath, 'w') as f:
for o in sorted_ops:

o_str = RuSentRelOpinionCollectionFormatter.__opinion_to_str(
str_value = RuSentRelOpinionCollectionFormatter.__try_opinion_to_str(
opinion=o,
labels_formatter=labels_formatter)

f.write(o_str)
if str_value is None:
continue

f.write(str_value)
f.write(u'\n')

def save_to_archive(self, collections_iter, labels_formatter):
raise NotImplementedError()

# endregion

# region private methods

@staticmethod
def _iter_opinions_from_file(input_file, labels_formatter):
assert(isinstance(labels_formatter, StringLabelsFormatter))
Expand All @@ -68,22 +73,43 @@ def _iter_opinions_from_file(input_file, labels_formatter):
if line == '\n':
continue

args = line.strip().split(',')
assert(len(args) >= 3)
str_opinion = RuSentRelOpinionCollectionFormatter.__try_str_to_opinion(
line=line,
labels_formatter=labels_formatter)

if str_opinion is None:
continue

yield str_opinion

# region private methods

@staticmethod
def __try_str_to_opinion(line, labels_formatter):
args = line.strip().split(',')
assert (len(args) >= 3)

source_value = args[0].strip()
target_value = args[1].strip()
str_label = args[2].strip()

source_value = args[0].strip()
target_value = args[1].strip()
sentiment = labels_formatter.str_to_label(args[2].strip())
if not labels_formatter.supports_value(str_label):
return None

yield Opinion(source_value=source_value,
target_value=target_value,
sentiment=sentiment)
return Opinion(source_value=source_value,
target_value=target_value,
sentiment=labels_formatter.str_to_label(str_label))

@staticmethod
def __opinion_to_str(opinion, labels_formatter):
def __try_opinion_to_str(opinion, labels_formatter):
assert(isinstance(opinion, Opinion))
assert(isinstance(labels_formatter, StringLabelsFormatter))

label = opinion.Sentiment

if not labels_formatter.supports_label(label):
return None

return u"{}, {}, {}, current".format(
opinion.SourceValue,
opinion.TargetValue,
Expand Down

0 comments on commit 11f0bc5

Please sign in to comment.