Skip to main content

WhisperNormalizer English Module

OpenAI’s English text standardisation module

What does this module do?

As per the text normalization/standardization approach Appendix Section C pp.21 the paper Robust Speech Recognition via Large-Scale Weak Supervision. The EnglishTextNormalizer does the following functionality:

  1. Remove any phrases between matching brackets ([, ]).
  2. Remove any phrases between matching parentheses ((, )).
  3. Remove any of the following words: hmm, mm, mhm, mmm, uh, um
  4. Remove whitespace characters that comes before an apostrophe ’
  5. Convert standard or informal contracted forms of English into the original form.
  6. Remove commas (,) between digits
  7. Remove periods (.) not followed by numbers
  8. Remove symbols as well as diacritics from the text, where symbols are the characters with the Unicode category starting with M, S, or P, except period, percent, and currency symbols that may be detected in the next step.
  9. Detect any numeric expressions of numbers and currencies and replace with a form using Arabic numbers, e.g. “Ten thousand dollars” → “$10000”.
  10. Convert British spellings into American spellings.
  11. Remove remaining symbols that are not part of any numeric expressions.
  12. Replace any successive whitespace characters with a space.

source

EnglishNumberNormalizer

def EnglishNumberNormalizer():

Convert any spelled-out numbers into arabic numbers, while handling:

  • remove any commas
  • keep the suffixes such as: 1960s, 274th, 32nd, etc.
  • spell out currency symbols after the number. e.g. $20 million -> 20000000 dollars
  • spell out one and ones
  • interpret successive single-digit numbers as nominal: one oh one -> 101

source

EnglishSpellingNormalizer

def EnglishSpellingNormalizer():

Applies British-American spelling mappings as listed in [1].

[1] https://web.archive.org/web/20230326222449/https://www.tysto.com/uk-us-spelling-list.html

n = EnglishSpellingNormalizer()
n("accessorise")
'accessorize'

source

EnglishTextNormalizer

def EnglishTextNormalizer(
    tts_mode:bool=False
):

Applies all the rules for normalizing English text as mentioned in OpenAI whisper paper. As per the text normalization/standardization approach Appendix Section C pp.21 the paper Robust Speech Recognition via Large-Scale Weak Supervision. The EnglishTextNormalizer does the following functionality:

  1. Remove any phrases between matching brackets ([, ]).
  2. Remove any phrases between matching parentheses ((, )).
  3. Remove any of the following words: hmm, mm, mhm, mmm, uh, um
  4. Remove whitespace characters that comes before an apostrophe ’
  5. Convert standard or informal contracted forms of English into the original form; 's is “is” after personal pronouns and wh-words; after indefinite pronouns it is “is” only when the next word starts a predicate (someone's here), and possessive before a noun (someone's car).
  6. Remove commas (,) between digits
  7. Remove periods (.) not followed by numbers
  8. Remove symbols as well as diacritics from the text, where symbols are the characters with the Unicode category starting with M, S, or P, except period, percent, and currency symbols that may be detected in the next step.
  9. Detect any numeric expressions of numbers and currencies and replace with a form using Arabic numbers, e.g. “Ten thousand dollars” → “$10000”.
  10. Convert British spellings into American spellings.
  11. Remove remaining symbols that are not part of any numeric expressions.
  12. Replace any successive whitespace characters with a space.

With tts_mode=True (requires pip install whisper_normalizer[tts]) the text is prepared for speech instead: step 9 is skipped so written numbers stay words, digits, currencies, percentages and decimals are spelled out, and &, +, = and @ are spoken as words. A leading - is spoken as minus, phone numbers are read digit by digit, and decades such as 1990s are spoken as words.

Testing EnglishTextNormalizer

normalizer = EnglishTextNormalizer()
assert normalizer("I paid $20 million for 1,234 colour widgets.") == "i paid $20000000 for 1234 color widgets"
normalizer("I'm a little teapot, short and stout. Tip me over and pour me out!")

# A decimal that starts with 1 keeps its point, and numbers keep their value.
assert normalizer("1.5") == "1.5"
assert normalizer("2.5") == "2.5"
assert normalizer("he is 1") == "he is one"
assert normalizer("one thousand million") == "1000000000"

# "'s" is "is" after pronouns and wh-words, and possessive elsewhere.
for text, expected in [
    ("he's ready", "he is ready"),
    ("she's here", "she is here"),
    ("it's mine", "it is mine"),
    ("that's right", "that is right"),
    ("there's a problem", "there is a problem"),
    ("here's the thing", "here is the thing"),
    ("what's this", "what is this"),
    ("who's there", "who is there"),
    ("where's the station", "where is the station"),
    ("when's the party", "when is the party"),
    ("why's that", "why is that"),
    ("how's it going", "how is it going"),
    # Indefinite pronouns: a predicate makes "'s" = "is",
    # while a following noun makes it possessive.
    ("someone's at the door", "someone is at the door"),
    ("someone's going to the store", "someone is going to the store"),
    ("someone's tired", "someone is tired"),
    ("someone's tired of it", "someone is tired of it"),
    ("someone's used to it", "someone is used to it"),
    ("someone's married to him", "someone is married to him"),
    ("someone's here", "someone is here"),
    ("someone's back", "someone is back"),
    ("someone's the best", "someone is the best"),
    ("someone's mine", "someone is mine"),
    ("someone's good at math", "someone is good at math"),
    ("someone's past the deadline", "someone is past the deadline"),
    ("someone's after you", "someone is after you"),
    ("someone's happy to help", "someone is happy to help"),
    ("someone's pretty good", "someone is pretty good"),
    ("someone's only here", "someone is only here"),
    ("someone's building a house", "someone is building a house"),
    ("someone's meeting my friend", "someone is meeting my friend"),
    ("somebody's calling", "somebody is calling"),
    ("anyone's welcome", "anyone is welcome"),
    ("anybody's welcome", "anybody is welcome"),
    ("everyone's ready", "everyone is ready"),
    ("everyone's a winner", "everyone is a winner"),
    ("everybody's happy", "everybody is happy"),
    ("nobody's here", "nobody is here"),
    ("nobody's home", "nobody is home"),
    ("nobody's my boss", "nobody is my boss"),
    ("no one's home", "no one is home"),
    ("something's off", "something is off"),
    ("something's wrong with it", "something is wrong with it"),
    ("anything's possible", "anything is possible"),
    ("everything's fine", "everything is fine"),
    ("nothing's wrong", "nothing is wrong"),
    ("someone's car", "someones car"),
    ("someone's new car", "someones new car"),
    ("someone's red car", "someones red car"),
    ("someone's best friend", "someones best friend"),
    ("someone's birthday", "someones birthday"),
    ("someone's only child", "someones only child"),
    ("someone's last chance", "someones last chance"),
    ("someone's next move", "someones next move"),
    ("someone's own car", "someones own car"),
    ("someone's other car", "someones other car"),
    ("someone's back door", "someones back door"),
    ("someone's home address", "someones home address"),
    ("someone's past mistakes", "someones past mistakes"),
    ("someone's after party", "someones after party"),
    ("someone's down payment", "someones down payment"),
    ("someone's off day", "someones off day"),
    ("someone's building", "someones building"),
    ("someone's meeting is at noon", "someones meeting is at noon"),
    ("someone's wedding", "someones wedding"),
    ("someone's morning", "someones morning"),
    ("someone's clothing", "someones clothing"),
    ("someone's bed", "someones bed"),
    ("someone's loved ones", "someones loved ones"),
    ("someone's used car", "someones used car"),
    ("someone's married life", "someones married life"),
    ("everyone's favorite movie", "everyones favorite movie"),
    ("everyone's happy place", "everyones happy place"),
    ("nobody's fault", "nobodys fault"),
    ("John's car", "johns car"),
    ("the dog's bone", "the dogs bone"),
    ("Wittgenstein's quote", "wittgensteins quote"),
    ("the 1990's", "the 1990s"),
]:
    assert normalizer(text) == expected, (text, normalizer(text), expected)

# Curly apostrophes normalize like straight ones.
assert normalizer("it’s John’s car") == "it is johns car"
assert normalizer("don’t stop") == "do not stop"
'i am a little teapot short and stout tip me over and pour me out'

Upstream Whisper parity tests

These are the cases from tests/test_normalizer.py in openai/whisper, so we notice whenever the English normalizers drift from upstream.

# Mirrors test_number_normalizer() (parametrized over both normalizers upstream).
for std in (EnglishNumberNormalizer(), EnglishTextNormalizer()):
    assert std("two") == "2"
    assert std("thirty one") == "31"
    assert std("five twenty four") == "524"
    assert std("nineteen ninety nine") == "1999"
    assert std("twenty nineteen") == "2019"

    assert std("two point five million") == "2500000"
    assert std("four point two billions") == "4200000000s"
    assert std("200 thousand") == "200000"
    assert std("200 thousand dollars") == "$200000"
    assert std("$20 million") == "$20000000"
    assert std("€52.4 million") == "€52400000"
    assert std("£77 thousands") == "£77000s"

    assert std("two double o eight") == "2008"

    assert std("three thousand twenty nine") == "3029"
    assert std("forty three thousand two hundred sixty") == "43260"
    assert std("forty three thousand two hundred and sixty") == "43260"

    assert std("nineteen fifties") == "1950s"
    assert std("thirty first") == "31st"
    assert std("thirty three thousand and three hundred and thirty third") == "33333rd"

    assert std("three billion") == "3000000000"
    assert std("millions") == "1000000s"

    assert std("july third twenty twenty") == "july 3rd 2020"
    assert std("august twenty sixth twenty twenty one") == "august 26th 2021"
    assert std("3 14") == "3 14"
    assert std("3.14") == "3.14"
    assert std("3 point 2") == "3.2"
    assert std("3 point 14") == "3.14"
    assert std("3 point 1") == "3.1"
    assert std("fourteen point 4") == "14.4"
    assert std("one point five") == "1.5"
    assert std("two point two five dollars") == "$2.25"
    assert std("two hundred million dollars") == "$200000000"
    assert std("$20.1 million") == "$20100000"

    assert std("ninety percent") == "90%"
    assert std("seventy six per cent") == "76%"
    assert std("one percent") == "1%"

    assert std("double oh seven") == "007"
    assert std("double zero seven") == "007"
    assert std("nine one one") == "911"
    assert std("nine double one") == "911"
    assert std("one triple oh one") == "10001"

    assert std("two thousandth") == "2000th"
    assert std("thirty two thousandth") == "32000th"

    assert std("minus 500") == "-500"
    assert std("positive twenty thousand") == "+20000"
    assert std("minus one") == "-1"

    assert std("two dollars and seventy cents") == "$2.70"
    assert std("3 cents") == "¢3"
    assert std("$0.36") == "¢36"
    assert std("three euros and sixty five cents") == "€3.65"
    assert std("one dollar and one cent") == "$1.01"

    assert std("three and a half million") == "3500000"
    assert std("forty eight and a half dollars") == "$48.5"
    assert std("b747") == "b 747"
    assert std("10 th") == "10th"
    assert std("10th") == "10th"

    assert std("one") == "one"
    assert std("ones") == "ones"

# Mirrors test_spelling_normalizer().
std = EnglishSpellingNormalizer()
assert std("mobilisation") == "mobilization"
assert std("cancelation") == "cancellation"

# Mirrors test_text_normalizer().
std = EnglishTextNormalizer()
assert std("Let's") == "let us"
assert std("he's like") == "he is like"
assert std("she's been like") == "she has been like"
assert std("10km") == "10 km"
assert std("10mm") == "10 mm"
assert std("RC232") == "rc 232"

assert (
    std("Mr. Park visited Assoc. Prof. Kim Jr.")
    == "mister park visited associate professor kim junior"
)

Where this package intentionally differs

Two Whisper number-normalizer bugs are fixed here, and they are not covered by the upstream tests above: "one thousand million" keeps its million, and a failed currency conversion returns the match instead of the whole string. The tts_mode cases further down are additions upstream does not have.

# Upstream returns "1000" here: the multiplier is dropped.
assert EnglishNumberNormalizer()("one thousand million") == "1000000000"

# A "1" inside another token (money, percent, decimals) is not rewritten.
text = EnglishTextNormalizer()
assert text("I paid $1") == "i paid $1"
assert text("1%") == "1%"
assert text("3.1") == "3.1"
assert text("he is 1") == "he is one"
article_text = """Language is like a map that we use to navigate the world, but it’s also like a prison that keeps us from seeing what’s beyond the walls.

But what if there was a way to break out of this prison, to expand our map, to explore new worlds with new words? This is the possibility and the challenge offered by instruction tuned language models like GPT 4, a cutting-edge technology that uses artificial neural networks to generate natural language texts based on user inputs.

GPT 4 can write anything from essays to novels to poems to tweets to code to recipes to jokes to lyrics to whatever you want. It can even write things that don’t exist yet, things that no human has ever thought of or said before.

As Wittgenstein’s quote suggests, language is a source of limitation and liberation. GPT 4 pushes this idea to the extreme by giving us access to unlimited language.

This could be the most significant new technology in modern history because it has the potential to change many domains and industries. From education to entertainment, from journalism to justice, from science to art, these models could enable new forms of learning, storytelling, reporting, reasoning, discovery, and creation.

They could also create new ethical, social, and cultural challenges that require careful reflection and regulation. How we use this technology will depend on how we recognize its implications for ourselves and others.

This technology is a form of “Artificial Intelligence”. The word “intelligence” derives from inter- (“between”) and legere (“to choose, pick out, read”). To be intelligent, then, is to be able to choose between things, to pick out what matters, to read what is written. Intelligence is not just a quantity or a quality; it is an activity, a process, a practice. It is something that we do with our minds and our words.

But when we let GPT 4 do this for us, are we not abdicating our intelligence? Are we not letting go of our ability to choose, to pick out, to read? Are we not becoming passive consumers of language instead of active producers?
"""
normalizer(article_text)
'language is like a map that we use to navigate the world but it is also like a prison that keeps us from seeing what is beyond the walls but what if there was a way to break out of this prison to expand our map to explore new worlds with new words this is the possibility and the challenge offered by instruction tuned language models like gpt 4 a cutting edge technology that uses artificial neural networks to generate natural language texts based on user inputs gpt 4 can write anything from essays to novels to poems to tweets to code to recipes to jokes to lyrics to whatever you want it can even write things that do not exist yet things that no human has ever thought of or said before as wittgensteins quote suggests language is a source of limitation and liberation gpt 4 pushes this idea to the extreme by giving us access to unlimited language this could be the most significant new technology in modern history because it has the potential to change many domains and industries from education to entertainment from journalism to justice from science to art these models could enable new forms of learning storytelling reporting reasoning discovery and creation they could also create new ethical social and cultural challenges that require careful reflection and regulation how we use this technology will depend on how we recognize its implications for ourselves and others this technology is a form of artificial intelligence the word intelligence derives from inter and legere to be intelligent then is to be able to choose between things to pick out what matters to read what is written intelligence is not just a quantity or a quality it is an activity a process a practice it is something that we do with our minds and our words but when we let gpt 4 do this for us are we not abdicating our intelligence are we not letting go of our ability to choose to pick out to read are we not becoming passive consumers of language instead of active producers'

tts_mode for English

With tts_mode=True the text is prepared for speech: written numbers stay words (the usual words-to-digits step is skipped), digits, currencies and percentages are spelled out, and a few symbols are spoken. Default behaviour is unchanged.

# tts_mode spells digits as words.
tts = EnglishTextNormalizer(tts_mode=True)
assert tts("I have 3 apples and 42 pears") == "i have three apples and forty two pears"
# Written numbers stay words in tts_mode: the usual words-to-digits step is skipped.
assert tts("I was born in nineteen eighty four") == "i was born in nineteen eighty four"
assert tts("a hundred apples") == "a hundred apples"

# Default behaviour is unchanged: tts_mode is off, and words still become digits.
default = EnglishTextNormalizer()
assert default.tts_mode is False
assert default("I was born in nineteen eighty four") == "i was born in 1984"
assert default("a hundred apples") == "a 100 apples"
assert default("I have three apples and 42 pears") == "i have 3 apples and 42 pears"

# Apostrophe handling is shared with default mode.
assert tts("It’s John’s car") == "it is johns car"
assert tts("Everyone's here") == "everyone is here"
# tts_mode: a currency symbol is spoken as the amount followed by the unit (singular for one).
for text, expected in [
    ("It costs $20", "it costs twenty dollars"),
    ("just $1", "just one dollar"),
    ("$1,200 total", "one thousand two hundred dollars total"),
    ("€5.50 each", "five euros fifty cents each"),
    ("£2.01", "two pounds one penny"),
    ("£3", "three pounds"),
    ("only 5¢", "only five cents"),
]:
    assert tts(text) == expected, (text, tts(text), expected)
# tts_mode: percentages and decimals are spoken (decimals digit by digit), and a few symbols become words.
for text, expected in [
    ("50% off", "fifty percent off"),
    ("3.5% growth", "three point five percent growth"),
    ("pi is 3.14", "pi is three point one four"),
    ("exactly 3.50", "exactly three point five zero"),
    ("salt & pepper", "salt and pepper"),
    ("2 + 2 = 4", "two plus two equals four"),
    ("meet me @ noon", "meet me at noon"),
]:
    assert tts(text) == expected, (text, tts(text), expected)
# Without the optional num2words package, tts_mode fails loudly with install instructions; default mode is unaffected.
import sys

_saved = sys.modules.get("num2words")
sys.modules["num2words"] = None  # makes `import num2words` raise ImportError
try:
    try:
        EnglishTextNormalizer(tts_mode=True)
        raise AssertionError("expected ImportError")
    except ImportError as e:
        assert "whisper_normalizer[tts]" in str(e)
    assert EnglishTextNormalizer()("I have three apples") == "i have 3 apples"
finally:
    if _saved is None:
        sys.modules.pop("num2words", None)
    else:
        sys.modules["num2words"] = _saved
# tts_mode: a price under one unit is just its minor unit; ordinals are spoken as ordinals.
for text, expected in [
    ("It costs $0.05", "it costs five cents"),
    ("£0.01", "one penny"),
    ("$0", "zero dollars"),
    ("the 1st and 2nd place", "the first and second place"),
    ("on the 21st, 3rd and 11th", "on the twenty first third and eleventh"),
    ("the 100th day", "the one hundredth day"),
]:
    assert tts(text) == expected, (text, tts(text), expected)

# tts_mode: signs, phone numbers and decades are spoken as words, not digits.
for text, expected in [
    ("-5 degrees", "minus five degrees"),
    ("call 555-1234", "call five five five one two three four"),
    ("call 555-123-4567", "call five five five one two three four five six seven"),
    ("the 1990s", "the nineteen nineties"),
    ("the 2010s", "the twenty tens"),
    ("the 2000s", "the two thousands"),
]:
    assert tts(text) == expected, (text, tts(text), expected)