"""Is this transcript English? Standard library only, so the GGUF runner needs no pip packages. Phonon-2.5 writes 25 languages; only English is cleaned. Score = share of common English words (half weight for words that are also words in another Phonon language, e.g. "is", "in", "to") minus twice the share of function words of the other 24 languages. Texts in a non-Latin script are never English. The threshold was set on held-out Phonon transcripts; the measured error rates are in results/langcheck.json. """ from __future__ import annotations import re EN_WORDS = frozenset(""" the and of that it you for are with this they have but not what all were we when your can there an which she how their if would about been has had them could i i'm it's don't my just our from or by one more some like into then than these those other because very also any who where why does did doesn't didn't can't won't isn't wasn't i've i'll i'd you're we're they're that's there's let's yeah yes okay well really think know mean going get got should shall must might much many such only even over after before through between under while again still most make made said say people time year way thing things something anything nothing everything good new first last """.split()) EN_WEAK = frozenset("is was in he to on a at his her as so do be will no its him us me up out off".split()) FOREIGN = frozenset(""" de het een en van niet ook maar dat zijn wordt und der das ist nicht ein eine mit sich auch le la les et est des du une pas que qui dans pour sur el los las y del por una con para es il di che della per non sono och att är det som för inte av ja je na se ne że nie jest się oraz ale jak o pa ki da ta te ti mi si sa su kao ili nu și cu în pe mai sau og ikke af til ei ole että sekä oli és az egy hogy nem van vagy yra kad bet ar arba un ir ka tas uz kā """.split()) _WORD = re.compile(r"[^\W\d_]+(?:'[^\W\d_]+)?") _NON_LATIN = re.compile("[^\u0000-ɏ‘-‟…]") THRESHOLD = 0.12 def scores(text: str) -> tuple[float, float]: """(English share, foreign share) of the words.""" # English "I" is always upper case; a lower-case "i" is Polish/Danish/Croatian words = [w.lower() for w in _WORD.findall(text.replace("’", "'")) if w != "i"] if not words: return 0.0, 0.0 en = sum(1.0 if w in EN_WORDS else 0.5 if w in EN_WEAK else 0.0 for w in words) fo = sum(w in FOREIGN or not w.isascii() for w in words) return en / len(words), fo / len(words) def english_score(text: str) -> float: en, fo = scores(text) return en - 2 * fo def is_english(text: str) -> bool: if _NON_LATIN.search(text): return False return english_score(text) >= THRESHOLD