I wanted to enter the world of NLP in Persian, and the first wall I hit was word normalization. People casually mix Arabic and Persian letters, throw in inconsistent spacing, repeated characters, Arabic numerals next to Persian ones, and a long tail of similar quirks. I couldn’t find a simple library for any of this, so I wrote Davat — a small Python package that uses regex to clean and normalize Persian text.
I later used Davat in my Persian SMS spam classification research, where it became part of the preprocessing pipeline.
A quick taste of what it does:
1
2
3
4
>>> from davat import normalize_persian
>>> normalize_persian("بِسْمِ اللَّهِ الرَّحْمنِ الرَّحِيمِ")
'بسم الله الرحمن الرحیم'
It also handles repeated-character collapse with an optional dictionary lookup:
1
2
3
4
5
>>> normalize_persian("اللله", use_dictionary=True)
'الله'
>>> normalize_persian("موسسسسسه", use_dictionary=True)
'موسسه'
And a clean() function for end-to-end preprocessing — strips links, mentions, hashtags, emojis, and non-Persian characters in one call:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
>>> from davat import clean
>>> text = """متنی برای برسی تابع تمیز کردن متن
... که #هشتگ_ها را خیلی عاااااللللییییی!!!! تبدیل به متن عادی میکند!
... منشنها @mh_salari و لینکها www.mh-salari.ir را حذف میکند.
... حروف غیر فارسی a b c d و اموجیها :( 🐈 را حذف میکند
... علائم دستوری/نگارشی ?!٫ را حذف نمیکند
... و ...
... http://localhost:8888"""
>>> print(clean(text))
متنی برای برسی تابع تمیز کردن متن
که هشتگ_ها را خیلی عالی! تبدیل به متن عادی میکند!
منشنها و لینکها را حذف میکند.
حروف غیر فارسی و اموجیها را حذف میکند
علائم دستوری/نگارشی؟!، را حذف نمیکند
و …