Spaces:
Sleeping
Sleeping
Download utils.py from frznkshn/TextNormalizer: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/frznkshn/TextNormalizer/resolve/main/utils.py
- Command line
-
hf download hf://spaces/frznkshn/TextNormalizer/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/frznkshn/TextNormalizer/resolve/main/utils.py
1.29 kB
| import re | |
| NO_LETTERS = "Unknown / No letters found" | |
| def detect_language(text: str) -> str: | |
| cleaned_text = re.sub(r'[\s\d\W_]+', '', text) | |
| if not cleaned_text: | |
| return NO_LETTERS | |
| persian_pattern = re.compile(r'[\u0600-\u06FF\u0750-\u077F\uFB50-\uFDFF\uFE70-\uFEFF]') | |
| english_pattern = re.compile(r'[a-zA-Z]') | |
| persian_count = len(persian_pattern.findall(cleaned_text)) | |
| english_count = len(english_pattern.findall(cleaned_text)) | |
| if persian_count > english_count: | |
| return "fa" | |
| elif english_count > persian_count: | |
| return "en" | |
| else: | |
| return "mixed" | |
| def detect_digit_language(text: str): | |
| """Guesses the language from the digits alone: Persian digits -> "fa", | |
| ASCII digits -> "en", no digits -> None. Used when a text has no letters.""" | |
| if re.search(r'[\u06F0-\u06F9\u0660-\u0669]', text): | |
| return "fa" | |
| if re.search(r'[0-9]', text): | |
| return "en" | |
| return None | |
| def convert_fa_num_to_en(text:str) -> str: | |
| fa_to_en = { | |
| '۰':'0', | |
| '۱':'1', | |
| '۲':'2', | |
| '۳':'3', | |
| '۴':'4', | |
| '۵':'5', | |
| '۶':'6', | |
| '۷':'7', | |
| '۸':'8', | |
| '۹':'9', | |
| } | |
| for k, v in fa_to_en.items(): | |
| text = text.replace(k, v) | |
| return text |