
import editdistance

from fairseq.scoring import register_scoring


@register_scoring("wer")
class WerScorer(object):
    def __init__(self, *unused):
        self.reset()

    def reset(self):
        self.distance = 0
        self.target_length = 0

    def add_string(self, ref, pred):
        pred_items = ref.split()
        targ_items = pred.split()
        self.distance += editdistance.eval(pred_items, targ_items)
        self.target_length += len(targ_items)

    def result_string(self):
        return f"WER: {self.score()}"

    def score(self):
        return (
            100.0 * self.distance / self.target_length if self.target_length > 0 else 0
        )


import editdistance

from fairseq.scoring import register_scoring


@register_scoring("wer")
class WerScorer(object):
    def __init__(self, *unused):
        self.reset()

    def reset(self):
        self.distance = 0
        self.ref_length = 0

    def add_string(self, ref, pred):
        ref_items = ref.split()
        pred_items = pred.split()
        self.distance += editdistance.eval(ref_items, pred_items)
        self.ref_length += len(ref_items)

    def result_string(self):
        return f"WER: {self.score()}"

    def score(self):
        return (
            100.0 * self.distance / self.ref_length if self.ref_length > 0 else 0
        )

Fix typos in WER scorer
Fix typos in WER scorer

Summary:
Fix typos in WER scorer
- The typos lead to using prediction length as the denominator in the formula, which is wrong.

Reviewed By: alexeib

Differential Revision: D23139261

fbshipit-source-id: d1bba0044365813603ce358388e880c1b3f9ec6b

  
  import editdistance
  
  from fairseq.scoring import register_scoring
  
  
  @register_scoring("wer")
  class WerScorer(object):
      def __init__(self, *unused):
          self.reset()
  
      def reset(self):
          self.distance = 0
-         self.target_length = 0
+         self.ref_length = 0
  
      def add_string(self, ref, pred):
+         ref_items = ref.split()
-         pred_items = ref.split()
+         pred_items = pred.split()
-         targ_items = pred.split()
-         self.distance += editdistance.eval(pred_items, targ_items)
+         self.distance += editdistance.eval(ref_items, pred_items)
-         self.target_length += len(targ_items)
+         self.ref_length += len(ref_items)
  
      def result_string(self):
          return f"WER: {self.score()}"
  
      def score(self):
          return (
-             100.0 * self.distance / self.target_length if self.target_length > 0 else 0
+             100.0 * self.distance / self.ref_length if self.ref_length > 0 else 0
          )
  
Fix typos in WER scorer
## Code Before:

import editdistance

from fairseq.scoring import register_scoring


@register_scoring("wer")
class WerScorer(object):
    def __init__(self, *unused):
        self.reset()

    def reset(self):
        self.distance = 0
        self.target_length = 0

    def add_string(self, ref, pred):
        pred_items = ref.split()
        targ_items = pred.split()
        self.distance += editdistance.eval(pred_items, targ_items)
        self.target_length += len(targ_items)

    def result_string(self):
        return f"WER: {self.score()}"

    def score(self):
        return (
            100.0 * self.distance / self.target_length if self.target_length > 0 else 0
        )

## Instruction:
Fix typos in WER scorer
## Code After:

import editdistance

from fairseq.scoring import register_scoring


@register_scoring("wer")
class WerScorer(object):
    def __init__(self, *unused):
        self.reset()

    def reset(self):
        self.distance = 0
        self.ref_length = 0

    def add_string(self, ref, pred):
        ref_items = ref.split()
        pred_items = pred.split()
        self.distance += editdistance.eval(ref_items, pred_items)
        self.ref_length += len(ref_items)

    def result_string(self):
        return f"WER: {self.score()}"

    def score(self):
        return (
            100.0 * self.distance / self.ref_length if self.ref_length > 0 else 0
        )
