Download code/tk/disfluency.py from Lucasgitguy-co/Turbo-Kev-Dictate-1.4B: direct link, hf CLI and curl.
- Browser
- Download file 5.81 kB
-
https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/disfluency.py
- Command line
-
hf download hf://Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/code/tk/disfluency.py
-
curl -L -o disfluency.py https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/disfluency.py
5.81 kB
| """Rule-based disfluencies for fluent text: input = text with fillers/repeats/restarts/self-corrections added, | |
| target = the original text. Every edit only inserts words, so the clean text is always recoverable by deletion. | |
| Phonon writes fillers mostly bare and lower-case ("about uh peppers"), sometimes with commas ("Just, uh, I mean,"). | |
| Both forms are produced. | |
| """ | |
| from __future__ import annotations | |
| import random | |
| import re | |
| BARE_FILLERS = ["uh", "um", "uh", "um", "er", "ah"] | |
| MARKERS = ["you know,", "I mean,", "like,", "well,", "so,", "uh, you know,", "um, like,", "sort of", "kind of"] | |
| CORRECTIONS = ["no,", "no, sorry,", "sorry,", "I mean,", "no wait,", "or rather,", "uh, no,", "actually, no,", "sorry, I meant"] | |
| LOWER_OK = {"the", "a", "an", "we", "it", "this", "that", "so", "and", "but", "my", "our", "they", "he", "she", "you", | |
| "there", "what", "how", "when", "if", "in", "on", "at", "for", "to", "these", "those", "his", "her", | |
| "their", "its", "one", "some", "all", "no", "yes", "can", "could", "would", "should", "do", "is", "are", | |
| "was", "were", "have", "has", "let's", "it's", "that's", "there's", "we're", "they're", "you're"} | |
| FUNCTION = {"i", "the", "a", "to", "we", "it", "and", "that", "in", "is", "of", "my", "you", "they", "so", "this"} | |
| DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
| NUMS = ["two", "three", "four", "five", "six", "seven", "eight", "ten", "twelve", "fifteen", "twenty", "thirty"] | |
| _SENT_END = re.compile(r"[.?!]$") | |
| def _bare(w: str) -> str: | |
| return re.sub(r"^[^\w']+|[^\w']+$", "", w) | |
| def _starts_sentence(toks: list[str], i: int) -> bool: | |
| return i == 0 or bool(_SENT_END.search(toks[i - 1])) | |
| def _cap(s: str) -> str: | |
| return s[:1].upper() + s[1:] | |
| def _insert(toks: list[str], i: int, phrase: str) -> list[str]: | |
| """Insert phrase before toks[i], fixing capitals the way an ASR transcript would show them.""" | |
| new = phrase.split() | |
| if _starts_sentence(toks, i) and i < len(toks): | |
| new[0] = _cap(new[0]) | |
| nxt = toks[i] | |
| if _bare(nxt).lower() in LOWER_OK and nxt[:1].isupper() and not nxt.isupper(): | |
| toks = toks[:i] + [nxt[:1].lower() + nxt[1:]] + toks[i + 1:] | |
| return toks[:i] + new + toks[i:] | |
| def _alt_word(w: str, rng: random.Random, pool: list[str]) -> str | None: | |
| b = _bare(w) | |
| if b in DAYS: | |
| return rng.choice([d for d in DAYS if d != b]) | |
| if b.lower() in NUMS: | |
| return rng.choice([n for n in NUMS if n != b.lower()]) | |
| if b.isdigit(): | |
| return str(int(b) + rng.choice([-2, -1, 1, 2, 10])) if int(b) > 2 else None | |
| cands = [p for p in pool if len(_bare(p)) > 3 and _bare(p).lower() != b.lower()] | |
| return _bare(rng.choice(cands)) if cands else None | |
| def add_disfluencies(text: str, rng: random.Random, n_edits: int | None = None) -> str: | |
| toks = text.split() | |
| if len(toks) < 4: | |
| return text | |
| n_edits = n_edits or min(rng.choice([1, 1, 2, 2, 3, 4]), max(1, len(toks) // 6)) | |
| for _ in range(n_edits): | |
| kind = rng.choices(["filler", "marker", "repeat", "restart", "correct"], [35, 20, 25, 12, 8])[0] | |
| i = rng.randrange(0, len(toks)) | |
| if kind == "filler": | |
| f = rng.choice(BARE_FILLERS) | |
| toks = _insert(toks, i, f + ("," if rng.random() < 0.3 else "")) | |
| elif kind == "marker": | |
| toks = _insert(toks, i, rng.choice(MARKERS)) | |
| elif kind == "repeat": | |
| # repeat a short run starting at a function word: "I I think", "the the", "we should, we should" | |
| starts = [k for k, t in enumerate(toks) if _bare(t).lower() in FUNCTION and not _SENT_END.search(t)] | |
| if not starts: | |
| continue | |
| k = rng.choice(starts) | |
| span = rng.choice([1, 1, 1, 2, 3]) | |
| run = [t.rstrip(",.?!;:") for t in toks[k:k + span]] | |
| if any(_SENT_END.search(t) for t in toks[k:k + span - 1]): | |
| continue | |
| sep = "," if (span > 1 and rng.random() < 0.6) else "" | |
| run[-1] += sep | |
| nxt = toks[k] # "The the money": the original word now follows the repeat | |
| if _bare(nxt).lower() in LOWER_OK and nxt[:1].isupper() and not nxt.isupper(): | |
| toks = toks[:k] + [nxt[:1].lower() + nxt[1:]] + toks[k + 1:] | |
| toks = toks[:k] + run + toks[k:] | |
| elif kind == "restart": | |
| # false start: the first words of the clause, then a different word, then the real clause | |
| k = rng.randrange(0, len(toks) - 2) | |
| span = rng.choice([1, 2, 2, 3]) | |
| seg = [t.rstrip(",.?!;:") for t in toks[k:k + span]] | |
| if any(_SENT_END.search(t) for t in toks[k:k + span]): | |
| continue | |
| alt = _alt_word(seg[-1], rng, toks) | |
| if not alt: | |
| continue | |
| seg[-1] = alt.lower() if not alt[:1].isupper() or alt in DAYS else alt | |
| seg[-1] += "," | |
| if _starts_sentence(toks, k): | |
| seg[0] = _cap(seg[0]) | |
| nxt = toks[k] | |
| if _bare(nxt).lower() in LOWER_OK and nxt[:1].isupper() and not nxt.isupper(): | |
| toks = toks[:k] + [nxt[:1].lower() + nxt[1:]] + toks[k + 1:] | |
| toks = toks[:k] + seg + toks[k:] | |
| else: | |
| # self-correction before a content word: "on Tuesday, no, sorry, Wednesday" | |
| cands = [k for k, t in enumerate(toks) if len(_bare(t)) > 3 and _bare(t).lower() not in LOWER_OK] | |
| if not cands: | |
| continue | |
| k = rng.choice(cands) | |
| alt = _alt_word(toks[k], rng, toks) | |
| if not alt: | |
| continue | |
| toks = toks[:k] + [alt + ","] + rng.choice(CORRECTIONS).split() + toks[k:] | |
| return " ".join(toks) | |