o
    Ti(L                  
   @   sD  d Z dZddlZddlZddlZddlZzddlmZ dd ZW n e	y-   dd ZY nw dd	d
ddddddd	Z
dZeZdadadadadai aedZedZedZedZedZefddZdd Zdd Zd0dd Zd!d" Zd1d#d$Zd1d%d&Zd1d'd(Z d1d)d*Z!d1d+d,Z"d-d. Z#e$d/kre#  dS dS )2u  
This module implements a simple conversion and localization between simplified and traditional Chinese using tables from MediaWiki.
It doesn't contains a segmentation function and uses maximal forward matching, so it's simple.
For a complete and accurate solution, see OpenCC.
For Chinese segmentation, see Jieba.

    >>> print(convert('我幹什麼不干你事。', 'zh-cn'))
    我干什么不干你事。
    >>> print(convert('人体内存在很多微生物', 'zh-tw'))
    人體內存在很多微生物

Support MediaWiki's convertion format:

    >>> print(convert_for_mw('在现代，机械计算-{}-机的应用已经完全被电子计算-{}-机所取代', 'zh-hk'))
    在現代，機械計算機的應用已經完全被電子計算機所取代
    >>> print(convert_for_mw('-{zh-hant:資訊工程;zh-hans:计算机工程学;}-是电子工程的一个分支，主要研究计算机软硬件和二者间的彼此联系。', 'zh-tw'))
    資訊工程是電子工程的一個分支，主要研究計算機軟硬體和二者間的彼此聯繫。
    >>> print(convert_for_mw('張國榮曾在英國-{zh:利兹;zh-hans:利兹;zh-hk:列斯;zh-tw:里茲}-大学學習。', 'zh-sg'))
    张国荣曾在英国利兹大学学习。

z1.4.3    N)resource_streamc                  G   s   t ttjj|  S N)r   __name__ospathjoinres r
   A/home/ubuntu/.local/lib/python3.10/site-packages/zhconv/zhconv.py<lambda>"   s    r   c                  G   s0   t tjtjjt tjtg| R  dS )Nrb)openr   r   normpathr   getcwddirname__file__r   r
   r
   r   r   $   s
     )zh-cnzh-hanszh-sgzh)zh-hkzh-hantzh-twr   )r   r   r   r   )r   r   r   r   )zh-myr   r   r   r   )zh-mor   r   r   r   )r   r   r   r   )r   r   r   r   )r   )	r   r   r   r   r   r   r   r   r   zzhcdict.jsonz	(-\{|\}-)z\s*\|\s*z\s*;\s*z\s*=>\s*z\s*:\s*c                 C   s   t rdS | tkrtt|  da nt| d}t| da W d   n1 s/w   Y  tt d t d< tt d t d< dS )z8
    Load the dictionary from a specific JSON file.
    Nutf-8r   SIMPONLYTRADONLY)	zhcdicts_DEFAULT_DICTjsonloadsget_module_resreaddecoder   	frozenset)filenamefr
   r
   r   loaddictD   s   r)   c                 C   s(  t du rtt | dkr!trt}nwt d  att d  t}ng| dkr:tr*t}n^t d  att d  t}nN| dksB| d	krWtrGt}nAt d  att d
  t}n1| dks_| dkrttrdt}n$t d  att d  t}n| dkr}t d }n| dkrt d }ni }| t	vrt
|t	| < |S )zj
    Generate or get convertion dict cache for certain locale.
    Dictionaries are loaded on demand.
    Nr   zh2Hanszh2CNr   zh2Hantzh2TWr   r   zh2HKr   r   zh2SGr   r   )r   r)   
DICTIONARY	dict_zhcncopyupdate	dict_zhtw	dict_zhhk	dict_zhsgpfsdictgetpfset)localegotr
   r
   r   getdictS   sD   

r;   c                 C   s>   g }| D ]}t t|D ]}||d |d   qqt|S )N   )rangelenappendr&   )convdictpfsetwordchr
   r
   r   r8      s   r8   Fc                 C   s   t du rtt d\}}|r6| D ]}|t d v r|d7 }q|t d v r'|d7 }q||kr.dS ||k r4dS dS | D ]}|t d v rC dS |t d v rL dS q8dS )a  
    Detect text is whether Simplified Chinese or Traditional Chinese.
    Returns True for Simplified; False for Traditional; None for unknown.
    If full=False, it returns once first simplified- or traditional-only
    character is encountered, so it's for quick and rough identification;
    else, it compares the count and returns the most likely one.
    Use `is` (True/False/None) to check the result.

    `s` must be unicode (Python 2) or str (Python 3), or you'll get None.
    N)r   r   r   r<   r   TF)r   r)   r0   )sfullsimptradrC   r
   r
   r   issimp   s,   
rH   c                 C   s8   t |  D ]}||v r||   S qtt| d | S )Nr   )Localesconverttuplevalues)r9   mappinglr
   r
   r   fallback   s
   rO   c                 C   s   |r|  ni }| D ]=}d|v r||v r|| ||d < q
|dd dkr7||v r6| D ]}|| ||< q-q
t||}| D ]}|||< q@q
|S )uI  
    Convert a list of conversion dict to a dict for a certain locale.

    >>> sorted(convtable2dict([{'zh-hk': '列斯', 'zh-hans': '利兹', 'zh': '利兹', 'zh-tw': '里茲'}, {':uni': '巨集', 'zh-cn': '宏'}], 'zh-cn').items())
    [('列斯', '利兹'), ('利兹', '利兹'), ('巨集', '宏'), ('里茲', '利兹')]
    :uniNzzh-han)r2   rL   rO   )	convtabler9   r3   rdictrrB   vr
   r
   r   convtable2dict   s    

rV   c                 C   s  t |}t| }|r3| }|| t }|D ]}tt|D ]}||d|d   q q||B }g }t| }d}	|	|k r|	}
| |	 }d}d}|
|k rm||v rm||v rY|}|
}|
d7 }
| |	|
d  }|
|k rm||v sQ|du rz| |	 }|	d7 }	n|d }	|| |	|k s?|S )zt
    Tokenize `s` according to corresponding locale dictionary.
    Don't use this for serious text processing.
    Nr<   r   )	r;   r7   r2   r3   setr=   r>   addr?   rD   r9   r3   zhdictrA   newsetrB   rC   Nposifragmaxwordmaxposr
   r
   r   tokenize   sB   


rb   c                 C   sF  |dks|t vr
| S t|}t| }t }|r3t }|D ]}tt|D ]}||d|d   q$qg }t| }d}	|	|k r|	}
| |	 }d}d}|
|k r||v sU||v r|rb||v rb|| }|
}n
||v rl|| }|
}|
d7 }
| |	|
d  }|
|k r||v sU||v sU|du r| |	 }|	d7 }	n|d }	|| |	|k s?d|S )u  
    Main convert function.

    :param s: must be `unicode` (Python 2) or `str` (Python 3).
    :param locale: should be one of ``('zh-hans', 'zh-hant', 'zh-cn', 'zh-sg'
                               'zh-tw', 'zh-hk', 'zh-my', 'zh-mo')``.
    :param update: a dict which updates the conversion table, eg.
        ``{'from1': 'to1', 'from2': 'to2'}``

    >>> print(convert('我幹什麼不干你事。', 'zh-cn'))
    我干什么不干你事。
    >>> print(convert('我幹什麼不干你事。', 'zh-cn', {'不干': '不幹'}))
    我干什么不幹你事。
    >>> print(convert('人体内存在很多微生物', 'zh-tw'))
    人體內存在很多微生物
    r   Nr<   r    )	rI   r;   r7   rW   r=   r>   rX   r?   r   rY   r
   r
   r   rJ      sH   	


rJ   c                    s  g }g }|r
|  ni }d}d}t| D ]}|dkr%|d7 }||7 }q|dkr|s2|| q||7 }|d8 }|r=qg }	t|dd d}
t|
dkr[d	 t|
d }nt|
d d t|
d }i }|D ]O}t|}t|dkrt	|d }n|r|	| d
|d i}n|d |d
< t	|d }t|dkrt
|d d||d< qpt
|d |d |||d < qp|	|  s|t||	d  nt fdddD rG D ]_}|dv r"|	D ]}||vr|| q|dkr!d
|v r||v r|||  q|t|d
 | q|t||	d  q|dkr?|	D ]}z|| W q) ty>   Y q)w qt|||}nft tt| @ }|rX|  ni }|D ]E}d
|v rr||v rp|| ||d
 < q\d	}t| D ]}||v r||v r|| } nqx| D ]}|r|nt||||< qq\|t|
d || d}q|r||7 }q|t||| q|r|t
|d|  || d|S )u  
    Recognizes MediaWiki's human conversion format.
    Use locale='zh' for no conversion.

    Reference: (all tests passed)
    https://zh.wikipedia.org/wiki/Help:高级字词转换语法
    https://www.mediawiki.org/wiki/Writing_systems/Syntax

    >>> print(convert_for_mw('在现代，机械计算-{}-机的应用已经完全被电子计算-{}-机所取代', 'zh-hk'))
    在現代，機械計算機的應用已經完全被電子計算機所取代
    >>> print(convert_for_mw('-{zh-hant:資訊工程;zh-hans:计算机工程学;}-是电子工程的一个分支，主要研究计算机软硬件和二者间的彼此联系。', 'zh-tw'))
    資訊工程是電子工程的一個分支，主要研究計算機軟硬體和二者間的彼此聯繫。
    >>> print(convert_for_mw('張國榮曾在英國-{zh:利兹;zh-hans:利兹;zh-hk:列斯;zh-tw:里茲}-大学學習。', 'zh-hant'))
    張國榮曾在英國里茲大學學習。
    >>> print(convert_for_mw('張國榮曾在英國-{zh:利兹;zh-hans:利兹;zh-hk:列斯;zh-tw:里茲}-大学學習。', 'zh-sg'))
    张国荣曾在英国利兹大学学习。
    >>> convert_for_mw('-{zh-hant:;\nzh-cn:}-', 'zh-tw') == ''
    True
    >>> print(convert_for_mw('毫米(毫公分)，符號mm，是長度單位和降雨量單位，-{zh-hans:台湾作-{公釐}-或-{公厘}-;zh-hant:港澳和大陸稱為-{毫米}-（台灣亦有使用，但較常使用名稱為毫公分）;zh-mo:台灣作-{公釐}-或-{公厘}-;zh-hk:台灣作-{公釐}-或-{公厘}-;}-。', 'zh-tw'))
    毫米(毫公分)，符號mm，是長度單位和降雨量單位，港澳和大陸稱為毫米（台灣亦有使用，但較常使用名稱為毫公分）。
    >>> print(convert_for_mw('毫米(毫公分)，符號mm，是長度單位和降雨量單位，-{zh-hans:台湾作-{公釐}-或-{公厘}-;zh-hant:港澳和大陸稱為-{毫米}-（台灣亦有使用，但較常使用名稱為毫公分）;zh-mo:台灣作-{公釐}-或-{公厘}-;zh-hk:台灣作-{公釐}-或-{公厘}-;}-。', 'zh-cn'))
    毫米(毫公分)，符号mm，是长度单位和降雨量单位，台湾作公釐或公厘。
    >>> print(convert_for_mw('毫米(毫公分)，符號mm，是長度單位和降雨量單位，-{zh-hans:台湾作-{公釐}-或-{公厘}-;zh-hant:港澳和大陸稱為-{毫米}-（台灣亦有使用，但較常使用名稱為毫公分）;zh-mo:台灣作-{公釐}-或-{公厘}-;zh-hk:台灣作-{公釐}-或-{公厘', 'zh-hk'))  # unbalanced test
    毫米(毫公分)，符號mm，是長度單位和降雨量單位，台灣作公釐或公厘
    >>> print(convert_for_mw('报头的“-{參攷消息}-”四字摘自鲁迅笔迹-{zh-hans:，“-{參}-”是“-{参}-”的繁体字，读音cān，与简体的“-{参}-”字相同；;zh-hant:，;}-“-{攷}-”是“考”的异体字，读音kǎo，与“考”字相同。', 'zh-tw'))
    報頭的「參攷消息」四字摘自魯迅筆跡，「攷」是「考」的異體字，讀音kǎo，與「考」字相同。
    >>> print(convert_for_mw('报头的“-{參攷消息}-”四字摘自鲁迅笔迹-{zh-hans:，“-{參}-”是“-{参}-”的繁体字，读音cān，与简体的“-{参}-”字相同；;zh-hant:，;}-“-{攷}-”是“考”的异体字，读音kǎo，与“考”字相同。', 'zh-cn'))
    报头的“參攷消息”四字摘自鲁迅笔迹，“參”是“参”的繁体字，读音cān，与简体的“参”字相同；“攷”是“考”的异体字，读音kǎo，与“考”字相同。
    >>> print(convert_for_mw('{{Col-break}}--&gt;', 'zh-hant'))
    {{Col-break}}--&gt;
    r   rc   z-{r<   z}-   z 	
;NrP   r   c                 3   s    | ]}| v V  qd S r   r
   ).0rC   flagr
   r   	<genexpr>q  s    z!convert_for_mw.<locals>.<genexpr>zATRD-HN)AHrj   -)r2   RE_langconvsplitr?   RE_splitflagstripr>   RE_splitmapRE_splituniRE_splitpairconvert_for_mwrO   anyrJ   remove
ValueErrorrV   r&   rI   rL   r   )rD   r9   r3   rC   rulesruledictnestedblockr_   newrulesdelimrM   rulemunipairr(   rT   fblimitlimitedruledictrU   rN   rB   r
   rg   r   rt   $  s    
















rt   c                 C   s   d}t || |S )Nuk  英國-{zh:利兹;zh-hans:利兹;zh-hk:列斯;zh-tw:里茲}-大学
-{zh-hans:计算机; zh-hant:電腦;}-
-{H|巨集=>zh-cn:宏;}-
测试：巨集、宏
-{简体字繁體字}-
北-{}-韓、北朝-{}-鲜
-{H|zh-cn:博客; zh-hk:網誌; zh-tw:部落格;}-
测试：博客、網誌、部落格
-{A|zh-cn:博客; zh-hk:網誌; zh-tw:部落格;}-
测试：博客、網誌、部落格
-{H|zh-cn:博客; zh-hk:網誌; zh-tw:部落格;}-
测试1：博客、網誌、部落格
-{-|zh-cn:博客; zh-hk:網誌; zh-tw:部落格;}-
测试2：博客、網誌、部落格
-{T|zh-cn:汤姆·汉克斯; zh-hk:湯·漢斯; zh-tw:湯姆·漢克斯;}-
-{D|zh-cn:汤姆·汉克斯; zh-hk:湯·漢斯; zh-tw:湯姆·漢克斯;}-
-{H|zh-cn:博客; zh-hk:網誌; zh-tw:部落格;}-
测试1：-{zh;zh-hans;zh-hant|博客、網誌、部落格}-
测试2：-{zh;zh-cn;zh-hk|博客、網誌、部落格}-)rt   )r9   r3   rD   r
   r
   r   test_convert_mw  s   r   c                  C   s  t tjdkrtjd tv rtjd } t}n0t tjdkr3tjd dkr3tjd tv r3tjd } t}ntdkr9tnd}td|  t	d t
  tj }|r|d}tjd	 dk rat|d
}||| }tjd	 dk rut|d
 nt| tj }|sPdS dS )z(
    Simple stdin/stdout interface.
    rd   r<      z-w__main__zpython -mzhconvzLusage: %s [-w] {zh-cn|zh-tw|zh-hk|zh-sg|zh-hans|zh-hant|zh} < input > outputz
r   r   N)r>   sysargvrI   rJ   rt   r   r   printexitr)   stdinreadlinerstripversion_infounicodeencode)r9   convertfuncthisfilelnrN   r	   r
   r
   r   main  s*   
*






r   r   )Fr   )%__doc____version__r   r   rer!   pkg_resourcesr   r#   ImportErrorrI   r    r0   r   r1   r6   r4   r5   r7   compilerm   ro   rq   rr   rs   r)   r;   r8   rH   rO   rV   rb   rJ   rt   r   r   r   r
   r
   r
   r   <module>   sd   




.
"


%
9 

