lingua-go

repository·main·Indexed 23 days ago

https://github.com/pemistahl/lingua-go

A high-accuracy language detection library for Go, optimized for short text fragments such as single words or phrases. It supports 75 languages using a combination of a rule-based engine and a multi-size N-gram model (sizes 1 to 5). The library operates offline without external APIs and offers features such as confidence value computation, mixed-language detection, and configurable accuracy modes (High vs. Low) to balance memory usage and performance.

Tokens
4.5K
Snippets
12
Records
17
Agent score
30%

What's inside lingua-go

  1. Overview of lingua-go

    main

    lingua-go is a Go library designed for language detection. It identifies the language of a given text fragment, making it suitable as a preprocessing step for NLP tasks like text classification, spell checking, or routing communications (e.g., emails) to the correct departments.

    Key features include:

    • High accuracy on short text: Unlike many other libraries, it performs well on single words, phrases, and short snippets (like social media posts).
    • Scalable accuracy: Detection quality remains high even as the number of supported languages increases.
    • Offline capability: It uses a combination of rule-based and statistical methods without relying on external APIs or word dictionaries.
    • Zero configuration: It is designed to work out of the box with minimal setup.
  2. How lingua-go achieves high accuracy

    main

    Lingua uses two primary mechanisms to improve language detection accuracy, especially for short texts:

    1. Multi-size N-gram Model: Unlike most libraries that only use trigrams (n-grams of size 3), Lingua uses n-grams of sizes 1 up to 5. This provides more reliable probabilities for short phrases or single words where fewer n-grams are available.
    2. Rule-based Engine: Before applying the statistical model, a rule-based engine determines the alphabet of the input text and searches for characters unique to specific languages. If a language can be uniquely identified via these rules, the statistical model is bypassed. Otherwise, the engine filters out languages that do not satisfy the input text's conditions, reducing memory consumption and improving runtime performance.

    Best Practice: To optimize performance and accuracy, always restrict the set of languages to be considered using the API methods if you have prior knowledge of the expected input.

  3. Understand Lingua's detection accuracy and testing methodology

    main

    Lingua's performance is evaluated using bundled test data for each supported language. The accuracy is measured across three distinct types of input data:

    1. Single word detection: Words with a minimum length of 5 characters.
    2. Word pair detection: Pairs of words with a minimum length of 10 characters.
    3. Sentence detection: Complete grammatical sentences of various lengths.

    The test data is derived from the Wortschatz corpora provided by Leipzig University. For each test, a random subset of 1,000 single words, 1,000 word pairs, and 1,000 sentences is extracted from corpora consisting of 10,000 sentences from various websites.

  4. Build lingua-go from source

    main

    To build the project from the source repository, ensure you have at least Go version 1.18 installed. Follow these steps:

    1. Clone the repository.
    2. Navigate to the directory.
    3. Run the build command.
    git clone https://github.com/pemistahl/lingua-go.git
    cd lingua-go
    go build
  5. Generate accuracy test reports

    main

    You can reproduce the accuracy results by generating test reports for both classifiers and all languages.

    Steps:

    1. Navigate to the cmd directory.
    2. Run the accuracy_reporter.go script.
    cd cmd
    go run accuracy_reporter.go

    Note for gocld3: To run the reporter successfully with gocld3, you must install the exact version 3.17.3 of Google's protocol buffers.

    Generated reports are written to the /accuracy-reports directory.

  6. Set a minimum relative distance for detection

    main

    To avoid incorrect detections caused by words that are spelled identically in multiple languages (e.g., prologue in English and French), you can specify a minimum relative distance using WithMinimumRelativeDistance(distance).

    If the difference between the probabilities of the candidate languages does not satisfy this threshold, the detector will return lingua.Unknown and exists will be false.

    Note: The distance threshold is dependent on text length. For very short phrases, do not set this value too high, or lingua.Unknown will be returned frequently.

    package main
    
    import (
        "fmt"
        "github.com/pemistahl/lingua-go"
    )
    
    func main() {
        languages := []lingua.Language{
            lingua.English,
            lingua.French,
            lingua.German,
            lingua.Spanish,
        }
    
        detector := lingua.NewLanguageDetectorBuilder().
            FromLanguages(languages...).
            WithMinimumRelativeDistance(0.9).
            Build()
    
        language, exists := detector.DetectLanguageOf("languages are awesome")
    
        fmt.Println(language)
        fmt.Println(exists)
    
        // Output:
        // Unknown
        // false
    }
  7. Basic language detection with lingua-go

    main

    To perform basic language detection, use lingua.NewLanguageDetectorBuilder() to specify the set of candidate languages via FromLanguages(...), then call Build() to create a LanguageDetector. Use DetectLanguageOf(text) to identify the most likely language. It returns the detected lingua.Language and a boolean exists indicating if a reliable detection was made.

    package main
    
    import (
        "fmt"
        "github.com/pemistahl/lingua-go"
    )
    
    func main() {
        languages := []lingua.Language{
            lingua.English,
            lingua.French,
            lingua.German,
            lingua.Spanish,
        }
    
        detector := lingua.NewLanguageDetectorBuilder().
            FromLanguages(languages...).
            Build()
    
        if language, exists := detector.DetectLanguageOf("languages are awesome"); exists {
            fmt.Println(language)
        }
    
        // Output: English
    }
  8. Configure Eager vs Lazy loading

    main

    By default, Lingua uses lazy-loading, loading language models only when they are deemed relevant by the rule-based engine. This saves memory but can introduce latency on the first request.

    For web services where low latency is critical, you can enable eager-loading (preloading all models into memory) using WithPreloadedLanguageModels() in the builder. Multiple LanguageDetector instances share the same language models in memory.

    lingua.NewLanguageDetectorBuilder().
        FromAllLanguages().
        WithPreloadedLanguageModels().
        Build()
  9. Configure High Accuracy vs Low Accuracy mode

    main

    Lingua offers two modes to balance accuracy and resource usage:

    1. High Accuracy (Default): High detection accuracy but high memory consumption (~1,800 MB if all models are loaded) and slower performance.
    2. Low Accuracy Mode: Significantly lower memory consumption (~110 MB) and faster performance. It achieves this by loading only a small subset of language models.

    Trade-off: Accuracy for short texts (less than 120 characters) drops significantly in low accuracy mode, but accuracy for longer texts remains mostly unaffected. Use WithLowAccuracyMode() to enable this.

    lingua.NewLanguageDetectorBuilder().
        FromAllLanguages().
        WithLowAccuracyMode().
        Build()
  10. How the accuracy reporter works

    main

    The accuracy_reporter is a CLI tool used to benchmark the language detection accuracy of lingua-go against other libraries like cld3 and whatlanggo. It evaluates detection performance across three different input granularities:

    1. Single words: Detection accuracy for individual words.
    2. Word pairs: Detection accuracy for pairs of words.
    3. Sentences: Detection accuracy for full sentences.

    The tool iterates through all supported languages, reads test data from a language-testdata directory, and generates detailed reports in Markdown format and an aggregated CSV file containing comparative accuracy metrics.