Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/test_normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def test_number_normalizer(std):

assert std("three and a half million") == "3500000"
assert std("forty eight and a half dollars") == "$48.5"
assert std("2 and a half million") == "2500000"
assert std("48 and a half dollars") == "$48.5"
assert std("and a half hours later") == "and a half hours later"
assert std("one and a half and a half") == "1.5 and a half"
assert std("b747") == "b 747"
assert std("10 th") == "10th"
assert std("10th") == "10th"
Expand Down
23 changes: 13 additions & 10 deletions whisper/normalizers/english.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,20 @@ def preprocess(self, s: str):

segments = re.split(r"\band\s+a\s+half\b", s)
for i, segment in enumerate(segments):
if len(segment.strip()) == 0:
continue
results.append(segment)
if i == len(segments) - 1:
results.append(segment)
else:
results.append(segment)
last_word = segment.rsplit(maxsplit=2)[-1]
if last_word in self.decimals or last_word in self.multipliers:
results.append("point five")
else:
results.append("and a half")
break

words = segment.split()
last_word = words[-1] if words else ""
if (
last_word in self.decimals
or last_word in self.multipliers
or re.fullmatch(r"\d+", last_word) # e.g. "2 and a half"
):
results.append("point five")
else: # not a number; keep the original words rather than dropping them
results.append("and a half")

s = " ".join(results)

Expand Down