Download src/utils/preprocess.py from dahutapea/Topic-Modeling-Tokopedia-Reviews: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/dahutapea/Topic-Modeling-Tokopedia-Reviews/resolve/main/src/utils/preprocess.py
- Command line
-
hf download hf://spaces/dahutapea/Topic-Modeling-Tokopedia-Reviews/src/utils/preprocess.py
-
curl -L -o preprocess.py https://huggingface.co/spaces/dahutapea/Topic-Modeling-Tokopedia-Reviews/resolve/main/src/utils/preprocess.py
1.16 kB
| """Indonesian review cleaning - the SAME pipeline used to train the topic models, so new | |
| text is processed identically before prediction: | |
| lowercase -> strip URLs/mentions/symbols -> cut elongation -> normalize slang -> letters only. | |
| """ | |
| import re | |
| import pandas as pd | |
| from utils.paths import LEXICON | |
| _slang = None | |
| def _slang_map(): | |
| """Lazy-load the colloquial Indonesian lexicon (slang -> formal).""" | |
| global _slang | |
| if _slang is None: | |
| lex = pd.read_csv(LEXICON) | |
| _slang = dict(zip(lex["slang"].astype(str), lex["formal"].astype(str))) | |
| return _slang | |
| def clean_review(text: str) -> str: | |
| t = str(text).lower() | |
| t = re.sub(r"http\S+|www\.\S+", " ", t) # URLs | |
| t = re.sub(r"@\w+", " ", t) # mentions | |
| t = re.sub(r"[^a-z\s]", " ", t) # keep letters + spaces | |
| t = re.sub(r"(.)\1{2,}", r"\1\1", t) # cut elongation: "bagusss" -> "baguss" | |
| t = re.sub(r"\s+", " ", t).strip() | |
| t = " ".join(_slang_map().get(w, w) for w in t.split()) # slang -> formal | |
| t = re.sub(r"[^a-z\s]", " ", t) # formal forms may add hyphens | |
| return re.sub(r"\s+", " ", t).strip() | |