Avro Phonetic - Bengali/Bangla transliteration library for PHP and Laravel
eru/avro-phonetic is a Laravel package for avro phonetic - bengali/bangla transliteration library for php and laravel.
It currently has 5 GitHub stars and 6 downloads on Packagist (latest version v1.0.0).
Install it with composer require eru/avro-phonetic.
Discover more Laravel packages by eru
or browse all Laravel packages to compare alternatives.
Last updated
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 → আমি বাংলায় গান গাই
Unlike traditional implementations that linearly scan through hundreds of patterns, we use a Trie (prefix tree) data structure. This provides:
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 characterscomposer require eru/avro-phonetic
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: আমার সনার বাংলা
// Quick conversion
echo avro('bangladesh'); // বাংলাদেশ
echo bangla('dhaka'); // ঢাকা
// Get instance for chaining
$result = avro()->parse('tumi kemon acho');
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.
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
// Convert text or get instance
avro(?string $text = null): Avro|string
// Always converts text
bangla(string $text): string
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'); // ফনে
$avro = Avro::fromGrammarFile('/path/to/custom-grammar.json');
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);
}
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.
# 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
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
└── ...
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)composer test)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This package is open-sourced software licensed under the GPL-3.0 License.
Made with ❤️ for the Bengali language