Move all imports to the top
@@ -4,6 +4,13 @@ import abc
import tensorflow as tf
+from rouge import FilesRouge
+from sacrebleu import corpus_bleu
+
+from opennmt.utils.wer import wer
+from opennmt.utils.ter import ter
+from opennmt.utils.fmeasure import fmeasure
+
class Scorer(abc.ABC):
"""Scores hypotheses against references."""
@@ -54,7 +61,6 @@ class ROUGEScorer(Scorer):
return {"rouge-1", "rouge-2", "rouge-l"}
def __call__(self, ref_path, hyp_path):
- from rouge import FilesRouge # pylint: disable=import-outside-toplevel
scorer = FilesRouge(metrics=list(self.scores_name))
rouge_scores = scorer.get_scores(hyp_path, ref_path, avg=True)
return {name:rouge_scores[name]["f"] for name in self.scores_name}
@@ -67,18 +73,18 @@ class BLEUScorer(Scorer):
super(BLEUScorer, self).__init__("bleu")
def __call__(self, ref_path, hyp_path):
- from sacrebleu import corpus_bleu # pylint: disable=import-outside-toplevel
with tf.io.gfile.GFile(ref_path) as ref_stream, tf.io.gfile.GFile(hyp_path) as sys_stream:
bleu = corpus_bleu(sys_stream, [ref_stream], force=True)
return bleu.score
+
class WERScorer(Scorer):
"""Scorer for WER."""
+
def __init__(self):
super(WERScorer, self).__init__("wer")
def __call__(self, ref_path, hyp_path):
- from opennmt.utils.wer import wer # pylint: disable=import-outside-toplevel
wer_score = wer(ref_path, hyp_path)
return wer_score
@@ -86,13 +92,14 @@ class WERScorer(Scorer):
""" Since the score shall be the lower the better """
return True
+
class TERScorer(Scorer):
"""Scorer for TER."""
+
def __init__(self):
super(TERScorer, self).__init__("ter")
def __call__(self, ref_path, hyp_path):
- from opennmt.utils.ter import ter # pylint: disable=import-outside-toplevel
ter_score = ter(ref_path, hyp_path)
return ter_score
@@ -101,9 +108,9 @@ class TERScorer(Scorer):
return True
-
class PRFScorer(Scorer):
"""Scorer for F-measure."""
+
def __init__(self):
super(PRFScorer, self).__init__("prfmeasure")
@@ -111,9 +118,7 @@ class PRFScorer(Scorer):
def scores_name(self):
return {"precision", "recall", "fmeasure"}
-
def __call__(self, ref_path, hyp_path):
- from opennmt.utils.fmeasure import fmeasure # pylint: disable=import-outside-toplevel
precision_score, recall_score, fmeasure_score = fmeasure(ref_path, hyp_path)
return {"precision":precision_score, "recall":recall_score, "fmeasure":fmeasure_score}
