Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
majiidd committed Nov 7, 2024
1 parent 7253aaf commit 27a2865
Showing 1 changed file with 20 additions and 57 deletions.
77 changes: 20 additions & 57 deletions persiantools/digits.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,10 @@
import re
from typing import Tuple

EN_TO_FA_MAP = {
"0": "۰",
"1": "۱",
"2": "۲",
"3": "۳",
"4": "۴",
"5": "۵",
"6": "۶",
"7": "۷",
"8": "۸",
"9": "۹",
}
AR_TO_FA_MAP = {
"٠": "۰",
"١": "۱",
"٢": "۲",
"٣": "۳",
"٤": "۴",
"٥": "۵",
"٦": "۶",
"٧": "۷",
"٨": "۸",
"٩": "۹",
}
FA_TO_EN_MAP = {
"۰": "0",
"۱": "1",
"۲": "2",
"۳": "3",
"۴": "4",
"۵": "5",
"۶": "6",
"۷": "7",
"۸": "8",
"۹": "9",
}
FA_TO_AR_MAP = {
"۰": "٠",
"۱": "١",
"۲": "٢",
"۳": "٣",
"۴": "٤",
"۵": "٥",
"۶": "٦",
"۷": "٧",
"۸": "٨",
"۹": "٩",
}

EN_TO_FA_REGEX = re.compile("|".join(EN_TO_FA_MAP.keys()))
AR_TO_FA_REGEX = re.compile("|".join(AR_TO_FA_MAP.keys()))
FA_TO_EN_REGEX = re.compile("|".join(FA_TO_EN_MAP.keys()))
FA_TO_AR_REGEX = re.compile("|".join(FA_TO_AR_MAP.keys()))
EN_TO_FA_MAP = str.maketrans("0123456789", "۰۱۲۳۴۵۶۷۸۹")
AR_TO_FA_MAP = str.maketrans("٠١٢٣٤٥٦٧٨٩", "۰۱۲۳۴۵۶۷۸۹")
FA_TO_EN_MAP = str.maketrans("۰۱۲۳۴۵۶۷۸۹", "0123456789")
FA_TO_AR_MAP = str.maketrans("۰۱۲۳۴۵۶۷۸۹", "٠١٢٣٤٥٦٧٨٩")

ONES = ("یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت", "نه")
TENS = ("بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود")
Expand Down Expand Up @@ -117,7 +68,10 @@ def en_to_fa(string: str) -> str:
>>> print(converted)
۰۱۲۳۴۵۶۷۸۹
"""
return EN_TO_FA_REGEX.sub(lambda x: EN_TO_FA_MAP[x.group()], string)
if not isinstance(string, str):
raise TypeError("Input must be a string")

return string.translate(EN_TO_FA_MAP)


def ar_to_fa(string: str) -> str:
Expand All @@ -139,7 +93,10 @@ def ar_to_fa(string: str) -> str:
>>> print(converted)
۰۱۲۳۴۵۶۷۸۹
"""
return AR_TO_FA_REGEX.sub(lambda x: AR_TO_FA_MAP[x.group()], string)
if not isinstance(string, str):
raise TypeError("Input must be a string")

return string.translate(AR_TO_FA_MAP)


def fa_to_en(string: str) -> str:
Expand All @@ -161,7 +118,10 @@ def fa_to_en(string: str) -> str:
>>> print(converted)
0123456789
"""
return FA_TO_EN_REGEX.sub(lambda x: FA_TO_EN_MAP[x.group()], string)
if not isinstance(string, str):
raise TypeError("Input must be a string")

return string.translate(FA_TO_EN_MAP)


def fa_to_ar(string: str) -> str:
Expand All @@ -183,7 +143,10 @@ def fa_to_ar(string: str) -> str:
>>> print(converted)
٠١٢٣٤٥٦٧٨٩
"""
return FA_TO_AR_REGEX.sub(lambda x: FA_TO_AR_MAP[x.group()], string)
if not isinstance(string, str):
raise TypeError("Input must be a string")

return string.translate(FA_TO_AR_MAP)


def _to_word(number: int, depth: bool) -> str:
Expand Down

0 comments on commit 27a2865

Please sign in to comment.