o
    Ti:                     @   s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ e  G dd deZG dd deZG dd deeZG dd deeZG dd deeZG dd deZeZdS )    )unicode_literals)deepcopy)	text_typecallable_check)PHRASES_DICTPINYIN_DICTRE_HANS)V2UMixin)NeutralToneWith5Mixin)ToneSandhiMixin)_remove_dup_and_empty)auto_discover)convertc                   @   s   e Zd Zdd ZdS )	Converterc                 K   s   t N)NotImplementedError)selfwordsstyle	heteronymerrorsstrictkwargs r   F/home/ubuntu/.local/lib/python3.10/site-packages/pypinyin/converter.pyr      s   zConverter.convertN)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )DefaultConverterc                 K   s   d S r   r   )r   r   r   r   r   __init__   s   zDefaultConverter.__init__c           
      K   s   g }t |r,| j|||||d}| |||}|dur|}| ||||||}t|S | j|||||d}	|	r=||	 t|S )uw  根据参数把汉字转成相应风格的拼音结果。

        :param words: 汉字字符串
        :type words: unicode
        :param style: 拼音风格
        :param heteronym: 是否启用多音字
        :type heteronym: bool
        :param errors: 如果处理没有拼音的字符
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :type strict: bool
        :return: 按风格转换后的拼音结果
        :rtype: list

        )r   r   r   r   Nr   r   r   r   )r   match_phrase_pinyinpost_pinyinconvert_styleshandle_nopinyinextendr   )
r   r   r   r   r   r   r   pys	post_datapyr   r   r   r   !   s$   

	

zDefaultConverter.convertc                 K      dS )u'  在把原始带声调的拼音按拼音风格转换前会调用 ``pre_convert_style`` 方法。

        如果返回值不为 ``None`` 会使用返回的结果代替 ``orig_pinyin``
        来进行后面的风格转换。

        :param han: 要处理的汉字
        :param orig_pinyin: 汉字对应的原始带声调拼音
        :param style: 要转换的拼音风格
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: ``None`` 或代替 ``orig_pinyin`` 参与拼音风格转换的拼音字符串。

        Nr   )r   hanorig_pinyinr   r   r   r   r   r   pre_convert_styleF   s   z"DefaultConverter.pre_convert_stylec           
      K   s\   | j ||||d}|dur|}n|}| j|||||d}| j|||||d}	|	du r,|}	|	S )u  按 ``style`` 的值对 ``orig_pinyin`` 进行处理，返回处理后的拼音

        转换风格前会调用 ``pre_convert_style`` 方法，
        转换后会调用 ``post_convert_style`` 方法。

        :param han: 要处理的单个汉字
        :param orig_pinyin: 汉字对应的原始带声调拼音
        :param style: 拼音风格
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: 按拼音风格转换处理后的拼音

        )r   r   N)r   r   default)r-   _convert_stylepost_convert_style)
r   r+   r,   r   r   r   pre_datapinyinconverted_pinyinr(   r   r   r   convert_styleX   s   

zDefaultConverter.convert_stylec                 K   r*   )u  在把原始带声调的拼音按拼音风格转换前会调用 ``pre_convert_style`` 方法。

        如果返回值不为 ``None`` 会使用返回的结果代替 ``converted_pinyin``
        作为拼音风格转换后的最终拼音结果。

        :param han: 要处理的汉字
        :param orig_pinyin: 汉字对应的原始带声调拼音
        :param converted_pinyin: 按拼音风格转换处理后的拼音
        :param style: 要转换的拼音风格
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: ``None`` 或代替 ``converted_pinyin`` 作为拼音风格转换后的拼音结果。

        Nr   )r   r+   r,   r3   r   r   r   r   r   r   r0   y      z#DefaultConverter.post_convert_stylec                 K   r*   )um  处理没有拼音的字符串前会调用 ``pre_handle_nopinyin`` 方法。

        如果返回值不为 ``None`` 会使用返回的结果作为处理没有拼音字符串的结果，
        不再使用内置方法进行处理。

        :param chars: 待处理的没有拼音的字符串
        :param errors: 如何处理
        :param heteronym: 是否需要处理多音字
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: ``None`` 或代替 ``chars`` 参与拼音风格转换的拼音字符串
                  或拼音结果 list。

        Nr   )r   charsr   r   r   r   r   r   r   r   pre_handle_nopinyin   s   z$DefaultConverter.pre_handle_nopinyinc           
      K   s   | j |||||d}|dur|}n|}| j|||||d}| j||||||d}	|	dur.|	}|s2g S t|trPt|d trI|rB|S dd |D S dd |D S |ggS )u  处理没有拼音的字符串。

        处理前会调用 ``pre_handle_nopinyin`` 方法，
        处理后会调用 ``post_handle_nopinyin`` 方法。

        :param chars: 待处理的没有拼音的字符串
        :param style: 拼音风格
        :param errors: 如何处理
        :param heteronym: 是否需要处理多音字
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :return: 处理后的拼音结果，如果为 ``None`` 或空 list 表示忽略这个字符串.
        :rtype: list
        )r   r   r   N)r   r   r   r2   r   c                 S   s   g | ]}|d  gqS )r   r   .0xr   r   r   
<listcomp>   s    z4DefaultConverter.handle_nopinyin.<locals>.<listcomp>c                 S   s   g | ]}|gqS r   r   )r9   ir   r   r   r;      s    )r7   _convert_nopinyin_charspost_handle_nopinyin
isinstancelist)
r   r6   r   r   r   r   r   r1   r)   r(   r   r   r   r%      s2   


z DefaultConverter.handle_nopinyinc                 K   r*   )uH  处理完没有拼音的字符串后会调用 ``post_handle_nopinyin`` 方法。

        如果返回值不为 ``None`` 会使用返回的结果作为处理没有拼音的字符串的结果。

        :param chars: 待处理的没有拼音的字符串
        :param errors: 如何处理
        :param heteronym: 是否需要处理多音字
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :param pinyin: 处理后的拼音信息，值为空 list 或包含拼音信息的 list
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: ``None`` 或代替 ``pinyin`` 做为处理结果。

        Nr   )r   r6   r   r   r   r   r2   r   r   r   r   r>      r5   z%DefaultConverter.post_handle_nopinyinc                 K   r*   )u'  找到汉字对应的拼音后，会调用 ``post_pinyin`` 方法。

        如果返回值不为 ``None`` 会使用返回的结果作为 han 的拼音数据。

        :param han: 单个汉字或者词语
        :param heteronym: 是否需要处理多音字
        :param pinyin: 单个汉字的拼音数据或词语的拼音数据 list
        :type pinyin: list
        :param kwargs: 其他关键字参数，暂时无用，用于以后扩展新的参数。
        :return: ``None`` 或代替 ``pinyin`` 作为 han 的拼音 list。

        Nr   )r   r+   r   r2   r   r   r   r   r#      s   zDefaultConverter.post_pinyinc           	      C   sF   g }|t v rtt | }|S |D ]}| |||||}|| q|S )u  词语拼音转换.

        :param phrase: 词语
        :param errors: 指定如何处理没有拼音的字符
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :return: 拼音列表
        :rtype: list
        )r   r   _single_pinyinr&   )	r   phraser   r   r   r   pinyin_listr+   r)   r   r   r   r"      s   zDefaultConverter._phrase_pinyinc                    s`   t |D ])\}}	||  |r fdd|	D ||< q|	d }
j |
dg||< q|S )u*   转换多个汉字的拼音结果的风格c                    s   g | ]}j  |d qS )r,   r   r   r4   r8   r+   r   r   r   r   r   r;     s    z3DefaultConverter.convert_styles.<locals>.<listcomp>r   rD   )	enumerater4   )r   rC   rB   r   r   r   r   r   idxitemr,   r   rF   r   r$     s   
zDefaultConverter.convert_stylesc                 C   s8   t |}|tvr| j|||||dS t| d}|gS )u  单字拼音转换.

        :param han: 单个汉字
        :param errors: 指定如何处理没有拼音的字符，详情请参考
                       :py:func:`~pypinyin.pinyin`
        :param strict: 只获取声母或只获取韵母相关拼音风格的返回结果
                       是否严格遵照《汉语拼音方案》来处理声母和韵母，
                       详见 :ref:`strict`
        :return: 返回拼音列表，多音字会有多个拼音项
        :rtype: list
        r    ,)ordr   r%   split)r   r+   r   r   r   r   numr'   r   r   r   rA     s   zDefaultConverter._single_pinyinc                 K   s(   |si }||d< t |||fd|i|S )Nr+   r.   rE   )r   r+   r2   r   r   r.   r   r   r   r   r/   1  s   zDefaultConverter._convert_stylec                 C   sd   t |r||S |dkr|S |dkrdS |dkr0t|dkr(ddd |D S td	t| S dS )
u(   转换没有拼音的字符。

        r.   ignoreNreplace    c                 s   s     | ]}t d t| V  qdS )%xN)r   rK   r8   r   r   r   	<genexpr>F  s    z;DefaultConverter._convert_nopinyin_chars.<locals>.<genexpr>rR   )r   lenjoinr   rK   )r   r6   r   r   r   r   r   r   r   r=   9  s   z(DefaultConverter._convert_nopinyin_charsN)r   r   r   r   r   r-   r4   r0   r7   r%   r>   r#   r"   r$   rA   r/   r=   r   r   r   r   r      s    %!1r   c                   @      e Zd ZdS )_v2UConverterNr   r   r   r   r   r   r   rW   K      rW   c                   @   rV   )_neutralToneWith5ConverterNrX   r   r   r   r   rZ   O  rY   rZ   c                   @   rV   )_toneSandhiConverterNrX   r   r   r   r   r[   S  rY   r[   c                       s:   e Zd Z		d fdd	Z fddZ fddZ  ZS )	UltimateConverterFc                    s,   t t| jdi | || _|| _|| _d S )Nr   )superr\   r   _v_to_u_neutral_tone_with_five_tone_sandhi)r   v_to_uneutral_tone_with_fivetone_sandhir   	__class__r   r   r   X  s   
zUltimateConverter.__init__c                    s   t t| j|||||fi |}|d ur|}| jr-t j|||||fi |}|d ur-|}| jrDt j|||||fi |}|d urD|}|S r   )r]   r\   r0   r^   rW   r_   rZ   )r   r+   r,   r3   r   r   r   r(   rd   r   r   r0   _  s0   



z$UltimateConverter.post_convert_stylec                    sV   t t| j|||fi |}|d ur|}| jr)t j|||fi |}|d ur)|}|S r   )r]   r\   r#   r`   r[   )r   r+   r   r2   r   r(   rd   r   r   r#   t  s    
zUltimateConverter.post_pinyin)FFF)r   r   r   r   r0   r#   __classcell__r   r   rd   r   r\   W  s    r\   N)
__future__r   copyr   pypinyin.compatr   r   pypinyin.constantsr   r   r   pypinyin.contrib.uvr	   pypinyin.contrib.neutral_toner
   pypinyin.contrib.tone_sandhir   pypinyin.utilsr   pypinyin.styler   r   r4   objectr   r   rW   rZ   r[   r\   _mixConverterr   r   r   r   <module>   s(     0,