eru/avro-phonetic
Composer 安装命令:
composer require eru/avro-phonetic
包简介
Avro Phonetic - Bengali/Bangla transliteration library for PHP and Laravel
README 文档
README
A high-performance Avro Phonetic transliteration library for PHP. Convert Banglish (Romanized Bengali) to Bengali script with blazing-fast Trie-based pattern matching and context-sensitive rule engine.
ami banglay gan gai → আমি বাংলায় গান গাই
✨ Features
- 🚀 Trie-Based Pattern Matching — O(m) lookup time instead of O(n×m) linear search
- 🎯 Context-Sensitive Rules — Intelligent conversion based on surrounding characters
- 🔌 Laravel Ready — Auto-discovery, Facade, and config publishing out of the box
- 📦 Standalone Compatible — Works with any PHP project, no framework required
- 🧪 Fully Tested — 180+ tests covering all functionality
- ⚡ Memory Efficient — Singleton pattern with lazy-loaded grammar
- 🔧 PHP 5.6+ — Compatible with PHP 5.6 through 8.x
🎯 What Makes This Unique?
Trie Data Structure for Fast Lookups
Unlike traditional implementations that linearly scan through hundreds of patterns, we use a Trie (prefix tree) data structure. This provides:
- O(m) lookup time where m is the pattern length (vs O(n×m) for linear search)
- Efficient longest-match finding — automatically finds the longest matching pattern
- Minimal memory overhead — shared prefixes are stored only once
Context-Sensitive Rule Engine
Our rule engine understands context! The same input can produce different outputs based on what comes before or after:
Avro::to('rri'); // ঋ (at word start) Avro::to('krri'); // কৃ (after consonant) Avro::to('OI'); // ঐ (standalone) Avro::to('kOI'); // কৈ (after consonant)
Supported rule scopes:
vowel/!vowel— Check if prefix/suffix is a vowelconsonant/!consonant— Check if prefix/suffix is a consonantpunctuation/!punctuation— Check for word boundariesexact/!exact— Match specific characters
📦 Installation
composer require eru/avro-phonetic
🚀 Quick Start
Basic Usage
use Eru\AvroPhonetic\Avro; // Static method echo Avro::to('ami banglay gan gai'); // Output: আমি বাংলায় গান গাই // Instance method $avro = new Avro(); echo $avro->parse('amar sonar bangla'); // Output: আমার সনার বাংলা
Helper Functions
// Quick conversion echo avro('bangladesh'); // বাংলাদেশ echo bangla('dhaka'); // ঢাকা // Get instance for chaining $result = avro()->parse('tumi kemon acho');
Laravel Usage
The package auto-registers with Laravel 5.5+. No additional setup required!
Using the Facade:
use Eru\AvroPhonetic\Facades\Avro; // In your controller public function store(Request $request) { $bengaliText = Avro::to($request->input('text')); // ... }
In Blade templates:
<p>{{ avro('ami tomake bhalobashi') }}</p> <p>{{ bangla('sundor bangladesh') }}</p>
Publish configuration (optional):
php artisan vendor:publish --tag=avro-phonetic-config
This creates config/avro-phonetic.php where you can customize the grammar file path.
📖 API Reference
Avro Class
use Eru\AvroPhonetic\Avro; // Static conversion Avro::to(string $text): string // Instance methods $avro = new Avro(); $avro->parse(string $text): string $avro->convert(string $text): string // Alias for parse() // Factory methods Avro::getInstance(): Avro // Singleton Avro::withGrammar(array $grammar): Avro // Custom grammar Avro::fromGrammarFile(string $path): Avro // Load from file // Access parser $avro->getParser(): PhoneticParser
Helper Functions
// Convert text or get instance avro(?string $text = null): Avro|string // Always converts text bangla(string $text): string
⚙️ Advanced Usage
Custom Grammar
You can provide your own grammar rules:
$customGrammar = [ 'patterns' => [ ['find' => 'ph', 'replace' => 'ফ', 'rules' => []], // ... more patterns ], 'vowel' => 'aeiou', 'consonant' => 'bcdfghjklmnpqrstvwxyz', 'number' => '1234567890', 'casesensitive' => 'oiudgjnrstyz', ]; $avro = Avro::withGrammar($customGrammar); echo $avro->parse('phone'); // ফনে
Load Grammar from File
$avro = Avro::fromGrammarFile('/path/to/custom-grammar.json');
Direct Parser Access
For performance-critical applications, access the parser directly:
use Eru\AvroPhonetic\PhoneticParser; $grammar = json_decode(file_get_contents('grammar.json'), true); $parser = new PhoneticParser($grammar); // Parse multiple texts foreach ($texts as $text) { $results[] = $parser->parse($text); }
🏎️ Performance
Benchmarks on typical hardware (PHP 8.x):
| Operation | Time |
|---|---|
| Single word conversion | ~0.05ms |
| Sentence (10 words) | ~0.1ms |
| Paragraph (100 words) | ~0.8ms |
| Trie initialization | ~15ms (one-time) |
The Trie is built once and reused via singleton pattern, making subsequent conversions extremely fast.
🧪 Testing
# Run all tests composer test # Or directly with PHPUnit ./vendor/bin/phpunit # Run specific test suite ./vendor/bin/phpunit --testsuite Parser ./vendor/bin/phpunit --testsuite Performance # With coverage report ./vendor/bin/phpunit --coverage-html coverage
📁 Project Structure
avro-phonetic/
├── src/
│ ├── Avro.php # Main entry point
│ ├── PhoneticParser.php # Core parser with rule engine
│ ├── Trie.php # Trie data structure
│ ├── TrieNode.php # Trie node class
│ ├── AvroPhoneticServiceProvider.php # Laravel service provider
│ ├── Facades/
│ │ └── Avro.php # Laravel facade
│ └── helpers.php # Global helper functions
├── config/
│ └── avro-phonetic.php # Laravel config
├── resources/
│ └── grammar.json # Avro phonetic rules
└── tests/
├── AvroTest.php
├── PhoneticParserTest.php
├── RuleEngineTest.php
├── TrieTest.php
└── ...
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Write tests for your changes
- Ensure all tests pass (
composer test) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📜 License
This package is open-sourced software licensed under the GPL-3.0 License.
🙏 Credits
- Based on the Avro Phonetic keyboard layout
- Rule engine ported from pyAvroPhonetic by Kaustav Das Modak
- Developed by Erfan Ahmed Siam
Made with ❤️ for the Bengali language
eru/avro-phonetic 适用场景与选型建议
eru/avro-phonetic 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 12 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「transliteration」 「laravel」 「phonetic」 「Bangla」 「avro」 「bengali」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eru/avro-phonetic 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eru/avro-phonetic 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eru/avro-phonetic 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple library to sanitize text elements
i18n helpers (transliteration)
A class for retrieving the cologne phonetic (Kölner Phonetik) value of a string.
A wannabe all-in-all Bangla String Manupulation Library!
Convert english to ipa
Transliteration package for Indic scripts
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-12-28