diff --git a/README.md b/README.md index 1933240..69c5263 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,22 @@ LanguageDetector LanguageDetector is a PHP library that detects the language from strings or texts. +Table of contents +================= +- [Features](#features) +- [Install](#install) +- [Examples](#usage) + - [Single detection](#single-detection) + - [Multiple detections](#multiple-detections) + - [Other methods](#other-methods) +- [API Methods](#api-methods) + - [evaluate()](#evaluate) + - [getLanguage()](#getlanguage) + - [getScores()](#getscores) + - [getSupportedLanguages()](#getsupportedlanguages) + - [getText()](#gettext) + + Features -------- @@ -15,37 +31,45 @@ Features - Packaged with a 2MB learned dataset - Small code, easy to modify - N-grams algorythm +- Supports PHP 5.4, 5.5, 5.6, 7.0, 7.1 and HHVM -Usage ------ +Install +------------ -### API Methods -```php - -require_once 'vendor/autoload.php'; +```sh +composer require landrok/language-detector:1.0 +``` -$detector = new LanguageDetector\LanguageDetector(); +________________________________________________________________________ -// Gets detected language -$lang = $detector->evaluate('My sentence is in english')->getLanguage(); +Examples +-------- +________________________________________________________________________ -// Gets all supported languages -$languages = $detector->getSupportedLanguages(); +### Single detection -// Gets all scores -$scores = $detector->getScores(); +This usage is not recommended if you want to make several consecutive evaluations -// Gets detected language again -$lang = $detector->getLanguage(); +```php +require_once 'vendor/autoload.php'; +// One line usage: instanciate, evaluate and get language +$lang = ( new LanguageDetector\LanguageDetector() ) + ->evaluate('My sentence is in english') + ->getLanguage(); ``` +________________________________________________________________________ + +### Multiple detections + +If you have a list of texts to detect, a possible way is to instanciate +once and then to translate your list. -### Common usage ```php require_once 'vendor/autoload.php'; -$strings = [ +$texts = [ 'My sentence is in english', 'My sentence number two is in english too' ]; @@ -54,22 +78,125 @@ $results = []; $detector = new LanguageDetector\LanguageDetector(); -foreach($strings as $string) +foreach ($texts as $text) { - $results[] = $detector->evaluate($string)->getLanguage(); + $results[] = $detector->evaluate($text)->getLanguage(); } +``` +________________________________________________________________________ + +### Other methods + +LanguageDetector has other information methods: + +```php +[...] + +// Gets all supported languages +$languages = $detector->getSupportedLanguages(); + +// Gets all scores +$scores = $detector->getScores(); ``` -### One line usage +________________________________________________________________________ -This usage is not recommended if you want to make several consecutive evaluations +API Methods +----------- + +________________________________________________________________________ +####evaluate() + +__Type__ *\LanguageDetector\LanguageDetector* + +It performs the evaluation. + +__Example__ ```php -require_once 'vendor/autoload.php'; +$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.'); -// One line usage: instanciate, evaluate and get language -$lang = ( new LanguageDetector\LanguageDetector() ) - ->evaluate('My sentence is in english') - ->getLanguage(); +// Then you have access to the detected language +$detector->getLanguage(); // Returns 'en' +``` +You can make a one line call + +```php +$detector->evaluate('My tailor is rich and Alison is in the kitchen with Bob.')->getLanguage(); // Returns 'en' +``` +________________________________________________________________________ + +####getLanguage() + +__Type__ *string* + +The detected language + +__Example__ + +```php +$detector->getLanguage(); // Returns 'en' +``` +________________________________________________________________________ +####getScores() + +__Type__ *array* + +A list of scores by languages, for all evaluated languages. + +__Example__ + +```php +$detector->getScores(); + +// Returns something like +Array +( + [en] => 0.43950135722745 + [nl] => 0.40898789832569 + [...] + [ja] => 0 + [fa] => 0 +) + + +``` +________________________________________________________________________ +####getSupportedLanguages() + +__Type__ *array* + +A list of supported languages. they will be evaluated. + +__Example__ + +```php +$detector->getSupportedLanguages(); + +// Returns something like +Array +( + [0] => af + [1] => ar + [...] + [51] => zh-cn + [52] => zh-tw + +) +``` +________________________________________________________________________ +####getText() + +__Type__ *string* + +Returns the last string which has been evaluated + +__Example__ + +```php +$detector->getText(); + +// Returns 'My tailor is rich and Alison is in the kitchen with Bob.' ``` +________________________________________________________________________