B `$@sTddlmZddlmZddlmZmZmZmZm Z ddl Z ddl m Z ddl mZmZmZddlmZddlmZdd lmZeeed d d Zeeed ddZeeeed ddZeeed ddZeeeedddZeeeedddZeeedddZee e dddZ ed d!d&eeeee d#d$d%Z!dS)')IncrementalDecoder) lru_cache)ListSetOptionalTupleDictN)CoherenceMatches) unicode_rangeis_unicode_range_secondaryis_multi_byte_encoding) is_suspiciously_successive_range) FREQUENCIES)Counter) iana_namereturncCst|rtdtd|j}|dd}t}xNtddD]@}|t |g}|r>t |}|dkrhq>t |dkr>| |q>Wt t|S) zF Return associated unicode ranges in a single byte code page. z.Function not supported on multi-byte code pagez encodings.{}ignore)errors0NF)r IOError importlib import_moduleformatrsetrangedecodebytesr r addsortedlist)rdecoderpZ seen_rangesichunkcharacter_ranger&p/private/var/folders/7j/8686xlfs15q3tgljmghtvg0r0000gn/T/pip-target-isidps9b/lib/python/charset_normalizer/cd.pyencoding_unicode_range s   r() primary_rangercCsDg}x:tD].\}}x$|D]}t||kr||PqWqW|S)z> Return inferred languages used with a unicode range. )ritemsr append)r) languageslanguage characters characterr&r&r'unicode_range_languages*s    r0cCs>t|}d}x|D]}d|kr|}PqW|dkr6dgSt|S)z Single-byte encoding language association. Some code page are heavily linked to particular language(s). This function does the correspondence. NZLatinz Latin Based)r(r0)rZunicode_rangesr)Zspecified_ranger&r&r'encoding_languages9s r1cCsb|ds&|ds&|ds&|dkr,dgS|ds>|dkrFdd gS|d sX|d kr^d gSgS) z Multi-byte encoding language association. Some code page are heavily linked to particular language(s). This function does the correspondence. Zshift_ iso2022_jpZeuc_j>cp932Japanesegb> big5hkscsbig5cp950ChinesezClassical Chinese iso2022_kr>cp949euc_krjohabKorean) startswith)rr&r&r'mb_encoding_languagesMs&r@)r.rcCs^g}xTtD]H\}}d}t|}x|D]}||kr(|d7}q(W||dkr||qW|S)zE Return associated languages associated to given characters. rg?)rr*lenr+)r.r,r-Zlanguage_charactersZcharacter_match_countcharacter_countr/r&r&r'alphabet_languages\s   rD)r-ordered_charactersrcs6|tkrtd|d}x |D]}|t|kr6q"t|dt||}t|t||d}|d|||||dfdd|Dd}fdd|Dd}t|dkr|dkr|d 7}q"t|dkr|dkr|d 7}q"|t|d ks|t|d kr"|d 7}q"q"W|t|S) aN Determine if a ordered characters list (by occurrence from most appearance to rarest) match a particular language. The result is a ratio between 0. (absolutely no correspondence) and 1. (near perfect fit). Beware that is function is not strict on the match in order to ease the detection. (Meaning close match is 1.) z{} not availablerNcsg|] }|kqSr&r&).0e)characters_beforer&r' sz1characters_popularity_compare..Tcsg|] }|kqSr&r&)rFrG)characters_afterr&r'rIsrAg?)r ValueErrorrindexcountrB)r-rEZcharacter_approved_countr/Zcharacters_before_sourceZcharacters_after_sourceZbefore_match_countZafter_match_countr&)rJrHr'characters_popularity_compareps, "rO)decoded_sequencercCsi}x~|D]v}|dkrq t|}d}x |D]}t||dkr.|}Pq.W|dkrV|}||krl|||<q |||7<q Wt|S)a Given a decoded text sequence, return a list of str. Unicode range / alphabet separation. Ex. a text containing English/Latin with a bit a Hebrew will return two items in the resulting list; One containing the latin letters and the other hebrew. FN)isalphar r lowerr values)rPZlayersr/r%Zlayer_target_rangeZdiscovered_ranger&r&r'alpha_unicode_splits"    rT)resultsrc Csi}g}xD|D]<}x6|D].}|\}}||kr8|g||<q|||qWqWx4|D],}||tt||t||dfqTWt|ddddS)z This function merge results previously given by the function coherence_ratio. The return type is the same as coherence_ratio. rKcSs|dS)NrAr&)xr&r&r'z(merge_coherence_ratios..T)keyreverse)r+roundsumrBr)rUZper_language_ratiosmergeresultZ sub_resultr-ratior&r&r'merge_coherence_ratioss&     r`i)maxsize皙?)rP threshold lg_inclusionrc Csg}d}|dk r|d}|dk r4d|kr4|dxt|D]}t|}|}tdd|D}|dkrnq>dd|D} xX|pt| D]H} t| | } | |krqn| d kr|d 7}|| t | d f|d krPqWq>Wt |d dddS)z Detect ANY language that can be identified in given sequence. The sequence will be analysed by layers. A layer = Character extraction by alphabets/ranges. rN,z Latin BasedcSsg|] \}}|qSr&r&)rFcor&r&r'rIsz#coherence_ratio.. cSsg|] \}}|qSr&r&)rFrfrgr&r&r'rIsg?rArKcSs|dS)NrAr&)rVr&r&r'rWrXz!coherence_ratio..T)rYrZ) splitremoverTr most_commonr\rDrOr+r[r) rPrcrdrUZsufficient_match_countZlayerZsequence_frequenciesrlrCZpopular_character_orderedr-r_r&r&r'coherence_ratios0    rm)rbN)"codecsr functoolsrtypingrrrrrrZcharset_normalizer.modelsr Zcharset_normalizer.utilsr r r Zcharset_normalizer.mdr Zcharset_normalizer.assetsr collectionsrstrr(r0r1r@rDfloatrOrTr`rmr&r&r&r's&      '!"