@@ -142,7 +142,6 @@ def pinyin_group(self, hans, style=Style.TONE, heteronym=False,
142142
143143 每个分组包含原始汉字和对应的拼音。拼音会根据情况进行处理:
144144 - 词语中的多个字的拼音会用空格连接
145- - 儿化音会合并处理(如:花儿 -> huar)
146145 - 需要隔音符的拼音会自动添加(如:西安 -> xi'an)
147146
148147 :param hans: 汉字字符串( ``'你好吗'`` )或列表( ``['你好', '吗']`` ).
@@ -179,18 +178,6 @@ def pinyin_group(self, hans, style=Style.TONE, heteronym=False,
179178 while i < len (han_list ):
180179 word = han_list [i ]
181180
182- # 检查是否需要与下一个字符合并(儿化音处理)
183- # 如果当前字是汉字且下一个字是"儿",合并处理
184- if (i + 1 < len (han_list ) and
185- han_list [i + 1 ] == '儿' and
186- RE_HANS .match (word )):
187- # 合并当前字和"儿"
188- word = word + '儿'
189- i += 1 # 跳过下一个字符
190- is_erhua = True
191- else :
192- is_erhua = False
193-
194181 # 获取该词的拼音
195182 pys = self .pinyin (
196183 word , style = style , heteronym = heteronym ,
@@ -209,69 +196,19 @@ def pinyin_group(self, hans, style=Style.TONE, heteronym=False,
209196 i += 1
210197 continue
211198
212- # 处理儿化音
213- if is_erhua and len (pys ) >= 2 :
214- # 获取倒数第二个拼音(花)和最后一个拼音(儿)
215- base_pinyin_list = pys [- 2 ]
216- er_pinyin_list = pys [- 1 ]
217-
218- if heteronym :
219- # 多音字模式:生成所有组合
220- combined = []
221- for base in base_pinyin_list :
222- for er in er_pinyin_list :
223- # 移除 er 的声母,只保留 r
224- er_suffix = 'r' if er else ''
225- combined .append (base + er_suffix )
226-
227- # 前面的拼音保持不变
228- if len (pys ) > 2 :
229- # 如果有多个字,前面的用空格连接
230- prev_pinyins = []
231- for j in range (len (pys ) - 2 ):
232- prev_pinyins .append (pys [j ])
233- # 为前面的拼音生成所有组合
234- if prev_pinyins :
235- prev_combinations = [
236- ' ' .join (p ) for p in product (* prev_pinyins )]
237- final_pinyins = [
238- prev + ' ' + comb
239- for prev in prev_combinations
240- for comb in combined ]
241- else :
242- final_pinyins = combined
243- else :
244- final_pinyins = combined
245- else :
246- # 非多音字模式:只取第一个
247- base = base_pinyin_list [0 ] if base_pinyin_list else ''
248- er = er_pinyin_list [0 ] if er_pinyin_list else ''
249- er_suffix = 'r' if er else ''
250- combined = base + er_suffix
251-
252- # 前面的拼音用空格连接
253- if len (pys ) > 2 :
254- prev_pinyins = [p [0 ] for p in pys [:- 2 ]]
255- final_pinyins = [' ' .join (prev_pinyins + [combined ])]
256- else :
257- final_pinyins = [combined ]
258-
259- result .append ({'hanzi' : word , 'pinyin' : final_pinyins })
199+ if heteronym :
200+ # 多音字模式:生成所有组合
201+ # 检查是否需要添加隔音符
202+ combinations = []
203+ for combo in product (* pys ):
204+ joined = _join_pinyin_with_separator (list (combo ))
205+ combinations .append (joined )
206+ result .append ({'hanzi' : word , 'pinyin' : combinations })
260207 else :
261- # 非儿化音处理
262- if heteronym :
263- # 多音字模式:生成所有组合
264- # 检查是否需要添加隔音符
265- combinations = []
266- for combo in product (* pys ):
267- joined = _join_pinyin_with_separator (list (combo ))
268- combinations .append (joined )
269- result .append ({'hanzi' : word , 'pinyin' : combinations })
270- else :
271- # 非多音字模式:只取第一个
272- pinyin_list = [p [0 ] if p else '' for p in pys ]
273- joined = _join_pinyin_with_separator (pinyin_list )
274- result .append ({'hanzi' : word , 'pinyin' : [joined ]})
208+ # 非多音字模式:只取第一个
209+ pinyin_list = [p [0 ] if p else '' for p in pys ]
210+ joined = _join_pinyin_with_separator (pinyin_list )
211+ result .append ({'hanzi' : word , 'pinyin' : [joined ]})
275212
276213 i += 1
277214
@@ -286,7 +223,6 @@ def lazy_pinyin_group(self, hans, style=Style.NORMAL,
286223
287224 每个分组包含原始汉字和对应的拼音字符串。拼音会根据情况进行处理:
288225 - 词语中的多个字的拼音会用空格连接
289- - 儿化音会合并处理(如:花儿 -> huar)
290226 - 需要隔音符的拼音会自动添加(如:西安 -> xi'an)
291227
292228 :param hans: 汉字字符串( ``'你好吗'`` )或列表( ``['你好', '吗']`` ).
@@ -519,13 +455,6 @@ def pinyin_group(hans, style=Style.TONE, heteronym=False,
519455 {'hanzi': '吗', 'pinyin': ['ma']},
520456 {'hanzi': '?', 'pinyin': []}]
521457 >>> # 如果西安在词库中,会输出 [{'hanzi': '西安', 'pinyin': ["xi'an"]}]
522- >>> # 如果花儿在词库中,会输出 [{'hanzi': '花儿', 'pinyin': ['huar']}]
523- >>> # 演示儿化音处理:如果词语以"儿"结尾,会自动合并
524- >>> result = pinyin_group('玩儿', style=Style.NORMAL)
525- >>> result[0]['hanzi']
526- '玩儿'
527- >>> 'r' in result[0]['pinyin'][0] # 儿化音包含 r
528- True
529458 """
530459 _pinyin = Pinyin (UltimateConverter (
531460 v_to_u = v_to_u , neutral_tone_with_five = neutral_tone_with_five ))
@@ -580,12 +509,6 @@ def lazy_pinyin_group(hans, style=Style.NORMAL,
580509 [{'hanzi': '你好', 'pinyin': 'ni hao'},
581510 {'hanzi': '吗', 'pinyin': 'ma'},
582511 {'hanzi': '?', 'pinyin': ''}]
583- >>> # 演示儿化音处理:如果词语以"儿"结尾,会自动合并
584- >>> result = lazy_pinyin_group('玩儿', style=Style.NORMAL)
585- >>> result[0]['hanzi']
586- '玩儿'
587- >>> 'r' in result[0]['pinyin'] # 儿化音包含 r
588- True
589512 """
590513 _pinyin = Pinyin (UltimateConverter (
591514 v_to_u = v_to_u , neutral_tone_with_five = neutral_tone_with_five ))
0 commit comments