@horizon-rs/language-guesser
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Language Guesser

Language Guesser is a robust and efficient library for detecting the language of a given text based on statistical analysis. It's perfect for applications that need to handle multilingual user input.

Installation

Install the package using npm:

npm install @horizon-rs/language-guesser

Or with yarn:

yarn add @horizon-rs/language-guesser

Usage

Detecting a list of possible languages of a text

import { Language } from '@horizon-rs/language-guesser';

const language = new Language();
const text = 'This is a sample text in English.';
const result = language.guess(text);

console.log('Detected language:', result);
Detected languages: [
  {
    alpha3: 'eng',
    alpha2: 'en',
    language: 'English',
    score: 0.9059288537549407
  },
...
]

Detecting the best single language of a given text

import { Language } from '@horizon-rs/language-guesser';

const language = new Language();
const text = 'This is a sample text in English.';
const result = language.guessBest(text);

console.log('Detected languages:', result);
Detected languages: {
  alpha3: 'eng',
  alpha2: 'en',
  language: 'English',
  score: 0.9059288537549407
}

Specifying expected language options for detection

import { Language } from '@horizon-rs/language-guesser';

const language = new Language();
const text = 'This is a sample text in English.';
const result = language.guessBest(text, ['eng']);

console.log('Detected language:', result);
Detected language: { alpha3: 'eng', alpha2: 'en', language: 'English', score: 1 }

API

Language Class

The Language class provides methods for language detection and related operations.

constructor()

Creates a new instance of the Language class.

guess(utterance: string, allowList?: string[], limit?: number): { alpha3: string; alpha2: string; language: string; score: number }[]

Detects the language of the provided utterance. You can optionally specify an allowList of accepted languages and a limit for the number of results.

guessBest(utterance: string, allowList?: string[]): { alpha3: string; alpha2: string; language: string; score: number } | undefined

Returns the best guess for the language of the utterance. You can optionally specify an allowList of accepted languages.

addExtraSentence(locale: string, sentence: string): void

Adds an extra sentence for language analysis. This can enhance the accuracy of language detection.

Contributing

We welcome contributions! Feel free to submit issues and pull requests. Let's make Language Guesser the best language detection library together!

Acknowledgements

Special thanks to AXA's NLP-JS, which served as a significant source of inspiration for this library.

License

This project is licensed under the MIT License - see the LICENSE file for details.

/@horizon-rs/language-guesser/

    Package Sidebar

    Install

    npm i @horizon-rs/language-guesser

    Weekly Downloads

    1,154

    Version

    0.1.1

    License

    MIT

    Unpacked Size

    314 kB

    Total Files

    12

    Last publish

    Collaborators

    • maximilianoneto