The most accurate natural language detection library for Rust, suitable for short text and mixed-language text
The most accurate natural language detection library for Rust, suitable for short text and mixed-language text
Its task is simple: It tells you which language some text is written in. This is very useful as a preprocessing step for linguistic data in natural language processing applications such as text classification and spell checking. Other use cases, for instance, might include routing e-mails to the right geographically located customer service department, based on the e-mails' languages.
Language detection is often done as part of large machine learning frameworks or natural language processing applications. In cases where you don't need the full-fledged functionality of those systems or don't want to learn the ropes of those, a small flexible library comes in handy.
So far, other comprehensive open source libraries in the Rust ecosystem for this task are CLD2, Whatlang and Whichlang. Unfortunately, most of them have two major drawbacks:
Lingua aims at eliminating these problems. She nearly does not need any configuration and yields pretty accurate results on both long and short text, even on single words and phrases. She draws on both rule-based and statistical Naive Bayes methods but does not use neural networks or any dictionaries of words. She does not need a connection to any external API or service either. Once the library has been downloaded, it can be used completely offline.
Compared to other language detection libraries, Lingua's focus is on quality over quantity, that is, getting detection right for a small set of languages first before adding new ones. Currently, the following 75 languages are supported:
Lingua is able to report accuracy statistics for some bundled test data available for each supported language. The test data for each language is split into three parts:
Both the language models and the test data have been created from separate documents of the Wortschatz corpora offered by Leipzig University, Germany. Data crawled from various news websites have been used for training, each corpus comprising one million sentences. For testing, corpora made of arbitrarily chosen websites have been used, each comprising ten thousand sentences. From each test corpus, a random unsorted subset of 1000 single words, 1000 word pairs and 1000 sentences has been extracted, respectively.
Given the generated test data, I have compared the detection results of Lingua, CLD2, Whatlang and Whichlang running over the data of Lingua's supported 75 languages. Languages that are not supported by the other libraries are simply ignored for the respective library during the detection process.
Each of the following sections contains five plots. The bar plots show the detailed accuracy results for each supported language. The box plots illustrate the distributions of the accuracy values for each classifier. The boxes themselves represent the areas which the middle 50 % of data lie within. Within the colored boxes, the horizontal lines mark the median of the distributions.
The first three plots in each section show the results for all supported languages in each classifier, respectively. The last two plots are restricted to the common subset of currently 16 languages that is supported by all compared classifiers. This distinction makes sense because the first box plot creates the impression that Whichlang is the most accurate classifier, but it is not. Whichlang supports only 16 languages whereas Lingua supports 75 languages. For the last box plot, the supported languages in Whatlang and Lingua have been restricted to those 16 languages supported by Whichlang. This provides for a more accurate comparison and shows that overall, Lingua is the most accurate language detection library in this comparison.
Bar plotBar plotBar plotBar plotBar plotBar plotBar plotBar plotThe tables found here show detailed statistics for each language and classifier including mean, median and standard deviation.
Various benchmarks for all classifiers can be run via:
cargo bench --features benchmarkThe benchmarks measure the processing time for classifying 2,000 sentences available in the common 16 languages that are supported by each language detector. The results below have been produced on an iMac 3.6 Ghz 8-Core Intel Core i9 with 40 GB RAM. Whichlang has the shortest processing time, Lingua the longest.
| Detector | Single Thread | Multiple Threads |
|---|---|---|
| Whichlang | 4.47 ms | 651.89 µs |
| CLD 2 | 23.92 ms | 4.81 ms |
| Whatlang (common languages) | 36.24 ms | 4.67 ms |
| Whatlang (all languages) | 89.11 ms | 10.68 ms |
| Lingua (low accuracy mode, common languages) | 492.50 ms | 62.01 ms |
| Lingua (high accuracy mode, common languages) | 640.30 ms | 109.43 ms |
| Lingua (low accuracy mode, all languages) | 2.0448 s | 229.23 ms |
| Lingua (high accuracy mode, all languages) | 3.1243 s | 407.69 ms |
The accuracy reporter script measures the time each language detector needs to classify 3000 input texts for each of the supported 75 languages.
| Detector | Time |
|---|---|
| Whichlang | 0.28 sec |
| CLD 2 | 1.70 sec |
| Whatlang | 7.84 sec |
| Lingua (low accuracy mode, multi-threaded) | 11.81 sec |
| Lingua (high accuracy mode, multi-threaded) | 21.13 sec |
Every language detector uses a probabilistic n-gram model trained on the character distribution in some training corpus. Most libraries only use n-grams of size 3 (trigrams) which is satisfactory for detecting the language of longer text fragments consisting of multiple sentences. For short phrases or single words, however, trigrams are not enough. The shorter the input text is, the less n-grams are available. The probabilities estimated from such few n-grams are not reliable. This is why Lingua makes use of n-grams of sizes 1 up to 5 which results in much more accurate prediction of the correct language.
A second important difference is that Lingua does not only use such a statistical model, but also a rule-based engine. This engine first determines the alphabet of the input text and searches for characters which are unique in one or more languages. If exactly one language can be reliably chosen this way, the statistical model is not necessary anymore. In any case, the rule-based engine filters out languages that do not satisfy the conditions of the input text. Only then, in a second step, the probabilistic n-gram model is taken into consideration. This makes sense because loading less language models means less memory consumption and better runtime performance.
In general, it is always a good idea to restrict the set of languages to be considered in the classification process using the respective api methods. If you know beforehand that certain languages are never to occur in an input text, do not let those take part in the classifcation process. The filtering mechanism of the rule-based engine is quite good, however, filtering based on your own knowledge of the input text is always preferable.
Even when taking all language models into account, the library uses only a few dozen megabytes of memory during runtime. This is because the models are stored as finite-state transducers (FSTs). FSTs allow to be searched on disk without actually reading them entirely into memory, making the library suitable for low-resource environments.
If you want to reproduce the accuracy results above, you can generate the test reports yourself for all classifiers and all languages by executing:
cargo build --release --bin accuracy_reports --features accuracy-reports
./target/release/accuracy_reportsAccuracy reports for only a subset of classifiers and / or languages can be created by passing command line arguments:
./target/release/accuracy_reports --detectors cld2 lingua-high-accuracy --languages bulgarian germanIt is important to use the --release flag here because loading the language models in debug mode takes too much time.
For each detector and language, a test report file is then written into
/accuracy-reports,
to be found next to the src directory. As an example, here is the current output of the Lingua German report (high accuracy mode):
…Add Lingua to your Cargo.toml file like so:
[dependencies]
lingua = "1.8.0"By default, this will download the language model dependencies for all 75 supported languages,
a total of approximately 300 MB. If your bandwidth or hard drive space is limited, or you simply
do not need all languages, you can specify a subset of the language models to be downloaded as
separate features in your Cargo.toml:
[dependencies]
lingua = { version = "1.8.0", default-features = false, features = ["french", "italian", "spanish"] }In order to build the source code yourself, you need the stable Rust toolchain installed on your machine so that cargo, the Rust package manager is availabl
No open issues yet, or sync has not completed.