{"id":16707822,"url":"https://github.com/landrok/language-detector","last_synced_at":"2025-04-05T07:07:44.554Z","repository":{"id":46241519,"uuid":"68449172","full_name":"landrok/language-detector","owner":"landrok","description":"A fast and reliable PHP library for detecting languages","archived":false,"fork":false,"pushed_at":"2023-12-21T13:00:16.000Z","size":956,"stargazers_count":123,"open_issues_count":6,"forks_count":19,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-29T06:08:27.297Z","etag":null,"topics":["language-detector","ngrams"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/landrok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["landrok"],"ko_fi":"landrok"}},"created_at":"2016-09-17T11:50:51.000Z","updated_at":"2025-03-21T07:25:48.000Z","dependencies_parsed_at":"2023-12-21T15:02:40.222Z","dependency_job_id":"23325365-78f0-4fa4-81dd-ac8058cc6dc0","html_url":"https://github.com/landrok/language-detector","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/landrok%2Flanguage-detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/landrok%2Flanguage-detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/landrok%2Flanguage-detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/landrok%2Flanguage-detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/landrok","download_url":"https://codeload.github.com/landrok/language-detector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247299833,"owners_count":20916190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["language-detector","ngrams"],"created_at":"2024-10-12T19:41:08.773Z","updated_at":"2025-04-05T07:07:44.537Z","avatar_url":"https://github.com/landrok.png","language":"PHP","funding_links":["https://github.com/sponsors/landrok","https://ko-fi.com/landrok"],"categories":[],"sub_categories":[],"readme":"LanguageDetector\n================\n\n[![Build Status](https://travis-ci.com/landrok/language-detector.svg?branch=master)](https://travis-ci.com/landrok/language-detector)\n[![Test Coverage](https://codeclimate.com/github/landrok/language-detector/badges/coverage.svg)](https://codeclimate.com/github/landrok/language-detector/coverage)\n[![Code Climate](https://codeclimate.com/github/landrok/language-detector/badges/gpa.svg)](https://codeclimate.com/github/landrok/language-detector)\n\nLanguageDetector is a PHP library that detects the language from a text\nstring.\n\nTable of contents\n=================\n- [Features](#features)\n- [Install](#install)\n- [Quick usage](#quick-usage)\n  - [Detect language](#detect-language)\n- [API Methods](#api-methods)\n  - [evaluate()](#evaluate)\n  - [getLanguage()](#getlanguage)\n  - [getLanguages()](#getLanguages)\n  - [getScores()](#getscores)\n  - [getSupportedLanguages()](#getsupportedlanguages)\n  - [getText()](#gettext)\n  - [options](#options)\n  - [For one-liners only](#for-one-liners-only)\n\n\n\nFeatures\n--------\n\n- More than 50 supported languages, including Klingon\n- Very fast, no database needed\n- Packaged with a 2MB dataset\n- Learning steps are already done, library is ready to use\n- Small code, small footprint\n- N-grams algorithm\n- Supports PHP 5.4+, 7+ and 8+ and HHVM\n  The latest release 1.4.x only supports PHP\u003e=7.4\n\n\nInstall\n------------\n\n```sh\ncomposer require landrok/language-detector\n```\n\n________________________________________________________________________\n\nQuick usage\n-----------\n\n### Detect language\n\nInstanciate a detector, pass a text and get the detected language.\n\n```php\nrequire_once 'vendor/autoload.php';\n\n$text = 'My tailor is rich and Alison is in the kitchen with Bob.';\n\n$detector = new LanguageDetector\\LanguageDetector();\n\n$language = $detector-\u003eevaluate($text)-\u003egetLanguage();\n\necho $language; // Prints something like 'en'\n```\n\nOnce it's instanciated, you can test multiple texts.\n\n```php\nrequire_once 'vendor/autoload.php';\n\n// An array of texts to evaluate\n$texts = [\n    'My tailor is rich and Alison is in the kitchen with Bob.',\n    'Mon tailleur est riche et Alison est dans la cuisine avec Bob'\n];\n\n$detector = new LanguageDetector\\LanguageDetector();\n\nforeach ($texts as $key =\u003e $text) {\n\n    $language = $detector-\u003eevaluate($text)-\u003egetLanguage();\n\n    echo sprintf(\n        \"Text %d, language=%s\\n\",\n        $key,\n        $language\n    );\n}\n\n```\n\nWould output something like:\n\n```sh\nText 0, language=en\nText 1, language=fr\n```\n\nAdditionally, you can use a _LanguageDetector_ instance as a string.\n\n```php\nrequire_once 'vendor/autoload.php';\n\n$text = 'My tailor is rich and Alison is in the kitchen with Bob.';\n\n$detector = new LanguageDetector\\LanguageDetector();\n\necho $detector-\u003eevaluate($text); // Prints something like 'en'\necho $detector; // Prints something like 'en' after an evaluate()\n```\n\n________________________________________________________________________\n\nAPI Methods\n-----------\n\n#### evaluate()\n\n__Type__ *\\LanguageDetector\\LanguageDetector*\n\nIt performs an evaluation on a given text.\n\n__Example__\n\nAfter an `evaluate()`, the result is stored and available for later use.\n\n```php\n$detector-\u003eevaluate('My tailor is rich and Alison is in the kitchen with Bob.');\n\n// Then you have access to the detected language\n$detector-\u003egetLanguage(); // Returns 'en'\n```\n\nYou can make a one line call.\n\n```php\n$detector-\u003eevaluate('My tailor is rich and Alison is in the kitchen with Bob.')\n         -\u003egetLanguage(); // Returns 'en'\n```\n\nIt's possible to directly print `evaluate()` output.\n\n```php\n// Returns 'en'\necho $detector-\u003eevaluate('My tailor is rich and Alison is in the kitchen with Bob.');\n\n```\n________________________________________________________________________\n\n#### getLanguage()\n\n__Type__ *string*\n\nThe detected language\n\n__Example__\n\n```php\n$detector-\u003egetLanguage(); // Returns 'en'\n```\n________________________________________________________________________\n\n#### getLanguages()\n\n__Type__ *array*\n\nA list of loaded models that will be evaluated.\n\n__Example__\n\n```php\n$detector-\u003egetLanguages(); // Returns something like ['de', 'en', 'fr']\n```\n________________________________________________________________________\n\n#### getScores()\n\n__Type__ *array*\n\nA list of scores by language, for all evaluated languages.\n\n__Example__\n\n```php\n$detector-\u003egetScores();\n\n// Returns something like\nArray\n(\n    [en] =\u003e 0.43950135722745\n    [nl] =\u003e 0.40898789832569\n    [...]\n    [ja] =\u003e 0\n    [fa] =\u003e 0\n)\n\n\n```\n________________________________________________________________________\n#### getSupportedLanguages()\n\n__Type__ *array*\n\nA list of supported languages that will be evaluated.\n\n__Example__\n\n```php\n$detector-\u003egetSupportedLanguages();\n\n// Returns something like\nArray\n(\n    [0] =\u003e af\n    [1] =\u003e ar\n    [...]\n    [51] =\u003e zh-cn\n    [52] =\u003e zh-tw\n\n)\n```\n________________________________________________________________________\n#### getText()\n\n__Type__ *string*\n\nReturns the last string which has been evaluated\n\n__Example__\n\n```php\n$detector-\u003egetText();\n\n// Returns 'My tailor is rich and Alison is in the kitchen with Bob.'\n```\n________________________________________________________________________\n\n#### Options\n\n__Type__ *\\LanguageDetector\\LanguageDetector*\n\nFor even better performance, loaded models can be specified explicitly.\n\n__Example__\n\n```php\n\n$text = 'My tailor is rich and Alison is in the kitchen with Bob.';\n\n$detector = new LanguageDetector(null, ['en', 'fr', 'de']);\n\n$language = $detector-\u003eevaluate($text);\n\necho $language; // Prints something like 'en'\n```\n________________________________________________________________________\n\n#### For one-liners only\n\n__Type__ *\\LanguageDetector\\LanguageDetector*\n\nWith a static call on detect() method, you can perform an evaluation on\na given text, in one line.\n\n__Example__\n\n```php\n\necho LanguageDetector\\LanguageDetector::detect(\n    'My tailor is rich and Alison is in the kitchen with Bob.'\n); // Returns 'en'\n```\n\nYou can use all API methods.\n\n```php\n$detector = LanguageDetector\\LanguageDetector::detect(\n    'My tailor is rich and Alison is in the kitchen with Bob.'\n);\n\n// en\necho $detector;\n\n// en\necho $detector-\u003egetLanguage();\n\n// An array of all scores, see API method\nprint_r($detector-\u003egetScores());\n\n// An array of all supported languages, see API method\nprint_r($detector-\u003egetSupportedLanguages());\n\n// The last evaluated string\necho $detector-\u003egetText();\n\n// Limit loaded languages for even better performance\necho LanguageDetector\\LanguageDetector::detect(\n    'My tailor is rich and Alison is in the kitchen with Bob.',\n    ['en', 'de', 'fr', 'es']\n); // en\n\n```\n________________________________________________________________________\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flandrok%2Flanguage-detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flandrok%2Flanguage-detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flandrok%2Flanguage-detector/lists"}