Download the PHP package avvertix/language-detect-php without Composer
On this page you can find all versions of the php package avvertix/language-detect-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download avvertix/language-detect-php
More information about avvertix/language-detect-php
Files in avvertix/language-detect-php
Package language-detect-php
Short Description Fast and lightweight natural language identification for PHP. A port of whatlang-rs.
License MIT
Homepage https://github.com/avvertix/language-detect-php
Informations about the package language-detect-php
Language Detection in PHP
Detects the natural language and writing system of a text. 70 languages, 25 scripts, no
extensions beyond mbstring, no network calls, no model files to download.
This is a port of whatlang-rs by Sergey Potapov.
Installation
Requires PHP 8.4 and ext-mbstring.
Usage
detect() returns null when the text carries no signal — empty, whitespace, digits and
punctuation only, or every candidate language filtered out.
Just the language, or just the script
detectScript() is much cheaper than a full detection: it only counts characters per
Unicode block and skips language scoring entirely.
Narrowing the candidates
If you know the text can only be one of a handful of languages, say so. Accuracy on short texts improves considerably.
Confidence
confidence runs from 0.0 to 1.0 and reflects how far ahead the winning language is of the
runner-up, scaled by how much text there was to go on — a short text needs a bigger lead to
earn the same confidence. isReliable() is true above 0.9.
Detection method
| Method | What it does | When it helps |
|---|---|---|
Combined (default) |
Blends the two below, weighted by text length | Almost always |
Trigram |
Compares three-character frequency profiles | Longer texts |
Alphabet |
Scores languages by the characters they use | Very short texts; fastest |
The combined method leans on the alphabet score for short input and shifts its weight to trigrams as the text grows past roughly a hundred characters.
Supported languages and scripts
How it works
- Script detection. Every character is matched against the Unicode ranges of 25 scripts; the script with the most characters wins. Punctuation and digits are ignored.
- Script to language. Most scripts imply a single language — Georgian script means Georgian — and detection stops there with full confidence. Five scripts (Latin, Cyrillic, Arabic, Devanagari, Hebrew) are shared, and go on to step 3.
- Language scoring. The alphabet pass scores each candidate by the characters it uses. The trigram pass ranks the text's three-character sequences by frequency and compares that ranking against each language's stored profile of its 300 most common trigrams.
Mandarin is special-cased: its script is also used to write Japanese, so the two are told apart by how much Kana is mixed in.
Parity with whatlang-rs
tests/fixtures/reference.json holds 783 samples with the language, script and confidence
whatlang-rs produces. Test ReferenceParityTest ensure parity between the PHP implementation
and the Rust implementation.
To regenerate the fixture you need cargo and a checkout of whatlang-rs beside this one:
The trigram profiles and alphabet tables under resources/ are likewise generated from the
Rust sources rather than transcribed by hand:
Testing
Credits
- Alessio Vertemati
- All Contributors
Derivation
Language Detection PHP is a derivative work of whatlang-rs (Rust, MIT) by Sergey Potapov. The detection algorithm, the trigram profiles and the alphabet tables all originate there.
License
The MIT License (MIT). Please see License File for more information.
All versions of language-detect-php with dependencies
ext-mbstring Version *