hamidgh83/language-detection

A language detection library for PHP. Detects the language from a given text string.

v3.5.5 2022-04-29 08:30 UTC

This package is auto-updated.

Last update: 2024-11-29 05:26:57 UTC


README

The package

Version Total Downloads Dependents License

language-detection

ُThis is an extension of language-detection where you can find its documentation.

This library can detect the language of a given text string. It can parse given training text in many different idioms into a sequence of N-grams and builds a database file in JSON format to be used in the detection phase. Then it can take a given text and detect its language using the database previously generated in the training phase. The library comes with text samples used for training and detecting text in 110 languages.

Installation with Composer

Note: This library requires the Multibyte String extension in order to work.

$ composer require hamidgh83/language-detection

Basic Usage

Basic usage of this library is the same as original library but the following methods has beed added:

bestResult()

Returns the best result as a string.

$ld->detect('Mag het een onsje meer zijn?')->bestResult();

Result:

string(2) "nl"

getSupportLanguages()

This method returns a list if supportted languages as an array.

$ld->getSupportLanguages();

Result:

Array
(
    0 => ab
    1 => af
    2 => am
    3 => ar
    4 => ay
    5 => az-Cyrl
    6 => az-Latn
    7 => be
    8 => bg
    9 => bi
    10 => bn
    11 => bo
    12 => br
    [...]
)

getLanguageProps($lang)

Every language has their own properties and you can get the it as follow:

$lng    = new Language();
$result = $lng->detect('This is an example text.');
$props  = $lng->getLanguageProps($result->bestResult());

Result:

Array
(
    'props' => Array(
        'language' => 'English',
        'language' => Array (
            'United Kingdom',
            'United States of America'
        ),
        'direction' => 'ltr'
    )
)

Supported languages

The library currently supports 110 languages. To get an overview of all supported languages please have a look at here.