easyr-ar 0.1.0__py3-none-any.whl
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.
- easyr_ar-0.1.0.dist-info/METADATA +145 -0
- easyr_ar-0.1.0.dist-info/RECORD +40 -0
- easyr_ar-0.1.0.dist-info/WHEEL +5 -0
- easyr_ar-0.1.0.dist-info/top_level.txt +1 -0
- easyre/__init__.py +65 -0
- easyre/_arabic.py +190 -0
- easyre/chunking/__init__.py +8 -0
- easyre/chunking/headers.py +46 -0
- easyre/chunking/separators.py +6 -0
- easyre/chunking/splitters.py +185 -0
- easyre/core.py +237 -0
- easyre/extract/__init__.py +52 -0
- easyre/extract/datetimes.py +223 -0
- easyre/extract/numbers.py +160 -0
- easyre/extract/phones.py +153 -0
- easyre/extract/simple.py +113 -0
- easyre/extract/structure.py +58 -0
- easyre/llm/__init__.py +16 -0
- easyre/llm/budget.py +45 -0
- easyre/llm/cost.py +82 -0
- easyre/llm/jsontools.py +222 -0
- easyre/llm/prices.json +101 -0
- easyre/llm/tokens.py +58 -0
- easyre/loaders/__init__.py +9 -0
- easyre/loaders/arabic_pdf.py +64 -0
- easyre/loaders/artifacts.py +104 -0
- easyre/loaders/docx.py +99 -0
- easyre/loaders/pdf.py +88 -0
- easyre/normalize/__init__.py +17 -0
- easyre/normalize/_util.py +35 -0
- easyre/normalize/arabic.py +64 -0
- easyre/normalize/digits.py +33 -0
- easyre/normalize/punctuation.py +76 -0
- easyre/normalize/whitespace.py +136 -0
- easyre/privacy/__init__.py +13 -0
- easyre/privacy/data/__init__.py +2 -0
- easyre/privacy/data/governorates.py +15 -0
- easyre/privacy/masking.py +101 -0
- easyre/privacy/pii.py +103 -0
- easyre/privacy/secrets.py +96 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: easyr-ar
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Arabic / English text preparation for parsing, LLM pipelines and RAG
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ismail-ashraf/easyre-ar
|
|
8
|
+
Project-URL: Issues, https://github.com/ismail-ashraf/easyre-ar/issues
|
|
9
|
+
Keywords: arabic,nlp,rag,text-processing,regex
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Natural Language :: Arabic
|
|
12
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
13
|
+
Requires-Python: >=3.10
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
Provides-Extra: phones
|
|
16
|
+
Requires-Dist: phonenumbers>=8.12; extra == "phones"
|
|
17
|
+
Provides-Extra: pdf
|
|
18
|
+
Requires-Dist: pypdf>=4.0; extra == "pdf"
|
|
19
|
+
Provides-Extra: pdf-mupdf
|
|
20
|
+
Requires-Dist: pymupdf>=1.23; extra == "pdf-mupdf"
|
|
21
|
+
Provides-Extra: docx
|
|
22
|
+
Requires-Dist: python-docx>=1.0; extra == "docx"
|
|
23
|
+
Provides-Extra: chunking
|
|
24
|
+
Requires-Dist: langchain-text-splitters>=0.2; extra == "chunking"
|
|
25
|
+
Provides-Extra: tokens
|
|
26
|
+
Requires-Dist: tiktoken>=0.5; extra == "tokens"
|
|
27
|
+
Provides-Extra: html
|
|
28
|
+
Requires-Dist: langchain-text-splitters>=0.2; extra == "html"
|
|
29
|
+
Requires-Dist: beautifulsoup4; extra == "html"
|
|
30
|
+
Requires-Dist: lxml; extra == "html"
|
|
31
|
+
Provides-Extra: semantic
|
|
32
|
+
Requires-Dist: langchain-text-splitters>=0.2; extra == "semantic"
|
|
33
|
+
Requires-Dist: langchain-experimental; extra == "semantic"
|
|
34
|
+
Provides-Extra: pydantic
|
|
35
|
+
Requires-Dist: pydantic>=2.0; extra == "pydantic"
|
|
36
|
+
Provides-Extra: litellm
|
|
37
|
+
Requires-Dist: litellm; extra == "litellm"
|
|
38
|
+
Provides-Extra: all
|
|
39
|
+
Requires-Dist: easyr-ar[chunking,docx,html,litellm,pdf,phones,pydantic,tokens]; extra == "all"
|
|
40
|
+
Provides-Extra: dev
|
|
41
|
+
Requires-Dist: pytest; extra == "dev"
|
|
42
|
+
Requires-Dist: ruff; extra == "dev"
|
|
43
|
+
Requires-Dist: reportlab; extra == "dev"
|
|
44
|
+
|
|
45
|
+
# easyre
|
|
46
|
+
|
|
47
|
+
Arabic / English text preparation for parsing, LLM pipelines and RAG.
|
|
48
|
+
`easyre` sits on top of `re` and a few optional libraries, so you call `extract.emails(text)`
|
|
49
|
+
instead of writing regular expressions. The core install has **no third-party dependencies**;
|
|
50
|
+
heavier features import their library lazily and tell you which extra to install.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
pip install easyre # core: normalize, extract, privacy, llm (text only)
|
|
54
|
+
pip install "easyre[pdf,docx,chunking,tokens,phones,pydantic]" # or: easyre[all]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Extra | Enables |
|
|
58
|
+
|---|---|
|
|
59
|
+
| `phones` | validated phone numbers (`phonenumbers`) |
|
|
60
|
+
| `pdf` / `pdf-mupdf` | `read_pdf` with pypdf / PyMuPDF (**PyMuPDF is AGPL-licensed**) |
|
|
61
|
+
| `docx` | `read_docx` |
|
|
62
|
+
| `chunking`, `html`, `semantic` | `chunk` strategies (`recursive`, `markdown`, `code` / `html` / `semantic`) |
|
|
63
|
+
| `tokens` | exact token counts and `chunk(strategy="tokens")` |
|
|
64
|
+
| `pydantic` | `parse_to_model` |
|
|
65
|
+
| `litellm` | extra token / price fallbacks |
|
|
66
|
+
|
|
67
|
+
A function whose extra is missing raises `MissingDependencyError` (an `ImportError`):
|
|
68
|
+
`read_pdf() requires the optional dependency 'pypdf'. Install it with: pip install easyre[pdf]`.
|
|
69
|
+
|
|
70
|
+
## One example per module
|
|
71
|
+
|
|
72
|
+
Every public function is importable from its module and from the top-level package.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
import easyre
|
|
76
|
+
from easyre import extract, normalize, privacy, loaders, chunking, llm
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**normalize** - remove diacritics, unify letters, digits, spaces (all support `return_map=True`):
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
normalize.strip_tashkeel("\u0645\u064E\u0631\u062D\u0628\u0627") # '\u0645\u0631\u062D\u0628\u0627'
|
|
83
|
+
normalize.normalize_digits("\u0662\u0660\u0662\u0666") # '2026'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**extract** - every extractor returns `list[Match]` with exact offsets (`text[m.start:m.end] == m.text`):
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
extract.emails("write to a.b@example.com")[0].value # 'a.b@example.com'
|
|
90
|
+
extract.dates("5 \u062F\u064A\u0633\u0645\u0628\u0631 2026")[0].value # datetime.date(2026, 12, 5)
|
|
91
|
+
extract.currencies("\u0665\u0660\u0660 \u062C\u0646\u064A\u0647")[0].value # {'amount': Decimal('500'), 'currency': 'EGP'}
|
|
92
|
+
easyre.register("invoice_no", r"INV-\d{4}-\d{3}") # your own extractor
|
|
93
|
+
extract.invoice_no("see INV-2026-004")[0].text # 'INV-2026-004'
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
**privacy** - mask, replace and redact (best-effort, not a substitute for human review):
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
privacy.mask("mail a.b@example.com", type="emails", keep_start=2) # 'mail a.*************'
|
|
100
|
+
privacy.replace("see https://x.io", type="urls") # 'see [URL]'
|
|
101
|
+
privacy.redact_secrets("key=AKIAIOSFODNN7EXAMPLE") # 'key=[REDACTED_SECRET]'
|
|
102
|
+
privacy.detect_pii("card 4111 1111 1111 1111") # [Match(type='credit_card', ...)]
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**loaders** - read files and repair the extracted text:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
pages = loaders.read_pdf("report.pdf") # list[Page]; needs easyre[pdf]
|
|
109
|
+
pages = loaders.fix_pdf_artifacts(pages) # drop headers / footers / page numbers
|
|
110
|
+
text = loaders.fix_arabic_pdf(pages[0].text) # presentation forms, bidi marks, reversed text
|
|
111
|
+
doc = loaders.read_docx("memo.docx")[0].text # Markdown-style text; needs easyre[docx]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**chunking** - sentence-aware (Arabic and English) chunks with exact offsets:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
chunks = chunking.chunk(pages[0].text, strategy="recursive", size=500, overlap=50)
|
|
118
|
+
chunks = chunking.add_context_header(chunks, doc_title="Annual report")
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**llm** - tokens, cost, budget, and parsing of model output:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
llm.count_tokens(chunks[0].text, model="gpt-4o") # TokenCount(tokens=..., exact=True)
|
|
125
|
+
llm.estimate_cost(1000, 500, model="gpt-4o") # USD, from the packaged prices.json
|
|
126
|
+
llm.fit_to_budget(chunks, max_tokens=3000) # longest prefix that fits
|
|
127
|
+
llm.extract_json('```json\n{"a": 1,}\n```') # {'a': 1}
|
|
128
|
+
llm.extract_tag("<answer>42</answer>", "answer") # '42'
|
|
129
|
+
llm.parse_to_model(reply, MyPydanticModel) # validated instance
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Notes
|
|
133
|
+
|
|
134
|
+
* `llm/prices.json` holds prices copied from the providers' official pricing pages (date in
|
|
135
|
+
`_meta`). Prices change: pass `prices=` to `estimate_cost` to use your own.
|
|
136
|
+
* `count_tokens` returns `exact=True` only when the model's own tokenizer was used.
|
|
137
|
+
* Arabic needs more tokens than English for the same meaning.
|
|
138
|
+
* System diagram: `docs/system-diagram.md`.
|
|
139
|
+
|
|
140
|
+
## Development
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
pip install -e ".[dev,all]"
|
|
144
|
+
pytest -q && ruff check .
|
|
145
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
easyre/__init__.py,sha256=xpRHpeO8M1NDKKw2wZfrb3tWvUtnGh1oEjQEXOLBpx8,1938
|
|
2
|
+
easyre/_arabic.py,sha256=QQOfDgsus5tcPQFDWsONQWQzhTGlv2a7l2bcopXy8Lo,7743
|
|
3
|
+
easyre/core.py,sha256=SA8kc7ChX_CEB0KXlmlBxtizhCQ_IDEJqgoDrmfjFS4,7495
|
|
4
|
+
easyre/chunking/__init__.py,sha256=dfKKsDM1ioYd_cESDPgSj_rKtoSB7xuT6KuC39OG4ZU,308
|
|
5
|
+
easyre/chunking/headers.py,sha256=_mfw4b_1ekf-EyCf6gjm8l2Gju4csmQjfeAaiv2UC1k,1749
|
|
6
|
+
easyre/chunking/separators.py,sha256=HvHvD7QLrIQoVQA0Jry8f2_sAK9NFZtWt_BgNAGJg6c,252
|
|
7
|
+
easyre/chunking/splitters.py,sha256=SnaG7KTOXfg3KKP7mzOqFU8nnv-5LmnqiNysf6-ol-k,7648
|
|
8
|
+
easyre/extract/__init__.py,sha256=4ub6laKWohlhU5m5qCKQsRp0mE1c2GkZbAXCMTaPAXU,1588
|
|
9
|
+
easyre/extract/datetimes.py,sha256=N20EcGPJdAxiv_8iPzo8hm0_mvS8jMRuaKZCTmSj-I8,7514
|
|
10
|
+
easyre/extract/numbers.py,sha256=Zj3V_Vfrhk234QQyp1_GEJ79Hnb1atVdrzDDgtJ3SeU,5452
|
|
11
|
+
easyre/extract/phones.py,sha256=d8I2exj7OGymdrBUArnCNKgHrCcHzbgTzl6xGh_eDxU,5686
|
|
12
|
+
easyre/extract/simple.py,sha256=abdhza0vb-R2g8FBZdSDxtTJI53wOWSmqKpFqPrZT0c,3727
|
|
13
|
+
easyre/extract/structure.py,sha256=cVjcI3aSWuEKb1a5_q24uT9x8YNxI1gnMj3rPu-F3Pc,1850
|
|
14
|
+
easyre/llm/__init__.py,sha256=1CVZX7HCKUP-P4lkZjydQbHYaFObfQNP2YcxtZyPqnE,423
|
|
15
|
+
easyre/llm/budget.py,sha256=yCVqFb4pqe3RnJ4fiPnUdb9qD85DpDSyj1wX_9qw7R8,1431
|
|
16
|
+
easyre/llm/cost.py,sha256=wWYX6AbCWY7o3i5xhbxtgUNB5DzxkPofmj5R6_kuxi4,3197
|
|
17
|
+
easyre/llm/jsontools.py,sha256=x6sTMW8b5Z0O47rVBjxUCOtCc4AgP0qdbfRQ5nDlFNY,7965
|
|
18
|
+
easyre/llm/prices.json,sha256=GObhB95p2Wq5AE23Vy4xgJy7zxhDmqeNLYhd_ReXywY,1997
|
|
19
|
+
easyre/llm/tokens.py,sha256=o_x_7Qc0j3GXVOEhb74SxFzQjDv_zX_U5Y4zg9SqdVY,2078
|
|
20
|
+
easyre/loaders/__init__.py,sha256=MDhbI0rAdt_c7KbpG9hj1u7gVO4M3eX4rBU1h-PtDb4,315
|
|
21
|
+
easyre/loaders/arabic_pdf.py,sha256=nhe0V-lnKzENxbxwY_hAIR08q5F0njK22VWsGYD2Ojw,2433
|
|
22
|
+
easyre/loaders/artifacts.py,sha256=utLoWsZ3PrgLEQFkPCBte_sQW_xsg8JqEoAKLyoyVdk,4066
|
|
23
|
+
easyre/loaders/docx.py,sha256=dxXgN4ixFjsEhYkqti6qzW05d1WRp0dG1rJZLaG0QrU,3756
|
|
24
|
+
easyre/loaders/pdf.py,sha256=JiIvJzOmIRJvMQS8JBYlhwj9cKT0wm7d80T_wy-5FLg,3379
|
|
25
|
+
easyre/normalize/__init__.py,sha256=QwlFSWD69MycSm_kytZtYS3HlJwAXWgdx45de9fwU6c,499
|
|
26
|
+
easyre/normalize/_util.py,sha256=ZKaVE4IAIDcPYUxkya1nzZ0VNPzYZSJzrhIqyRvVwI4,1036
|
|
27
|
+
easyre/normalize/arabic.py,sha256=1XAKzH6gsSuFeQLLnws8nvIr2H9GkB9SZb58zIFH2FE,2035
|
|
28
|
+
easyre/normalize/digits.py,sha256=osfxzKEorfOOaYh7VYVTJrzPYV7me00J0teL88XieU8,1046
|
|
29
|
+
easyre/normalize/punctuation.py,sha256=O28lqsKoXLQ7s_7JVWFcdqhcibil1Wh_1P6VZwuq_sM,2611
|
|
30
|
+
easyre/normalize/whitespace.py,sha256=1AUCGM6RSPOIVB_ww0eh7-xM7_tjuthOlLXukEHisMc,3970
|
|
31
|
+
easyre/privacy/__init__.py,sha256=QRyLk4Il-w0viE_6W4qlAGnAaQVRBgC_WrlCag6MGCQ,582
|
|
32
|
+
easyre/privacy/masking.py,sha256=EQq6DSITmrUPv2fyPXr9KPwQGk5ITAdszbeVWEHF5NY,3286
|
|
33
|
+
easyre/privacy/pii.py,sha256=MCvLeruPj2WE_lXZW7Ie_VuzNX7Ro9QNE339suNzP_w,4095
|
|
34
|
+
easyre/privacy/secrets.py,sha256=hdet-aVsNjm-qITmAfm67fdsF_SPnwdO4ISK0o8DGEs,3528
|
|
35
|
+
easyre/privacy/data/__init__.py,sha256=8hk1hvT8YrXMd3whTZZK7Gx72290KAiN9l0Mx1yjtSo,88
|
|
36
|
+
easyre/privacy/data/governorates.py,sha256=ELhQ8fNc8WnLwKWBcxeYJvZmhyF_QodLRLeezmWNLJM,744
|
|
37
|
+
easyr_ar-0.1.0.dist-info/METADATA,sha256=UDuY2RUg3tg4gSazCPNe3a_63eUYvjOWGAi3jV7E43M,6338
|
|
38
|
+
easyr_ar-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
39
|
+
easyr_ar-0.1.0.dist-info/top_level.txt,sha256=rL3aVkN8LwG0z8U9t-t95ek5LBQ37wgQTnnku9dnYKc,7
|
|
40
|
+
easyr_ar-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
easyre
|
easyre/__init__.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"""easyre - Arabic / English text preparation for parsing, LLM pipelines and RAG."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from . import chunking, extract, llm, loaders, normalize, privacy
|
|
5
|
+
from .chunking import add_context_header, chunk
|
|
6
|
+
from .core import (
|
|
7
|
+
Chunk,
|
|
8
|
+
EasyreError,
|
|
9
|
+
ExtractionError,
|
|
10
|
+
Match,
|
|
11
|
+
MissingDependencyError,
|
|
12
|
+
Page,
|
|
13
|
+
ParseError,
|
|
14
|
+
TokenCount,
|
|
15
|
+
UnknownModelError,
|
|
16
|
+
register,
|
|
17
|
+
)
|
|
18
|
+
from .extract import (
|
|
19
|
+
bullet_points,
|
|
20
|
+
codes,
|
|
21
|
+
currencies,
|
|
22
|
+
dates,
|
|
23
|
+
emails,
|
|
24
|
+
hashtags,
|
|
25
|
+
links_with_titles,
|
|
26
|
+
mentions,
|
|
27
|
+
percentages,
|
|
28
|
+
phones,
|
|
29
|
+
times,
|
|
30
|
+
urls,
|
|
31
|
+
)
|
|
32
|
+
from .llm import (
|
|
33
|
+
count_tokens,
|
|
34
|
+
estimate_cost,
|
|
35
|
+
extract_json,
|
|
36
|
+
extract_tag,
|
|
37
|
+
fit_to_budget,
|
|
38
|
+
parse_to_model,
|
|
39
|
+
)
|
|
40
|
+
from .loaders import fix_arabic_pdf, fix_pdf_artifacts, read_docx, read_pdf
|
|
41
|
+
from .normalize import (
|
|
42
|
+
clean_whitespace,
|
|
43
|
+
collapse_repeats,
|
|
44
|
+
normalize_arabic,
|
|
45
|
+
normalize_digits,
|
|
46
|
+
normalize_punctuation,
|
|
47
|
+
remove_tatweel,
|
|
48
|
+
strip_tashkeel,
|
|
49
|
+
)
|
|
50
|
+
from .privacy import detect_pii, mask, national_id, redact_secrets, replace
|
|
51
|
+
|
|
52
|
+
__version__ = "0.1.0"
|
|
53
|
+
__all__ = [
|
|
54
|
+
"extract", "normalize", "privacy", "llm", "loaders", "chunking",
|
|
55
|
+
"chunk", "add_context_header", "read_pdf", "read_docx", "fix_pdf_artifacts", "fix_arabic_pdf",
|
|
56
|
+
"count_tokens", "estimate_cost", "fit_to_budget",
|
|
57
|
+
"extract_json", "extract_tag", "parse_to_model",
|
|
58
|
+
"mask", "replace", "detect_pii", "national_id", "redact_secrets",
|
|
59
|
+
"Match", "Chunk", "Page", "TokenCount", "register",
|
|
60
|
+
"EasyreError", "MissingDependencyError", "ExtractionError", "ParseError", "UnknownModelError",
|
|
61
|
+
"normalize_arabic", "normalize_digits", "strip_tashkeel", "remove_tatweel",
|
|
62
|
+
"clean_whitespace", "collapse_repeats", "normalize_punctuation",
|
|
63
|
+
"emails", "urls", "hashtags", "mentions", "percentages", "codes", "bullet_points",
|
|
64
|
+
"links_with_titles", "phones", "dates", "times", "currencies",
|
|
65
|
+
] # fmt: skip
|
easyre/_arabic.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Arabic constants, char maps, month/currency/time tables and small regex helpers."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
|
|
6
|
+
# ------------------------------------------------------------------ character classes
|
|
7
|
+
ARABIC_LETTERS = "[\u0621-\u063A\u0641-\u064A\u0671-\u06D3]"
|
|
8
|
+
TASHKEEL = "[\u064B-\u065F\u0670]"
|
|
9
|
+
TATWEEL = "\u0640"
|
|
10
|
+
ALEF_VARIANTS = "\u0623\u0625\u0622\u0671" # -> \u0627
|
|
11
|
+
YAA = "\u064A"
|
|
12
|
+
ALEF_MAKSURA = "\u0649" # -> \u064A
|
|
13
|
+
TAA_MARBUTA = "\u0629" # -> \u0647
|
|
14
|
+
HAA = "\u0647"
|
|
15
|
+
ARABIC_INDIC_DIGITS = "\u0660\u0661\u0662\u0663\u0664\u0665\u0666\u0667\u0668\u0669"
|
|
16
|
+
EXTENDED_ARABIC_INDIC_DIGITS = "\u06F0\u06F1\u06F2\u06F3\u06F4\u06F5\u06F6\u06F7\u06F8\u06F9"
|
|
17
|
+
ARABIC_QUESTION = "\u061F"
|
|
18
|
+
ARABIC_COMMA = "\u060C"
|
|
19
|
+
ARABIC_SEMICOLON = "\u061B"
|
|
20
|
+
ARABIC_PERCENT = "\u066A"
|
|
21
|
+
ARABIC_DECIMAL_SEP = "\u066B"
|
|
22
|
+
ARABIC_THOUSANDS_SEP = "\u066C"
|
|
23
|
+
PRESENTATION_FORMS = "[\uFB50-\uFDFF\uFE70-\uFEFF]"
|
|
24
|
+
BIDI_CONTROLS = "[\u200E\u200F\u202A-\u202E\u2066-\u2069]"
|
|
25
|
+
|
|
26
|
+
# digit classes usable inside regex character classes (no brackets)
|
|
27
|
+
DIGIT_CLASS = "0-9\u0660-\u0669\u06F0-\u06F9"
|
|
28
|
+
|
|
29
|
+
_TASHKEEL_CHARS = frozenset(chr(c) for c in range(0x064B, 0x0660)) | {"\u0670"}
|
|
30
|
+
_ALEF_TABLE = {ord(c): "\u0627" for c in ALEF_VARIANTS}
|
|
31
|
+
_DIGIT_TO_ASCII = {ord(c): str(i) for i, c in enumerate(ARABIC_INDIC_DIGITS)}
|
|
32
|
+
_DIGIT_TO_ASCII.update({ord(c): str(i) for i, c in enumerate(EXTENDED_ARABIC_INDIC_DIGITS)})
|
|
33
|
+
_DIGIT_TO_ARABIC = {ord(str(i)): c for i, c in enumerate(ARABIC_INDIC_DIGITS)}
|
|
34
|
+
|
|
35
|
+
ARABIC_LETTER_RE = re.compile(ARABIC_LETTERS)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _to_ascii_digits(text: str) -> str:
|
|
39
|
+
"""1:1 translation of Arabic-Indic / Extended Arabic-Indic digits to ASCII (same length)."""
|
|
40
|
+
return text.translate(_DIGIT_TO_ASCII)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_ALEF_CLASS = "[\u0627\u0623\u0625\u0622]"
|
|
44
|
+
_YAA_CLASS = "[\u064A\u0649]"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _fuzzy(word: str) -> str:
|
|
48
|
+
"""Regex for an Arabic word tolerant to alef / yaa variants and optional tashkeel.
|
|
49
|
+
|
|
50
|
+
Every alef form (\\u0627 \\u0623 \\u0625 \\u0622) becomes the class of all four, \\u064A also
|
|
51
|
+
matches \\u0649, and ``TASHKEEL*`` is allowed between letters.
|
|
52
|
+
"""
|
|
53
|
+
parts: list[str] = []
|
|
54
|
+
for ch in word:
|
|
55
|
+
if ch in "\u0627\u0623\u0625\u0622":
|
|
56
|
+
parts.append(_ALEF_CLASS)
|
|
57
|
+
elif ch == "\u064A":
|
|
58
|
+
parts.append(_YAA_CLASS)
|
|
59
|
+
else:
|
|
60
|
+
parts.append(re.escape(ch))
|
|
61
|
+
return (TASHKEEL + "*").join(parts)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _fuzzy_phrase(phrase: str, sep: str = r"\s+") -> str:
|
|
65
|
+
"""Fuzzy regex for a multi-word Arabic phrase (words joined by ``sep``)."""
|
|
66
|
+
return sep.join(_fuzzy(w) for w in phrase.split())
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _canon(s: str) -> str:
|
|
70
|
+
"""Canonical lookup key: lower-case, no tashkeel, alef/yaa unified, single spaces."""
|
|
71
|
+
s = "".join(c for c in s if c not in _TASHKEEL_CHARS).translate(_ALEF_TABLE)
|
|
72
|
+
s = s.replace("\u0649", "\u064A").lower()
|
|
73
|
+
return " ".join(s.split())
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# ------------------------------------------------------------------------ month tables
|
|
77
|
+
MONTHS_EN = {
|
|
78
|
+
"january": 1, "jan": 1, "february": 2, "feb": 2, "march": 3, "mar": 3,
|
|
79
|
+
"april": 4, "apr": 4, "may": 5, "june": 6, "jun": 6, "july": 7, "jul": 7,
|
|
80
|
+
"august": 8, "aug": 8, "september": 9, "sept": 9, "sep": 9, "october": 10, "oct": 10,
|
|
81
|
+
"november": 11, "nov": 11, "december": 12, "dec": 12,
|
|
82
|
+
} # fmt: skip
|
|
83
|
+
|
|
84
|
+
MONTHS_AR = {
|
|
85
|
+
"\u064A\u0646\u0627\u064A\u0631": 1,
|
|
86
|
+
"\u0643\u0627\u0646\u0648\u0646 \u0627\u0644\u062B\u0627\u0646\u064A": 1,
|
|
87
|
+
"\u0641\u0628\u0631\u0627\u064A\u0631": 2,
|
|
88
|
+
"\u0634\u0628\u0627\u0637": 2,
|
|
89
|
+
"\u0645\u0627\u0631\u0633": 3,
|
|
90
|
+
"\u0622\u0630\u0627\u0631": 3,
|
|
91
|
+
"\u0623\u0628\u0631\u064A\u0644": 4,
|
|
92
|
+
"\u0625\u0628\u0631\u064A\u0644": 4,
|
|
93
|
+
"\u0646\u064A\u0633\u0627\u0646": 4,
|
|
94
|
+
"\u0645\u0627\u064A\u0648": 5,
|
|
95
|
+
"\u0623\u064A\u0627\u0631": 5,
|
|
96
|
+
"\u064A\u0648\u0646\u064A\u0648": 6,
|
|
97
|
+
"\u062D\u0632\u064A\u0631\u0627\u0646": 6,
|
|
98
|
+
"\u064A\u0648\u0644\u064A\u0648": 7,
|
|
99
|
+
"\u062A\u0645\u0648\u0632": 7,
|
|
100
|
+
"\u0623\u063A\u0633\u0637\u0633": 8,
|
|
101
|
+
"\u0622\u0628": 8,
|
|
102
|
+
"\u0633\u0628\u062A\u0645\u0628\u0631": 9,
|
|
103
|
+
"\u0623\u064A\u0644\u0648\u0644": 9,
|
|
104
|
+
"\u0623\u0643\u062A\u0648\u0628\u0631": 10,
|
|
105
|
+
"\u062A\u0634\u0631\u064A\u0646 \u0627\u0644\u0623\u0648\u0644": 10,
|
|
106
|
+
"\u0646\u0648\u0641\u0645\u0628\u0631": 11,
|
|
107
|
+
"\u062A\u0634\u0631\u064A\u0646 \u0627\u0644\u062B\u0627\u0646\u064A": 11,
|
|
108
|
+
"\u062F\u064A\u0633\u0645\u0628\u0631": 12,
|
|
109
|
+
"\u0643\u0627\u0646\u0648\u0646 \u0627\u0644\u0623\u0648\u0644": 12,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
# --------------------------------------------------------------- percent marker phrases
|
|
113
|
+
PERCENT_PHRASES_AR = [
|
|
114
|
+
"\u0641\u064A \u0627\u0644\u0645\u064A\u0629", # fi al-miya
|
|
115
|
+
"\u0641\u064A \u0627\u0644\u0645\u0627\u0626\u0629", # fi al-maa'a
|
|
116
|
+
"\u0641\u064A \u0627\u0644\u0645\u0626\u0629", # fi al-mi'a (common MSA spelling)
|
|
117
|
+
"\u0628\u0627\u0644\u0645\u064A\u0629",
|
|
118
|
+
"\u0628\u0627\u0644\u0645\u0627\u0626\u0629",
|
|
119
|
+
"\u0628\u0627\u0644\u0645\u0626\u0629",
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
# --------------------------------------------------------------------- time markers
|
|
123
|
+
TIME_STEMS_AR = {
|
|
124
|
+
"am": ["\u0635\u0628\u0627\u062D", "\u0641\u062C\u0631"], # sabah, fajr
|
|
125
|
+
"pm": ["\u0645\u0633\u0627\u0621", "\u0639\u0635\u0631"], # masaa, asr
|
|
126
|
+
"noon": ["\u0638\u0647\u0631"], # dhuhr
|
|
127
|
+
"night": ["\u0644\u064A\u0644"], # layl
|
|
128
|
+
}
|
|
129
|
+
TIME_SINGLE_AM = "\u0635"
|
|
130
|
+
TIME_SINGLE_PM = "\u0645"
|
|
131
|
+
TIME_SUFFIX = "(?:\u064B?\u0627\u064B?|\u064B)?"
|
|
132
|
+
|
|
133
|
+
# ------------------------------------------------------------------ currency tables
|
|
134
|
+
# value markers resolved at run time from the function arguments
|
|
135
|
+
POUND_DEFAULT = "@pound"
|
|
136
|
+
RIYAL_DEFAULT = "@riyal"
|
|
137
|
+
|
|
138
|
+
CURRENCY_NAMES_AR = {
|
|
139
|
+
"\u062C\u0646\u064A\u0647": POUND_DEFAULT,
|
|
140
|
+
"\u062C\u0646\u064A\u0647\u0627": POUND_DEFAULT,
|
|
141
|
+
"\u062C\u0646\u064A\u0647\u0627\u062A": POUND_DEFAULT,
|
|
142
|
+
"\u062C\u0646\u064A\u0629": POUND_DEFAULT, # common misspelling
|
|
143
|
+
"\u062C\u0646\u064A\u0647 \u0645\u0635\u0631\u064A": "EGP",
|
|
144
|
+
"\u062C\u0646\u064A\u0647 \u0627\u0633\u062A\u0631\u0644\u064A\u0646\u064A": "GBP",
|
|
145
|
+
"\u062C\u0646\u064A\u0647 \u0625\u0633\u062A\u0631\u0644\u064A\u0646\u064A": "GBP",
|
|
146
|
+
"\u0631\u064A\u0627\u0644": RIYAL_DEFAULT,
|
|
147
|
+
"\u0631\u064A\u0627\u0644 \u0633\u0639\u0648\u062F\u064A": "SAR",
|
|
148
|
+
"\u0631\u064A\u0627\u0644 \u0642\u0637\u0631\u064A": "QAR",
|
|
149
|
+
"\u0631\u064A\u0627\u0644 \u0639\u0645\u0627\u0646\u064A": "OMR",
|
|
150
|
+
"\u062F\u0631\u0647\u0645": "AED",
|
|
151
|
+
"\u062F\u0631\u0627\u0647\u0645": "AED",
|
|
152
|
+
"\u062F\u0631\u0647\u0645 \u0625\u0645\u0627\u0631\u0627\u062A\u064A": "AED",
|
|
153
|
+
"\u062F\u064A\u0646\u0627\u0631": None,
|
|
154
|
+
"\u062F\u064A\u0646\u0627\u0631 \u0643\u0648\u064A\u062A\u064A": "KWD",
|
|
155
|
+
"\u062F\u064A\u0646\u0627\u0631 \u0623\u0631\u062F\u0646\u064A": "JOD",
|
|
156
|
+
"\u062F\u064A\u0646\u0627\u0631 \u0628\u062D\u0631\u064A\u0646\u064A": "BHD",
|
|
157
|
+
"\u062F\u0648\u0644\u0627\u0631": "USD",
|
|
158
|
+
"\u062F\u0648\u0644\u0627\u0631\u0627": "USD",
|
|
159
|
+
"\u062F\u0648\u0644\u0627\u0631\u0627\u062A": "USD",
|
|
160
|
+
"\u064A\u0648\u0631\u0648": "EUR",
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
# Arabic abbreviations: regex source -> currency
|
|
164
|
+
CURRENCY_ABBR_AR = [
|
|
165
|
+
("\u062C\\.\u0645", "EGP"), # ج.م
|
|
166
|
+
("\u0631\\.\u0633", "SAR"), # ر.س
|
|
167
|
+
("\u062F\\.[\u0625\u0627]", "AED"), # د.إ
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
ISO_CODES = ["EGP", "USD", "EUR", "GBP", "SAR", "AED", "KWD", "QAR", "BHD", "OMR", "JOD"]
|
|
171
|
+
|
|
172
|
+
# English names (singular form, lower-case). Plural "s" is accepted and stripped.
|
|
173
|
+
CURRENCY_NAMES_EN = {
|
|
174
|
+
"dollar": "USD",
|
|
175
|
+
"euro": "EUR",
|
|
176
|
+
"egyptian pound": "EGP",
|
|
177
|
+
"pound sterling": "GBP",
|
|
178
|
+
"pound": None,
|
|
179
|
+
"riyal": RIYAL_DEFAULT,
|
|
180
|
+
"saudi riyal": "SAR",
|
|
181
|
+
"qatari riyal": "QAR",
|
|
182
|
+
"omani rial": "OMR",
|
|
183
|
+
"dirham": "AED",
|
|
184
|
+
"dinar": None,
|
|
185
|
+
"kuwaiti dinar": "KWD",
|
|
186
|
+
"jordanian dinar": "JOD",
|
|
187
|
+
"bahraini dinar": "BHD",
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
CURRENCY_SYMBOLS = {"$": "USD", "\u20AC": "EUR", "\u00A3": "GBP", "e\u00A3": "EGP"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Chunking for RAG (wraps langchain-text-splitters; the full langchain package is not needed)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from .headers import add_context_header
|
|
5
|
+
from .separators import AR_EN_SEPARATORS
|
|
6
|
+
from .splitters import chunk
|
|
7
|
+
|
|
8
|
+
__all__ = ["chunk", "add_context_header", "AR_EN_SEPARATORS"]
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""add_context_header: prepend a title / heading path to every chunk."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from ..core import Chunk
|
|
5
|
+
|
|
6
|
+
_HEADING_KEYS = ("h1", "h2", "h3", "h4", "h5", "h6")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def add_context_header(
|
|
10
|
+
chunks: list[Chunk],
|
|
11
|
+
doc_title: str | None = None,
|
|
12
|
+
*,
|
|
13
|
+
use_headings: bool = True,
|
|
14
|
+
separator: str = "\n\n",
|
|
15
|
+
) -> list[Chunk]:
|
|
16
|
+
"""Return new chunks whose text starts with ``doc_title > h1 > h2 ...``.
|
|
17
|
+
|
|
18
|
+
The heading path comes from the chunk metadata keys ``h1``..``h6`` (set by the ``markdown``
|
|
19
|
+
and ``html`` strategies). A chunk with an empty header is copied unchanged. Otherwise
|
|
20
|
+
``text = header + separator + old text``; ``start`` / ``end`` keep the offsets of the original
|
|
21
|
+
passage; ``metadata`` is a copy plus ``"context_header"`` and ``"raw_text"``. Inputs are
|
|
22
|
+
never modified.
|
|
23
|
+
|
|
24
|
+
Example:
|
|
25
|
+
>>> c = Chunk("body", 10, 14, {"h1": "Intro"})
|
|
26
|
+
>>> add_context_header([c], "Guide")[0].text
|
|
27
|
+
'Guide > Intro\\n\\nbody'
|
|
28
|
+
"""
|
|
29
|
+
if not isinstance(chunks, (list, tuple)) or not all(isinstance(c, Chunk) for c in chunks):
|
|
30
|
+
raise TypeError("chunks must be a list of Chunk")
|
|
31
|
+
out: list[Chunk] = []
|
|
32
|
+
for c in chunks:
|
|
33
|
+
parts: list[str] = []
|
|
34
|
+
if doc_title:
|
|
35
|
+
parts.append(doc_title)
|
|
36
|
+
if use_headings:
|
|
37
|
+
parts.extend(str(c.metadata[k]) for k in _HEADING_KEYS if c.metadata.get(k))
|
|
38
|
+
header = " > ".join(parts)
|
|
39
|
+
if not header:
|
|
40
|
+
out.append(Chunk(c.text, c.start, c.end, dict(c.metadata)))
|
|
41
|
+
continue
|
|
42
|
+
meta = dict(c.metadata)
|
|
43
|
+
meta["context_header"] = header
|
|
44
|
+
meta["raw_text"] = c.text
|
|
45
|
+
out.append(Chunk(header + separator + c.text, c.start, c.end, meta))
|
|
46
|
+
return out
|