o
    <)T                     @   s   d dl Z d dl mZ d dlZzd dlZW n ey!   d dlZY nw ddlmZ ddlm	Z	m
Z
 ddlmZ G dd deZeeed	Zdad
d Zdd Zdd ZdS )    N)path   )Detector)	ErrorCodeLangDetectException)LangProfilec                   @   s^   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dddZ
dd Zdd Zdd ZdS )DetectorFactorya  
    Language Detector Factory Class.

    This class manages an initialization and constructions of Detector.

    Before using language detection library,
    load profiles with DetectorFactory.load_profile(str)
    and set initialization parameters.

    When the language detection,
    construct Detector instance via DetectorFactory.create().
    See also Detector's sample code.
    Nc                 C   s   i | _ g | _d S N)word_lang_prob_maplanglistself r   b/var/www/html/development/chatbot/venv/lib/python3.10/site-packages/langdetect/detector_factory.py__init__      
zDetectorFactory.__init__c           	   	   C   s  t |}|sttjd| t|d}}|D ]m}|dr qt||}t	|s,qd }zOz-t
jd dk r=t|d}nt|ddd}t|}tdi |}| ||| |d7 }W n tyk   ttjd	|    ttjd
| W |r}|  q|r|  w w d S )NzNot found profile: r   .   rzutf-8)encodingr   zCannot open "%s"zProfile format error in "%s"r   )oslistdirr   r   NeedLoadProfileErrorlen
startswithr   joinisfilesysversion_infoopenjsonloadr   add_profileIOErrorFileLoadErrorFormatErrorclose)	r   profile_directory
list_fileslangsizeindexfilenamef	json_dataprofiler   r   r   load_profile#   s>   




zDetectorFactory.load_profilec                 C   st   t |d}}|dk rttjd|D ]$}zt|}tdi |}| ||| |d7 }W q   ttjdd S )Nr      zNeed more than 2 profiles.r   zProfile format error.r   )	r   r   r   r   r    loadsr   r"   r%   )r   json_profilesr)   r*   json_profiler-   r.   r   r   r   load_json_profileB   s   
z!DetectorFactory.load_json_profilec                 C   s   |j }|| jv rttjd| j| |jD ]5}|| jvr&dg| | j|< t|}d|  kr4dkrLn qd|j	| |j
|d   }|| j| |< qd S )Nz$Duplicate the same language profile.g        r   r   g      ?)namer   r   r   DuplicateLangErrorappendfreqr
   r   getn_words)r   r.   r*   r)   langwordlengthprobr   r   r   r"   P   s   


zDetectorFactory.add_profilec                 C   s   g | _ i | _d S r	   )r   r
   r   r   r   r   clear^   r   zDetectorFactory.clearc                 C   s   |   }|dur|| |S )z5Construct Detector instance with smoothing parameter.N)_create_detector	set_alpha)r   alphadetectorr   r   r   createb   s   
zDetectorFactory.createc                 C   s   | j s	ttjdt| S )NzNeed to load profiles.)r   r   r   r   r   r   r   r   r   r@   i   s   z DetectorFactory._create_detectorc                 C   s
   || _ d S r	   )seed)r   rE   r   r   r   set_seedn      
zDetectorFactory.set_seedc                 C   s
   t | jS r	   )listr   r   r   r   r   get_lang_listq   rG   zDetectorFactory.get_lang_listr	   )__name__
__module____qualname____doc__rE   r   r/   r4   r"   r?   rD   r@   rF   rI   r   r   r   r   r      s    
r   profilesc                   C   s    t d u rt a t t d S d S r	   )_factoryr   r/   PROFILES_DIRECTORYr   r   r   r   init_factoryx   s   rQ   c                 C       t   t }||  | S r	   )rQ   rO   rD   r7   detecttextrC   r   r   r   rS   ~      
rS   c                 C   rR   r	   )rQ   rO   rD   r7   get_probabilitiesrT   r   r   r   detect_langs   rV   rX   )r   r   r   
simplejsonr    ImportErrorrC   r   lang_detect_exceptionr   r   utils.lang_profiler   objectr   r   dirname__file__rP   rO   rQ   rS   rX   r   r   r   r   <module>   s"    f