Download code/opusdb/text.py from KeremTT/OPUS-5.5-DATABASE: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/datasets/KeremTT/OPUS-5.5-DATABASE/resolve/main/code/opusdb/text.py
- Command line
-
hf download hf://datasets/KeremTT/OPUS-5.5-DATABASE/code/opusdb/text.py
-
curl -L -o text.py https://huggingface.co/datasets/KeremTT/OPUS-5.5-DATABASE/resolve/main/code/opusdb/text.py
3.25 kB
| """Text normalisation, hashing, PII scrubbing and cheap quality heuristics.""" | |
| import hashlib | |
| import re | |
| import unicodedata | |
| _WS = re.compile(r"\s+") | |
| _WORD = re.compile(r"\w+", re.UNICODE) | |
| EMAIL = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b") | |
| IPV4 = re.compile(r"\b(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)\b") | |
| PHONE = re.compile(r"(?<!\w)(?:\+?\d{1,3}[ .-]?)?(?:\(\d{3}\)|\d{3})[ .-]\d{3}[ .-]\d{2,4}(?!\w)") | |
| TR_CHARS = set("çğıöşüÇĞİÖŞÜ") | |
| TR_WORDS = {"ve", "bir", "bu", "için", "ile", "da", "de", "olarak", "çok", "daha", "gibi", "olan", "ne", "ama"} | |
| EN_WORDS = {"the", "and", "of", "to", "is", "in", "that", "it", "for", "with", "as", "was", "on", "are"} | |
| def normalize(text: str) -> str: | |
| text = unicodedata.normalize("NFKC", text) | |
| return _WS.sub(" ", text).strip() | |
| def key(text: str) -> str: | |
| """Hash used for exact de-duplication (case/whitespace/punctuation-insensitive).""" | |
| canon = " ".join(_WORD.findall(normalize(text).casefold())) | |
| return hashlib.blake2b(canon.encode(), digest_size=16).hexdigest() | |
| def stable_id(*parts: str) -> str: | |
| return hashlib.sha256("\x1f".join(parts).encode()).hexdigest()[:24] | |
| def words(text: str): | |
| return _WORD.findall(text.casefold()) | |
| def ngrams(tokens, n): | |
| return {" ".join(tokens[i:i + n]) for i in range(len(tokens) - n + 1)} | |
| def scrub_pii(text: str) -> str: | |
| text = EMAIL.sub("<EMAIL>", text) | |
| text = IPV4.sub("<IP_ADDRESS>", text) | |
| return text | |
| def guess_language(text: str) -> str: | |
| sample = text[:4000] | |
| toks = words(sample) | |
| if not toks: | |
| return "unk" | |
| tr = sum(t in TR_WORDS for t in toks) + 3 * sum(c in TR_CHARS for c in sample) / max(1, len(sample) / 100) | |
| en = sum(t in EN_WORDS for t in toks) | |
| if tr > en and tr > 2: | |
| return "tr" | |
| if en >= 2 or (len(toks) < 30 and sample.isascii()): | |
| return "en" | |
| return "other" | |
| def gopher_quality(text: str): | |
| """A subset of the Gopher (Rae et al., 2021) document quality rules. Returns list of failures.""" | |
| fails = [] | |
| toks = text.split() | |
| n = len(toks) | |
| if n < 50: | |
| fails.append("too_short") | |
| if n > 100_000: | |
| fails.append("too_long") | |
| if n: | |
| mean = sum(len(t) for t in toks) / n | |
| if not 3 <= mean <= 10: | |
| fails.append("mean_word_length") | |
| if sum(t.startswith(("#", "...")) or t.endswith("…") for t in toks) / n > 0.1: | |
| fails.append("symbol_ratio") | |
| alpha = sum(any(c.isalpha() for c in t) for t in toks) / n | |
| if alpha < 0.8: | |
| fails.append("non_alpha_words") | |
| lines = [l for l in text.splitlines() if l.strip()] | |
| if lines: | |
| if sum(l.lstrip().startswith(("•", "-", "*")) for l in lines) / len(lines) > 0.9: | |
| fails.append("bullet_lines") | |
| if sum(l.rstrip().endswith(("...", "…")) for l in lines) / len(lines) > 0.3: | |
| fails.append("ellipsis_lines") | |
| dup = 1 - len(set(lines)) / len(lines) | |
| if dup > 0.3: | |
| fails.append("duplicate_lines") | |
| return fails | |
| def approx_tokens(text: str) -> int: | |
| """~4 characters per token for English/code; used only where an exact count is too costly.""" | |
| return max(1, round(len(text) / 4)) | |