Last active
June 12, 2026 07:56
-
-
Save hamidb80/026661053fe1fb9b3fd97b05d0ffa3ae to your computer and use it in GitHub Desktop.
fixes stupid persian characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from pprint import pp | |
| REPL = { | |
| '·': '.', | |
| '»': '"', | |
| '×': '×', | |
| 'ː': ':', | |
| '˙': '.', | |
| '،': '،', | |
| '؍': '/', | |
| '؟': '؟', | |
| 'ؠ': 'ی', | |
| 'ء': 'ء', | |
| 'آ': 'آ', | |
| 'ؤ': 'ؤ', | |
| 'إ': 'إ', | |
| 'ئ': 'ئ', | |
| 'ا': 'ا', | |
| 'ب': 'ب', | |
| 'ت': 'ت', | |
| 'ث': 'ث', | |
| 'ج': 'ج', | |
| 'ح': 'ح', | |
| 'خ': 'خ', | |
| 'د': 'د', | |
| 'ذ': 'ذ', | |
| 'ر': 'ر', | |
| 'ز': 'ز', | |
| 'س': 'س', | |
| 'ش': 'ش', | |
| 'ص': 'ص', | |
| 'ض': 'ض', | |
| 'ط': 'ط', | |
| 'ظ': 'ظ', | |
| 'ع': 'ع', | |
| 'غ': 'غ', | |
| 'ف': 'ف', | |
| 'ق': 'ق', | |
| 'ل': 'ل', | |
| 'م': 'م', | |
| 'ن': 'ن', | |
| 'ه': 'ه', | |
| 'و': 'و', | |
| 'ي': 'ی', | |
| 'ً': 'ً', | |
| '٪': '%', | |
| '٫': '.', | |
| '٬': ',', | |
| 'ٱ': 'ا', | |
| 'پ': 'پ', | |
| 'چ': 'چ', | |
| 'ژ': 'ژ', | |
| 'ک': 'ک', | |
| 'گ': 'گ', | |
| 'ۂ': 'ه', | |
| 'ی': 'ی', | |
| '۰': '0', | |
| '٠': '0', | |
| '۱': '1', | |
| '۲': '2', | |
| '۳': '3', | |
| '۴': '4', | |
| '۵': '5', | |
| '۶': '6', | |
| '۷': '7', | |
| '۸': '8', | |
| '٩': '9', | |
| '\u200c': ' ', | |
| '‐': '-', | |
| '‑': '-', | |
| '–': '-', | |
| '—': '-', | |
| '―': '-', | |
| '’': "'", | |
| '‚': ',', | |
| '“': '"', | |
| '”': '"', | |
| '•': '*', | |
| '…': '…', | |
| '‹': '<', | |
| '›': '>', | |
| '⁺': '+', | |
| '⁻': '-', | |
| '⁼': '=', | |
| 'ﭘ': 'پ', | |
| 'ﭼ': 'چ', | |
| 'ﮋ': 'ژ', | |
| 'ﮒ': 'گ', | |
| 'ﮓ': 'گ', | |
| 'ﮔ': 'گ', | |
| 'ﮕ': 'گ', | |
| '﹑': ',', | |
| '﹔': ';', | |
| '﹕': ':', | |
| '﹖': '?', | |
| '﹗': '!', | |
| '﹙': '(', | |
| '﹚': ')', | |
| '﹟': '#', | |
| '﹠': '&', | |
| '﹤': '<', | |
| '﹥': '>', | |
| 'ﺁ': 'آ', | |
| 'ﺄ': 'أ', | |
| 'ﺆ': 'و', | |
| 'ﺋ': 'ئ', | |
| 'ﺌ': 'ئ', | |
| 'ﺍ': 'ا', | |
| 'ﺎ': 'ا', | |
| 'ﺏ': 'ب', | |
| 'ﺐ': 'ب', | |
| 'ﺑ': 'ب', | |
| 'ﺒ': 'ب', | |
| 'ﺕ': 'ت', | |
| 'ﺖ': 'ت', | |
| 'ﺗ': 'ت', | |
| 'ﺘ': 'ت', | |
| 'ﺚ': 'ث', | |
| 'ﺛ': 'ث', | |
| 'ﺜ': 'ث', | |
| 'ﺝ': 'ج', | |
| 'ﺞ': 'ج', | |
| 'ﺟ': 'ج', | |
| 'ﺠ': 'ج', | |
| 'ﺢ': 'ح', | |
| 'ﺣ': 'ح', | |
| 'ﺤ': 'ح', | |
| 'ﺥ': 'خ', | |
| 'ﺦ': 'خ', | |
| 'ﺧ': 'خ', | |
| 'ﺨ': 'خ', | |
| 'ﺩ': 'د', | |
| 'ﺪ': 'د', | |
| 'ﺬ': 'ذ', | |
| 'ﺭ': 'ر', | |
| 'ﺮ': 'ر', | |
| 'ﺯ': 'ز', | |
| 'ﺰ': 'ز', | |
| 'ﺲ': 'س', | |
| 'ﺳ': 'س', | |
| 'ﺴ': 'س', | |
| 'ﺵ': 'ش', | |
| 'ﺶ': 'ش', | |
| 'ﺷ': 'ش', | |
| 'ﺸ': 'ش', | |
| 'ﺹ': 'ص', | |
| 'ﺺ': 'ص', | |
| 'ﺻ': 'ص', | |
| 'ﺼ': 'ص', | |
| 'ﺽ': 'ض', | |
| 'ﺿ': 'ض', | |
| 'ﻀ': 'ض', | |
| 'ﻁ': 'ط', | |
| 'ﻂ': 'ط', | |
| 'ﻃ': 'ط', | |
| 'ﻄ': 'ط', | |
| 'ﻈ': 'ظ', | |
| 'ﻉ': 'ع', | |
| 'ﻊ': 'ع', | |
| 'ﻋ': 'ع', | |
| 'ﻌ': 'ع', | |
| 'ﻐ': 'غ', | |
| 'ﻑ': 'ف', | |
| 'ﻒ': 'ف', | |
| 'ﻓ': 'ف', | |
| 'ﻔ': 'ف', | |
| 'ﻖ': 'ق', | |
| 'ﻗ': 'ق', | |
| 'ﻘ': 'ق', | |
| 'ﻚ': 'ک', | |
| 'ﻛ': 'ک', | |
| 'ﻜ': 'ک', | |
| 'ﻝ': 'ل', | |
| 'ﻞ': 'ل', | |
| 'ﻟ': 'ل', | |
| 'ﻠ': 'ل', | |
| 'ﻡ': 'م', | |
| 'ﻢ': 'م', | |
| 'ﻣ': 'م', | |
| 'ﻤ': 'م', | |
| 'ﻥ': 'ن', | |
| 'ﻦ': 'ن', | |
| 'ﻧ': 'ن', | |
| 'ﻨ': 'ن', | |
| 'ﻩ': 'ه', | |
| 'ﻪ': 'ه', | |
| 'ﻫ': 'ه', | |
| 'ﻬ': 'ه', | |
| 'ﻭ': 'و', | |
| 'ﻮ': 'و', | |
| 'ﻱ': 'ی', | |
| 'ﻲ': 'ی', | |
| 'ﻳ': 'ی', | |
| 'ﻴ': 'ی', | |
| 'ﻻ': 'لا', | |
| 'ﻼ': 'لا', | |
| '"': '"', | |
| '#': '#', | |
| '&': '&', | |
| ''': "'", | |
| '*': '*', | |
| '+': '+', | |
| '/': '/', | |
| '<': '<', | |
| '=': '=', | |
| '>': '>', | |
| '@': '@', | |
| '\': '\\', | |
| '`': '`'} | |
| # ----------------------------------- | |
| def is_ascii(char): | |
| return ord(char) < 128 | |
| def purify_persian(s: str) -> str: | |
| """Process Persian text and return a set of unique characters""" | |
| result = [] | |
| seen = set() | |
| for ch in s: | |
| if ch in REPL: | |
| result.append(REPL[ch]) | |
| else: | |
| result.append(ch) | |
| if not is_ascii(ch) and ch not in seen: | |
| print("missing: ", ch) | |
| seen.add(ch) | |
| return ''.join(result) | |
| # --------------------------------------- | |
| if __name__ == "__main__": | |
| N = len(sys.argv) | |
| if N == 3: | |
| finpath = sys.argv[1] | |
| foutpath = sys.argv[2] | |
| with open(finpath, 'r', encoding='utf-8') as f: | |
| content = f.read() | |
| result = purify_persian(content) | |
| with open(foutpath, 'w', encoding='utf-8') as f: | |
| f.write(result) | |
| if N == 2: | |
| pp(dict(sorted(REPL.items()))) | |
| else: | |
| print(""" | |
| usage: | |
| python app.py input_file_path output_file_path | |
| see replacements: | |
| python app.py etc | |
| see help: | |
| python app.py | |
| """, file=sys.stderr) | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment