Files
init_dataParser/app/common/text_normalizer.py
T
2025-12-04 13:47:58 +03:30

562 lines
28 KiB
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
class TextNormalizer:
def __init__(self, options=None):
self.options = (
options
if options
else {
"charArNormal": True,
"errorLine": True,
"alphabetsNormal": False,
"emptylinesNormal": False,
"hamzehNormal": False,
"spaceCorrection": False,
"trimLine": False,
}
)
def split_words(self, text):
if not text:
return []
### “”
words = re.split(
r"[\t\n\r\.\'\"«»\)\(\]\[\{\}\%\#\$\*\<\>\/,;،؛ \-!?:٭؟]+", text
)
return words
def normalize_search_qanon(self, text, error_state=False):
if not text:
return ""
text = text.strip()
# اصلاح خطاهای متن در کد خط
text = self.replace_alphabets(text)
# اعداد چسبیده به کلمات را با یک فاصله جدا می کند
# text = self.word_digit_correction(text)
text = text.replace("\u00a0", " ") # No Break space
text = re.sub(r"\u200e|\u200f", " ", text) # LTR, RTL
# فاصله های تکراری و خطوط خالی تکراری را یکی می کند
text = re.sub(r"( )+", " ", text)
text = re.sub(r"(\n)+", "", text)
# نرمال کردن حروف اختلافی عربی و فارسی
text = self.yeke_normal(text)
# text = self.hamzah_normal(text)
# text = self.alaem_normal(text)
return text
def normalize_qanon(self, text, error_state=False):
if not text:
return ""
#چون NaN تنها مقداری است که خودش با خودش برابر نیست
if text != text : # True اگر NaN باشد:
return ""
text = text.strip()
# اصلاح خطاهای متن در کد خط
text = text.replace("\\n", "\n")
text = text.replace("\\r", "\r")
text = re.sub(r"(\r\n)+", "\n", text)
text = text.replace("\r", "\n")
# یکسان سازی اختلاف حروف کیبوردهای مختلف - کدهای مختلف ولی نمایش یکسان
if error_state:
text = self.replace_alphabets_error(text)
else:
text = self.replace_alphabets(text)
# اعداد چسبیده به کلمات را با یک فاصله جدا می کند
text = self.word_digit_correction(text)
# فاصله های تکراری و خطوط خالی تکراری را یکی می کند
text = re.sub(r"( )+", " ", text)
text = re.sub(r"(\n)+", "\n", text)
# نرمال کردن حروف اختلافی عربی و فارسی
text = self.yeke_normal(text)
return text
def normalize_qanon_test(self, text):
if not text:
return ""
text = text.replace("\u00a0", " ") # No Break space
text = re.sub(r"\u200e|\u200f", " ", text) # LTR, RTL
text = self.word_digit_correction(text)
text = re.sub(r"( )+", " ", text)
text = self.space_correction(text)
text = self.heha_correction(text)
return text
def normalize_text(self, text):
if not text:
return ""
text = text.strip()
# اصلاح خطاهای متن در کد خط
if self.options.get("errorLine", False):
text = text.replace("\\n", "\n")
text = text.replace("\\r", "\r")
text = re.sub(r"(\r\n)+", "\n", text)
text = text.replace("\r", "\n")
# یکسان سازی اختلاف حروف کیبوردهای مختلف - کدهای مختلف ولی نمایش یکسان
if self.options.get("alphabetsNormal", False):
text = self.replace_alphabets(text)
# حذف خطوط و فاصله‌های خالی
if self.options.get("emptylinesNormal", False):
text = re.sub(r"( )+", " ", text)
text = re.sub(r"(\n)+", "\n", text)
# نرمال کردن حروف اختلافی عربی و فارسی
if self.options.get("charArNormal", True):
text = self.yeke_normal(text)
# پرش از اختلافات همزه‌ا
if self.options.get("hamzehNormal", False):
text = self.hamzah_normal(text)
# اصلاح فاصله‌ها در ترکیب کلمات
if self.options.get("spaceCorrection", False):
text = self.space_correction(text)
# حذف خطوط اضافی اول و انتهای متن
if self.options.get("trimLine", False):
text = text.strip()
return text
def alaem_normal(self, text):
text = re.sub(
r"[\t\n\r\.\'\"«»\)\(\]\[\{\}\%\#\$\*\<\>\/,;،؛\-!?:٭؟]+", "", text
)
return text
def yeke_normal(self, text):
text = text.replace("ي", "ی")
text = text.replace("ك", "ک")
return text
def hamzah_normal(self, text):
text = re.sub(r"ئ|ﺋ", "ی", text)
text = re.sub(r"ؤ|ﺅ", "و", text)
text = re.sub(r"ﺔ|ۀ|ة", "ه", text)
text = re.sub(r"إ|أ", "ا", text)
return text
def replace_alphabets_error(self, text):
if not text:
return ""
text = re.sub(r"\u200e|\u200f", " ", text) # LTR, RTL
text = re.sub(r"\u00AD|\u00AC", "\u200c", text) # نیم اسپیس خاص
text = re.sub(r"(\u002C)+", "\u200c", text) # نیم اسپیس تکراری
text = re.sub(r"ﺁ|آ", "آ", text)
text = re.sub(r"ﺎ|ٱ|ﺍ", "ا", text)
text = re.sub(r"ٲ|أ|ﺄ|ﺃ", "أ", text)
text = re.sub(r"ﺈ|ﺇ", "إ", text)
text = re.sub(r"ﺐ|ﺏ|ﺑ|ﺒ", "ب", text)
text = re.sub(r"ﭖ|ﭗ|ﭙ|ﭘ", "پ", text)
text = re.sub(r"ﭡ|ٺ|ٹ|ﭞ|ٿ|ټ|ﺕ|ﺗ|ﺖ|ﺘ", "ت", text)
text = re.sub(r"ﺙ|ﺛ|ﺚ|ﺜ", "ث", text)
text = re.sub(r"ﺝ|ڃ|ﺠ|ﺟ", "ج", text)
text = re.sub(r"ڃ|ﭽ|ﭼ", "چ", text)
text = re.sub(r"ﺢ|ﺤ|څ|ځ|ﺣ", "ح", text)
text = re.sub(r"ﺥ|ﺦ|ﺨ|ﺧ", "خ", text)
text = re.sub(r"ڏ|ډ|ﺪ|ﺩ", "د", text)
text = re.sub(r"ﺫ|ﺬ|ذ", "ذ", text)
text = re.sub(r"ڙ|ڗ|ڒ|ڑ|ڕ|ﺭ|ﺮ", "ر", text)
text = re.sub(r"ﺰ|ﺯ", "ز", text)
text = re.sub(r"ﮊ", "ژ", text)
text = re.sub(r"ݭ|ݜ|ﺱ|ﺲ|ښ|ﺴ|ﺳ", "س", text)
text = re.sub(r"ﺵ|ﺶ|ﺸ|ﺷ", "ش", text)
text = re.sub(r"ﺺ|ﺼ|ﺻ|ﺹ", "ص", text)
text = re.sub(r"ﺽ|ﺾ|ﺿ|ﻀ", "ض", text)
text = re.sub(r"ﻁ|ﻂ|ﻃ|ﻄ", "ط", text)
text = re.sub(r"ﻆ|ﻇ|ﻈ", "ظ", text)
text = re.sub(r"ڠ|ﻉ|ﻊ|ﻋ|ﻌ", "ع", text)
text = re.sub(r"ﻎ|ۼ|ﻍ|ﻐ|ﻏ", "غ", text)
text = re.sub(r"ﻒ|ﻑ|ﻔ|ﻓ", "ف", text)
text = re.sub(r"ﻕ|ڤ|ﻖ|ﻗ|ﻘ", "ق", text)
text = re.sub(r"ڭ|ﻚ|ﮎ|ﻜ|ﮏ|ګ|ﻛ|ﮑ|ﮐ|ڪ|ك", "ک", text)
text = re.sub(r"ﮚ|ﮒ|ﮓ|ﮕ|ﮔ", "گ", text)
text = re.sub(r"ﻝ|ﻞ|ﻠ|ڵ|ﻟ", "ل", text)
text = re.sub(r"ﻵ|ﻶ|ﻷ|ﻸ|ﻹ|ﻺ|ﻻ|ﻼ", "لا", text)
text = re.sub(r"ﻡ|ﻤ|ﻢ|ﻣ", "م", text)
text = re.sub(r"ڼ|ﻦ|ﻥ|ﻨ|ﻧ", "ن", text)
text = re.sub(r"ވ|ﯙ|ۈ|ۋ|ﺆ|ۊ|ۇ|ۏ|ۅ|ۉ|ﻭ|ﻮ|ؤ", "و", text)
text = re.sub(r"ﻬ|ھ|ﻩ|ﻫ|ﻪ|ە|ہ", "ه", text)
text = re.sub(r"ﭛ|ﻯ|ۍ|ﻰ|ﻱ|ﻲ|ں|ﻳ|ﻴ|ﯼ|ې|ﯽ|ﯾ|ﯿ|ێ|ے|ى|ي", "ی", text)
text = re.sub(r"¬", "‌", text)
text = re.sub(r"•|·|●|·|・|∙|。|ⴰ", ".", text)
text = re.sub(r",|٬|٫|‚|،", "،", text)
text = re.sub(r"ʕ", "؟", text)
text = re.sub(r"”|“", '"', text)
text = re.sub(r"۰|٠", "0", text)
text = re.sub(r"۱|١", "1", text)
text = re.sub(r"۲|٢", "2", text)
text = re.sub(r"۳|٣", "3", text)
text = re.sub(r"۴|٤", "4", text)
text = re.sub(r"۵", "5", text)
text = re.sub(r"۶|٦", "6", text)
text = re.sub(r"۷|٧", "7", text)
text = re.sub(r"۸|٨", "8", text)
text = re.sub(r"۹|٩", "9", text)
text = re.sub(r"²", "2", text)
text = re.sub(r"|ِ|ُ|َ|ٍ|ٌ|ً|", "", text)
text = re.sub(
r"ـ", "", text
) # حذف حروف کشیده # این باعث خطای حذف اضافی شد و با خط بعدی جایگزین شد
return text
def replace_alphabets(self, text):
if not text:
return ""
text = re.sub(r"\u200e|\u200f", " ", text) # LTR, RTL
text = re.sub(r"\u00AD|\u00AC", "\u200c", text) # نیم اسپیس خاص
text = re.sub(r"(\u002C)+", "\u200c", text) # نیم اسپیس تکراری
text = re.sub(r"ﺁ|آ", "آ", text)
text = re.sub(r"ﺎ|ٱ|ﺍ", "ا", text)
text = re.sub(r"ٲ|أ|ﺄ|ﺃ", "أ", text)
text = re.sub(r"ﺈ|ﺇ", "إ", text)
text = re.sub(r"ﺐ|ﺏ|ﺑ|ﺒ", "ب", text)
text = re.sub(r"ﭖ|ﭗ|ﭙ|ﭘ", "پ", text)
text = re.sub(r"ﭡ|ٺ|ٹ|ﭞ|ٿ|ټ|ﺕ|ﺗ|ﺖ|ﺘ", "ت", text)
text = re.sub(r"ﺙ|ﺛ|ﺚ|ﺜ", "ث", text)
text = re.sub(r"ﺝ|ڃ|ﺠ|ﺟ", "ج", text)
text = re.sub(r"ڃ|ﭽ|ﭼ", "چ", text)
text = re.sub(r"ﺢ|ﺤ|څ|ځ|ﺣ", "ح", text)
text = re.sub(r"ﺥ|ﺦ|ﺨ|ﺧ", "خ", text)
text = re.sub(r"ڏ|ډ|ﺪ|ﺩ", "د", text)
text = re.sub(r"ﺫ|ﺬ|ذ", "ذ", text)
text = re.sub(r"ڙ|ڗ|ڒ|ڑ|ڕ|ﺭ|ﺮ", "ر", text)
text = re.sub(r"ﺰ|ﺯ", "ز", text)
text = re.sub(r"ﮊ", "ژ", text)
text = re.sub(r"ݭ|ݜ|ﺱ|ﺲ|ښ|ﺴ|ﺳ", "س", text)
text = re.sub(r"ﺵ|ﺶ|ﺸ|ﺷ", "ش", text)
text = re.sub(r"ﺺ|ﺼ|ﺻ|ﺹ", "ص", text)
text = re.sub(r"ﺽ|ﺾ|ﺿ|ﻀ", "ض", text)
text = re.sub(r"ﻁ|ﻂ|ﻃ|ﻄ", "ط", text)
text = re.sub(r"ﻆ|ﻇ|ﻈ", "ظ", text)
text = re.sub(r"ڠ|ﻉ|ﻊ|ﻋ|ﻌ", "ع", text)
text = re.sub(r"ﻎ|ۼ|ﻍ|ﻐ|ﻏ", "غ", text)
text = re.sub(r"ﻒ|ﻑ|ﻔ|ﻓ", "ف", text)
text = re.sub(r"ﻕ|ڤ|ﻖ|ﻗ|ﻘ", "ق", text)
text = re.sub(r"ڭ|ﻚ|ﮎ|ﻜ|ﮏ|ګ|ﻛ|ﮑ|ﮐ|ڪ|ك", "ک", text)
text = re.sub(r"ﮚ|ﮒ|ﮓ|ﮕ|ﮔ", "گ", text)
text = re.sub(r"ﻝ|ﻞ|ﻠ|ڵ|ﻟ", "ل", text)
text = re.sub(r"ﻵ|ﻶ|ﻷ|ﻸ|ﻹ|ﻺ|ﻻ|ﻼ", "لا", text)
text = re.sub(r"ﻡ|ﻤ|ﻢ|ﻣ", "م", text)
text = re.sub(r"ڼ|ﻦ|ﻥ|ﻨ|ﻧ", "ن", text)
text = re.sub(r"ވ|ﯙ|ۈ|ۋ|ﺆ|ۊ|ۇ|ۏ|ۅ|ۉ|ﻭ|ﻮ|ؤ", "و", text)
text = re.sub(r"ﻬ|ھ|ﻩ|ﻫ|ﻪ|ە|ہ", "ه", text)
text = re.sub(r"ﭛ|ﻯ|ۍ|ﻰ|ﻱ|ﻲ|ں|ﻳ|ﻴ|ﯼ|ې|ﯽ|ﯾ|ﯿ|ێ|ے|ى|ي", "ی", text)
text = re.sub(r"¬", "‌", text)
text = re.sub(r"•|·|●|·|・|∙|。|ⴰ", ".", text)
text = re.sub(r",|٬|٫|‚|،", "،", text)
text = re.sub(r"ʕ", "؟", text)
text = re.sub(r"”|“", '"', text)
text = re.sub(r"۰|٠", "0", text)
text = re.sub(r"۱|١", "1", text)
text = re.sub(r"۲|٢", "2", text)
text = re.sub(r"۳|٣", "3", text)
text = re.sub(r"۴|٤", "4", text)
text = re.sub(r"۵", "5", text)
text = re.sub(r"۶|٦", "6", text)
text = re.sub(r"۷|٧", "7", text)
text = re.sub(r"۸|٨", "8", text)
text = re.sub(r"۹|٩", "9", text)
text = re.sub(r"²", "2", text)
text = re.sub(r"|ِ|ُ|َ|ٍ|ٌ|ً|", "", text)
# text = re.sub(r"ـ", "", text) # حذف حروف کشیده # این باعث خطای حذف اضافی شد و با خط بعدی جایگزین شد
text = re.sub(
r"([\u0600-\u06FF])ـ([\u0600-\u06FF])", r"\1\2", text
) # حذف حروف کشیده
text = re.sub(r"([\u0600-\u06FF])ـ", r"\1", text) # حذف حروف کشیده
text = text.strip("\u200c").strip()
return text
def repair_qanon_html(self, html):
if not html :
return ""
text = html
text = re.sub(r'(\t)+', ' ', text)
text = re.sub(r'( )+', ' ', text)
text = text.replace("\\n", "")
text = text.replace("\\r", "")
text = re.sub(r"(\r\n)+", "", text)
text = text.replace("\r", "")
text = re.sub(r"(\d+?) /(\d+?) /(\d+?)", r"\1/\2/\3", text)
text = re.sub(r"(\d+?)/ (\d+?)/ (\d+?)", r"\1/\2/\3", text)
# جابجا کردن اعداد تاریخ
# text = re.sub(r"(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})", r"\3/\2/\1", text)
text = re.sub(r"(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})", r"\3/\2/\1", text)
return text
def date_correction(self, text, day_to_year=True):
text = re.sub(r"(\d+?)ر(\d+?)ر(\d+?)", r"\1/\2/\3", text)
text = re.sub(r"(\d+?)ر(\d+?)", r"\1/\2", text)
text = re.sub(r"(\d+?) /(\d+?) /(\d+?)", r"\1/\2/\3", text)
# text = re.sub(r"(\d+?) \\(\d+?) \\(\d+?)", r"\1/\2/\3", text)
# # فرمت YYYY/MM/DD یا YYYY-MM-DD --> YYYY/MM/DD
# text = re.sub(r'(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})', r'\1/\2/\3', text)
if not day_to_year :
# فرمت DD/MM/YYYY یا DD-MM-YYYY ---> YYYY/MM/DD
text = re.sub(r"^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})", r"\1/\2/\3", text)
text = re.sub(r"^(\d{2})[\/\-](\d{2})[\/\-](\d{4})", r"\3/\2/\1", text)
text = re.sub(r"^(\d{1})[\/\-](\d{1})[\/\-](\d{4})", r"\3/0\2/0\1", text)
text = re.sub(r"^(\d{1})[\/\-](\d{2})[\/\-](\d{4})", r"\3/\2/0\1", text)
text = re.sub(r"^(\d{2})[\/\-](\d{1})[\/\-](\d{4})", r"\3/0\2/\1", text)
else :
# فرمت DD/MM/YYYY یا DD-MM-YYYY ---> YYYY/MM/DD
text = re.sub(r"^(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})", r"\3/\2/\1", text)
text = re.sub(r"^(\d{2})[\/\-](\d{2})[\/\-](\d{4})", r"\1/\2/\3", text)
text = re.sub(r"^(\d{1})[\/\-](\d{1})[\/\-](\d{4})", r"0\1/0\2/\3", text)
text = re.sub(r"^(\d{1})[\/\-](\d{2})[\/\-](\d{4})", r"0\1/\2/\3", text)
text = re.sub(r"^(\d{2})[\/\-](\d{1})[\/\-](\d{4})", r"\1/0\2/\3", text)
return text
def space_correction(self, text):
text = re.sub(r"^(بی|می|نمی)( )", r"\1‌", text)
text = re.sub(r"( )(می|نمی|بی)( )", r"\1\2‌", text)
text = re.sub(
r"( )(هایی|ها|های|ایی|هایم|هایت|هایش|هایمان|هایتان|هایشان|ات|ان|ین|انی|بان|ام|ای|یم|ید|اید|اند|بودم|بودی|بود|بودیم|بودید|بودند|ست)( )",
r"‌\2\3",
text,
)
# text = re.sub(r"( )(شده|نشده)( )", r"‌\2‌", text)
text = re.sub(
r"( )(طلبان|طلب|گرایی|گرایان|شناس|شناسی|گذاری|گذار|گذاران|شناسان|گیری|پذیری|بندی|آوری|سازی|بندی|کننده|کنندگان|گیری|پرداز|پردازی|پردازان|آمیز|سنجی|ریزی|داری|دهنده|آمیز|پذیری|پذیر|پذیران|گر|ریز|ریزی|رسانی|یاب|یابی|گانه|گانه‌ای|انگاری|گا|بند|رسانی|دهندگان|دار)( )",
r"‌\2\3",
text,
)
return text
# اعداد چسبیده به کلمات را با یک فاصله جدا می کند
def word_digit_correction(self, text):
text = re.sub(r"(\200C)(\d+)", r" \2", text)
text = re.sub(r"(\d+)(\200C)", r"\1 ", text)
text = re.sub(
r"(\200C)([\t\n\r\.\'\"«»\)\(\]\[\{\}\#\$\*\<\>\/,;،؛ \-!?:٭؟])",
r" \2",
text,
)
text = re.sub(
r"([\t\n\r\.\'\"«»\)\(\]\[\{\}\#\$\*\<\>\/,;،؛ \-!?:٭؟])(\200C)",
r"\1 ",
text,
)
# ## شماره22 -> شماره 22 22/ش2
# ### no ش/22 شد\22
# text = re.sub(r"([^\d \/\\]+?)(\d)", r"\1 \2", text)
# text = re.sub(r"(\d+)([^\d \/\\]+?)", r"\1 \2", text)
text = re.sub(r"[^\/](\d+)([\u0600-\u06FF\u0750-\u077F]+?)", r"\1 \2", text)
text = re.sub(r"([\u0600-\u06FF\u0750-\u077F]+?)(\d+)", r"\1 \2", text)
# ر54ر44ر135
text = re.sub(r"(\d+?)ر(\d+?)ر(\d+?)", r"\1\/\2\/\3", text)
text = re.sub(r"(\d+?)ر(\d+?)", r"\1\/\2", text)
# text = re.sub(r"(\w+)(\d+)", r"\1 \2", text)
# text = re.sub(r"(\d+)(\w+)", r"\1 \2", text)
return text
## رسیدگیها -> رسیدگی‌ها
def heha_correction(self, text):
text = re.sub(r"(ه)(ها )", r"\1‌\2", text)
text = re.sub(r"(ه)(های )", r"\1‌\2", text)
text = re.sub(r"(ی)(ها )", r"\1‌\2", text)
text = re.sub(r"(ی)(های )", r"\1‌\2", text)
return text
def pad_left(self, s, length, char=" "):
if len(s) >= length:
return s
else:
return char * (length - len(s)) + s
# erab
def remove_arabic_diacritics(self, text):
# Regular expression to match all Arabic diacritics
diacritics_pattern = r"[\u064B-\u0652\u0670]"
return re.sub(diacritics_pattern, "", text)
# TEST M
def replace_full_alphabets_error(self, text):
if not text:
return ""
self.pattern = [
(re.compile(r"\u200e|\u200f"), " "), # LTR, RTL
(re.compile(r"\u00AD|\u00AC"), "\u200c"), # نیم اسپیس خاص
(re.compile(r"(\u002C)+"), "\u200c"), # نیم اسپیس تکراری
(re.compile(r"ﺁ|آ"), "آ"),
(re.compile(r"ﺎ|ٱ|ﺍ"), "ا"),
(re.compile(r"ٲ|أ|ﺄ|ﺃ"), "أ"),
(re.compile(r"ﺈ|ٳ|ﺇ"), "إ"),
(re.compile(r"ﺐ|ﺏ|ﺑ|ٮ|ﺒ"), "ب"),
(re.compile(r"ﭖ|ﭗ|ﭙ|ﭘ|ݒ|ݐ"), "پ"),
(re.compile(r"ﭡ|ٺ|ٹ|ﭞ|ٿ|ټ|ﺕ|ﺗ|ﺖ|ݓ|ﺘ"), "ت"),
(re.compile(r"ﺙ|ﺛ|ﺚ|ݑ|ݖ|ﺜ"), "ث"),
(re.compile(r"ﺝ|ڃ|ﺠ|ﺟ|ڄ"), "ج"),
(re.compile(r"ڃ|ﭽ|ݘ|ﭻ|ﭼ"), "چ"),
(re.compile(r"ﺢ|ﺤ|څ|ځ|ﺣ"), "ح"),
(re.compile(r"ﺥ|ﺦ|ﺨ|ﺧ"), "خ"),
(re.compile(r"ڏ|ډ|ﺪ|ڌ|ﺩ"), "د"),
(re.compile(r"ﺫ|ﺬ|ذ"), "ذ"),
(re.compile(r"ڙ|ڗ|ڒ|ڑ|ڕ|ﺭ|ړ|ڔ|ݫ|ݬ|ﺮ"), "ر"),
(re.compile(r"ﺰ|ﺯ"), "ز"),
(re.compile(r"ﮋ|ﮊ"), "ژ"),
(re.compile(r"ݭ|ݜ|ﺱ|ﺲ|ښ|ﺴ|ﺳ"), "س"),
(re.compile(r"ﺵ|ﺶ|ﺸ|ﺷ"), "ش"),
(re.compile(r"ﺺ|ﺼ|ﺻ|ﺹ"), "ص"),
(re.compile(r"ﺽ|ﺾ|ﺿ|ﻀ"), "ض"),
(re.compile(r"ﻁ|ﻂ|ﻃ|ڟ|ﻄ"), "ط"),
(re.compile(r"ﻆ|ﻇ|ﻈ"), "ظ"),
(re.compile(r"ڠ|ﻉ|ﻊ|ﻋ|ﻌ"), "ع"),
(re.compile(r"ﻎ|ۼ|ﻍ|ﻐ|ﻏ"), "غ"),
(re.compile(r"ﻒ|ڤ|ﻑ|ﻔ|ڡ|ݠ|ݡ|ﻓ"), "ف"),
(re.compile(r"ﻕ|ﻖ|ﻗ|ڨ|ﻘ|ٯ"), "ق"),
(re.compile(r"ڭ|ﻚ|ﮎ|ﻜ|ﮏ|ګ|ﻛ|ﮑ|ﮐ|ڪ|ݢ|ݣ|ݤ|ك"), "ک"),
(re.compile(r"ﮚ|ﮒ|ﮓ|ﮕ|ڳ|ﮔ"), "گ"),
(re.compile(r"ﻝ|ﻞ|ﻠ|ڵ|ﻟ"), "ل"),
(re.compile(r"ﻵ|ﻶ|ﻷ|ﻸ|ﻹ|ﻺ|ﻻ|ﻼ"), "لا"),
(re.compile(r"ﻡ|ﻤ|ﻢ|ݥ|ݦ|ﻣ"), "م"),
(re.compile(r"ڼ|ﻦ|ﻥ|ﻨ|ݔ|ݧ|ݨ|ݩ|ݪ|ﻧ"), "ن"),
(re.compile(r"ވ|ﯙ|ۈ|ۋ|ﺆ|ۊ|ۇ|ۏ|ۅ|ۉ|ﻭ|ﻮ|ٶ|ۆ|ؤ"), "و"),
(re.compile(r"ﻬ|ھ|ﻩ|ﻫ|ﻪ|ە|ۂ|ۃ|٥|ﮪ|ﮬ|ﮭ|ہ"), "ه"),
(re.compile(r"ﭛ|ﻯ|ۍ|ﻰ|ﻱ|ﻲ|ں|ﻳ|ﻴ|ﯼ|ې|ﯽ|ﯾ|ﯿ|ێ|ے|ى|ي"), "ی"),
(re.compile(r"¬"), "‌"),
(re.compile(r"•|·|●|·|・|∙|。|ⴰ"), "."),
(re.compile(r",|٬|٫|‚|،"), "،"),
(re.compile(r"ʕ"), "؟"),
(re.compile(r"”|“"), '"'),
(re.compile(r"۰|٠"), "0"),
(re.compile(r"۱|١"), "1"),
(re.compile(r"۲|٢"), "2"),
(re.compile(r"۳|٣|³"), "3"),
(re.compile(r"۴|٤"), "4"),
(re.compile(r"۵"), "5"),
(re.compile(r"۶|٦"), "6"),
(re.compile(r"۷|٧"), "7"),
(re.compile(r"۸|٨"), "8"),
(re.compile(r"۹|٩"), "9"),
(re.compile(r"²"), "2"),
(re.compile(r"|ِ|ُ|َ|ٍ|ٌ|ً|"), ""),
(
re.compile(r"ـ"),
"",
), # حذف حروف کشیده # این باعث خطای حذف اضافی شد و با خط بعدی جایگزین شد
]
if not text:
return ""
for pattern, repl in self.pattern:
text = pattern.sub(repl, text)
return text
def full_hamzah_normal(self, text):
text = re.sub(r"ئ|ﺌ|ﺋ", "ی", text)
text = re.sub(r"ؤ|ﺅ", "و", text)
text = re.sub(r"ﺔ|ۀ|ﺓ|ة", "ه", text)
text = re.sub(r"إ|أ", "ا", text)
return text
# TEST M
# ـکشیدگی هایی که به جای _ به کار رفته اند
def keshide_corrected(self, text):
(r"(\d)ـ(\d)", r"\1_\2"), # فاصله بین دو عدد
(r"(\d)ـ", r"\1_"), # عدد و خط تیره
(r"ـ(\d)", r"_\1"), # خط تیره و عدد
(r"\sـ\s", r" _ "), # فاصله‌های خالی بین خط تیره
(
r"(?<![ویهبتثجحخدذرزسشصضطظعغفقكلمنپچژکگ])ـ(?![ویهبتثجحخدذرزسشصضطظعغفقكلمنپچژکگ])",
r"_",
),
(r"\sـ\s", r" _ "),
pass
# # تست برنامه
# options = {
# "charArNormal": True, # نرمال کردن حروف عربی و فارسی
# "errorLine": True, # اصلاح خطاهای مربوط به خطوط و کاراکترها
# "alphabetsNormal": True, # نرمال‌سازی حروف
# "emptylinesNormal": True, # حذف فاصله‌های اضافی
# "hamzehNormal": True, # اصلاح همزه‌ها
# "spaceCorrection": True, # اصلاح فاصله‌ها در ترکیب کلمات
# "trimLine": True # حذف فضای خالی اضافی در ابتدا و انتهای متن
# }
normalizer = TextNormalizer()
# text = """
# سلام -- ـ ـ خوبی
# دادی ییی بی داد
# آیا ١٢۳۴۴۵۶۷٨ ، دادی یا ندادی؟
# ـ تغییرات جدید ٠۹۸٧٦٥٤٣٢١
# درباره‌ی ﻻ|ﻻ یا مثلا ﻷ صحبت کنیم.
# """
# text = """
# بند 2 ـ متن زير جايگزين ماده (17) قانون مي‌ شود و مواد (19)، (20) و (22) و مواد (27) تا (32) و تبصره‌ هاي آنها حذف مي ‌شوند:
# ماده 17 ـ اموال و دارايي ‌هايي كه در نتيجه فوت شخص اعم از فوت واقعي يا فرضي انتقال مي ‌يابد، به شرح زير مشمول ماليات است:
# 1 ـ نسبت به سپرده ‌هاي بانكي، اوراق مشاركت و ساير اوراق بهادار به ‌استثناي موارد مندرج در بند (2) اين ماده و سودهاي متعلق به آنها و همچنين سود سهام و سهم ‌الشركه تا تاريخ ثبت انتقال به نام وراث و يا پرداخت و تحويل به آنها به نرخ سه درصد (3 ٪)
# 2 ـ نسبت به سهام و سهم‌ الشركه و حق تقدم آنها يك و نيم (5 /1) برابر نرخهاي مذكور در تبصره (1) ماده (143) و ماده (143 مكرر) اين قانون طبق مقررات مزبور در تاريخ ثبت انتقال به نام وراث
# 3 ـ نسبت به حق ‌الامتياز و ساير اموال و حقوق مالي كه در بندهاي مذكور به آنها تصريح نشده است، به نرخ ده‌ درصد (10 ٪) ارزش روز در تاريخ تحويل يا ثبت انتقال به نام وراث
# 4 ـ نسبت به انواع وسايل نقليه موتوري، زميني، دريايي و هوايي به نرخ دو درصد (2 ٪) بهاي اعلامي توسط سازمان امور مالياتي كشور در تاريخ ثبت انتقال به نام وراث
# 5 ـ نسبت به املاك و حق واگذاري محل يك‌ و نيم (5 /1) برابر نرخهاي مذكور در ماده (59) اين قانون به مأخذ ارزش معاملاتي املاك و يا به مأخذ ارزش روز حق واگذاري حسب مورد، در تاريخ ثبت انتقال به نام وراث
# 6 ـ نسبت به اموال و دارايي ‌هاي متعلق به متوفاي ايراني كه در خارج از كشور واقع شده است پس از كسر ماليات بر ارثي كه از آن بابت به دولت محل وقوع اموال و دارايي ‌ها پرداخت شده است به نرخ ده ‌درصد (10 ٪) ارزش ماترك كه مأخذ محاسبه ماليات بر ارث در كشور محل وقوع مال قرار گرفته است. در صورت عدم شمول ماليات بر ارث در كشور مزبور به مأخذ ارزش روز انتقال يا تحويل به نام وراث
# تبصره 1 ـ محاسبه و اخذ ماليات بر ارث در مورد متوفيان قبل از لازم ‌الاجراء شدن اين قانون (1 /1 /1395) اعم از اينكه پرونده مالياتي براي آنها تشكيل شده يا نشده باشد، مشمول حكم اين ماده نخواهد بود.
# تبصره 2 ـ نرخهاي مذكور در اين ماده مربوط به وراث طبقه اول است. در صورتي كه وراث طبقات دوم و سوم باشند، نرخهاي مذكور در اين ماده به ترتيب دو و چهار برابر خواهد شد.
# تبصره 3 ـ در صورتي كه متوفي و وراث، تبعه خارجي باشند، اموال و دارايي ‌هاي متوفي كه در ايران واقع است، مشمول ماليات به نرخ وراث طبقه اول خواهد بود.
# تبصره 4 ـ در مواردي كه وراث سهم خود از اموال موضوع بندهاي (2)، (4) و (5) اين ماده را به اشخاص ثالث يا وراث ديگر انتقال دهند، علاوه بر ماليات بر ارث به شرح اين فصل، مشمول ماليات طبق مقررات فصول مربوط خواهند بود.
# تبصره 5 ـ حقوق ناشي از عقود اجاره به شرط تمليك با بانكها و ساير نهادهاي مالي و اعتباري، نسبت به عرصه و اعيان املاك بر اساس ارزش معاملاتي در تاريخ ثبت انتقال به نام وراث محاسبه خواهد شد.
# """
# # text="سلـام"
# # text="46- متون زیر به عنوان تبصره‌ های ( 3 ) و ( 4 ) به ماده (-187-) قانون الحاق می ‌شود:"
# normalized_text = normalizer.normalize_qanon(text)
# print(normalized_text)
# html= ' قانون متمم بودجه سال 1321 كشور مصوب 05/ 09/ 1321 '
# html = '''
# <TD WIDTH=35 STYLE="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in"> <br> 3 </TD> <TD WIDTH=389 STYLE="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding: 0in"> <br> قانون بودجه كل سال 1316 كشور مصوب 23/ 12/ 1315 </TD>
# '''
# print(html)
# print( normalizer.repair_qanon_html(html) )