o
    'h0                     @  s.  d dl mZ d dl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mZmZmZmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZ d8ddZd9ddZe d8ddZe d8ddZeedd:ddZ	d;d<d!d"Z d=d%d&Z!d>d(d)Z"d?d-d.Z#d@d/d0Z$ed1d	dAdBd6d7Z%dS )C    )annotationsN)IncrementalDecoder)Counter)	lru_cache   )FREQUENCIESKO_NAMESLANGUAGE_SUPPORTED_COUNTTOO_SMALL_SEQUENCEZH_NAMES) is_suspiciously_successive_range)CoherenceMatches)is_accentuatedis_latinis_multi_byte_encodingis_unicode_range_secondaryunicode_range	iana_namestrreturn	list[str]c                   s   t | rtdtd|  j}|dd}i d tddD ]/}|t|g}|rNt|}|du r4qt	|d	u rN|vrBd|< |  d
7  <  d
7  qt
 fddD S )zF
    Return associated unicode ranges in a single byte code page.
    z.Function not supported on multi-byte code pagez
encodings.ignore)errorsr   @      NFr   c                   s    g | ]}|   d kr|qS )g333333? ).0character_rangecharacter_countseen_rangesr   Q/var/www/html/olx_land/venv/lib/python3.10/site-packages/charset_normalizer/cd.py
<listcomp>8   s
    z*encoding_unicode_range.<locals>.<listcomp>)r   OSError	importlibimport_moduler   rangedecodebytesr   r   sorted)r   decoderpichunkr   r   r   r!   encoding_unicode_range   s.   
r.   primary_rangec                 C  s>   g }t  D ]\}}|D ]}t|| kr||  nqq|S )z>
    Return inferred languages used with a unicode range.
    )r   itemsr   append)r/   	languageslanguage
characters	characterr   r   r!   unicode_range_languages@   s   
r6   c                 C  s<   t | }d}|D ]
}d|vr|} nq|du rdgS t|S )z
    Single-byte encoding language association. Some code page are heavily linked to particular language(s).
    This function does the correspondence.
    NLatinLatin Based)r.   r6   )r   unicode_rangesr/   specified_ranger   r   r!   encoding_languagesO   s   r;   c                 C  s`   |  ds|  ds|  ds| dkrdgS |  ds| tv r"dgS |  ds+| tv r.d	gS g S )
z
    Multi-byte encoding language association. Some code page are heavily linked to particular language(s).
    This function does the correspondence.
    shift_
iso2022_jpeuc_jcp932JapanesegbChinese
iso2022_krKorean)
startswithr   r   )r   r   r   r!   mb_encoding_languagesc   s   rF   )maxsizer3   tuple[bool, bool]c                 C  sB   d}d}t |  D ]}|st|rd}|rt|du rd}q||fS )zg
    Determine main aspects from a supported language if it contains accents and if is pure Latin.
    FT)r   r   r   )r3   target_have_accentstarget_pure_latinr5   r   r   r!   get_target_featuresx   s   rK   Fr4   ignore_non_latinboolc                   s   g }t dd  D }t D ]6\}}t|\}}|r |du r q|du r'|r'qt|}t fdd|D }	|	| }
|
dkrE|||
f qt|dd d	d
}dd |D S )zE
    Return associated languages associated to given characters.
    c                 s  s    | ]}t |V  qd S N)r   )r   r5   r   r   r!   	<genexpr>       z%alphabet_languages.<locals>.<genexpr>Fc                   s   g | ]}| v r|qS r   r   )r   cr4   r   r!   r"      s    z&alphabet_languages.<locals>.<listcomp>g?c                 S     | d S Nr   r   xr   r   r!   <lambda>       z$alphabet_languages.<locals>.<lambda>Tkeyreversec                 S  s   g | ]}|d  qS )r   r   )r   compatible_languager   r   r!   r"          )anyr   r0   rK   lenr1   r)   )r4   rL   r2   source_have_accentsr3   language_charactersrI   rJ   r   character_match_countratior   rR   r!   alphabet_languages   s$   rd   ordered_charactersfloatc                 C  s  | t vrt|  dd}tt |  }t|}tt |  }|dk}t|td|D ]\}}||vr2q)t |  |}	|| }
t||
 }|du rPt||	 dkrPq)|du rct||	 |d k rc|d7 }q)t |  d|	 }t |  |	d	 }|d| }||d	 }tt|t|@ }tt|t|@ }t|dkr|dkr|d7 }q)t|dkr|dkr|d7 }q)|t| d
ks|t| d
kr|d7 }q)q)|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 availabler      F   T   r   Ng?)	r   
ValueErrorsetr_   zipr&   indexintabs)r3   re   character_approved_countFREQUENCIES_language_setordered_characters_count target_language_characters_countlarge_alphabetr5   character_rankcharacter_rank_in_languageexpected_projection_ratiocharacter_rank_projectioncharacters_before_sourcecharacters_after_sourcecharacters_beforecharacters_afterbefore_match_countafter_match_countr   r   r!   characters_popularity_compare   sf   

r   decoded_sequencec                 C  s   i }| D ]?}|  du rqt|}|du rqd}|D ]}t||du r'|} nq|du r.|}||vr9| ||< q||  | 7  < qt| 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   lowerlistvalues)r   layersr5   r   layer_target_rangediscovered_ranger   r   r!   alpha_unicode_split   s,   r   resultslist[CoherenceMatches]r   c                   sf   i  | D ]}|D ]}|\}}| vr|g |< q |  | qq fdd D }t|dd ddS )z
    This function merge results previously given by the function coherence_ratio.
    The return type is the same as coherence_ratio.
    c                   s.   g | ]}|t t | t |  d fqS )rh   )roundsumr_   )r   r3   per_language_ratiosr   r!   r"   1  s    z*merge_coherence_ratios.<locals>.<listcomp>c                 S  rS   rT   r   rU   r   r   r!   rW   <  rX   z(merge_coherence_ratios.<locals>.<lambda>TrY   )r1   r)   )r   result
sub_resultr3   rc   merger   r   r!   merge_coherence_ratios#  s   

r   c                   s   t   | D ]}|\}}|dd}| vrg  |<  | | qt fdd D r@g } D ]}||t | f q0|S | S )u   
    We shall NOT return "English—" in CoherenceMatches because it is an alternative
    of "English". This function only keeps the best match and remove the em-dash in it.
    u   — c                 3  s     | ]}t  | d kV  qdS )r   N)r_   )r   eindex_resultsr   r!   rO   O  s    z/filter_alt_coherence_matches.<locals>.<genexpr>)dictreplacer1   r^   max)r   r   r3   rc   
no_em_namefiltered_resultsr   r   r!   filter_alt_coherence_matches?  s   r   i   皙?	thresholdlg_inclusion
str | Nonec                 C  s   g }d}d}|dur| dng }d|v rd}|d t| D ]K}t|}| }	tdd |	D }
|
tkr8q d	d
 |	D }|pEt||D ]$}t||}||k rRqF|dkrZ|d7 }|	|t
|df |dkrj nqFq tt|dd ddS )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.
    Fr   N,r8   Tc                 s  s    | ]\}}|V  qd S rN   r   r   rQ   or   r   r!   rO   q  rP   z"coherence_ratio.<locals>.<genexpr>c                 S  s   g | ]\}}|qS r   r   r   r   r   r!   r"   v  r]   z#coherence_ratio.<locals>.<listcomp>g?r   rh   ri   c                 S  rS   rT   r   rU   r   r   r!   rW     rX   z!coherence_ratio.<locals>.<lambda>rY   )splitremover   r   most_commonr   r
   rd   r   r1   r   r)   r   )r   r   r   r   rL   sufficient_match_countlg_inclusion_listlayersequence_frequenciesr   r   popular_character_orderedr3   rc   r   r   r!   coherence_ratioZ  s@   	
r   )r   r   r   r   )r/   r   r   r   )r3   r   r   rH   )F)r4   r   rL   rM   r   r   )r3   r   re   r   r   rf   )r   r   r   r   )r   r   r   r   )r   r   r   r   )r   N)r   r   r   rf   r   r   r   r   )&
__future__r   r$   codecsr   collectionsr   	functoolsr   typingTypeCounterconstantr   r   r	   r
   r   mdr   modelsr   utilsr   r   r   r   r   r.   r6   r;   rF   rK   rd   r   r   r   r   r   r   r   r   r!   <module>   s6    
	
%
#
P
'
