All Projects → Yurunsoft → Chineseutil

Yurunsoft / Chineseutil

Licence: mit
PHP 中文工具包,支持汉字转拼音、拼音分词、简繁互转、数字、金额大写;QQ群:17916227

Projects that are alternatives of or similar to Chineseutil

hanzi-pinyin-font
Chinese font displaying Hanzi (汉字) characters with by transliteration/pronunciation (Pīnyīn).
Stars: ✭ 79 (-80.87%)
Mutual labels:  pinyin, chinese
PHP-Chinese
PHP Chinese Conversion (中文繁簡轉換)
Stars: ✭ 37 (-91.04%)
Mutual labels:  chinese, chinese-simplified
ark-pixel-font
Open source Pan-CJK pixel font / 开源的泛中日韩像素字体
Stars: ✭ 1,767 (+327.85%)
Mutual labels:  chinese, chinese-simplified
Gpy
Go 语言汉字转拼音工具
Stars: ✭ 136 (-67.07%)
Mutual labels:  chinese, pinyin
syng
A free, open source, cross-platform, Chinese-To-English dictionary for desktops.
Stars: ✭ 108 (-73.85%)
Mutual labels:  pinyin, chinese
Icopy Site.github.io
icopy.site github mirror
Stars: ✭ 142 (-65.62%)
Mutual labels:  chinese, chinese-simplified
pinyin data
🐼 Easy to use and portable pronunciation data for Hanzi characters.
Stars: ✭ 13 (-96.85%)
Mutual labels:  pinyin, chinese
Go Pinyin
汉字转拼音
Stars: ✭ 907 (+119.61%)
Mutual labels:  chinese, pinyin
PyCasia
A python library to work with the CASIA Chinese handwriting database.
Stars: ✭ 38 (-90.8%)
Mutual labels:  chinese, chinese-simplified
pinyin4js
A opensource javascript library for converting chinese to pinyin。welcome Star : P
Stars: ✭ 153 (-62.95%)
Mutual labels:  pinyin, chinese
Cn sort
中文排序:按拼音/笔顺快速排序简体中文词组(百万数量级,可含中英/多音字)。如果对您有所帮助,欢迎点个star鼓励一下。
Stars: ✭ 102 (-75.3%)
Mutual labels:  chinese, pinyin
rust-pinyin
汉字转拼音
Stars: ✭ 111 (-73.12%)
Mutual labels:  pinyin, chinese
Chinese Copywriting Guidelines
Chinese copywriting guidelines for better written communication/中文文案排版指北
Stars: ✭ 10,648 (+2478.21%)
Mutual labels:  chinese, chinese-simplified
Somiao Pinyin
Somiao Pinyin: Train your own Chinese Input Method with Seq2seq Model 搜喵拼音输入法
Stars: ✭ 209 (-49.39%)
Mutual labels:  chinese, pinyin
Chinese Xinhua
📙 中华新华字典数据库。包括歇后语,成语,词语,汉字。
Stars: ✭ 8,705 (+2007.75%)
Mutual labels:  chinese, chinese-simplified
eslint-config-mingelz
A shared ESLint configuration with Chinese comments. 一份带有完整中文注释的 ESLint 规则。
Stars: ✭ 15 (-96.37%)
Mutual labels:  chinese, chinese-simplified
Opentracing Specification Zh
OpenTracing标准(中文版) `zh` (Chinese) translation of the opentracing/specification
Stars: ✭ 717 (+73.61%)
Mutual labels:  chinese, chinese-simplified
Hanbaobao
Mandarin Chinese text segmentation and mobile dictionary Android app (中文分词)
Stars: ✭ 17 (-95.88%)
Mutual labels:  chinese, pinyin
chinese-rhymer
轻量中文押韵神器,100%绝对可用,傻瓜式命令行操作,秒速实现烈焰单押,闪电双押,龙卷三押以及海啸式四押,目前版本 v0.2.6。Search for rhymes for Chinese words, with 1, 2, 3 and 4 characters, released on PyPI with current version of 0.2.6.
Stars: ✭ 72 (-82.57%)
Mutual labels:  pinyin, chinese
alfred-chinese-converter
支持 OpenCC 簡繁體中文詞彙級別轉換、異體字轉換以及地區習慣用詞轉換的 Alfred 2 workflow
Stars: ✭ 42 (-89.83%)
Mutual labels:  chinese, chinese-simplified

ChineseUtil

PHP 中文工具包,支持汉字转拼音、拼音分词、简繁互转、数字转换、金额数字转换。

由于中文的博大精深,字有多音字,简体字和繁体字也有多种对应。并且本类库返回的所有结果,均为包含所有组合的数组。

本类库字典数据总共收录 73925 个汉字,包括:3955 个简体字,1761 个繁体字,68209 个其它汉字。

模式

性能模式 (Memory)

使用 SQLite 作为数据载体,一次性加载所有数据到变量,内存占用高(80+ MB),性能最佳。

适合用于运行 Cli 任务。

需要 PDO 和 PDO_SQLITE 扩展支持。

通用模式 (SQLite)

使用 SQLite 作为数据载体,每次查询都通过 SQL 查询,内存占用低(100-200 KB),性能中等。

适合用于大部分场景。

需要 PDO 和 PDO_SQLITE 扩展支持。

兼容模式 (JSON)

使用精简过的 JSON 数据作为数据载体,一次性加载所有数据到变量,内存占用中(30+ MB),性能差。

内存占用量以实际为准,根据版本、扩展等环境的不同,占用的内存容量不一样,上述值为我电脑上的情况,仅供参考。

适合无法使用 PDO 的场景。

由于精简了数据,一些拼音结果需要经过代码计算处理才可以得出,所以性能较差。

FFI 模式 (FFI)

需要 PHP >= 7.4 并且启用 FFI 扩展,代码全部由 C++ 开发,性能和内存占用都比 PHP 实现的要好。

FFI 动态库 C++ 代码:https://github.com/Yurunsoft/chinese-util-cpp

Swoole FFI 模式 (SwooleFFI)

需要 PHP >= 7.4 并且启用 FFI、Swoole 扩展,代码全部由 C++ 开发,性能和内存占用都比 PHP 实现的要好。

不会阻塞 PHP 代码所在线程。


默认情况下,优先使用通用模式,如果环境不支持 PDO 将采用兼容模式。

你可以在未执行任何初始化或者转换处理之前,设置使用何种模式运行。

// 设为性能模式
Chinese::setMode('Memory');
// 设为通用模式
Chinese::setMode('SQLite');
// 设为兼容模式
Chinese::setMode('JSON');
// 设为 FFI 模式
Chinese::setMode('FFI');
// 设为Swoole FFI 模式
Chinese::setMode('SwooleFFI');

无论何种模式,拼音分词所需数据总是从 JSON 数据中加载。

FFI 参数设置:(一般用于自己编译的情况)

use Yurun\Util\Chinese\FFIDriver;

FFIDriver::$library = '.so 文件路径';
FFIDriver::$characterDataPath = '字符数据文件路径';
FFIDriver::$pinyinDataPath = '拼音数据文件路径';

使用说明

Composer 直接安装

composer require yurunsoft/chinese-util

Composer 项目配置引入

"require": {
    "yurunsoft/chinese-util" : "~2.0"
}

功能

汉字转拼音

use \Yurun\Util\Chinese;
use \Yurun\Util\Chinese\Pinyin;
$string = '恭喜發財!123';
echo $string, PHP_EOL;

echo '全拼:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN));

echo '首字母:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_FIRST));

echo '读音:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND));

echo '读音数字:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER));

echo '自选返回格式 + 以文本格式返回 + 自定义分隔符:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN | Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER, ' '));

echo '所有结果:', PHP_EOL;
var_dump(Chinese::toPinyin($string));

echo '不分割无拼音字符:', PHP_EOL;
var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN, ' ', false));

// 结果太长,请自行运行代码查看

拼音分词

结果是字符串:

use \Yurun\Util\Chinese;
$string2 = 'xianggang';
echo '"', $string2, '"的分词结果:', PHP_EOL;
var_dump(Chinese::splitPinyin($string2));

输出结果:

"xianggang"的分词结果:
array(2) {
  [0]=>
  string(11) "xiang gang"
  [1]=>
  string(12) "xi ang gang"
}

结果是数组:

use \Yurun\Util\Chinese;
$string2 = 'xianggang';
echo '"', $string2, '"的分词结果:', PHP_EOL;
var_dump(Chinese::splitPinyinArray($string2));

输出结果:

"xianggang"的分词结果:
array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "xiang"
    [1]=>
    string(4) "gang"
  }
  [1]=>
  array(3) {
    [0]=>
    string(2) "xi"
    [1]=>
    string(3) "ang"
    [2]=>
    string(4) "gang"
  }
}

简繁互转

use \Yurun\Util\Chinese;
$string3 = '中华人民共和国!恭喜發財!';
echo '"', $string3, '"的简体转换:', PHP_EOL;
var_dump(Chinese::toSimplified($string3));
echo '"', $string3, '"的繁体转换:', PHP_EOL;
var_dump(Chinese::toTraditional($string3));

输出结果:

"中华人民共和国!恭喜發財!"的简体转换:
array(1) {
  [0]=>
  string(39) "中华人民共和国!恭喜发财!"
}
"中华人民共和国!恭喜發財!"的繁体转换:
array(1) {
  [0]=>
  string(39) "中華人民共和國!恭喜發財!"
}

数字转换

use Yurun\Util\Chinese\Number;
function test($number)
{
    $chinese = Number::toChinese($number, [
        'tenMin'    =>  true, // “一十二” => “十二”
    ]);
    $afterNumber = Number::toNumber($chinese);
    echo $number, '=>', $chinese, '=>', $afterNumber, '=>', 0 === bccomp($number, $afterNumber, 20) ? 'true' : 'false', PHP_EOL;
}

test(1.234);
test(-1234567890.666);
test(pi());

输出结果:

1.234=>一点二三四=>1.234=>true
-1234567890.666=>负十二亿三千四百五十六万七千八百九十点六六六=>-1234567890.666=>true
3.1415926535898=>三点一四一五九二六五三五八九八=>3.1415926535898=>true

金额数字转换

use Yurun\Util\Chinese\Money;
function test($number)
{
    $chinese = Money::toChinese($number, [
        'tenMin'    =>  true, // “一十二” => “十二”
    ]);
    $afterMoney = Money::toNumber($chinese);
    echo $number, '=>', $chinese, '=>', $afterMoney, '=>', 0 === bccomp($number, $afterMoney) ? 'true' : 'false', PHP_EOL;
}

test(1.234);
test(-1234567890.666);

输出结果:

输出结果:
1.234=>壹圆贰角叁分肆厘=>1.234=>true
-1234567890.666=>负壹拾贰亿叁仟肆佰伍拾陆万柒仟捌佰玖拾圆陆角陆分陆厘=>-1234567890.666=>true
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].