
    OgL              
          S SK Jr  S SK Jr  S SK Jr  S SK Jr  S SKrS SKrS SK	r	S SK
Jr  \R                  r\R                  rSrSrS	r0 r " S
 S5      r " S S5      rS rS rS rS rS r0 SS_SS_SS_SS_SS_SS_SS _SS_S S!_S"S_S#S$_S%S&_S'S(_S)\	R2                  " 5       S$-
  _S*S_S+S,_S-S._S/S0S S0S1S$S2S0S3.ErS4 rS5 rS6 rS7 rS8 rS9 r g):    )absolute_import)division)print_function)unicode_literalsN)chainz</s><>c                   >    \ rS rSrS rS rS	S jrS	S jrS	S jrSr	g)
_Meter   c                     Xl         X l        g Nfm)selffasttext_modelmeters      V/Users/admin/workspace/ai/Jarvis/env/lib/python3.13/site-packages/fasttext/FastText.py__init___Meter.__init__   s        c                     U R                   R                  U5      nU R                  R                  U5      nU(       a  [	        U6 u  pEO/ SpT[
        R                  " USS9[
        R                  " USS94$ )z>Return scores and the gold of each sample for a specific label Fcopy)r   get_label_idr   scoreVsTruezipnparray)r   labellabel_id	pair_listy_scoresy_trues         r   score_vs_true_Meter.score_vs_true   sc    66&&u-FF&&x0	"IHf "Bfxxu-rxxU/KKKr   Nc                 0   U(       a7  U R                   R                  U5      nU R                  R                  U5      nOU R                  R	                  5       nU(       a  [        U6 u  pEO/ SpT[        R                  " USS9[        R                  " USS94$ )zReturn precision/recall curver   Fr   )r   r   r   precisionRecallCurveLabelprecisionRecallCurver   r    r!   )r   r"   r#   r$   	precisionrecalls         r   precision_recall_curve_Meter.precision_recall_curve+   sx    vv**51H88BI335I #YIv!#Rvxx	.e0LLLr   c                     U(       a8  U R                   R                  U5      nU R                  R                  X15      nU$ U R                  R	                  U5      nU$ )z#Return precision for a given recall)r   r   r   precisionAtRecallLabelprecisionAtRecall)r   r-   r"   r#   r,   s        r   precision_at_recall_Meter.precision_at_recall:   sQ    vv**51H55hGI  008Ir   c                     U(       a8  U R                   R                  U5      nU R                  R                  X15      nU$ U R                  R	                  U5      nU$ )z#Return recall for a given precision)r   r   r   recallAtPrecisionLabelrecallAtPrecision)r   r,   r"   r#   r-   s        r   recall_at_precision_Meter.recall_at_precisionD   sO    vv**51HVV228GF  VV--i8Fr   r   r   )
__name__
__module____qualname____firstlineno__r   r'   r.   r3   r8   __static_attributes__r   r   r   r   r      s    
LMr   r   c                   "   \ rS rSrSrS"S jrS#S jrS rS rS r	S	 r
S$S
 jrS$S jrS rS rS rS%S jrS rS&S jrS rS rS'S jrS'S jrS%S jrS rS(S jrS(S jrS)S jr          S*S jrS r\S 5       r\S 5       r S r!S  r"S!r#g)+	_FastTextO   ak  
This class defines the API to inspect models and should not be used to
create objects. It will be returned by functions such as load_model or
train.

In general this API assumes to be given only unicode for Python2 and the
Python3 equvalent called str for any string-like arguments. All unicode
strings are then encoded as UTF-8 and fed to the fastText C++ API.
Nc                     [         R                   " 5       U l        Ub  U R                  R                  U5        S U l        S U l        U R                  U5        g r   )fasttextr   	loadModel_words_labelsset_args)r   
model_pathargss      r   r   _FastText.__init__Z   sD    ""$!FFZ(dr   c           	      X    U(       a#  / SQnU H  n[        X[        X5      5        M     g g )N)lrdimwsepochminCountminCountLabelminnmaxnneg
wordNgramslossbucketthreadlrUpdateRatetr"   verbosepretrainedVectors)setattrgetattr)r   rI   	arg_namesarg_names       r   rG   _FastText.set_argsb   s,    I( &(?@ &+ r   c                 6    U R                   R                  5       $ r   )r   isQuantr   s    r   is_quantized_FastText.is_quantized{   s    vv~~r   c                 N    U R                   R                  5       nUR                  $ )z;Get the dimension (size) of a lookup vector (hidden layer).)r   getArgsrM   )r   as     r   get_dimension_FastText.get_dimension~   s    FFNNuur   c                     U R                  5       n[        R                  " U5      nU R                  R	                  X15        [
        R                  " U5      $ )z&Get the vector representation of word.)rj   rC   Vectorr   getWordVectorr    r!   )r   wordrM   bs       r   get_word_vector_FastText.get_word_vector   s>      "OOC Q%xx{r   c                     UR                  S5      S:w  a  [        S5      eUS-  nU R                  5       n[        R                  " U5      nU R
                  R                  X15        [        R                  " U5      $ )z
Given a string, get a single vector represenation. This function
assumes to be given a single line of text. We split words on
whitespace (space, newline, tab, vertical tab) and the control
characters carriage return, formfeed and the null character.

2predict processes one line at a time (remove '\n'))	find
ValueErrorrj   rC   rm   r   getSentenceVectorr    r!   )r   textrM   rp   s       r   get_sentence_vector_FastText.get_sentence_vector   se     99T?b RSS  "OOC   )xx{r   c                 :    U R                   R                  XU5      $ r   )r   getNN)r   ro   kon_unicode_errors       r   get_nearest_neighbors_FastText.get_nearest_neighbors   s    vv||D%566r   c                 <    U R                   R                  XX4U5      $ r   )r   getAnalogies)r   wordAwordBwordCr   r   s         r   get_analogies_FastText.get_analogies   s    vv""5;KLLr   c                 8    U R                   R                  U5      $ )zc
Given a word, get the word id within the dictionary.
Returns -1 if word is not in the dictionary.
)r   	getWordIdr   ro   s     r   get_word_id_FastText.get_word_id   s    
 vv%%r   c                 8    U R                   R                  U5      $ )zf
Given a label, get the label id within the dictionary.
Returns -1 if label is not in the dictionary.
)r   
getLabelId)r   r"   s     r   r   _FastText.get_label_id   s    
 vv  ''r   c                 8    U R                   R                  U5      $ )zG
Given a subword, return the index (within input matrix) it hashes to.
)r   getSubwordId)r   subwords     r   get_subword_id_FastText.get_subword_id   s     vv""7++r   c                 t    U R                   R                  X5      nUS   [        R                  " US   5      4$ )z4
Given a word, get the subwords and their indicies.
r      )r   getSubwordsr    r!   )r   ro   r   pairs       r   get_subwords_FastText.get_subwords   s4     vv!!$9Awa)))r   c                     U R                  5       n[        R                  " U5      nU R                  R	                  X15        [
        R                  " U5      $ )zC
Given an index, get the corresponding vector of the Input Matrix.
)rj   rC   rm   r   getInputVectorr    r!   )r   indrM   rp   s       r   get_input_vector_FastText.get_input_vector   s@       "OOC a%xx{r   c                 H   S n[        U5      [        :X  a8  U Vs/ s H
  oe" U5      PM     nnU R                  R                  XX45      u  pxXx4$ U" U5      nU R                  R	                  XX45      n	U	(       a  [        U	6 u  pO/ SpU[        R                  " U
SS94$ s  snf )aE  
Given a string, get a list of labels and a list of
corresponding probabilities. k controls the number
of returned labels. A choice of 5, will return the 5
most probable labels. By default this returns only
the most likely label and probability. threshold filters
the returned labels by a threshold on probability. A
choice of 0.5 will return labels with at least 0.5
probability. k and threshold will be applied together to
determine the returned labels.

This function assumes to be given
a single line of text. We split words on whitespace (space,
newline, tab, vertical tab) and the control characters carriage
return, formfeed and the null character.

If the model is not supervised, this function will throw a ValueError.

If given a list of strings, it will return a list of results as usually
received for a single line of text.
c                 P    U R                  S5      S:w  a  [        S5      eU S-  n U $ )Nrt   ru   rv   rw   rx   entrys    r   check _FastText.predict.<locals>.check   s-    zz$2% !VWWTMELr   r   Fr   )typelistr   multilinePredictpredictr   r    r!   )r   rz   r   	thresholdr   r   r   
all_labels	all_probspredictionsprobslabelss               r   r   _FastText.predict   s    .	 :.23dUE%LdD3$(FF$;$;%!J ((;D&&..)NK #[ 1v!#Rv288E666 4s   Bc                     U R                   R                  5       (       a  [        S5      e[        R                  " U R                   R                  5       5      $ )ze
Get a reference to the full input matrix of a Model. This only
works if the model is not quantized.
Can't get quantized Matrix)r   rc   rx   r    r!   getInputMatrixrd   s    r   get_input_matrix_FastText.get_input_matrix   s=    
 66>>9::xx--/00r   c                     U R                   R                  5       (       a  [        S5      e[        R                  " U R                   R                  5       5      $ )zf
Get a reference to the full output matrix of a Model. This only
works if the model is not quantized.
r   )r   rc   rx   r    r!   getOutputMatrixrd   s    r   get_output_matrix_FastText.get_output_matrix   s=    
 66>>9::xx..011r   c                     U R                   R                  U5      nU(       a  US   [        R                  " US   5      4$ US   $ )z
Get the entire list of words of the dictionary optionally
including the frequency of the individual words. This
does not include any subwords. For that please consult
the function get_subwords.
r   r   )r   getVocabr    r!   )r   include_freqr   r   s       r   	get_words_FastText.get_words  s?     vv/0GRXXd1g.//7Nr   c                    U R                   R                  5       nUR                  [        R                  :X  aE  U R                   R                  U5      nU(       a  US   [        R                  " US   5      4$ US   $ U R                  U5      $ )z
Get the entire list of labels of the dictionary optionally
including the frequency of the individual labels. Unsupervised
models use words as labels, which is why get_labels
will call and return get_words for this type of
model.
r   r   )	r   rh   model
model_name
supervised	getLabelsr    r!   r   )r   r   r   ri   r   s        r   
get_labels_FastText.get_labels  sr     FFNN77j+++66##$45DQ$q'!233Aw>>,//r   c                     S n[        U5      [        :X  a2  U Vs/ s H
  oC" U5      PM     nnU R                  R                  X5      $ U" U5      nU R                  R	                  X5      $ s  snf )z
Split a line of text into words and labels. Labels must start with
the prefix used to create the model (__label__ by default).
c                 P    U R                  S5      S:w  a  [        S5      eU S-  n U $ )Nrt   ru   z3get_line processes one line at a time (remove '\n')r   r   s    r   r   !_FastText.get_line.<locals>.check(  s-    zz$2% !WXXTMELr   )r   r   r   multilineGetLinegetLine)r   rz   r   r   r   s        r   get_line_FastText.get_line"  sc    	 :.23dUE%LdD366**4BB;D66>>$99	 4s   A,c                 :    U R                   R                  U5        g)z Save the model to the given pathN)r   	saveModel)r   paths     r   
save_model_FastText.save_model5  s    r   c                 :    U R                   R                  XU5      $ )z2Evaluate supervised model using file given by path)r   testr   r   r   r   s       r   r   _FastText.test9  s    vv{{4I..r   c                 :    U R                   R                  XU5      $ )z
Return the precision and recall score for each label.

The returned value is a dictionary, where the key is the label.
For example:
f.test_label(...)
{'__label__italian-cuisine' : {'precision' : 0.7, 'recall' : 0.74}}
)r   	testLabelr   s       r   
test_label_FastText.test_label=  s     vv33r   c                 N    [        X R                  R                  X5      5      nU$ r   )r   r   getMeter)r   r   r   r   s       r   	get_meter_FastText.get_meterH  s    tVV__T56r   c                 J   U R                   R                  5       nU(       d  UR                  nU(       d  UR                  nU(       d  UR                  nU(       d  UR
                  nU(       a  U(       d  [        S5      eUc  SnU R                   R                  XX4XVXxX5
        g)zN
Quantize the model reducing the size of the model and
it's memory footprint.
z"Need input file path if retrainingN )r   rh   rO   rL   rX   r[   rx   quantize)r   inputqoutcutoffretrainrO   rL   rX   r[   dsubqnormri   s               r   r   _FastText.quantizeM  sz    " FFNNGGEBXXFiiG5ABB=E%Vd	
r   c                     U R                   R                  UR                  [        R                  5      UR                  [        R                  5      5        g)zS
Set input and output matrices. This function assumes you know what you
are doing.
N)r   setMatricesastyper    float32)r   input_matrixoutput_matrixs      r   set_matrices_FastText.set_matriceso  s9    
 	

+]-A-A"**-M	
r   c                 ^    U R                   c  U R                  5       U l         U R                   $ r   )rE   r   rd   s    r   words_FastText.wordsx  s$    ;;..*DK{{r   c                 ^    U R                   c  U R                  5       U l         U R                   $ r   )rF   r   rd   s    r   r   _FastText.labels~  s$    <<??,DL||r   c                 $    U R                  U5      $ r   )rq   r   s     r   __getitem___FastText.__getitem__  s    ##D))r   c                     XR                   ;   $ r   )r   r   s     r   __contains___FastText.__contains__  s    zz!!r   )rF   rE   r   )NNr   )
   strict)r   )r           r   )Fr   )r   r   )ru   )
NFr   FNNNN   F)$r:   r;   r<   r=   __doc__r   rG   re   rj   rq   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r>   r   r   r   r@   r@   O   s    A2 
7M&(,*,7\120$:&/	4  
D
  
  
*"r   r@   c                     U S:X  a  [         R                  $ U S:X  a  [         R                  $ U S:X  a  [         R                  $ [	        S5      e)Ncbowskipgramr   zUnrecognized model name)r   r  r  r   rx   strings    r   _parse_model_stringr	    sH    """$$$233r   c                     U S:X  a  [         R                  $ U S:X  a  [         R                  $ U S:X  a  [         R                  $ U S:X  a  [         R                  $ [        S5      e)NnshssoftmaxovazUnrecognized loss name)	loss_namer  r  r  r  rx   r  s    r   _parse_loss_stringr    sU    ~||~||   }}122r   c                    [        U S   5      U S'   [        U S   5      U S'   [        U S   5      [        :X  a  [	        U S   5      U S'   [
        R                  " 5       nU R                  5        H)  u  p4[        X#U5        X1;   d  M  UR                  U5        M+     SUl
        SUl        UR                  S::  a  UR                  S:X  a  SUl        U$ )Nr   rV   autotuneModelSizer   r   r   )r	  r  r   intstrrC   rI   itemsr]   	setManualoutput
saveOutputrU   rS   rW   )rI   manually_set_argsri   r   vs        r   _build_argsr    s    'W6DM%d6l3DLD$%&#-$'-@(A$B !A**,a!KKN  AHAL||qQVVq[Hr   c                 N    [         R                   " 5       nUR                  U 5      $ )z?Given a string of text, tokenize it and return a list of tokens)rC   tokenize)rz   r   s     r   r  r    s    A::dr   c                     [        U S9$ )z8Load a model given a filepath and return a model object.)rH   )r@   )r   s    r   
load_modelr    s    %%r   r   r  rL   g?rM   d   rN      rO   rP   rQ   rR      rS      rT   rU   r   rV   r  rW   i rX   rY   rZ   g-C6?r"   	__label__r  r   f1i,  )r[   r\   seedautotuneValidationFileautotuneMetricautotunePredictionsautotuneDurationr  c                 X   SSSSSS.n0 n[        5       n[        [        X 5      UR                  5       5       HI  u  pxXt;   a  XG   nXr;  a  [	        SU-  5      eXu;   a  [	        SU-  5      eXU'   UR                  U5        MK     UR                  5        H  u  pxXu;  d  M  XU'   M     XV4$ )	NrP   rU   rY   r"   r\   )	min_countword_ngramslr_update_ratelabel_prefixpretrained_vectorsz unexpected keyword argument '%s'z!multiple values for argument '%s')setr   r   r  	TypeErroradd)	arg_listarg_dictr_   default_values	param_mapretr  r`   	arg_values	            r   	read_argsr:    s    #(1I C!&s9'?AQ!R  *H$>IJJ??(JKK!Hh' "S "0!5!5!7%M "8 ##r   c            	      >   [         R                  5       nUR                  SSSSSSS.5        / SQn[        XX25      u  pE[	        XE5      n[        US9n[        R                  " UR                  U5        UR                  UR                  R                  5       5        U$ )	a%  
Train a supervised model and return a model object.

input must be a filepath. The input text does not need to be tokenized
as per the tokenize function, but it must be preprocessed and encoded
as UTF-8. You might want to consult standard preprocessing scripts such
as tokenizer.perl mentioned here: http://www.statmt.org/wmt07/baseline.html

The input file must must contain at least one label per line. For an
example consult the example datasets which are part of the fastText
repository such as the dataset pulled by classification-example.sh.
g?r   r   r  r   )rL   rP   rR   rS   rV   r   )r   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r"   r[   r\   r&  r'  r(  r)  r*  r  rI   )unsupervised_defaultr   updater:  r  r@   rC   trainr   rG   rh   )kargskwargssupervised_defaultr_   rI   r  ri   fts           r   train_supervisedrD    s     .224!	
	I6 (yUDD,A		BNN244KKIr   c                      / SQn[        XU[        5      u  p4[        X45      n[        US9n[        R
                  " UR                  U5        UR                  UR                  R                  5       5        U$ )aZ  
Train an unsupervised model and return a model object.

input must be a filepath. The input text does not need to be tokenized
as per the tokenize function, but it must be preprocessed and encoded
as UTF-8. You might want to consult standard preprocessing scripts such
as tokenizer.perl mentioned here: http://www.statmt.org/wmt07/baseline.html

The input field must not contain any labels or use the specified label prefix
unless it is ok for those words to be ignored. For an example consult the
dataset pulled by the example script word-vector-example.sh, which is
part of the fastText repository.
)r   r   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r"   r[   r\   r<  )	r:  r=  r  r@   rC   r?  r   rG   rh   )r@  rA  r_   rI   r  ri   rC  s          r   train_unsupervisedrF  5  s^    I, (yBVWDD,A		BNN244KKIr   c                      [        S5      e)Nz`cbow` is not supported any more. Please use `train_unsupervised` with model=`cbow`. For more information please refer to https://fasttext.cc/blog/2019/06/25/blog-post.html#2-you-were-using-the-unofficial-fasttext-module	Exceptionr@  rA  s     r   r  r  a  s    
 	g r   c                      [        S5      e)Nz`skipgram` is not supported any more. Please use `train_unsupervised` with model=`skipgram`. For more information please refer to https://fasttext.cc/blog/2019/06/25/blog-post.html#2-you-were-using-the-unofficial-fasttext-modulerH  rJ  s     r   r  r  g  s    
 	o r   c                      [        S5      e)Nz`supervised` is not supported any more. Please use `train_supervised`. For more information please refer to https://fasttext.cc/blog/2019/06/25/blog-post.html#2-you-were-using-the-unofficial-fasttext-modulerH  rJ  s     r   r   r   m  s    
 	Y r   )!
__future__r   r   r   r   fasttext_pybindrC   numpyr    multiprocessing	itertoolsr   r  r   EOSBOWEOWdisplayed_errorsr   r@   r	  r  r  r  r  	cpu_countr=  r:  rD  rF  r  r  r   r   r   r   <module>rW     s   '  % ' "   	  
		 2 2jy" y"x	4
3$&
Z$ 
3 	!	
 Q  Q A A 
1 ! D g o'')A- C  !" [#$  3 :$89x)Xr   