2017 © Pedro PelĂĄez
 

library languagedetector

simple library to classify texts

image

crodas/languagedetector

simple library to classify texts

  • Tuesday, January 31, 2017
  • by crodas
  • Repository
  • 31 Watchers
  • 304 Stars
  • 65,497 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 61 Forks
  • 7 Open issues
  • 7 Versions
  • 5 % Grown

The README.md

LanguageDetector Build Status Flattr this git repo

PHP Class to detect languages from any free text., (*1)

It follows the approach described in the paper, a given text is tokenized into N-Grams (we cleanup whitespaces before doing this step). Then we sort the tokens and we compare against a language model., (*2)

How it works

The first thing we need is a language model (which looks like this file) that is used to compare the texts against at classification time. This process must done before anything, and it can be generated with an script similar to this file., (*3)

// register the autoloader
require 'lib/LanguageDetector/autoload.php';

// it could use a little bit of memory, but it's fine
// because this process runs once.
ini_set('memory_limit', '1G');

// we load the configuration (which will be serialized
// later into our language model file
$config = new LanguageDetector\Config;

$c = new LanguageDetector\Learn($config);
foreach (glob(__DIR__ . '/samples/*') as $file) { 
    // feed with examples ('language', 'text');
    $c->addSample(basename($file), file_get_contents($file));
}

// some callback so we know where the process is 
$c->addStepCallback(function($lang, $status) {
    echo "Learning {$lang}: $status\n";
});

// save it in `datafile`. 
// we currently support the `php` serialization but it's trivial
// to add other formats, just extend `\LanguageDetector\Format\AbstractFormat`. 
//You can check example at https://github.com/crodas/LanguageDetector/blob/master/lib/LanguageDetector/Format/PHP.php
$c->save(AbstractFormat::initFormatByPath('language.php'));

Once we have our language model file (in this case language.php) we're ready to classify texts by their language., (*4)

// register the autoloader
require 'lib/LanguageDetector/autoload.php';

// we load the language model, it would create
// the $config object for us.
$detect = LanguageDetector\Detect::initByPath('language.php');

$lang = $detect->detect("Agricultura (-ae, f.), sensu latissimo, 
est summa omnium artium et scientiarum et technologiarum quae de 
terris colendis et animalibus creandis curant, ut poma, frumenta, 
charas, carnes, textilia, et aliae res e terra bene producantur. 
Specialius, agronomia est ars et scientia quae terris colendis student, 
agricultio autem animalibus creandis.")

var_dump($lang);

And that's it., (*5)

Algorithms

The project is designed to work with modules, which means you can provide your own algorithm for sorting and comparing the N-Grams. By default the library implements the PageRank as sorting algorithm, and out of place (described in the paper) as comparing., (*6)

In order to supply your own algorithms, you must change the $config at learning stage to load your own classes (which by the way should implement some interaces)., (*7)

The Versions

31/01 2017

dev-develop

dev-develop

simple library to classify texts

  Sources   Download

BSD-4-Clause

The Requires

 

by CĂ©sar D. Rodas

29/08 2014

dev-master

9999999-dev

simple library to classify texts

  Sources   Download

BSD-4-Clause

The Development Requires

by CĂ©sar D. Rodas

06/11 2013

v0.1.1

0.1.1.0

simple library to classify texts

  Sources   Download

BSD-4-Clause

The Development Requires

by CĂ©sar D. Rodas

05/11 2013

v0.1.0

0.1.0.0

simple library to classify texts

  Sources   Download

BSD-4-Clause

The Development Requires

by CĂ©sar D. Rodas

20/07 2013

dev-redaktor-patch-2

dev-redaktor-patch-2

simple library to classify texts

  Sources   Download

by CĂ©sar D. Rodas

23/05 2013

dev-adam-lynch-patch-1

dev-adam-lynch-patch-1

simple library to classify texts

  Sources   Download

by CĂ©sar D. Rodas

11/04 2013

dev-better-learning

dev-better-learning

simple library to classify texts

  Sources   Download

by CĂ©sar D. Rodas