cld2-php-ext icon indicating copy to clipboard operation
cld2-php-ext copied to clipboard

:uk: :it: :de: This extension wraps CLD2 (Compact Language Detector 2) that detects over 80 languages in Unicode UTF-8 text.

Compact Language Detector 2 PHP Extension

This extension wraps CLD2 (Compact Language Detector 2) that detects over 80 languages in Unicode UTF-8 text.

Usage

Detector

$cld2 = new \CLD2Detector();

$cld2->setTldHint('it'); // optional, hints about the Top level domain (it: italian, fr: french, de: german etc..)
$cld2->setLanguageHint(CLD2Language::GERMAN); // optional, hints about the language.
$cld2->setEncodingHint(CLD2Encoding::UTF8); // optional, hints about text encoding
 
var_dump($cld2->detect('My name is Melissa'));

// Output
array(5) {
  'language_id' =>
  int(0)
  'language_code' =>
  string(2) "en"
  'language_name' =>
  string(7) "ENGLISH"
  'language_probability' =>
  int(95)
  'is_reliable' =>
  bool(true)
}

Language

Get language name from Language id

$lang = CLD2Language::PAMPANGA;
echo CLD2Language::languageName($lang);

// Output
string(8) "PAMPANGA"

Get language code from Language id

echo CLD2Language::languageCode(CLD2Language::PAMPANGA);

// Output
string(3) "pam"

Get language id from name where Name can be either full name or ISO code, or can be ISO code embedded in a language-script combination such as "en-Latn-GB"

echo CLD2Language::getLanguageFromName('PAMPANGA');
// or
echo CLD2Language::getLanguageFromName('pam');

// Output
int(176)

Encoding

Get encoding name from id

$enc = CLD2Encoding::UTF8;
echo CLD2Encoding::encodingName($enc);

// Output
string(4) "UTF8"

Installation

From PECL

Not submitted yet.

Build from source

First of all clone current repository and enter into its directory.

git clone [email protected]:fntlnz/cld2-php-ext.git
cd cld2-php-ext

Compile CLD2 library

wget -nv -O - https://github.com/CLD2Owners/cld2/archive/master.tar.gz | tar zx
cd cld2-master/internal
CFLAGS="-Wno-narrowing" ./compile_libs.sh
sudo cp libcld2.so /usr/local/lib

Compile CLD2 PHP extension

Come back to the cld2-php-ext directory and execute:

phpize
./configure --with-cld2=cld2-master
make -j
sudo make install

Do not forget to add extension=cld2.so to your PHP ini.

If you have a libtool version mismatch, this may help:

rm aclocal.m4
autoreconf -i

Contributing

See CONTRIBUTING.md

NOTES

Analytics