Skip to content

Commit

Permalink
Merge pull request #18 from veskoch/master
Browse files Browse the repository at this point in the history
Implements a flag to allow ignoring of empty hypotheses
  • Loading branch information
pltrdy committed Sep 19, 2018
2 parents cfad8a0 + c12462d commit 1048ef3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion bin/rouge_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def main():
parser.add_argument('-f', '--file', help="File mode", action='store_true')
parser.add_argument('-a', '--avg', help="Average mode",
action='store_true')
parser.add_argument('--ignore_empty', action='store_true',
help="Ignore empty hypothesis")
parser.add_argument('hypothesis', type=str, help='Text of file path')
parser.add_argument('reference', type=str, help='Text or file path')

Expand All @@ -20,7 +22,8 @@ def main():
assert(os.path.isfile(ref))

files_rouge = FilesRouge(hyp, ref)
scores = files_rouge.get_scores(avg=args.avg)
scores = files_rouge.get_scores(avg=args.avg,
ignore_empty=args.ignore_empty)

print(json.dumps(scores, indent=2))
else:
Expand Down
13 changes: 10 additions & 3 deletions rouge/rouge.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def line_count(path):
self.ref_path = ref_path
self.batch_lines = batch_lines

def get_scores(self, avg=False):
def get_scores(self, avg=False, ignore_empty=False):
"""Calculate ROUGE scores between each pair of
lines (hyp_file[i], ref_file[i]).
Args:
Expand All @@ -46,7 +46,8 @@ def get_scores(self, avg=False):
with io.open(ref_path, encoding="utf-8", mode="r") as ref_file:
refs = [line[:-1] for line in ref_file]

return self.rouge.get_scores(hyps, refs, avg=avg)
return self.rouge.get_scores(hyps, refs, avg=avg,
ignore_empty=ignore_empty)


class Rouge:
Expand Down Expand Up @@ -74,10 +75,16 @@ def __init__(self, metrics=None, stats=None):
if s not in Rouge.AVAILABLE_STATS:
raise ValueError("Unknown stat '%s'" % s)

def get_scores(self, hyps, refs, avg=False):
def get_scores(self, hyps, refs, avg=False, ignore_empty=False):
if isinstance(hyps, six.string_types):
hyps, refs = [hyps], [refs]

if ignore_empty:
# Filter out hyps of 0 length
hyps_and_refs = zip(hyps, refs)
hyps_and_refs = [_ for _ in hyps_and_refs if len(_[0]) > 0]
hyps, refs = zip(*hyps_and_refs)

assert(type(hyps) == type(refs))
assert(len(hyps) == len(refs))

Expand Down

0 comments on commit 1048ef3

Please sign in to comment.