Spaces:
Running
Running
Download src/utils/nlp/lang_detect.py from Leon4gr45/brain: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/src/utils/nlp/lang_detect.py
- Command line
-
hf download hf://spaces/Leon4gr45/brain/src/utils/nlp/lang_detect.py
-
curl -L -o lang_detect.py https://huggingface.co/spaces/Leon4gr45/brain/resolve/main/src/utils/nlp/lang_detect.py
1.11 kB
| """ | |
| File: /lang_detect.py | |
| Project: nlp | |
| Created Date: Sunday February 8th 2026 | |
| Author: Christian Nonis <alch.infoemail@gmail.com> | |
| ----- | |
| Last Modified: Sunday February 8th 2026 11:21:40 am | |
| Modified By: Christian Nonis <alch.infoemail@gmail.com> | |
| ----- | |
| """ | |
| import spacy | |
| from spacy.language import Language | |
| from spacy_langdetect import LanguageDetector | |
| import functools | |
| from langid import classify | |
| from src.utils.nlp.spacy import MODEL_NAMES, _spacy_manager | |
| def _langid_cached(text: str) -> tuple[str, float]: | |
| return classify(text) | |
| def get_lang_detector(nlp, name): | |
| return LanguageDetector() | |
| nlp_det = spacy.blank("en") | |
| Language.factory("language_detector", func=get_lang_detector) | |
| nlp_det.add_pipe("language_detector", last=True) | |
| def detect_language(text: str) -> str: | |
| return nlp_det(text)._.language["language"] | |
| def get_nlp_for_text(text: str): | |
| lang = detect_language(text) | |
| if lang not in MODEL_NAMES: | |
| return None | |
| return _spacy_manager.get_model(lang) | |
| def langid_detect(text: str) -> tuple[str, float]: | |
| return _langid_cached(text) | |