πΈπ¦ Ψ§ΩΨΉΨ±Ψ¨ΩΨ© | π¬π§ English
Topics:
eidcloudarabic-nlptoken-optimizerllm-cost-reductionarabic-llmtext-compressionphp8
High-efficiency Arabic text compression and token optimization engine for Large Language Models (LLMs) written in pure PHP 8.2+. Reduces LLM token billings and prompt payload sizes by 25% to 40%+ while preserving semantic integrity and model comprehension across OpenAI GPT-4o, Anthropic Claude 3.5, Meta Llama 3, and Mistral.
flowchart TD
A["Raw Arabic Prompt / Document"] --> B["Markdown Code Block Protector"]
B --> C["Tatweel / Kashida Stripper (Ω)"]
C --> D["Tashkeel / Diacritics Normalizer"]
D --> E["Erratic Hamza Normalizer (Ψ£ Ψ₯ Ψ’ Ω± -> Ψ§)"]
E --> F["Teh Marbuta & Alef Maksura (Optional)"]
F --> G["Repeating Punctuation Collapser"]
G --> H["Whitespace & RTL Spacer Normalizer"]
H --> I["Markdown Code Block Restorer"]
I --> J["Token Estimator (BPE / o200k / cl100k / Llama3)"]
J --> K["Optimized Output & Token Savings Metrics"]
- Zero External Dependencies: 100% pure PHP 8.2+ engine utilizing native
mbstringand regex primitives. - Drastic LLM Token Reduction: Strips aesthetic decorations (Kashida), vocalization diacritics (Tashkeel), erratic Hamza splits, and redundant spacing that cause BPE token fragmentation.
- Preserves Markdown & Code: Safely protects fenced code blocks (
```php ... ```) and inline backticks (`...`) from normalization. - Mathematical BPE Token Estimator: Accurately estimates token consumption across
GPT-4o(o200k),GPT-4(cl100k),Llama 3, andClaude 3.5. - Configurable Optimization Presets:
Conservative: Preserves Tashkeel and Hamzas for legal and religious texts.Balanced(Default): Full LLM prompt optimization; removes Tatweel, Tashkeel, normalizes Hamzas, and cleans punctuation.Aggressive: Adds Teh Marbuta (Ψ© -> Ω) and Alef Maksura (Ω -> Ω) conversion for maximum search index compression.
- Multi-Format CLI: Streamlines terminal workflows with interactive
--statsreports and machine-readable--jsonoutput.
| Input Text Sample | Original Tokens | Optimized Tokens | Tokens Saved | Cost Reduction |
|---|---|---|---|---|
Ψ§ΩΩΨΩΩ
ΩΨ―Ω ΩΩΩΩΩΩΩ Ψ±ΩΨ¨ΩΩ Ψ§ΩΩΨΉΩΨ§ΩΩΩ
ΩΩΩΩ (Full Tashkeel) |
16 | 4 | 75.0% | -75% |
Ω
ΩΩΩΩΩΨ±ΨΩΩΩΩΩΨ¨ΩΩΩΩΩΨ§Ω Ψ¨ΩΩΩΩΩΩΩΩΩΩΩΩΩΩ
Ω !!!!!!! (Tatweel + Punctuation) |
28 | 4 | 85.7% | -85.7% |
| Standard Arabic Prompt (1,000 words mixed) | ~1,850 | ~1,120 | 39.4% | -39.4% |
- PHP: 8.2 or higher
- Extensions:
ext-mbstring(standard in PHP)
composer require eidcloud/arabic-token-optimizerOr clone the repository directly (requires no vendor dependencies):
git clone https://github.com/shadialhasan/eidcloud-arabic-token-optimizer.git
cd eidcloud-arabic-token-optimizerThe repository provides a standalone CLI binary: bin/eidcloud-arabic-opt.
# Display help and options
php bin/eidcloud-arabic-opt --help
# Compress raw text string directly
php bin/eidcloud-arabic-opt compress "Ω
ΩΩΩΩΨ±ΨΩΩΩΨ¨ΩΩΩΨ§Ω Ψ¨ΩΩΩΩΩΩΩΩΩ
Ω ΩΩΩΩ ΨΉΩΩΨ§ΩΩΩΩ
Ω Ψ§ΩΨ°ΩΩΩΩΨ§Ψ‘Ω Ψ§ΩΨ₯Ψ΅ΩΨ·ΩΩΩΨ§ΨΉΩΩΩΩ !!!!!!!"
# Compress text and display token savings report
php bin/eidcloud-arabic-opt compress "ΩΨ΅ ΨͺΨ¬Ψ±ΩΨ¨Ω Ψ·ΩΩΩ" --stats
# Compress a file and output to a new file
php bin/eidcloud-arabic-opt compress input_prompt.txt --out=optimized_prompt.txt --stats
# Output machine-readable JSON for automated pipelines
php bin/eidcloud-arabic-opt compress input.txt --json
# Pipe standard input
cat query.txt | php bin/eidcloud-arabic-opt compress - --statsββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β‘ EidCloud Arabic Token Optimization Report β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Target Model : gpt-4o β
β Execution Time : 1.84 ms β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Metric β Original β Optimized β Saved / Reduction β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Tokens (BPE Est.) β 108 β 16 β 92 (85.2%) β
β Characters β 127 β 36 β 91 (71.7%) β
β Bytes (UTF-8) β 240 β 66 β 174 (72.5%) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β Cost Rate (per 1M tokens) : $2.50 USD β
β Projected Savings / 1M Requests : $230.00 USD β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
use EidCloud\ArabicTokenOptimizer\ArabicTokenOptimizer;
$raw = "Ω
ΩΩΩΩΩΨ±ΨΩΩΩΩΩΨ¨ΩΩΩΩΩΨ§Ω Ψ¨ΩΩΩΩΩΩΩΩΩΩΩΩΩ
Ω ΩΩΩΩΩΩΩ ΨΉΩΩΩΩΩΨ§ΩΩΩΩΩΩΩ
Ω Ψ§ΩΨ°ΩΩΩΩΨ§Ψ‘Ω !!!!!!!";
$clean = ArabicTokenOptimizer::compress($raw);
echo $clean;
// Output: Ω
Ψ±ΨΨ¨Ψ§ Ψ¨ΩΩ
ΩΩ ΨΉΨ§ΩΩ
Ψ§ΩΨ°ΩΨ§Ψ‘!use EidCloud\ArabicTokenOptimizer\ArabicTokenOptimizer;
use EidCloud\ArabicTokenOptimizer\Engine\OptimizationOptions;
$prompt = file_get_contents('large_arabic_context.txt');
// Optimize using balanced preset (default)
$result = ArabicTokenOptimizer::optimize($prompt);
echo "Optimized Text: " . $result->optimizedText . "\n";
echo "Tokens Saved: " . $result->tokensSaved . "\n";
echo "Savings Rate: " . $result->tokensSavingsPercentage . "%\n";
echo "Cost Saved (USD): $" . $result->estimatedCostSavingsUsd . "\n";
echo "Execution Time: " . $result->executionTimeMs . " ms\n";
// Export full metrics to JSON
echo $result->toJson();use EidCloud\ArabicTokenOptimizer\Engine\Optimizer;
use EidCloud\ArabicTokenOptimizer\Engine\OptimizationOptions;
$options = (new OptimizationOptions(
stripTatweel: true,
normalizeWhitespace: true,
normalizeHamza: true,
normalizeTehMarbuta: false, // preserve Ψ©
normalizeAlefMaksura: false, // preserve Ω
stripRepeatingPunctuation: true,
stripTashkeel: true,
preserveMarkdownCode: true,
targetModel: 'gpt-4o'
));
$optimizer = new Optimizer();
$result = $optimizer->optimize($text, $options);The test suite runs with zero third-party dependencies:
php tests/run_tests.phpAll 23+ unit and functional edge-case tests execute in milliseconds and return exit code 0.
- Eng. MHD. Shadi AL-Hasan
- Location: Damascus, Syria
- Email: mhd.shadi.alhasan@gmail.com
- Phone:
+963934005922 - GitHub: @shadialhasan
This software is released under the MIT License.
Copyright (c) 2026 MHD. Shadi AL-Hasan. See the LICENSE file for details.
Eng. MHD. Shadi AL-Hasan
- Role: Executive CTO & Enterprise Solutions Architect
- Email: mhd.shadi.alhasan@gmail.com
- Phone / WhatsApp: +963934005922
- Location: Damascus, Syria
- GitHub: shadialhasan
This project is licensed under the MIT License - see the LICENSE file for details.
Copyright (c) 2026 MHD. Shadi AL-Hasan. All rights reserved.