nitotm/efficient-language-detector
Fast, accurate language detection in pure PHP (mbstring required). No dependencies. Supports 60 languages and multiple database sizes/modes (array/string/bytes/disk) to balance speed vs memory, with performance comparable to C++ detectors.
Installation:
composer require nitotm/efficient-language-detector
Use --prefer-dist to exclude tests/benchmarks or --prefer-source to include them.
First Detection:
use Nitotm\Eld\LanguageDetector;
$detector = new LanguageDetector(); // Default: 'large' size, 'string' mode
$result = $detector->detect('Bonjour, comment ça va?');
echo $result->language; // Outputs: 'fr'
Key Properties:
language: ISO 639-1 code (e.g., 'fr').scores(): Associative array of language probabilities (e.g., ['fr' => 0.95, 'en' => 0.03]).isReliable(): Boolean indicating confidence in detection.Basic Detection:
$detector = new LanguageDetector();
$text = "Hola, ¿cómo estás?";
$detection = $detector->detect($text);
if ($detection->isReliable()) {
// Proceed with $detection->language
}
Language Subsets:
string, bytes, disk modes):
$detector->langSubset(['en', 'es', 'fr']); // Limits detection to these languages
array mode):
$subsetFile = $detector->langSubset(['en', 'es'], save: true);
$subsetDetector = new LanguageDetector($subsetFile, null, 'array');
Output Schemes:
$detector->setOutputScheme('ISO639_2T'); // Changes output to ISO 639-2/T
Text Cleanup (Optional):
$detector->enableTextCleanup(true); // Removes URLs, emails, etc. (may reduce accuracy)
Batch Processing:
$texts = ["Bonjour", "Hallo", "こんにちは"];
$results = array_map(fn($text) => $detector->detect($text), $texts);
Caching Detector Instances:
array mode, enable OPcache to optimize performance:
opcache.interned_strings_buffer=128
opcache.memory_consumption=256
Database Mode Selection:
array: Fastest (requires OPcache), highest memory usage.string/bytes: Balanced speed/memory (2x slower than array).disk: Lowest memory (slower, but scalable for large datasets).CLI Usage:
php bin/eld "Bonjour" # Returns detected language
Memory Limits:
array mode can consume significant memory (e.g., extralarge may need 1GB+).string/bytes modes for memory-constrained environments (e.g., shared hosting).Text Encoding:
mb_detect_encoding() if unsure:
if (mb_detect_encoding($text) !== 'UTF-8') {
$text = mb_convert_encoding($text, 'UTF-8');
}
Subset Overhead:
array mode, the first langSubset() call is slow (builds a new database). Save subsets for reuse:
$subsetFile = $detector->langSubset(['en', 'es'], save: true);
Text Cleanup Tradeoff:
enableTextCleanup(true) removes URLs/emails, which may contain language hints (e.g., .com.br suggests Portuguese). Use sparingly.Disk Mode Latency:
disk mode is slow for single detections but ideal for batch processing (e.g., processing 1000+ texts).Check Reliability:
if (!$detection->isReliable()) {
log::warning("Unreliable detection for text: {$text}");
}
Inspect Scores:
scores() for close competitors:
$scores = $detection->scores();
$topScore = max($scores);
if ($topScore < 0.7) {
// Handle ambiguous cases (e.g., fallback to user input or default language)
}
Database Info:
$info = $detector->info();
// Debug supported languages, mode, or memory usage
Benchmarking:
extralarge in disk mode uses ~0.5MB RAM but is 10x slower than array mode.Custom Language Models:
BlobDataBuilder for custom builds.Fallback Logic:
$fallbackLanguages = ['en', 'es'];
$detection = $detector->detect($text);
$language = in_array($detection->language, $fallbackLanguages)
? $detection->language
: $fallbackLanguages[0];
Event Listeners:
$detector->detect($text); // Trigger custom logic via events
CLI Automation:
php bin/eld "input.txt" > output.json
How can I help you explore Laravel packages today?