whatlang-php 使用指南
- whatlang-php:轻量高效的 PHP 语言检测库
- 项目简介
- 快速开始
- 基本使用
- 只检测语言
- 只检测书写系统
- 使用便捷函数
- 检测结果对象
- 高级用法
- 1. 语言过滤
- 只检测指定语言(白名单)
- 排除指定语言(黑名单)
- 2. 语言信息
- 3. 支持的语言
- 实际应用示例
- 示例 1:多语言网站内容检测
- 示例 2:用户输入语言验证
- 总结
whatlang-php:轻量高效的 PHP 语言检测库
whatlang-php是一个基于 whatlang-rs 库开发的 PHP 扩展/包,用于快速、准确地检测文本的语言。它支持检测 70 种语言和 26 种书写系统,具有高准确率和极快的检测速度,特别适合需要实时语言检测的 PHP 应用场景。
项目简介
- GitHub 仓库:SIugnur/whatlang-php
- 主要功能:检测给定文本的语言、脚本(书写系统)和置信度
- 核心优势:
- 高准确率:即使在短文本上也能保持较高准确率
- 轻量级:依赖少,易于集成
- 多语言支持:支持 70 种语言和 26 种书写系统检测
快速开始
composerrequire siugnur/whatlang-php基本使用
<?phprequire_once'vendor/autoload.php';useWhatlang\Detector;useWhatlang\Lang;useWhatlang\Script;$detector=Detector::new();$info=$detector->detect("Hello World");echo$info->lang();// Lang::Engecho$info->script();// Script::Latinecho$info->confidence();// 1.0echo$info->isReliable();// true只检测语言
$detector=Detector::new();$lang=$detector->detectLang("Bonjour le monde");echo$lang;// Lang::Fra只检测书写系统
$detector=Detector::new();$script=$detector->detectScript("Привет мир");echo$script;// Script::Cyrillic使用便捷函数
usefunctionWhatlang\{detect,detectLang,detectScript};$info=detect("こんにちは世界");echo$info->lang();// Lang::Jpn$lang=detectLang("你好世界");echo$lang;// Lang::Cmn检测结果对象
detect()方法返回一个检测结果对象,包含以下方法:
lang()- 返回语言枚举值(如Lang::Eng)script()- 返回书写系统枚举值(如Script::Latin)confidence()- 返回置信度分数(0.0 到 1.0)isReliable()- 返回布尔值,表示检测是否可靠
高级用法
1. 语言过滤
只检测指定语言(白名单)
$detector=Detector::withAllowlist([Lang::Eng,Lang::Spa,Lang::Fra]);$lang=$detector->detectLang("Hello World");// 如果白名单包含 Eng,返回 Eng,否则返回 null排除指定语言(黑名单)
$detector=Detector::withDenylist([Lang::Jpn,Lang::Kor]);$lang=$detector->detectLang("こんにちは");echo$lang;// null (日文被排除)2. 语言信息
// 获取语言属性Lang::Eng->code();// "eng"Lang::Eng->name();// "English"Lang::Eng->engName();// "English"// 通过代码获取语言Lang::fromCode("eng");// Lang::EngLang::fromCode("zho");// Lang::Cmn3. 支持的语言
whatlang-php 支持 70 种语言,包括:
| 语言 | 代码 | 语言 | 代码 |
|---|---|---|---|
| 中文 | zho | 英语 | eng |
| 俄语 | rus | 西班牙语 | spa |
| 法语 | fra | 德语 | deu |
| 日语 | jpn | 韩语 | kor |
| 阿拉伯语 | ara | 印地语 | hin |
| 葡萄牙语 | por | 意大利语 | ita |
| 越南语 | vie | 土耳其语 | tur |
| 荷兰语 | nld | 波兰语 | pol |
| 希腊语 | ell | 希伯来语 | heb |
| 波斯语 | fas | 泰语 | tha |
| 孟加拉语 | ben | 乌克兰语 | ukr |
查看全部 70 种语言…
实际应用示例
示例 1:多语言网站内容检测
classContentProcessor{private$detector;publicfunction__construct(){$this->detector=Detector::new();}publicfunctionprocessContent($content){$result=$this->detector->detect($content);if($result->isReliable()){$langCode=$result->langCode();switch($langCode){case'zh':return$this->processChinese($content);case'en':return$this->processEnglish($content);case'ja':return$this->processJapanese($content);default:return$this->processOther($content,$langCode);}}return$this->processUnknown($content);}// ... 各种语言的处理方法}示例 2:用户输入语言验证
classUserRegistration{private$detector;private$allowedLanguages=['en','zh','fr','es','de'];publicfunction__construct(){$this->detector=Detector::new();}publicfunctionvalidateBio($bio){if(empty($bio)){returntrue;}$result=$this->detector->detectWithOptions($bio,$this->allowedLanguages);if(!$result->isReliable()){thrownewException("无法识别个人简介的语言,请使用支持的语言书写");}returntrue;}}总结
whatlang-php是一个强大且高效的 PHP 语言检测库,特别适合需要快速、准确语言检测的应用场景。无论是构建多语言网站、内容分析系统还是用户输入验证,它都能提供可靠的解决方案。
通过简单的 Composer 安装和直观的 API,您可以轻松地将语言检测功能集成到您的 PHP 项目中。
开始使用:
composerrequire siugnur/whatlang-php欢迎 Star 项目并参与贡献!