overtrue / pinyin

🇨🇳 基于词库的中文转拼音优质解决方案
https://github.com/overtrue/pinyin
MIT License
4.32k stars 767 forks source link

$pinyin = Pinyin::polyphones('你电脑电脑电脑什么的',Converter::TONE_STYLE_NUMBER); #195

Closed 5wcx closed 1 year ago

5wcx commented 1 year ago

为什么解析出来的字去除重复了?输出结果是:Array ( [你] => Array ( [0] => ni3 ) [电] => Array ( [0] => dian4 ) [脑] => Array ( [0] => nao3 ) [什] => Array ( [0] => shen2 [1] => shi2 ) [么] => Array ( [0] => me [1] => yao1 [2] => mo2 [3] => ma ) [的] => Array ( [0] => de [1] => di1 [2] => di2 [3] => di4 ) )
三个“电脑” 只剩下一个了 请问是什么问题? 如何处理呢?

overtrue commented 1 year ago

默认是这样的, 不过我刚加了一个新方法支持你的需求:

Pinyin::polyphones('你电脑电脑电脑什么的', Converter::TONE_STYLE_NUMBER)->toArray()

默认返回格式:

[
    '你' => ['ni3'],
    '电' => ['dian4'],
    '脑' => ['nao3'],
    '什' => ['shen2', 'shi2'],
    '么' => ['me', 'yao1', 'mo2', 'ma'],
    '的' => ['de', 'di1', 'di2', 'di4'],
]

新增方法 polyphonesAsArray:

Pinyin::polyphonesAsArray('你电脑电脑电脑什么的', Converter::TONE_STYLE_NUMBER)->toArray()

返回结构示例:

[
    ['你' => ['ni3']],
    ['电' => ['dian4']],
    ['脑' => ['nao3']],
    ['电' => ['dian4']],
    ['脑' => ['nao3']],
    ['电' => ['dian4']],
    ['脑' => ['nao3']],
    ['什' => ['shen2', 'shi2']],
    ['么' => ['me', 'yao1', 'mo2', 'ma']],
    ['的' => ['de', 'di1', 'di2', 'di4']],
]