Skip to content

pemistahl/lingua

Repository files navigation

lingua

ci build status codecov supported languages Kotlin platforms badge license badge javadoc Maven Central Download


1. What does this library do?

Its task is simple: It tells you which language some provided textual data 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.

2. Why does this library exist?

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, three other comprehensive open source libraries working on the JVM for this task are Apache Tika, Apache OpenNLP and Optimaize Language Detector. Unfortunately, especially the latter has three major drawbacks:

  1. Detection only works with quite lengthy text fragments. For very short text snippets such as Twitter messages, it doesn't provide adequate results.
  2. The more languages take part in the decision process, the less accurate are the detection results.
  3. Configuration of the library is quite cumbersome and requires some knowledge about the statistical methods that are used internally.

Lingua aims at eliminating these problems. It nearly doesn't need any configuration and yields pretty accurate results on both long and short text, even on single words and phrases. It draws on both rule-based and statistical methods but does not use any dictionaries of words. It does not need a connection to any external API or service either. Once the library has been downloaded, it can be used completely offline.

3. Which languages are supported?

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:

  • A
    • Afrikaans
    • Albanian
    • Arabic
    • Armenian
    • Azerbaijani
  • B
    • Basque
    • Belarusian
    • Bengali
    • Norwegian Bokmal
    • Bosnian
    • Bulgarian
  • C
    • Catalan
    • Chinese
    • Croatian
    • Czech
  • D
    • Danish
    • Dutch
  • E
    • English
    • Esperanto
    • Estonian
  • F
    • Finnish
    • French
  • G
    • Ganda
    • Georgian
    • German
    • Greek
    • Gujarati
  • H
    • Hebrew
    • Hindi
    • Hungarian
  • I
    • Icelandic
    • Indonesian
    • Irish
    • Italian
  • J
    • Japanese
  • K
    • Kazakh
    • Korean
  • L
    • Latin
    • Latvian
    • Lithuanian
  • M
    • Macedonian
    • Malay
    • Maori
    • Marathi
    • Mongolian
  • N
    • Norwegian Nynorsk
  • P
    • Persian
    • Polish
    • Portuguese
    • Punjabi
  • R
    • Romanian
    • Russian
  • S
    • Serbian
    • Shona
    • Slovak
    • Slovene
    • Somali
    • Sotho
    • Spanish
    • Swahili
    • Swedish
  • T
    • Tagalog
    • Tamil
    • Telugu
    • Thai
    • Tsonga
    • Tswana
    • Turkish
  • U
    • Ukrainian
    • Urdu
  • V
    • Vietnamese
  • W
    • Welsh
  • X
    • Xhosa
  • Y
    • Yoruba
  • Z
    • Zulu

4. How good is it?

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:

  1. a list of single words with a minimum length of 5 characters
  2. a list of word pairs with a minimum length of 10 characters
  3. a list of complete grammatical sentences of various lengths

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, Apache Tika, Apache OpenNLP and Optimaize Language Detector using parameterized JUnit tests running over the data of Lingua's supported 75 languages. Languages that are not supported by the other libraries are simply ignored for those during the detection process.

Each of the following sections contains two plots. The bar plot shows the detailed accuracy results for each supported language. The box plot illustrates 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.

4.1 Single word detection


Single Word Detection Performance


Bar plot Single Word Detection Performance



4.2 Word pair detection


Word Pair Detection Performance


Bar plot Word Pair Detection Performance



4.3 Sentence detection


Sentence Detection Performance


Bar plot Sentence Detection Performance



4.4 Average detection


Average Detection Performance


Bar plot Average Detection Performance



4.5 Mean, median and standard deviation

The table below shows detailed statistics for each language and classifier including mean, median and standard deviation.

Open table
Language Average Single Words Word Pairs Sentences
Lingua
(high accuracy mode)
Lingua
(low accuracy mode)
  Tika   OpenNLP Optimaize Lingua
(high accuracy mode)
Lingua
(low accuracy mode)
  Tika   OpenNLP Optimaize Lingua
(high accuracy mode)
Lingua
(low accuracy mode)
  Tika   OpenNLP Optimaize Lingua
(high accuracy mode)
Lingua
(low accuracy mode)
  Tika   OpenNLP Optimaize
Afrikaans 79 64 71 72 39 58 38 44 41 3 81 62 70 75 22 97 93 98 99 93
Albanian 88 80 79 71 70 69 54 54 40 38 95 86 84 73 73 100 99 99 100 98
Arabic 98 94 97 84 89 96 88 94 65 72 99 96 99 88 94 100 99 100 99 100
Armenian 100 100 - 100 - 100 100 - 100 - 100 100 - 100 - 100 100 - 100 -
Azerbaijani 90 82 - 82 - 77 71 - 60 - 92 78 - 86 - 99 96 - 99 -
Basque 84 74 83 77 66 71 56 64 56 33 87 76 86 82 70 93 91 98 92 95
Belarusian 97 92 96 91 87 92 80 92 78 69 99 95 98 95 92 100 100 100 100 99
Bengali 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Bokmal 58 49 - 66 - 39 27 - 42 - 59 47 - 69 - 75 74 - 87 -
Bosnian 35 29 - 26 - 29 23 - 12 - 35 29 - 22 - 40 36 - 44 -
Bulgarian 87 78 73 83 48 70 56 52 62 18 91 81 69 87 36 99 96 96 100 91
Catalan 70 58 58 42 31 51 33 32 11 2 74 60 57 32 16 86 81 84 81 77
Chinese 100 100 69 78 31 100 100 20 40 0 100 100 86 94 2 100 100 100 100 91
Croatian 72 60 74 50 41 53 36 54 23 8 74 57 72 44 24 90 85 97 81 91
Czech 80 71 72 67 49 66 54 54 42 21 84 72 75 70 46 91 87 88 90 81
Danish 81 70 83 60 55 61 45 63 34 19 84 70 86 52 51 98 95 99 94 96
Dutch 77 64 60 61 39 55 36 31 31 6 81 61 52 57 19 96 94 98 97 91
English 81 62 64 52 41 55 29 30 10 2 89 62 62 46 23 99 96 99 99 97
Esperanto 84 66 - 76 - 67 44 - 50 - 85 61 - 78 - 98 92 - 100 -
Estonian 92 83 84 59 61 80 62 66 29 23 96 88 88 60 63 100 99 100 88 98
Finnish 96 91 94 86 79 90 77 86 68 51 98 95 96 91 86 100 100 100 100 100
French 89 77 78 59 54 74 52 55 25 18 94 83 80 55 48 99 97 99 98 97
Ganda 91 84 - - - 79 65 - - - 95 87 - - - 100 100 - - -
Georgian 100 100 - 100 - 100 100 - 100 - 100 100 - 100 - 100 100 - 100 -
German 89 80 74 67 55 74 57 50 38 21 94 84 71 66 46 100 99 100 98 99
Greek 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Gujarati 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Hebrew 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Hindi 73 33 80 58 51 61 11 65 28 16 64 20 75 49 38 93 67 99 99 98
Hungarian 95 90 88 78 77 87 77 75 53 51 98 94 91 82 82 100 100 100 100 99
Icelandic 93 88 90 76 78 83 72 76 53 53 97 92 94 76 82 100 99 100 99 99
Indonesian 60 48 60 29 18 39 25 37 10 0 61 46 62 25 1 81 72 82 52 54
Irish 91 85 90 78 80 82 70 80 56 58 94 90 92 82 85 96 95 99 97 98
Italian 87 71 80 64 51 69 42 58 31 12 92 74 84 61 43 100 98 99 100 98
Japanese 100 100 25 95 98 100 100 1 87 99 100 100 5 100 100 100 100 68 100 96
Kazakh 92 90 - 85 - 80 78 - 66 - 96 93 - 90 - 99 99 - 100 -
Korean 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Latin 87 73 - 70 - 72 49 - 43 - 93 76 - 71 - 97 93 - 96 -
Latvian 93 87 90 86 78 85 75 78 72 56 97 90 93 88 82 99 97 98 98 97
Lithuanian 95 87 89 79 72 86 76 74 56 40 98 89 92 83 77 100 98 99 99 98
Macedonian 84 72 83 68 46 66 52 66 37 10 86 70 83 68 32 99 95 100 98 97
Malay 31 31 23 19 4 26 22 19 10 0 38 36 22 20 0 30 36 28 27 11
Maori 92 83 - 92 - 84 64 - 85 - 92 88 - 90 - 99 98 - 100 -
Marathi 85 41 90 81 71 74 20 81 62 43 85 30 92 83 74 96 72 98 98 96
Mongolian 97 96 - 84 - 93 89 - 66 - 99 98 - 88 - 99 99 - 99 -
Nynorsk 66 52 - 55 - 41 25 - 24 - 66 49 - 47 - 90 81 - 92 -
Persian 90 80 81 75 62 78 62 65 53 29 94 80 79 74 58 100 98 99 99 99
Polish 95 90 90 83 81 85 77 76 61 57 98 93 93 89 86 100 99 100 100 100
Portuguese 81 69 63 58 40 59 42 34 22 7 85 70 58 54 19 98 95 98 98 94
Punjabi 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Romanian 87 72 78 67 55 69 49 57 34 24 92 74 80 68 50 99 94 97 99 91
Russian 90 78 80 50 53 76 59 62 20 22 95 84 85 43 50 98 92 94 86 87
Serbian 88 78 73 73 46 74 62 57 46 18 90 80 70 74 39 99 91 90 98 80
Shona 91 81 - - - 78 56 - - - 96 86 - - - 100 100 - - -
Slovak 84 75 76 70 47 64 49 53 39 12 90 78 76 73 38 99 97 98 99 92
Slovene 82 67 74 71 37 61 39 53 43 3 87 68 72 72 18 99 93 98 99 90
Somali 92 85 91 69 79 82 64 78 35 50 96 90 94 74 88 100 100 100 98 100
Sotho 85 72 - - - 67 43 - - - 90 75 - - - 99 97 - - -
Spanish 70 56 59 42 32 44 26 29 8 0 69 49 50 25 6 97 94 97 93 91
Swahili 81 70 75 73 60 60 43 50 45 26 84 68 75 74 58 98 97 99 99 98
Swedish 84 72 71 69 50 64 46 44 41 15 88 76 72 69 42 99 95 97 97 94
Tagalog 78 66 77 61 61 52 36 53 27 23 83 67 79 57 62 99 96 99 98 97
Tamil 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Telugu 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100
Thai 99 99 100 100 100 100 100 100 100 100 100 100 100 100 100 98 98 100 99 100
Tsonga 84 72 - - - 66 46 - - - 89 73 - - - 98 97 - - -
Tswana 84 71 - - - 65 44 - - - 88 73 - - - 99 96 - - -
Turkish 94 87 81 72 70 84 71 62