支持汉转拼和拼音分词的PHP中文工具类ChineseUtil

本文给大家推荐的是一款PHP的中文分词工具类,功能包括支持汉字转拼音、拼音分词、简繁互转,非常不错,有需要的小伙伴可以参考下。

PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转。

PHP Chinese Tool class, support Chinese pinyin, pinyin participle, simplified and traditional conversion

目前本类库拥有的三个功能,都是在实际开发过程中整理出来的。这次使用的数据不同于以前我开源过汉字转拼音和简繁互转,数据都是从字典网站采集下来的,比以前的数据更加准确。

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

本类库字典数据加载后会占用 40+ MB 内存,在访问量大的接口要使用此类汉字转拼音、繁简转换功能时,推荐用 Swoole 开发一个异步服务程序,只需加载一次数据,就可以持续高效地为你提供服务。

使用说明

Composer 直接安装

composer require yurunsoft/chinese-util

Composer 项目配置引入

  1. "require": {
  2. "yurunsoft/chinese-util" : "~1.0"
  3. }

功能

汉字转拼音

  1. use \Yurun\Util\Chinese;
  2. $string = '恭喜發財!把我翻译成拼音看下?';
  3. echo $string, PHP_EOL;
  4. echo '所有结果:', PHP_EOL;
  5. var_dump(Chinese::toPinyin($string));
  6. echo '全拼:', PHP_EOL;
  7. var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN));
  8. echo '首字母:', PHP_EOL;
  9. var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_FIRST));
  10. echo '读音:', PHP_EOL;
  11. var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND));
  12. echo '读音数字:', PHP_EOL;
  13. var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER));
  14. echo '自选 + 自定义分隔符:', PHP_EOL;
  15. var_dump(Chinese::toPinyin($string, Pinyin::CONVERT_MODE_PINYIN | Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER, '/'));
  16. /**
  17. 输出结果:
  18. array(4) {
  19. ["pinyin"]=>
  20. array(1) {
  21. [0]=>
  22. string(58) "gong xi fa cai ! ba wo fan yi cheng pin yin kan xia ? "
  23. }
  24. ["pinyinSound"]=>
  25. array(4) {
  26. [0]=>
  27. string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kàn xià "
  28. [1]=>
  29. string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kàn xià "
  30. [2]=>
  31. string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kān xià "
  32. [3]=>
  33. string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kān xià "
  34. }
  35. ["pinyinSoundNumber"]=>
  36. array(4) {
  37. [0]=>
  38. string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "
  39. [1]=>
  40. string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "
  41. [2]=>
  42. string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "
  43. [3]=>
  44. string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "
  45. }
  46. ["pinyinFirst"]=>
  47. array(1) {
  48. [0]=>
  49. string(34) "g x f c ! b w f y c p y k x ? "
  50. }
  51. }
  52. 全拼:
  53. array(1) {
  54. ["pinyin"]=>
  55. array(1) {
  56. [0]=>
  57. string(58) "gong xi fa cai ! ba wo fan yi cheng pin yin kan xia ? "
  58. }
  59. }
  60. 首字母:
  61. array(1) {
  62. ["pinyinFirst"]=>
  63. array(1) {
  64. [0]=>
  65. string(34) "g x f c ! b w f y c p y k x ? "
  66. }
  67. }
  68. 读音:
  69. array(1) {
  70. ["pinyinSound"]=>
  71. array(4) {
  72. [0]=>
  73. string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kàn xià "
  74. [1]=>
  75. string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kàn xià "
  76. [2]=>
  77. string(63) "gōng xǐ fā cái bǎ wǒ fān yì chéng pīn yīn kān xià "
  78. [3]=>
  79. string(63) "gōng xǐ fā cái bà wǒ fān yì chéng pīn yīn kān xià "
  80. }
  81. }
  82. 读音数字:
  83. array(1) {
  84. ["pinyinSoundNumber"]=>
  85. array(4) {
  86. [0]=>
  87. string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "
  88. [1]=>
  89. string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan4 xia4 "
  90. [2]=>
  91. string(63) "gong1 xi3 fa1 cai2 ba3 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "
  92. [3]=>
  93. string(63) "gong1 xi3 fa1 cai2 ba4 wo3 fan1 yi4 cheng2 pin1 yin1 kan1 xia4 "
  94. }
  95. }
  96. 自选 + 自定义分隔符:
  97. array(2) {
  98. ["pinyin"]=>
  99. array(1) {
  100. [0]=>
  101. string(58) "gong/xi/fa/cai/!/ba/wo/fan/yi/cheng/pin/yin/kan/xia/?/"
  102. }
  103. ["pinyinSoundNumber"]=>
  104. array(4) {
  105. [0]=>
  106. string(63) "gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/kan4/xia4/"
  107. [1]=>
  108. string(63) "gong1/xi3/fa1/cai2/ba4/wo3/fan1/yi4/cheng2/pin1/yin1/kan4/xia4/"
  109. [2]=>
  110. string(63) "gong1/xi3/fa1/cai2/ba3/wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/"
  111. [3]=>
  112. string(63) "gong1/xi3/fa1/cai2/ba4/wo3/fan1/yi4/cheng2/pin1/yin1/kan1/xia4/"
  113. }
  114. }
  115. * /
  116. 拼音分词
  117. use \Yurun\Util\Chinese;
  118. $string2 = 'xianggang';
  119. echo '"', $string2, '"的分词结果:', PHP_EOL;
  120. var_dump(Chinese::splitPinyin($string2));
  121. /**
  122. 输出结果:
  123. "xianggang"的分词结果:
  124. array(2) {
  125. [0]=>
  126. string(12) "xi ang gang "
  127. [1]=>
  128. string(11) "xiang gang "
  129. }
  130. * /
  131. 简繁互转
  132. use \Yurun\Util\Chinese;
  133. $string3 = '中华人民共和国!恭喜發財!';
  134. echo '"', $string3, '"的简体转换:', PHP_EOL;
  135. var_dump(Chinese::toSimplified($string3));
  136. echo '"', $string3, '"的繁体转换:', PHP_EOL;
  137. var_dump(Chinese::toTraditional($string3));
  138. /**
  139. 输出结果:
  140. "中华人民共和国!恭喜發財!"的简体转换:
  141. array(1) {
  142. [0]=>
  143. string(39) "中华人民共和国!恭喜发财!"
  144. }
  145. "中华人民共和国!恭喜發財!"的繁体转换:
  146. array(1) {
  147. [0]=>
  148. string(39) "中華人民共和國!恭喜發財!"
  149. }
  150. * /