Download parakeet_lib/transcripts.py from luvette/parakeet-canadian-tooling: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/luvette/parakeet-canadian-tooling/resolve/main/parakeet_lib/transcripts.py
- Command line
-
hf download hf://luvette/parakeet-canadian-tooling/parakeet_lib/transcripts.py
-
curl -L -o transcripts.py https://huggingface.co/luvette/parakeet-canadian-tooling/resolve/main/parakeet_lib/transcripts.py
3.74 kB
| """Transcript normalisation toward the Parakeet/Cassini tokenizer's output convention. | |
| The Parakeet tokenizer was trained on text that went through NeMo's | |
| ``EnglishTextNormalizer``-style pipeline: lowercase, punctuation retained but | |
| spaced off, numerals and symbols verbalised. We can't import NeMo here, so we | |
| implement the subset that matters for transcript matching, driven by the actual | |
| tokenizer vocabulary where available. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| import unicodedata | |
| _DIGIT_WORDS = { | |
| "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", | |
| "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", | |
| } | |
| _TEENS = {str(i): w for i, w in enumerate( | |
| "ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(), 10)} | |
| _TENS = {"20": "twenty", "30": "thirty", "40": "forty", "50": "fifty", | |
| "60": "sixty", "70": "seventy", "80": "eighty", "90": "ninety"} | |
| _SYMBOLS = { | |
| "&": "and", "%": "percent", "$": "dollars", "+": "plus", "=": "equals", | |
| "@": "at", "#": "number", "°": "degrees", "£": "pounds", "€": "euros", | |
| "—": "", "–": "", "…": "", | |
| } | |
| _PUNCT_KEEP = set(".,?!-:;\"'") | |
| _WS = re.compile(r"\s+") | |
| def _int_to_words(n: int) -> str: | |
| if n < 0: | |
| return "minus " + _int_to_words(-n) | |
| if n < 10: | |
| return _DIGIT_WORDS[str(n)] | |
| if n < 20: | |
| return _TEENS[str(n)] | |
| if n < 100: | |
| t, r = divmod(n, 10) | |
| return _TENS[str(t * 10)] + (" " + _DIGIT_WORDS[str(r)] if r else "") | |
| if n < 1000: | |
| h, r = divmod(n, 100) | |
| return _DIGIT_WORDS[str(h)] + " hundred" + (" " + _int_to_words(r) if r else "") | |
| if n < 1_000_000: | |
| th, r = divmod(n, 1000) | |
| return _int_to_words(th) + " thousand" + (" " + _int_to_words(r) if r else "") | |
| if n < 1_000_000_000: | |
| m, r = divmod(n, 1_000_000) | |
| return _int_to_words(m) + " million" + (" " + _int_to_words(r) if r else "") | |
| b, r = divmod(n, 1_000_000_000) | |
| return _int_to_words(b) + " billion" + (" " + _int_to_words(r) if r else "") | |
| _NUM = re.compile(r"\d[\d,]*(?:\.\d+)?") | |
| def _expand_number(m: re.Match) -> str: | |
| s = m.group(0).replace(",", "") | |
| if "." in s: | |
| whole, frac = s.split(".", 1) | |
| return _int_to_words(int(whole or 0)) + " point " + " ".join(_DIGIT_WORDS[c] for c in frac) | |
| # years / long identifiers -> digit-by-digit when >= 4 digits and starts with 1 or 2 | |
| if len(s) >= 4 and s[0] in "12" and len(s) in (4, 5): | |
| return " ".join(_DIGIT_WORDS[c] for c in s) | |
| return _int_to_words(int(s)) | |
| def normalise_transcript(text: str, expand_contractions: bool = True) -> str: | |
| """Bring a reference transcript into the model's output convention.""" | |
| if not text: | |
| return "" | |
| t = unicodedata.normalize("NFKC", text).strip() | |
| t = t.lower() | |
| for sym, rep in _SYMBOLS.items(): | |
| t = t.replace(sym, f" {rep} ") | |
| if expand_contractions: | |
| from .text import expand_contractions as _expand | |
| t = _expand(t) | |
| t = _NUM.sub(_expand_number, t) | |
| # space-off punctuation but keep sentence punctuation | |
| out = [] | |
| for ch in t: | |
| if ch in _PUNCT_KEEP: | |
| out.append(" " + ch + " ") | |
| elif ch.isalnum() or ch == "'" or ch.isspace(): | |
| out.append(ch) | |
| else: | |
| out.append(" ") | |
| t = "".join(out) | |
| # collapse spaces before punctuation | |
| t = re.sub(r"\s+([.,?!:;])", r"\1", t) | |
| t = _WS.sub(" ", t).strip() | |
| return t | |
| def strip_specials(text: str) -> str: | |
| """Remove any special tokens that leak from decode().""" | |
| for tok in ("<blank>", "<pad>", "<s>", "</s>", "<unk>", "<|endoftext|>"): | |
| text = text.replace(tok, " ") | |
| return _WS.sub(" ", text).strip() | |