Skip to content

Commit

Permalink
Merge pull request #13 from guillaumekln/python2
Browse files Browse the repository at this point in the history
Fix Python 2 compatibility for file scoring
  • Loading branch information
pltrdy authored May 16, 2018
2 parents 91529df + 41c799d commit 809299c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions rouge/rouge.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import
import six
import rouge.rouge_score as rouge_score
import io
import os


Expand All @@ -15,8 +16,9 @@ def __init__(self, hyp_path, ref_path, metrics=None, stats=None,

def line_count(path):
count = 0
for line in open(path):
count += 1
with open(path, "rb") as f:
for line in f:
count += 1
return count

hyp_lc = line_count(hyp_path)
Expand All @@ -39,8 +41,10 @@ def get_scores(self, avg=False):
"""
hyp_path, ref_path = self.hyp_path, self.ref_path

hyps = [line[:-1] for line in open(hyp_path).readlines()]
refs = [line[:-1] for line in open(ref_path).readlines()]
with io.open(hyp_path, encoding="utf-8", mode="r") as hyp_file:
hyps = [line[:-1] for line in hyp_file]
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)

Expand Down

0 comments on commit 809299c

Please sign in to comment.