mariesig commited on
Commit
333d628
·
1 Parent(s): aa5ee52

Add WER normalization

Browse files
Files changed (2) hide show
  1. requirements.txt +1 -1
  2. utils.py +35 -4
requirements.txt CHANGED
@@ -15,4 +15,4 @@ datasets
15
  torchcodec==0.10
16
  torch==2.10
17
  torchaudio
18
- pyloudnorm
 
15
  torchcodec==0.10
16
  torch==2.10
17
  torchaudio
18
+ pyloudnorm
utils.py CHANGED
@@ -12,6 +12,8 @@ from PIL import Image
12
 
13
  from constants import TARGET_LOUDNESS, TARGET_TP, VAD_OFF, VAD_ON
14
 
 
 
15
 
16
  def get_vad_labels(vad_timestamps: list[list[float]], length: float) -> list[dict]:
17
  subtitles = []
@@ -166,12 +168,41 @@ def spec_image(
166
  return Image.open(buf).convert("RGB")
167
 
168
 
169
- def compute_wer(reference: str, hypothesis: str) -> float:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  """
171
  Compute Word Error Rate (WER) between reference and hypothesis transcripts.
 
 
 
172
  """
173
- ref_words = reference.split()
174
- hyp_words = hypothesis.split()
175
 
176
  d = np.zeros((len(ref_words) + 1, len(hyp_words) + 1), dtype=np.uint16)
177
 
@@ -237,4 +268,4 @@ def normalize_lufs(x: np.ndarray, sr: int) -> np.ndarray:
237
  return y.astype(np.float32)
238
  except Exception as e:
239
  warnings.warn(f"LUFS normalization failed, returning input unchanged: {e}")
240
- return x.astype(np.float32)
 
12
 
13
  from constants import TARGET_LOUDNESS, TARGET_TP, VAD_OFF, VAD_ON
14
 
15
+ _WER_NORMALIZERS: dict[str, object] = {}
16
+
17
 
18
  def get_vad_labels(vad_timestamps: list[list[float]], length: float) -> list[dict]:
19
  subtitles = []
 
168
  return Image.open(buf).convert("RGB")
169
 
170
 
171
+ def _normalize_wer_text(text: str, normalizer: Optional[str] = "basic") -> str:
172
+ """Normalize transcript text before WER tokenization."""
173
+ if normalizer is None:
174
+ return text.strip()
175
+
176
+ if normalizer not in {"basic", "english"}:
177
+ raise ValueError(
178
+ f"Unsupported normalizer '{normalizer}'. Use 'basic', 'english', or None."
179
+ )
180
+
181
+ if normalizer == "english":
182
+ if normalizer not in _WER_NORMALIZERS:
183
+ from whisper_normalizer.english import EnglishTextNormalizer
184
+
185
+ _WER_NORMALIZERS[normalizer] = EnglishTextNormalizer()
186
+ else:
187
+ if normalizer not in _WER_NORMALIZERS:
188
+ from whisper_normalizer.basic import BasicTextNormalizer
189
+
190
+ _WER_NORMALIZERS[normalizer] = BasicTextNormalizer()
191
+
192
+ return _WER_NORMALIZERS[normalizer](text)
193
+
194
+
195
+ def compute_wer(
196
+ reference: str, hypothesis: str, normalizer: Optional[str] = "basic"
197
+ ) -> float:
198
  """
199
  Compute Word Error Rate (WER) between reference and hypothesis transcripts.
200
+
201
+ Text is normalized before tokenization. Supported normalizers are
202
+ ``"basic"``, ``"english"``, or ``None`` to disable normalization.
203
  """
204
+ ref_words = _normalize_wer_text(reference, normalizer=normalizer).split()
205
+ hyp_words = _normalize_wer_text(hypothesis, normalizer=normalizer).split()
206
 
207
  d = np.zeros((len(ref_words) + 1, len(hyp_words) + 1), dtype=np.uint16)
208
 
 
268
  return y.astype(np.float32)
269
  except Exception as e:
270
  warnings.warn(f"LUFS normalization failed, returning input unchanged: {e}")
271
+ return x.astype(np.float32)