Skip to content

Commit

Permalink
Done #139, related to #137.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolay-r committed Jun 30, 2021
1 parent 11f0bc5 commit cb04b85
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions common/opinions/formatter.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

class OpinionCollectionsFormatter(object):

def iter_opinions_from_file(self, filepath, labels_formatter):
def iter_opinions_from_file(self, filepath, labels_formatter, error_on_non_supported):
raise NotImplementedError()

def save_to_file(self, collection, filepath, labels_formatter):
def save_to_file(self, collection, filepath, labels_formatter, error_on_non_supported):
raise NotImplementedError()

def save_to_archive(self, collections_iter, labels_formatter):
Expand Down
3 changes: 2 additions & 1 deletion contrib/bert/run_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def _handle_iteration(self, iter_index):
self._experiment.DataIO.OpinionFormatter.save_to_file(
collection=collection,
filepath=filepath,
labels_formatter=self.__labels_formatter))
labels_formatter=self.__labels_formatter,
error_on_non_supported=False))

# evaluate
result = self._experiment.evaluate(data_type=self.__data_type,
Expand Down
3 changes: 2 additions & 1 deletion contrib/experiment_rusentrel/exp_sl/opinions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ def read_result_opinion_collection(self, data_type, doc_id, epoch_index):

def __custom_read(self, filepath, labels_fmt):
opinions = self.__opinion_formatter.iter_opinions_from_file(filepath=filepath,
labels_formatter=labels_fmt)
labels_formatter=labels_fmt,
error_on_non_supported=False)

return self.__create_collection(opinions)

Expand Down
3 changes: 2 additions & 1 deletion contrib/networks/core/callback/utils_model_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def __convert_output_to_opinion_collections(exp_io, opin_ops, doc_ops, labels_sc
epoch_index=epoch_index),
save_to_file_func=lambda filepath, collection: opin_fmt.save_to_file(collection=collection,
filepath=filepath,
labels_formatter=labels_formatter))
labels_formatter=labels_formatter,
error_on_non_supported=False))


def __log_wrap_samples_iter(it):
Expand Down
3 changes: 2 additions & 1 deletion contrib/source/rusentrel/opinions/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ def iter_opinions_from_doc(doc_id,
inner_path=RuSentRelIOUtils.get_sentiment_opin_filepath(doc_id),
process_func=lambda input_file: RuSentRelOpinionCollectionFormatter._iter_opinions_from_file(
input_file=input_file,
labels_formatter=labels_fmt),
labels_formatter=labels_fmt,
error_on_non_supported=True),
version=version)
27 changes: 17 additions & 10 deletions contrib/source/rusentrel/opinions/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@ class RuSentRelOpinionCollectionFormatter(OpinionCollectionsFormatter):

# region protected public methods

def iter_opinions_from_file(self, filepath, labels_formatter):
def iter_opinions_from_file(self, filepath, labels_formatter, error_on_non_supported=True):
"""
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))
assert(isinstance(error_on_non_supported, bool))

with open(filepath, 'r') as input_file:

it = RuSentRelOpinionCollectionFormatter._iter_opinions_from_file(
input_file=input_file,
labels_formatter=labels_formatter)
labels_formatter=labels_formatter,
error_on_non_supported=error_on_non_supported)

for opinion in it:
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
"""
def save_to_file(self, collection, filepath, labels_formatter, error_on_non_supported=True):
assert(isinstance(collection, OpinionCollection))
assert(isinstance(filepath, unicode))
assert(isinstance(labels_formatter, StringLabelsFormatter))
assert(isinstance(error_on_non_supported, bool))

def __opinion_key(opinion):
assert(isinstance(opinion, Opinion))
Expand All @@ -52,7 +51,11 @@ def __opinion_key(opinion):
labels_formatter=labels_formatter)

if str_value is None:
continue
if error_on_non_supported:
raise Exception("Opinion label `{label}` is not supported by formatter".format(
label=o.Sentiment))
else:
continue

f.write(str_value)
f.write(u'\n')
Expand All @@ -63,8 +66,9 @@ def save_to_archive(self, collections_iter, labels_formatter):
# endregion

@staticmethod
def _iter_opinions_from_file(input_file, labels_formatter):
def _iter_opinions_from_file(input_file, labels_formatter, error_on_non_supported):
assert(isinstance(labels_formatter, StringLabelsFormatter))
assert(isinstance(error_on_non_supported, bool))

for line in input_file.readlines():

Expand All @@ -78,7 +82,10 @@ def _iter_opinions_from_file(input_file, labels_formatter):
labels_formatter=labels_formatter)

if str_opinion is None:
continue
if error_on_non_supported:
raise Exception("Line '{line}' has non supported label")
else:
continue

yield str_opinion

Expand Down
3 changes: 2 additions & 1 deletion tests/contrib/experiment_rusentrel/test_rusentrel_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def iter_doc_opinions(doc_id, result_version, labels_formatter):
inner_path=path.join(u"{}.opin.txt".format(doc_id)),
process_func=lambda input_file: RuSentRelOpinionCollectionFormatter._iter_opinions_from_file(
input_file=input_file,
labels_formatter=labels_formatter),
labels_formatter=labels_formatter,
error_on_non_supported=True),
version=result_version)


Expand Down

0 comments on commit cb04b85

Please sign in to comment.