tidy-arabic 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tidy_arabic-0.1.0/LICENSE +21 -0
- tidy_arabic-0.1.0/PKG-INFO +132 -0
- tidy_arabic-0.1.0/README.md +114 -0
- tidy_arabic-0.1.0/pyproject.toml +29 -0
- tidy_arabic-0.1.0/setup.cfg +4 -0
- tidy_arabic-0.1.0/tests/test_cli.py +45 -0
- tidy_arabic-0.1.0/tests/test_core.py +106 -0
- tidy_arabic-0.1.0/tidy_arabic/__init__.py +24 -0
- tidy_arabic-0.1.0/tidy_arabic/__main__.py +6 -0
- tidy_arabic-0.1.0/tidy_arabic/cli.py +81 -0
- tidy_arabic-0.1.0/tidy_arabic/core.py +124 -0
- tidy_arabic-0.1.0/tidy_arabic.egg-info/PKG-INFO +132 -0
- tidy_arabic-0.1.0/tidy_arabic.egg-info/SOURCES.txt +14 -0
- tidy_arabic-0.1.0/tidy_arabic.egg-info/dependency_links.txt +1 -0
- tidy_arabic-0.1.0/tidy_arabic.egg-info/entry_points.txt +2 -0
- tidy_arabic-0.1.0/tidy_arabic.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mahmoud Fakhry
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tidy-arabic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Small library and CLI for cleaning and normalizing Arabic text.
|
|
5
|
+
Author-email: Mahmoud Fakhry <Prof027@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mahmoud-fakhry/tidy-arabic
|
|
8
|
+
Keywords: arabic,text-cleaning,normalization,nlp,cli
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# tidy-arabic
|
|
20
|
+
|
|
21
|
+
Small, dependency-free Python library and CLI for cleaning and normalizing Arabic text.
|
|
22
|
+
|
|
23
|
+
Raw Arabic text collected from the web, PDFs or user input often comes with
|
|
24
|
+
decorative tatweel (kashida) characters, inconsistent digit styles and messy
|
|
25
|
+
whitespace. `tidy-arabic` fixes that in one call, so downstream tools (search,
|
|
26
|
+
NLP pipelines, databases) see clean, consistent text.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- Remove tatweel/kashida (`ـ`), including inside words
|
|
31
|
+
- Optionally strip Arabic diacritics (tashkeel: harakat, tanween, shadda, sukun)
|
|
32
|
+
- Convert Arabic-Indic digits (`٠١٢٣٤٥٦٧٨٩`) to Western digits and back
|
|
33
|
+
- Normalize whitespace runs into single spaces
|
|
34
|
+
- Works as a Python library and as a command line tool
|
|
35
|
+
- No dependencies; Python 3.10+
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install git+https://github.com/mahmoud-fakhry/tidy-arabic.git
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or clone the repo and use it in place: it is a plain package with no dependencies.
|
|
44
|
+
|
|
45
|
+
## Library usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from tidy_arabic import clean
|
|
49
|
+
|
|
50
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
51
|
+
# 'بسم الله العدد: 789'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Individual helpers:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from tidy_arabic import (
|
|
58
|
+
remove_tatweel, # العــربية -> العربية
|
|
59
|
+
strip_diacritics, # بِسْمِ -> بسم
|
|
60
|
+
eastern_to_western_digits, # ٤٢ -> 42
|
|
61
|
+
western_to_eastern_digits, # 42 -> ٤٢
|
|
62
|
+
normalize_whitespace, # "أ ب" -> "أ ب"
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`clean()` options: `strip_tatweel=True`, `diacritics=False`,
|
|
67
|
+
`digits=None | "western" | "eastern"`, `normalize_spaces=True`.
|
|
68
|
+
|
|
69
|
+
## CLI usage
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
73
|
+
العربية 123
|
|
74
|
+
|
|
75
|
+
$ tidy-arabic --diacritics --digits western input.txt > clean.txt
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Flags: `--diacritics` strip tashkeel, `--keep-tatweel`, `--digits western|eastern|keep`,
|
|
79
|
+
`--no-normalize-spaces`. With no file arguments it reads standard input.
|
|
80
|
+
|
|
81
|
+
## Running the tests
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install pytest
|
|
85
|
+
python -m pytest
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Tests also run in GitHub Actions on every push (Python 3.10-3.13).
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
MIT, see [LICENSE](LICENSE). Maintained by Mahmoud Fakhry (Prof027@gmail.com).
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
<div dir="rtl">
|
|
97
|
+
|
|
98
|
+
# tidy-arabic بالعربية
|
|
99
|
+
|
|
100
|
+
مكتبة بايثون صغيرة وأداة سطر أوامر لتنظيف النصوص العربية وتنسيقها، بدون أي اعتماديات.
|
|
101
|
+
|
|
102
|
+
النصوص العربية المأخوذة من الويب أو ملفات PDF أو مدخلات المستخدمين تأتي غالبًا
|
|
103
|
+
بحروف التطويل (الكشيدة) وبالأرقام بصيغ مختلفة وبمسافات غير منتظمة.
|
|
104
|
+
هذه المكتبة تنظّف كل ذلك باستدعاء واحد.
|
|
105
|
+
|
|
106
|
+
## المميزات
|
|
107
|
+
|
|
108
|
+
- إزالة التطويل/الكشيدة (ـ) حتى داخل الكلمات
|
|
109
|
+
- حذف التشكيل العربي عند الطلب (الحركات والتنوين والشدة والسكون)
|
|
110
|
+
- تحويل الأرقام العربية المشرقية (٠١٢٣٤٥٦٧٨٩) إلى الأرقام الغربية وبالعكس
|
|
111
|
+
- توحيد المسافات المتكررة إلى مسافة واحدة
|
|
112
|
+
- تعمل كمكتبة بايثون وكأداة سطر أوامر
|
|
113
|
+
|
|
114
|
+
## مثال
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from tidy_arabic import clean
|
|
118
|
+
|
|
119
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
120
|
+
# 'بسم الله العدد: 789'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
ومن سطر الأوامر:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
127
|
+
العربية 123
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
الرخصة: MIT. يطوّرها محمود فخري (Prof027@gmail.com).
|
|
131
|
+
|
|
132
|
+
</div>
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# tidy-arabic
|
|
2
|
+
|
|
3
|
+
Small, dependency-free Python library and CLI for cleaning and normalizing Arabic text.
|
|
4
|
+
|
|
5
|
+
Raw Arabic text collected from the web, PDFs or user input often comes with
|
|
6
|
+
decorative tatweel (kashida) characters, inconsistent digit styles and messy
|
|
7
|
+
whitespace. `tidy-arabic` fixes that in one call, so downstream tools (search,
|
|
8
|
+
NLP pipelines, databases) see clean, consistent text.
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Remove tatweel/kashida (`ـ`), including inside words
|
|
13
|
+
- Optionally strip Arabic diacritics (tashkeel: harakat, tanween, shadda, sukun)
|
|
14
|
+
- Convert Arabic-Indic digits (`٠١٢٣٤٥٦٧٨٩`) to Western digits and back
|
|
15
|
+
- Normalize whitespace runs into single spaces
|
|
16
|
+
- Works as a Python library and as a command line tool
|
|
17
|
+
- No dependencies; Python 3.10+
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install git+https://github.com/mahmoud-fakhry/tidy-arabic.git
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Or clone the repo and use it in place: it is a plain package with no dependencies.
|
|
26
|
+
|
|
27
|
+
## Library usage
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from tidy_arabic import clean
|
|
31
|
+
|
|
32
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
33
|
+
# 'بسم الله العدد: 789'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Individual helpers:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
from tidy_arabic import (
|
|
40
|
+
remove_tatweel, # العــربية -> العربية
|
|
41
|
+
strip_diacritics, # بِسْمِ -> بسم
|
|
42
|
+
eastern_to_western_digits, # ٤٢ -> 42
|
|
43
|
+
western_to_eastern_digits, # 42 -> ٤٢
|
|
44
|
+
normalize_whitespace, # "أ ب" -> "أ ب"
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`clean()` options: `strip_tatweel=True`, `diacritics=False`,
|
|
49
|
+
`digits=None | "western" | "eastern"`, `normalize_spaces=True`.
|
|
50
|
+
|
|
51
|
+
## CLI usage
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
55
|
+
العربية 123
|
|
56
|
+
|
|
57
|
+
$ tidy-arabic --diacritics --digits western input.txt > clean.txt
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Flags: `--diacritics` strip tashkeel, `--keep-tatweel`, `--digits western|eastern|keep`,
|
|
61
|
+
`--no-normalize-spaces`. With no file arguments it reads standard input.
|
|
62
|
+
|
|
63
|
+
## Running the tests
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
pip install pytest
|
|
67
|
+
python -m pytest
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Tests also run in GitHub Actions on every push (Python 3.10-3.13).
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
MIT, see [LICENSE](LICENSE). Maintained by Mahmoud Fakhry (Prof027@gmail.com).
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
<div dir="rtl">
|
|
79
|
+
|
|
80
|
+
# tidy-arabic بالعربية
|
|
81
|
+
|
|
82
|
+
مكتبة بايثون صغيرة وأداة سطر أوامر لتنظيف النصوص العربية وتنسيقها، بدون أي اعتماديات.
|
|
83
|
+
|
|
84
|
+
النصوص العربية المأخوذة من الويب أو ملفات PDF أو مدخلات المستخدمين تأتي غالبًا
|
|
85
|
+
بحروف التطويل (الكشيدة) وبالأرقام بصيغ مختلفة وبمسافات غير منتظمة.
|
|
86
|
+
هذه المكتبة تنظّف كل ذلك باستدعاء واحد.
|
|
87
|
+
|
|
88
|
+
## المميزات
|
|
89
|
+
|
|
90
|
+
- إزالة التطويل/الكشيدة (ـ) حتى داخل الكلمات
|
|
91
|
+
- حذف التشكيل العربي عند الطلب (الحركات والتنوين والشدة والسكون)
|
|
92
|
+
- تحويل الأرقام العربية المشرقية (٠١٢٣٤٥٦٧٨٩) إلى الأرقام الغربية وبالعكس
|
|
93
|
+
- توحيد المسافات المتكررة إلى مسافة واحدة
|
|
94
|
+
- تعمل كمكتبة بايثون وكأداة سطر أوامر
|
|
95
|
+
|
|
96
|
+
## مثال
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from tidy_arabic import clean
|
|
100
|
+
|
|
101
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
102
|
+
# 'بسم الله العدد: 789'
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
ومن سطر الأوامر:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
109
|
+
العربية 123
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
الرخصة: MIT. يطوّرها محمود فخري (Prof027@gmail.com).
|
|
113
|
+
|
|
114
|
+
</div>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tidy-arabic"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Small library and CLI for cleaning and normalizing Arabic text."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [{ name = "Mahmoud Fakhry", email = "Prof027@gmail.com" }]
|
|
13
|
+
keywords = ["arabic", "text-cleaning", "normalization", "nlp", "cli"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Text Processing :: Linguistic",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.urls]
|
|
23
|
+
Homepage = "https://github.com/mahmoud-fakhry/tidy-arabic"
|
|
24
|
+
|
|
25
|
+
[project.scripts]
|
|
26
|
+
tidy-arabic = "tidy_arabic.cli:main"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.packages.find]
|
|
29
|
+
include = ["tidy_arabic*"]
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Tests for the tidy-arabic command line interface."""
|
|
2
|
+
|
|
3
|
+
import subprocess
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def run_cli(args=(), stdin_text=""):
|
|
11
|
+
result = subprocess.run(
|
|
12
|
+
[sys.executable, "-m", "tidy_arabic", *args],
|
|
13
|
+
input=stdin_text,
|
|
14
|
+
capture_output=True,
|
|
15
|
+
text=True,
|
|
16
|
+
cwd=PROJECT_ROOT,
|
|
17
|
+
)
|
|
18
|
+
assert result.returncode == 0, result.stderr
|
|
19
|
+
return result.stdout
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_stdin_default_strips_tatweel_and_normalizes_spaces():
|
|
23
|
+
assert run_cli(stdin_text="العــربية جميلة") == "العربية جميلة\n"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_digits_western_flag():
|
|
27
|
+
assert run_cli(["--digits", "western"], "العدد ٤٢") == "العدد 42\n"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_digits_eastern_flag():
|
|
31
|
+
assert run_cli(["--digits", "eastern"], "العدد 42") == "العدد ٤٢\n"
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_diacritics_flag():
|
|
35
|
+
assert run_cli(["--diacritics"], "بِسْمِ اللَّهِ") == "بسم الله\n"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_keep_tatweel_flag():
|
|
39
|
+
assert run_cli(["--keep-tatweel"], "العــربية") == "العــربية\n"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_file_input(tmp_path):
|
|
43
|
+
sample = tmp_path / "sample.txt"
|
|
44
|
+
sample.write_text("مـرحـبا ١٢٣", encoding="utf-8")
|
|
45
|
+
assert run_cli(["--digits", "western", str(sample)]) == "مرحبا 123\n"
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"""Tests for the core cleaning helpers."""
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from tidy_arabic import (
|
|
6
|
+
clean,
|
|
7
|
+
eastern_to_western_digits,
|
|
8
|
+
normalize_whitespace,
|
|
9
|
+
remove_tatweel,
|
|
10
|
+
strip_diacritics,
|
|
11
|
+
western_to_eastern_digits,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TestRemoveTatweel:
|
|
16
|
+
def test_removes_tatweel_between_letters(self):
|
|
17
|
+
assert remove_tatweel("العــربية") == "العربية"
|
|
18
|
+
|
|
19
|
+
def test_removes_tatweel_inside_word(self):
|
|
20
|
+
assert remove_tatweel("مـرحـبا") == "مرحبا"
|
|
21
|
+
|
|
22
|
+
def test_leaves_text_without_tatweel_alone(self):
|
|
23
|
+
assert remove_tatweel("نص عادي") == "نص عادي"
|
|
24
|
+
|
|
25
|
+
def test_removes_multiple_tatweels(self):
|
|
26
|
+
assert remove_tatweel("ـــكلمةـــ") == "كلمة"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TestStripDiacritics:
|
|
30
|
+
def test_strips_harakat(self):
|
|
31
|
+
assert strip_diacritics("بِسْمِ اللَّهِ") == "بسم الله"
|
|
32
|
+
|
|
33
|
+
def test_strips_tanween(self):
|
|
34
|
+
assert strip_diacritics("كتابٌ جميلٌ") == "كتاب جميل"
|
|
35
|
+
|
|
36
|
+
def test_strips_dagger_alef(self):
|
|
37
|
+
assert strip_diacritics("هَٰذَا") == "هذا"
|
|
38
|
+
|
|
39
|
+
def test_leaves_plain_text_alone(self):
|
|
40
|
+
assert strip_diacritics("مرحبا بالعالم") == "مرحبا بالعالم"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class TestDigitConversion:
|
|
44
|
+
def test_arabic_indic_to_western(self):
|
|
45
|
+
assert eastern_to_western_digits("رقم ٣٤٥") == "رقم 345"
|
|
46
|
+
|
|
47
|
+
def test_all_arabic_indic_digits(self):
|
|
48
|
+
assert eastern_to_western_digits("٠١٢٣٤٥٦٧٨٩") == "0123456789"
|
|
49
|
+
|
|
50
|
+
def test_extended_arabic_indic_digits(self):
|
|
51
|
+
assert eastern_to_western_digits("۰۱۲۳۴۵۶۷۸۹") == "0123456789"
|
|
52
|
+
|
|
53
|
+
def test_western_to_arabic_indic(self):
|
|
54
|
+
assert western_to_eastern_digits("123") == "١٢٣"
|
|
55
|
+
|
|
56
|
+
def test_round_trip(self):
|
|
57
|
+
original = "سنة ٢٠٢٦"
|
|
58
|
+
assert western_to_eastern_digits(eastern_to_western_digits(original)) == original
|
|
59
|
+
|
|
60
|
+
def test_mixed_text(self):
|
|
61
|
+
assert eastern_to_western_digits("عنده ٥ كتب و 12 قلما") == "عنده 5 كتب و 12 قلما"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class TestNormalizeWhitespace:
|
|
65
|
+
def test_collapses_spaces(self):
|
|
66
|
+
assert normalize_whitespace("كلمة أخرى") == "كلمة أخرى"
|
|
67
|
+
|
|
68
|
+
def test_trims_ends(self):
|
|
69
|
+
assert normalize_whitespace(" مرحبا ") == "مرحبا"
|
|
70
|
+
|
|
71
|
+
def test_collapses_newlines_and_tabs(self):
|
|
72
|
+
assert normalize_whitespace("سطر أول\n\tسطر ثان") == "سطر أول سطر ثان"
|
|
73
|
+
|
|
74
|
+
def test_empty_string(self):
|
|
75
|
+
assert normalize_whitespace("") == ""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TestClean:
|
|
79
|
+
def test_defaults_strip_tatweel_and_normalize_spaces(self):
|
|
80
|
+
assert clean(" العــربية جميلة ") == "العربية جميلة"
|
|
81
|
+
|
|
82
|
+
def test_defaults_keep_diacritics_and_digits(self):
|
|
83
|
+
assert clean("بِسْمِ ١٢٣") == "بِسْمِ ١٢٣"
|
|
84
|
+
|
|
85
|
+
def test_keep_tatweel(self):
|
|
86
|
+
assert clean("العــربية", strip_tatweel=False) == "العــربية"
|
|
87
|
+
|
|
88
|
+
def test_strip_diacritics_option(self):
|
|
89
|
+
assert clean("بِسْمِ اللَّهِ", diacritics=True) == "بسم الله"
|
|
90
|
+
|
|
91
|
+
def test_digits_western_option(self):
|
|
92
|
+
assert clean("العدد ٤٢", digits="western") == "العدد 42"
|
|
93
|
+
|
|
94
|
+
def test_digits_eastern_option(self):
|
|
95
|
+
assert clean("العدد 42", digits="eastern") == "العدد ٤٢"
|
|
96
|
+
|
|
97
|
+
def test_no_normalize_spaces(self):
|
|
98
|
+
assert clean("أ ب", normalize_spaces=False) == "أ ب"
|
|
99
|
+
|
|
100
|
+
def test_everything_together(self):
|
|
101
|
+
text = " بِسْمِ اللَّهِ العــدد: ٧٨٩ "
|
|
102
|
+
assert clean(text, diacritics=True, digits="western") == "بسم الله العدد: 789"
|
|
103
|
+
|
|
104
|
+
def test_invalid_digits_value(self):
|
|
105
|
+
with pytest.raises(ValueError):
|
|
106
|
+
clean("نص", digits="roman")
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""tidy-arabic: small helpers for cleaning and normalizing Arabic text."""
|
|
2
|
+
|
|
3
|
+
from .core import (
|
|
4
|
+
TATWEEL,
|
|
5
|
+
clean,
|
|
6
|
+
eastern_to_western_digits,
|
|
7
|
+
normalize_whitespace,
|
|
8
|
+
remove_tatweel,
|
|
9
|
+
strip_diacritics,
|
|
10
|
+
western_to_eastern_digits,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__version__ = "0.1.0"
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"TATWEEL",
|
|
17
|
+
"clean",
|
|
18
|
+
"eastern_to_western_digits",
|
|
19
|
+
"normalize_whitespace",
|
|
20
|
+
"remove_tatweel",
|
|
21
|
+
"strip_diacritics",
|
|
22
|
+
"western_to_eastern_digits",
|
|
23
|
+
"__version__",
|
|
24
|
+
]
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Command line interface for tidy-arabic.
|
|
2
|
+
|
|
3
|
+
Reads files (or standard input), cleans the Arabic text and writes the
|
|
4
|
+
result to standard output.
|
|
5
|
+
|
|
6
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
7
|
+
العربية 123
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import sys
|
|
14
|
+
|
|
15
|
+
from .core import clean
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
19
|
+
parser = argparse.ArgumentParser(
|
|
20
|
+
prog="tidy-arabic",
|
|
21
|
+
description="Clean and normalize Arabic text from files or standard input.",
|
|
22
|
+
)
|
|
23
|
+
parser.add_argument(
|
|
24
|
+
"files",
|
|
25
|
+
nargs="*",
|
|
26
|
+
metavar="FILE",
|
|
27
|
+
help="Files to clean (UTF-8). Reads standard input when omitted or when FILE is '-'.",
|
|
28
|
+
)
|
|
29
|
+
parser.add_argument(
|
|
30
|
+
"--diacritics",
|
|
31
|
+
action="store_true",
|
|
32
|
+
help="strip Arabic diacritics (tashkeel)",
|
|
33
|
+
)
|
|
34
|
+
parser.add_argument(
|
|
35
|
+
"--keep-tatweel",
|
|
36
|
+
action="store_true",
|
|
37
|
+
help="keep tatweel/kashida characters (removed by default)",
|
|
38
|
+
)
|
|
39
|
+
parser.add_argument(
|
|
40
|
+
"--digits",
|
|
41
|
+
choices=["western", "eastern", "keep"],
|
|
42
|
+
default="keep",
|
|
43
|
+
help="convert Arabic-Indic digits to Western or the other way around (default: keep)",
|
|
44
|
+
)
|
|
45
|
+
parser.add_argument(
|
|
46
|
+
"--no-normalize-spaces",
|
|
47
|
+
dest="normalize_spaces",
|
|
48
|
+
action="store_false",
|
|
49
|
+
help="do not collapse whitespace runs into single spaces",
|
|
50
|
+
)
|
|
51
|
+
return parser
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def main(argv: list[str] | None = None) -> int:
|
|
55
|
+
args = build_parser().parse_args(argv)
|
|
56
|
+
digits = None if args.digits == "keep" else args.digits
|
|
57
|
+
|
|
58
|
+
sources = args.files or ["-"]
|
|
59
|
+
cleaned_parts = []
|
|
60
|
+
for name in sources:
|
|
61
|
+
if name == "-":
|
|
62
|
+
data = sys.stdin.read()
|
|
63
|
+
else:
|
|
64
|
+
with open(name, encoding="utf-8") as handle:
|
|
65
|
+
data = handle.read()
|
|
66
|
+
cleaned_parts.append(
|
|
67
|
+
clean(
|
|
68
|
+
data,
|
|
69
|
+
strip_tatweel=not args.keep_tatweel,
|
|
70
|
+
diacritics=args.diacritics,
|
|
71
|
+
digits=digits,
|
|
72
|
+
normalize_spaces=args.normalize_spaces,
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
sys.stdout.write("\n".join(part for part in cleaned_parts if part) + "\n")
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Core text-cleaning helpers for tidy-arabic.
|
|
2
|
+
|
|
3
|
+
Small, dependency-free functions for cleaning and normalizing Arabic text:
|
|
4
|
+
removing tatweel (kashida), stripping diacritics, converting between
|
|
5
|
+
Arabic-Indic and Western digits, and normalizing whitespace.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import re
|
|
11
|
+
|
|
12
|
+
TATWEEL = "ـ" # U+0640, the kashida/tatweel elongation character
|
|
13
|
+
|
|
14
|
+
# Arabic harakat (short vowel marks), tanween, shadda, sukun, Quranic
|
|
15
|
+
# annotation signs and the superscript (dagger) alef.
|
|
16
|
+
_DIACRITIC_CHARS = "".join(
|
|
17
|
+
chr(c)
|
|
18
|
+
for c in list(range(0x0610, 0x061B)) # Quranic annotation signs
|
|
19
|
+
+ list(range(0x064B, 0x0660)) # tanween, harakat, shadda, sukun and relatives
|
|
20
|
+
+ [0x0670] # dagger alef
|
|
21
|
+
)
|
|
22
|
+
_DIACRITICS_RE = re.compile("[" + re.escape(_DIACRITIC_CHARS) + "]")
|
|
23
|
+
|
|
24
|
+
# Arabic-Indic digits used in Egypt and the Levant (U+0660..U+0669), plus the
|
|
25
|
+
# extended Arabic-Indic digits common in Persian and Urdu text (U+06F0..U+06F9).
|
|
26
|
+
_EASTERN_TO_WESTERN = {chr(0x0660 + i): str(i) for i in range(10)}
|
|
27
|
+
_EASTERN_TO_WESTERN.update({chr(0x06F0 + i): str(i) for i in range(10)})
|
|
28
|
+
_EASTERN_TO_WESTERN_TABLE = str.maketrans(_EASTERN_TO_WESTERN)
|
|
29
|
+
_WESTERN_TO_EASTERN_TABLE = str.maketrans({str(i): chr(0x0660 + i) for i in range(10)})
|
|
30
|
+
|
|
31
|
+
_WHITESPACE_RUN_RE = re.compile(r"\s+")
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"TATWEEL",
|
|
35
|
+
"remove_tatweel",
|
|
36
|
+
"strip_diacritics",
|
|
37
|
+
"eastern_to_western_digits",
|
|
38
|
+
"western_to_eastern_digits",
|
|
39
|
+
"normalize_whitespace",
|
|
40
|
+
"clean",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def remove_tatweel(text: str) -> str:
|
|
45
|
+
"""Remove every tatweel (kashida) character, including inside words.
|
|
46
|
+
|
|
47
|
+
>>> remove_tatweel("العــربية")
|
|
48
|
+
'العربية'
|
|
49
|
+
"""
|
|
50
|
+
return text.replace(TATWEEL, "")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def strip_diacritics(text: str) -> str:
|
|
54
|
+
"""Remove Arabic diacritical marks (harakat, tanween, shadda, sukun).
|
|
55
|
+
|
|
56
|
+
Letters themselves are untouched, so the stripped text stays readable.
|
|
57
|
+
"""
|
|
58
|
+
return _DIACRITICS_RE.sub("", text)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def eastern_to_western_digits(text: str) -> str:
|
|
62
|
+
"""Convert Arabic-Indic digits (٠١٢٣٤٥٦٧٨٩) to Western digits (0-9).
|
|
63
|
+
|
|
64
|
+
Extended Arabic-Indic digits (۰۱۲۳۴۵۶۷۸۹, used in Persian/Urdu text)
|
|
65
|
+
are converted as well. Non-digit characters are left alone.
|
|
66
|
+
"""
|
|
67
|
+
return text.translate(_EASTERN_TO_WESTERN_TABLE)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def western_to_eastern_digits(text: str) -> str:
|
|
71
|
+
"""Convert Western digits (0-9) to Arabic-Indic digits (٠١٢٣٤٥٦٧٨٩).
|
|
72
|
+
|
|
73
|
+
The U+0660 block is used, which is the standard in Egypt and the Levant.
|
|
74
|
+
Non-digit characters are left alone.
|
|
75
|
+
"""
|
|
76
|
+
return text.translate(_WESTERN_TO_EASTERN_TABLE)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def normalize_whitespace(text: str) -> str:
|
|
80
|
+
"""Collapse every run of whitespace to a single space and trim the ends.
|
|
81
|
+
|
|
82
|
+
Newlines and tabs count as whitespace, so multi-line input becomes one
|
|
83
|
+
line. Use the individual helpers instead if you need to keep line breaks.
|
|
84
|
+
"""
|
|
85
|
+
return _WHITESPACE_RUN_RE.sub(" ", text).strip()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def clean(
|
|
89
|
+
text: str,
|
|
90
|
+
*,
|
|
91
|
+
strip_tatweel: bool = True,
|
|
92
|
+
diacritics: bool = False,
|
|
93
|
+
digits: str | None = None,
|
|
94
|
+
normalize_spaces: bool = True,
|
|
95
|
+
) -> str:
|
|
96
|
+
"""Clean Arabic text in one call.
|
|
97
|
+
|
|
98
|
+
Parameters
|
|
99
|
+
----------
|
|
100
|
+
text:
|
|
101
|
+
The text to clean.
|
|
102
|
+
strip_tatweel:
|
|
103
|
+
Remove tatweel/kashida characters (default True).
|
|
104
|
+
diacritics:
|
|
105
|
+
Also strip Arabic diacritics/tashkeel (default False).
|
|
106
|
+
digits:
|
|
107
|
+
"western" converts ٠-٩ to 0-9, "eastern" converts 0-9 to ٠-٩,
|
|
108
|
+
None (default) leaves digits as they are.
|
|
109
|
+
normalize_spaces:
|
|
110
|
+
Collapse whitespace runs to a single space (default True).
|
|
111
|
+
"""
|
|
112
|
+
if strip_tatweel:
|
|
113
|
+
text = remove_tatweel(text)
|
|
114
|
+
if diacritics:
|
|
115
|
+
text = strip_diacritics(text)
|
|
116
|
+
if digits == "western":
|
|
117
|
+
text = eastern_to_western_digits(text)
|
|
118
|
+
elif digits == "eastern":
|
|
119
|
+
text = western_to_eastern_digits(text)
|
|
120
|
+
elif digits is not None:
|
|
121
|
+
raise ValueError("digits must be 'western', 'eastern' or None")
|
|
122
|
+
if normalize_spaces:
|
|
123
|
+
text = normalize_whitespace(text)
|
|
124
|
+
return text
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tidy-arabic
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Small library and CLI for cleaning and normalizing Arabic text.
|
|
5
|
+
Author-email: Mahmoud Fakhry <Prof027@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/mahmoud-fakhry/tidy-arabic
|
|
8
|
+
Keywords: arabic,text-cleaning,normalization,nlp,cli
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# tidy-arabic
|
|
20
|
+
|
|
21
|
+
Small, dependency-free Python library and CLI for cleaning and normalizing Arabic text.
|
|
22
|
+
|
|
23
|
+
Raw Arabic text collected from the web, PDFs or user input often comes with
|
|
24
|
+
decorative tatweel (kashida) characters, inconsistent digit styles and messy
|
|
25
|
+
whitespace. `tidy-arabic` fixes that in one call, so downstream tools (search,
|
|
26
|
+
NLP pipelines, databases) see clean, consistent text.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
|
|
30
|
+
- Remove tatweel/kashida (`ـ`), including inside words
|
|
31
|
+
- Optionally strip Arabic diacritics (tashkeel: harakat, tanween, shadda, sukun)
|
|
32
|
+
- Convert Arabic-Indic digits (`٠١٢٣٤٥٦٧٨٩`) to Western digits and back
|
|
33
|
+
- Normalize whitespace runs into single spaces
|
|
34
|
+
- Works as a Python library and as a command line tool
|
|
35
|
+
- No dependencies; Python 3.10+
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install git+https://github.com/mahmoud-fakhry/tidy-arabic.git
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or clone the repo and use it in place: it is a plain package with no dependencies.
|
|
44
|
+
|
|
45
|
+
## Library usage
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from tidy_arabic import clean
|
|
49
|
+
|
|
50
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
51
|
+
# 'بسم الله العدد: 789'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Individual helpers:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from tidy_arabic import (
|
|
58
|
+
remove_tatweel, # العــربية -> العربية
|
|
59
|
+
strip_diacritics, # بِسْمِ -> بسم
|
|
60
|
+
eastern_to_western_digits, # ٤٢ -> 42
|
|
61
|
+
western_to_eastern_digits, # 42 -> ٤٢
|
|
62
|
+
normalize_whitespace, # "أ ب" -> "أ ب"
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`clean()` options: `strip_tatweel=True`, `diacritics=False`,
|
|
67
|
+
`digits=None | "western" | "eastern"`, `normalize_spaces=True`.
|
|
68
|
+
|
|
69
|
+
## CLI usage
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
73
|
+
العربية 123
|
|
74
|
+
|
|
75
|
+
$ tidy-arabic --diacritics --digits western input.txt > clean.txt
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Flags: `--diacritics` strip tashkeel, `--keep-tatweel`, `--digits western|eastern|keep`,
|
|
79
|
+
`--no-normalize-spaces`. With no file arguments it reads standard input.
|
|
80
|
+
|
|
81
|
+
## Running the tests
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install pytest
|
|
85
|
+
python -m pytest
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Tests also run in GitHub Actions on every push (Python 3.10-3.13).
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
MIT, see [LICENSE](LICENSE). Maintained by Mahmoud Fakhry (Prof027@gmail.com).
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
<div dir="rtl">
|
|
97
|
+
|
|
98
|
+
# tidy-arabic بالعربية
|
|
99
|
+
|
|
100
|
+
مكتبة بايثون صغيرة وأداة سطر أوامر لتنظيف النصوص العربية وتنسيقها، بدون أي اعتماديات.
|
|
101
|
+
|
|
102
|
+
النصوص العربية المأخوذة من الويب أو ملفات PDF أو مدخلات المستخدمين تأتي غالبًا
|
|
103
|
+
بحروف التطويل (الكشيدة) وبالأرقام بصيغ مختلفة وبمسافات غير منتظمة.
|
|
104
|
+
هذه المكتبة تنظّف كل ذلك باستدعاء واحد.
|
|
105
|
+
|
|
106
|
+
## المميزات
|
|
107
|
+
|
|
108
|
+
- إزالة التطويل/الكشيدة (ـ) حتى داخل الكلمات
|
|
109
|
+
- حذف التشكيل العربي عند الطلب (الحركات والتنوين والشدة والسكون)
|
|
110
|
+
- تحويل الأرقام العربية المشرقية (٠١٢٣٤٥٦٧٨٩) إلى الأرقام الغربية وبالعكس
|
|
111
|
+
- توحيد المسافات المتكررة إلى مسافة واحدة
|
|
112
|
+
- تعمل كمكتبة بايثون وكأداة سطر أوامر
|
|
113
|
+
|
|
114
|
+
## مثال
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from tidy_arabic import clean
|
|
118
|
+
|
|
119
|
+
clean(" بِسْمِ اللَّهِ العــدد: ٧٨٩ ", diacritics=True, digits="western")
|
|
120
|
+
# 'بسم الله العدد: 789'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
ومن سطر الأوامر:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
$ echo "العــربية ١٢٣" | tidy-arabic --digits western
|
|
127
|
+
العربية 123
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
الرخصة: MIT. يطوّرها محمود فخري (Prof027@gmail.com).
|
|
131
|
+
|
|
132
|
+
</div>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
tests/test_cli.py
|
|
5
|
+
tests/test_core.py
|
|
6
|
+
tidy_arabic/__init__.py
|
|
7
|
+
tidy_arabic/__main__.py
|
|
8
|
+
tidy_arabic/cli.py
|
|
9
|
+
tidy_arabic/core.py
|
|
10
|
+
tidy_arabic.egg-info/PKG-INFO
|
|
11
|
+
tidy_arabic.egg-info/SOURCES.txt
|
|
12
|
+
tidy_arabic.egg-info/dependency_links.txt
|
|
13
|
+
tidy_arabic.egg-info/entry_points.txt
|
|
14
|
+
tidy_arabic.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tidy_arabic
|