
    Ng                         d dl Z d dl mZ d dlZ	 d dlZn# e$ r d dlZY nw xY wddlmZ ddlm	Z	m
Z
 ddlmZ  G d de          Z ej         ej        e          d	          Zdad
 Zd Zd ZdS )    N)path   )Detector)	ErrorCodeLangDetectException)LangProfilec                   N    e Zd ZdZdZd Zd Zd Zd Zd Z	ddZ
d	 Zd
 Z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                 "    i | _         g | _        d S N)word_lang_prob_maplanglistselfs    W/var/www/html/ai-engine/env/lib/python3.11/site-packages/langdetect/detector_factory.py__init__zDetectorFactory.__init__   s    "$    c                    t          j        |          }|st          t          j        d|z             t          |          d}}|D ]7}|                    d          rt          j        ||          }t          j	        |          sCd }	 t          j        d         dk     rt          |d          }nt          |dd          }t          j        |          }t          di |}|                     |||           |dz  }nH# t"          $ r t          t          j        d	|z             t          t          j        d
|z            xY w	 |r|                                 # |r|                                 w w xY wd S )NzNot found profile: r   .   rzutf-8)encodingr   zCannot open "%s"zProfile format error in "%s" )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profiles	            r   load_profilezDetectorFactory.load_profile#   s   Z 122
 	q%i&DF[^oFopppj//1%" 	 	H""3'' y!2H==H;x(( A#A&**Xs++AAXsW===A IaLL	%22	22  %:::
 b b b))*ACUX`C`aaal))*?A_bjAjkkk	   GGIII  GGIIII-	 	s    A5DE$AEE$$E=c                 *   t          |          d}}|dk     rt          t          j        d          |D ]^}	 t	          j        |          }t          di |}|                     |||           |dz  }@#  t          t          j        d          xY w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_profiler1   r2   s          r   load_json_profilez!DetectorFactory.load_json_profileB   s    m,,a%a<<%i&DFbccc) 	Z 	ZLZ J|44	%22	22  %:::
Z))*?AXYYY	Z 	Zs   <A44Bc                    |j         }|| j        v rt          t          j        d          | j                            |           |j        D ]v}|| j        vrdg|z  | j        |<   t          |          }d|cxk    rdk    rAn 8d|j        	                    |          z  |j
        |dz
           z  }|| j        |         |<   wd S )Nz$Duplicate the same language profile.g        r   r   g      ?)namer   r   r   DuplicateLangErrorappendfreqr   r   getn_words)r   r2   r.   r-   langwordlengthprobs           r   r&   zDetectorFactory.add_profileP   s    |4=  %i&BDjkkkT"""L 	< 	<D4222140@'-YYFFaW\--d333gofqj6QQ7;'-e4	< 	<r   c                 "    g | _         i | _        d S r   )r   r   r   s    r   clearzDetectorFactory.clear^   s    "$r   c                 \    |                                  }||                    |           |S )z5Construct Detector instance with smoothing parameter.)_create_detector	set_alpha)r   alphadetectors      r   createzDetectorFactory.createb   s2    ((**u%%%r   c                 b    | j         st          t          j        d          t	          |           S )NzNeed to load profiles.)r   r   r   r   r   r   s    r   rH   z DetectorFactory._create_detectori   s-    } 	`%i&DF^___~~r   c                     || _         d S r   )seed)r   rO   s     r   set_seedzDetectorFactory.set_seedn   s    			r   c                 *    t          | j                  S r   )listr   r   s    r   get_lang_listzDetectorFactory.get_lang_listq   s    DM"""r   r   )__name__
__module____qualname____doc__rO   r   r3   r9   r&   rF   rL   rH   rP   rS   r   r   r   r
   r
      s          D    >Z Z Z< < <% % %     
  # # # # #r   r
   profilesc                  r    t           /t                      a t                               t                     d S d S r   )_factoryr
   r3   PROFILES_DIRECTORYr   r   r   init_factoryr\   x   s6    "$$011111 r   c                     t                       t                                          }|                    |            |                                S r   )r\   rZ   rL   r=   detecttextrK   s     r   r^   r^   ~   s<    NNN  HOOD??r   c                     t                       t                                          }|                    |            |                                S r   )r\   rZ   rL   r=   get_probabilitiesr_   s     r   detect_langsrc      s>    NNN  HOOD%%'''r   )r   r   r!   
simplejsonr$   ImportErrorrK   r   lang_detect_exceptionr   r   utils.lang_profiler   objectr
   r   dirname__file__r[   rZ   r\   r^   rc   r   r   r   <module>rk      s4   				       



   KKKKK       A A A A A A A A + + + + + +c# c# c# c# c#f c# c# c#L TY|t|H55zBB 2 2 2  ( ( ( ( (s    	!!