Ë
    a›¾j|6  ã                   ó8  — d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
 d dlZd dlmZmZmZm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 ddlmZ ddlm Z  dZ! e«       jE                  e!«      d   Z#dZ$dZ%dee   dee&ef   fd„Z'd„ Z( G d„ de «      Z)d„ Z*y)é    N)Úislice)ÚAnyÚCallableÚDictÚIterableÚListÚOptionalÚTuple)ÚConfigÚModelÚ	OptimizerÚset_dropout_rateé   )ÚErrors)ÚLanguage)ÚScorer)ÚDoc)ÚExampleÚvalidate_examplesÚvalidate_get_examples)ÚVocabé   )ÚTrainablePipeaW  
[model]
@architectures = "spacy.TextCatEnsemble.v2"

[model.tok2vec]
@architectures = "spacy.Tok2Vec.v2"

[model.tok2vec.embed]
@architectures = "spacy.MultiHashEmbed.v2"
width = 64
rows = [2000, 2000, 500, 1000, 500]
attrs = ["NORM", "LOWER", "PREFIX", "SUFFIX", "SHAPE"]
include_static_vectors = false

[model.tok2vec.encode]
@architectures = "spacy.MaxoutWindowEncoder.v2"
width = ${model.tok2vec.embed.width}
window_size = 1
maxout_pieces = 3
depth = 2

[model.linear_model]
@architectures = "spacy.TextCatBOW.v3"
exclusive_classes = true
length = 262144
ngram_size = 1
no_output_layer = false
Úmodelz€
[model]
@architectures = "spacy.TextCatBOW.v3"
exclusive_classes = true
length = 262144
ngram_size = 1
no_output_layer = false
a`  
[model]
@architectures = "spacy.TextCatReduce.v1"
exclusive_classes = true
use_reduce_first = false
use_reduce_last = false
use_reduce_max = false
use_reduce_mean = true

[model.tok2vec]
@architectures = "spacy.HashEmbedCNN.v2"
pretrained_vectors = null
width = 96
depth = 4
embed_size = 2000
window_size = 1
maxout_pieces = 3
subword_features = true
ÚexamplesÚreturnc                 ó4   — t        j                  | dfddi|¤ŽS )NÚcatsÚmulti_labelF)r   Ú
score_cats)r   Úkwargss     ú`/home/spazio/kingdom_calls/work5/transenv/lib/python3.12/site-packages/spacy/pipeline/textcat.pyÚtextcat_scorer#   L   s/   € Ü×ÑØØñð ðð ñ	ð ó    c                  ó   — t         S ©N)r#   © r$   r"   Úmake_textcat_scorerr(   U   s   € ÜÐr$   c                   ó   — e Zd ZdZ	 d$edœdedededede	e
   dd	fd
„Zed„ «       Zedee   fd„«       Zedee   fd„«       Zdee   fd„Zdee   dd	fd„Zdd	d	dœdee   dede	e   de	eeef      deeef   f
d„Zdd	d	dœdee   dede	e   de	eeef      deeef   f
d„Zdee   deej8                  ej8                  f   fd„Zdee   deeef   fd„Zdedefd„Z d	d	d	dœde
g ee   f   de	e!   d e	ee      d!e	e   dd	f
d"„Z"dee   fd#„Z#y	)%ÚTextCategorizerzmPipeline component for single-label text classification.

    DOCS: https://spacy.io/api/textcategorizer
    )ÚscorerÚvocabr   ÚnameÚ	thresholdr+   r   Nc                óv   — || _         || _        || _        d| _        g |ddœ}t	        |«      | _        || _        y)aa  Initialize a text categorizer for single-label classification.

        vocab (Vocab): The shared vocabulary.
        model (thinc.api.Model): The Thinc Model powering the pipeline component.
        name (str): The component instance name, used to add entries to the
            losses during training.
        threshold (float): Unused, not needed for single-label (exclusive
            classes) classification.
        scorer (Optional[Callable]): The scoring method. Defaults to
                Scorer.score_cats for the attribute "cats".

        DOCS: https://spacy.io/api/textcategorizer#init
        N)Úlabelsr.   Úpositive_label)r,   r   r-   Ú_rehearsal_modelÚdictÚcfgr+   )Úselfr,   r   r-   r.   r+   r4   s          r"   Ú__init__zTextCategorizer.__init___   sE   € ð, ˆŒ
ØˆŒ
ØˆŒ	Ø $ˆÔàØ"Ø"ñ
ˆô
 ˜“9ˆŒØˆ�r$   c                  ó   — y)NFr'   ©r5   s    r"   Úsupport_missing_valuesz&TextCategorizer.support_missing_values�   s   € ð
 r$   c                 ó2   — t        | j                  d   «      S )z†RETURNS (Tuple[str]): The labels currently added to the component.

        DOCS: https://spacy.io/api/textcategorizer#labels
        r0   )Útupler4   r8   s    r"   r0   zTextCategorizer.labelsˆ   s   € ô �T—X‘X˜hÑ'Ó(Ð(r$   c                 ó   — | j                   S )z†RETURNS (List[str]): Information about the component's labels.

        DOCS: https://spacy.io/api/textcategorizer#label_data
        )r0   r8   s    r"   Ú
label_datazTextCategorizer.label_data�   s   € ð �{‰{Ðr$   Údocsc                 óš  — t        d„ |D «       «      ss|D �cg c]  }|j                  ‘Œ }}| j                  j                  j                  }|j                  t        t        |«      «      t        | j                  «      f«      }|S | j                  j                  |«      }| j                  j                  j                  |«      }|S c c}w )zþApply the pipeline's model to a batch of docs, without modifying them.

        docs (Iterable[Doc]): The documents to predict.
        RETURNS: The models prediction for each document.

        DOCS: https://spacy.io/api/textcategorizer#predict
        c              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr&   ©Úlen©Ú.0Údocs     r"   ú	<genexpr>z*TextCategorizer.predict.<locals>.<genexpr>    ó   è ø€ Ò, ”3�s—8Ñ,ùó   ‚)ÚanyÚtensorr   ÚopsÚxpÚzerosrB   Úlistr0   ÚpredictÚasarray)r5   r>   rE   ÚtensorsrL   Úscoress         r"   rO   zTextCategorizer.predict˜   s›   € ô Ñ, tÔ,Ô,à-1Ö2 c�s—z“zÐ2ˆGÐ2Ø—‘—‘×"Ñ"ˆBØ—X‘Xœs¤4¨£:›´°D·K±KÓ0@ÐAÓBˆFØˆMØ—‘×#Ñ# DÓ)ˆØ—‘—‘×'Ñ'¨Ó/ˆØˆùò 3s   —Cc                 óž   — t        |«      D ]?  \  }}t        | j                  «      D ]"  \  }}t        |||f   «      |j                  |<   Œ$ ŒA y)a  Modify a batch of Doc objects, using pre-computed scores.

        docs (Iterable[Doc]): The documents to modify.
        scores: The scores to set, produced by TextCategorizer.predict.

        DOCS: https://spacy.io/api/textcategorizer#set_annotations
        N)Ú	enumerater0   Úfloatr   )r5   r>   rR   ÚirE   ÚjÚlabels          r"   Úset_annotationszTextCategorizer.set_annotationsª   sQ   € ô   “oò 	6‰FˆAˆsÜ% d§k¡kÓ2ò 6‘��5Ü"'¨¨q°!¨t©Ó"5�—‘˜’ñ6ñ	6r$   ç        )ÚdropÚsgdÚlossesr   r[   r\   r]   c                óØ  — |€i }|j                  | j                  d«       t        |d«       | j                  |«       t	        d„ |D «       «      s|S t        | j                  |«       | j                  j                  |D �cg c]  }|j                  ‘Œ c}«      \  }}| j                  ||«      \  }}	 ||	«       |�| j                  |«       || j                  xx   |z  cc<   |S c c}w )a1  Learn from a batch of documents and gold-standard information,
        updating the pipe's model. Delegates to predict and get_loss.

        examples (Iterable[Example]): A batch of Example objects.
        drop (float): The dropout rate.
        sgd (thinc.api.Optimizer): The optimizer.
        losses (Dict[str, float]): Optional record of the loss during training.
            Updated using the component name as the key.
        RETURNS (Dict[str, float]): The updated losses dictionary.

        DOCS: https://spacy.io/api/textcategorizer#update
        rZ   zTextCategorizer.updatec              3   ób   K  — | ]'  }|j                   rt        |j                   «      nd –— Œ) y­w)r   N)Ú	predictedrB   )rD   Úegs     r"   rF   z)TextCategorizer.update.<locals>.<genexpr>Ï   s$   è ø€ ÒOÀ¨¯ª”3�r—|‘|Ô$¸!Ó;ÑOùs   ‚-/)Ú
setdefaultr-   r   Ú_validate_categoriesrI   r   r   Úbegin_updater`   Úget_lossÚfinish_update)
r5   r   r[   r\   r]   ra   rR   Ú	bp_scoresÚlossÚd_scoress
             r"   ÚupdatezTextCategorizer.update¶   sÒ   € ð( ˆ>ØˆFØ×Ñ˜$Ÿ)™) SÔ)Ü˜(Ð$<Ô=Ø×!Ñ! (Ô+ÜÑOÀhÔOÔOàˆMÜ˜Ÿ™ TÔ*Ø ŸJ™J×3Ñ3ÈHÖ4UÀb°R·\³\Ò4UÓVÑˆ�	ØŸ™ x°Ó8‰ˆˆhÙ�(ÔØˆ?Ø×Ñ˜sÔ#Øˆt�y‰yÓ˜TÑ!ÓØˆùò 5Vs   ÂC'c                ó4  — |€i }|j                  | j                  d«       | j                  €|S t        |d«       | j	                  |«       |D �cg c]  }|j
                  ‘Œ }}t        d„ |D «       «      s|S t        | j                  |«       | j                  j                  |«      \  }}| j                  j                  |«      \  }	}
||	z
  } ||«       |�| j                  |«       || j                  xx   |dz  j                  «       z  cc<   |S c c}w )a«  Perform a "rehearsal" update from a batch of data. Rehearsal updates
        teach the current model to make predictions similar to an initial model,
        to try to address the "catastrophic forgetting" problem. This feature is
        experimental.

        examples (Iterable[Example]): A batch of Example objects.
        drop (float): The dropout rate.
        sgd (thinc.api.Optimizer): The optimizer.
        losses (Dict[str, float]): Optional record of the loss during training.
            Updated using the component name as the key.
        RETURNS (Dict[str, float]): The updated losses dictionary.

        DOCS: https://spacy.io/api/textcategorizer#rehearse
        rZ   zTextCategorizer.rehearsec              3   ó2   K  — | ]  }t        |«      –— Œ y ­wr&   rA   rC   s     r"   rF   z+TextCategorizer.rehearse.<locals>.<genexpr>ù   rG   rH   r   )rb   r-   r2   r   rc   r`   rI   r   r   rd   rf   Úsum)r5   r   r[   r\   r]   ra   r>   rR   rg   ÚtargetÚ_Úgradients               r"   ÚrehearsezTextCategorizer.rehearseÛ   s  € ð, ˆ>ØˆFØ×Ñ˜$Ÿ)™) SÔ)Ø× Ñ Ð(ØˆMÜ˜(Ð$>Ô?Ø×!Ñ! (Ô+Ø'/Ö0 �—“Ð0ˆÐ0ÜÑ, tÔ,Ô,àˆMÜ˜Ÿ™ TÔ*Ø ŸJ™J×3Ñ3°DÓ9Ñˆ�	Ø×)Ñ)×6Ñ6°tÓ<‰	ˆ�Ø˜F‘?ˆÙ�(ÔØˆ?Ø×Ñ˜sÔ#Øˆt�y‰yÓ˜h¨™k×.Ñ.Ó0Ñ0ÓØˆùò 1s   ÁDc                 ó"  — t        t        |«      «      }t        j                  |t        | j                  «      fd¬«      }t        j
                  |t        | j                  «      fd¬«      }t        |«      D ]m  \  }}t        | j                  «      D ]P  \  }}||j                  j                  v r|j                  j                  |   |||f<   Œ=| j                  sŒJd|||f<   ŒR Œo | j                  j                  j                  |«      }||fS )NÚf)ÚdtyperZ   )rB   rN   ÚnumpyrM   r0   ÚonesrT   Ú	referencer   r9   r   rK   rP   )	r5   r   Únr_examplesÚtruthsÚnot_missingrV   ra   rW   rX   s	            r"   Ú_examples_to_truthz"TextCategorizer._examples_to_truth  sì   € ô œ$˜x›.Ó)ˆÜ—‘˜k¬3¨t¯{©{Ó+;Ð<ÀCÔHˆÜ—j‘j +¬s°4·;±;Ó/?Ð!@ÈÔLˆÜ˜xÓ(ò 	,‰EˆAˆrÜ% d§k¡kÓ2ò ,‘��5Ø˜BŸL™L×-Ñ-Ñ-Ø#%§<¡<×#4Ñ#4°UÑ#;�F˜1˜a˜4’LØ×0Ó0Ø(+�K  1 Ò%ñ	,ð	,ð —‘—‘×'Ñ'¨Ó/ˆØ�{Ð"Ð"r$   c                 ó  — t        |d«       | j                  |«       | j                  |«      \  }}| j                  j                  j                  |«      }||z
  }||z  }|dz  j                  «       }t        |«      |fS )ae  Find the loss and gradient of loss for the batch of documents and
        their predicted scores.

        examples (Iterable[Examples]): The batch of examples.
        scores: Scores representing the model's predictions.
        RETURNS (Tuple[float, float]): The loss and the gradient.

        DOCS: https://spacy.io/api/textcategorizer#get_loss
        zTextCategorizer.get_lossr   )r   rc   r{   r   rK   rP   ÚmeanrU   )r5   r   rR   ry   rz   ri   Úmean_square_errors          r"   re   zTextCategorizer.get_loss  s�   € ô 	˜(Ð$>Ô?Ø×!Ñ! (Ô+Ø"×5Ñ5°hÓ?Ñˆ�Ø—j‘j—n‘n×,Ñ,¨[Ó9ˆØ˜F‘?ˆØ�KÑˆØ% q™[×.Ñ.Ó0ÐÜÐ&Ó'¨Ð1Ð1r$   rX   c                 óæ  — t        |t        «      st        t        j                  «      ‚|| j
                  v ry| j                  «        | j                  d   j                  |«       | j                  rZd| j                  j                  v rB | j                  j                  d   | j                  t        | j
                  «      «      | _	        | j                  j                  j                  |«       y)zÎAdd a new label to the pipe.

        label (str): The label to add.
        RETURNS (int): 0 if label is already present, otherwise 1.

        DOCS: https://spacy.io/api/textcategorizer#add_label
        r   r0   Úresize_outputr   )Ú
isinstanceÚstrÚ
ValueErrorr   ÚE187r0   Ú_allow_extra_labelr4   Úappendr   ÚattrsrB   r,   ÚstringsÚadd)r5   rX   s     r"   Ú	add_labelzTextCategorizer.add_label(  s®   € ô ˜%¤Ô%ÜœVŸ[™[Ó)Ð)Ø�D—K‘KÑØØ×ÑÔ!Ø�‰�Ñ×!Ñ! %Ô(Ø�:Š:˜/¨T¯Z©Z×-=Ñ-=Ñ=Ø:˜Ÿ™×)Ñ)¨/Ñ:¸4¿:¹:ÄsÈ4Ï;É;ÓGWÓXˆDŒJØ�
‰
×Ñ×Ñ˜uÔ%Ør$   )Únlpr0   r1   Úget_examplesr‹   r0   r1   c                ó|  — t        |d«       | j                   |«       «       |€9 |«       D ].  }|j                  j                  D ]  }| j	                  |«       Œ Œ0 n|D ]  }| j	                  |«       Œ t        | j                  «      dk  rt        t        j                  «      ‚|�’|| j                  vr6t        j                  j                  || j                  ¬«      }t        |«      ‚t        | j                  «      dk7  r6t        j                  j                  || j                  ¬«      }t        |«      ‚|| j                  d<   t        t         |«       d«      «      }	|	D �
cg c]  }
|
j                   ‘Œ }}
| j#                  |	«      \  }}| j%                  «        t        |«      dkD  s/J t        j&                  j                  | j(                  ¬«      «       ‚t        |«      dkD  s/J t        j&                  j                  | j(                  ¬«      «       ‚| j*                  j-                  ||¬	«       yc c}
w )
a  Initialize the pipe for training, using a representative set
        of data examples.

        get_examples (Callable[[], Iterable[Example]]): Function that
            returns a representative sample of gold-standard Example objects.
        nlp (Language): The current nlp object the component is part of.
        labels (Optional[Iterable[str]]): The labels to add to the component, typically generated by the
            `init labels` command. If no labels are provided, the get_examples
            callback is used to extract the labels from the data.
        positive_label (Optional[str]): The positive label for a binary task with exclusive classes,
            `None` otherwise and by default.

        DOCS: https://spacy.io/api/textcategorizer#initialize
        zTextCategorizer.initializeNr   )Ú	pos_labelr0   r1   é
   r   )r-   )ÚXÚY)r   rc   Úyr   rŠ   rB   r0   rƒ   r   ÚE867ÚE920ÚformatÚE919r4   rN   r   rw   r{   Ú_require_labelsÚE923r-   r   Ú
initialize)r5   rŒ   r‹   r0   r1   ÚexampleÚcatrX   ÚerrÚsubbatchra   Ú
doc_sampleÚlabel_samplero   s                 r"   r™   zTextCategorizer.initialize;  sÖ  € ô, 	˜lÐ,HÔIØ×!Ñ!¡,£.Ô1Øˆ>Ù'›>ò (�Ø"Ÿ9™9Ÿ>™>ò (�CØ—N‘N 3Õ'ñ(ñ(ð  ò &�Ø—‘˜uÕ%ð&äˆt�{‰{Ó˜aÒÜœVŸ[™[Ó)Ð)ØÐ%Ø T§[¡[Ñ0Ü—k‘k×(Ñ(°>È$Ï+É+Ð(ÓV�Ü  “oÐ%Ü�4—;‘;Ó 1Ò$Ü—k‘k×(Ñ(°>È$Ï+É+Ð(ÓV�Ü  “oÐ%Ø%3ˆ�‰Ð!Ñ"Üœ™|›~¨rÓ2Ó3ˆØ-5Ö6 r�b—l“lÐ6ˆ
Ð6Ø×1Ñ1°(Ó;‰ˆ�aØ×ÑÔÜ�:‹ Ò"ÐF¤F§K¡K×$6Ñ$6¸D¿I¹IÐ$6Ó$FÓFÐ"Ü�<Ó  1Ò$ÐH¤f§k¡k×&8Ñ&8¸d¿i¹iÐ&8Ó&HÓHÐ$Ø�
‰
×Ñ 
¨lÐÕ;ùò 7s   Å(H9c                 ó„  — |D ]»  }t        |j                  j                  j                  «       «      }|j	                  d«      dkD  r=t        t        j                  j                  |j                  j                  ¬«      «      ‚|D ]6  }|dk(  rŒ	|dk(  rŒt        t        j                  j                  |¬«      «      ‚ Œ½ y)zKCheck whether the provided examples all have single-label cats annotations.g      ð?r   )ÚvaluerZ   )ÚvalN)
rN   rw   r   ÚvaluesÚcountrƒ   r   ÚE895r•   ÚE851)r5   r   ÚexÚvalsr¢   s        r"   rc   z$TextCategorizer._validate_categoriesl  s¢   € àò 	BˆBÜ˜Ÿ™×)Ñ)×0Ñ0Ó2Ó3ˆDØ�z‰z˜#‹ Ò"Ü ¤§¡×!3Ñ!3¸"¿,¹,×:KÑ:KÐ!3Ó!LÓMÐMØò B�Ø˜s›
 c¨S£jÜ$¤V§[¡[×%7Ñ%7¸CÐ%7Ó%@ÓAÐAñBñ		Br$   )Útextcat)$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r   r‚   rU   r	   r   r6   Úpropertyr9   r
   r0   r   r=   r   r   rO   rY   r   r   r   rj   rq   ru   Úndarrayr{   re   ÚintrŠ   r   r™   rc   r'   r$   r"   r*   r*   Y   sm  „ ñð ð	 ð &3ò àð ð ð ð ð	 ð ð ð ˜Ñ"ð ð 
ó ðD ñó ðð ð)˜˜c™
ò )ó ð)ð ð˜D ™Iò ó ðð˜H S™Mó ð$
6 H¨S¡Mð 
6¸dó 
6ð  Ø#'Ø-1ò#à˜7Ñ#ð#ð ð	#ð
 �iÑ ð#ð ˜˜c 5˜jÑ)Ñ*ð#ð 
ˆc�5ˆjÑ	ó#ðR Ø#'Ø-1ò)à˜7Ñ#ð)ð ð	)ð
 �iÑ ð)ð ˜˜c 5˜jÑ)Ñ*ð)ð 
ˆc�5ˆjÑ	ó)ðV#Ø  Ñ)ð#à	ˆu�}‰}˜eŸm™mÐ+Ñ	,ó#ð2 ¨'Ñ!2ð 2¸uÀUÈEÀ\Ñ?Ró 2ð&˜sð  só ð. #'Ø*.Ø(,ò/<à˜r 8¨GÑ#4Ð4Ñ5ð/<ð �hÑð	/<ð
 ˜ #™Ñ'ð/<ð ! ™ð/<ð 
ó/<ðbB¨X°gÑ->ô Br$   r*   c                 óx   — | dk(  r!t        j                  d«      }|j                  S t        dt        › d| › �«      ‚)NÚmake_textcatzspacy.pipeline.factorieszmodule z has no attribute )Ú	importlibÚimport_moduler²   ÚAttributeErrorrª   )r-   Úmodules     r"   Ú__getattr__r·   x  s@   € Øˆ~ÒÜ×(Ñ(Ð)CÓDˆØ×"Ñ"Ð"Ü
˜7¤8 *Ð,>¸t¸fÐEÓ
FÐFr$   )+r³   Ú	itertoolsr   Útypingr   r   r   r   r   r	   r
   ru   Ú	thinc.apir   r   r   r   Úerrorsr   Úlanguager   r+   r   Útokensr   Útrainingr   r   r   r,   r   Útrainable_piper   Úsingle_label_default_configÚfrom_strÚDEFAULT_SINGLE_TEXTCAT_MODELÚsingle_label_bow_configÚsingle_label_cnn_configr‚   r#   r(   r*   r·   r'   r$   r"   ú<module>rÅ      s¤   ðÛ Ý ß G× GÑ Gã ß @Ó @å Ý Ý Ý ß HÑ HÝ Ý )ðÐ ñ8  &›x×0Ñ0Ð1LÓMÈgÑVÐ ðÐ ðÐ ð*˜H WÑ-ð ¸DÀÀcÀ¹Nó òô[B�mô [Bó~Gr$   