Ë
    îÍ:jmx  ã                   ó   — d dl Z d dlZd dlmZmZmZmZ d dlZddl	m
Z
 ddlmZmZmZmZ ddlmZmZmZmZ  e«       r
d dlZddlmZ  e«       r
d dlZdd	lmZ  G d
„ de«      Z G d„ de«      Z e ed¬«      d«       G d„ de«      «       ZeZy)é    N)ÚAnyÚOptionalÚUnionÚoverloadé   )ÚBasicTokenizer)ÚExplicitEnumÚadd_end_docstringsÚis_tf_availableÚis_torch_availableé   )ÚArgumentHandlerÚChunkPipelineÚDatasetÚbuild_pipeline_init_args)Ú/TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES)Ú,MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMESc                   ó,   — e Zd ZdZdeeee   f   fd„Zy)Ú"TokenClassificationArgumentHandlerz5
    Handles arguments for token classification.
    Úinputsc                 ó   — |j                  dd«      }|j                  d«      }|�;t        |t        t        f«      r%t	        |«      dkD  rt        |«      }t	        |«      }nWt        |t
        «      r|g}d}nAt        �t        |t        «      st        |t        j                  «      r||d |fS t        d«      ‚|j                  d«      }|r?t        |t        «      rt        |d   t        «      r|g}t	        |«      |k7  rt        d«      ‚||||fS )	NÚis_split_into_wordsFÚ	delimiterr   r   zAt least one input is required.Úoffset_mappingz;offset_mapping should have the same batch size as the input)
ÚgetÚ
isinstanceÚlistÚtupleÚlenÚstrr   ÚtypesÚGeneratorTypeÚ
ValueError)Úselfr   Úkwargsr   r   Ú
batch_sizer   s          ú€/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/transformers/pipelines/token_classification.pyÚ__call__z+TokenClassificationArgumentHandler.__call__    sÿ   € Ø$Ÿj™jÐ)>ÀÓFÐØ—J‘J˜{Ó+ˆ	àÐ¤*¨V´d¼E°]Ô"CÌÈFËÐVWÊÜ˜&“\ˆFÜ˜V›‰JÜ˜¤Ô$Ø�XˆFØ‰JÜÐ ¤Z°¼Ô%@ÄJÈvÔW\×WjÑWjÔDkØÐ.°°iÐ?Ð?äÐ>Ó?Ð?àŸ™Ð$4Ó5ˆÙÜ˜.¬$Ô/´J¸~ÈaÑ?PÔRWÔ4XØ"0Ð!1�Ü�>Ó" jÒ0Ü Ð!^Ó_Ð_ØÐ*¨N¸IÐEÐEó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r   r(   © r)   r'   r   r      s"   „ ñðF˜u S¨$¨s©) ^Ñ4ô Fr)   r   c                   ó$   — e Zd ZdZdZdZdZdZdZy)ÚAggregationStrategyzDAll the valid aggregation strategies for TokenClassificationPipelineÚnoneÚsimpleÚfirstÚaverageÚmaxN)	r*   r+   r,   r-   ÚNONEÚSIMPLEÚFIRSTÚAVERAGEÚMAXr.   r)   r'   r0   r0   8   s   „ ÙNà€DØ€FØ€EØ€GØ
�Cr)   r0   T)Úhas_tokenizeraÍ
  
        ignore_labels (`list[str]`, defaults to `["O"]`):
            A list of labels to ignore.
        grouped_entities (`bool`, *optional*, defaults to `False`):
            DEPRECATED, use `aggregation_strategy` instead. Whether or not to group the tokens corresponding to the
            same entity together in the predictions or not.
        stride (`int`, *optional*):
            If stride is provided, the pipeline is applied on all the text. The text is split into chunks of size
            model_max_length. Works only with fast tokenizers and `aggregation_strategy` different from `NONE`. The
            value of this argument defines the number of overlapping tokens between chunks. In other words, the model
            will shift forward by `tokenizer.model_max_length - stride` tokens each step.
        aggregation_strategy (`str`, *optional*, defaults to `"none"`):
            The strategy to fuse (or not) tokens based on the model prediction.

                - "none" : Will simply not do any aggregation and simply return raw results from the model
                - "simple" : Will attempt to group entities following the default schema. (A, B-TAG), (B, I-TAG), (C,
                  I-TAG), (D, B-TAG2) (E, B-TAG2) will end up being [{"word": ABC, "entity": "TAG"}, {"word": "D",
                  "entity": "TAG2"}, {"word": "E", "entity": "TAG2"}] Notice that two consecutive B tags will end up as
                  different entities. On word based languages, we might end up splitting words undesirably : Imagine
                  Microsoft being tagged as [{"word": "Micro", "entity": "ENTERPRISE"}, {"word": "soft", "entity":
                  "NAME"}]. Look for FIRST, MAX, AVERAGE for ways to mitigate that and disambiguate words (on languages
                  that support that meaning, which is basically tokens separated by a space). These mitigations will
                  only work on real words, "New york" might still be tagged with two different entities.
                - "first" : (works only on word based models) Will use the `SIMPLE` strategy except that words, cannot
                  end up with different tags. Words will simply use the tag of the first token of the word when there
                  is ambiguity.
                - "average" : (works only on word based models) Will use the `SIMPLE` strategy except that words,
                  cannot end up with different tags. scores will be averaged first across tokens, and then the maximum
                  label is applied.
                - "max" : (works only on word based models) Will use the `SIMPLE` strategy except that words, cannot
                  end up with different tags. Word entity will simply be the token with the maximum score.c                   óú  ‡ — e Zd ZdZdZdZdZdZdZ e	«       fˆ fd„	Z
	 	 	 	 	 	 	 	 d)dee   dee   d	ee   d
eeeeef         dedee   dee   fd„Zedededeeeef      fd„«       Zedee   dedeeeeef         fd„«       Zdeeee   f   dedeeeeef      eeeeef         f   fˆ fd„Zd*d„Zd„ Zej4                  dfd„Zd„ Z	 	 d+dedej<                  dej<                  d
eeeeef         dej<                  d	edeeee         deeeeef         dee   fd„Zd ee   d	edee   fd!„Z d"ee   d	edefd#„Z!d"ee   d	edee   fd$„Z"d"ee   defd%„Z#d&edeeef   fd'„Z$d"ee   dee   fd(„Z%ˆ xZ&S ),ÚTokenClassificationPipelineuv	  
    Named Entity Recognition pipeline using any `ModelForTokenClassification`. See the [named entity recognition
    examples](../task_summary#named-entity-recognition) for more information.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> token_classifier = pipeline(model="Jean-Baptiste/camembert-ner", aggregation_strategy="simple")
    >>> sentence = "Je m'appelle jean-baptiste et je vis Ã  montrÃ©al"
    >>> tokens = token_classifier(sentence)
    >>> tokens
    [{'entity_group': 'PER', 'score': 0.9931, 'word': 'jean-baptiste', 'start': 12, 'end': 26}, {'entity_group': 'LOC', 'score': 0.998, 'word': 'montrÃ©al', 'start': 38, 'end': 47}]

    >>> token = tokens[0]
    >>> # Start and end provide an easy way to highlight words in the original text.
    >>> sentence[token["start"] : token["end"]]
    ' jean-baptiste'

    >>> # Some models use the same idea to do part of speech.
    >>> syntaxer = pipeline(model="vblagoje/bert-english-uncased-finetuned-pos", aggregation_strategy="simple")
    >>> syntaxer("My name is Sarah and I live in London")
    [{'entity_group': 'PRON', 'score': 0.999, 'word': 'my', 'start': 0, 'end': 2}, {'entity_group': 'NOUN', 'score': 0.997, 'word': 'name', 'start': 3, 'end': 7}, {'entity_group': 'AUX', 'score': 0.994, 'word': 'is', 'start': 8, 'end': 10}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'sarah', 'start': 11, 'end': 16}, {'entity_group': 'CCONJ', 'score': 0.999, 'word': 'and', 'start': 17, 'end': 20}, {'entity_group': 'PRON', 'score': 0.999, 'word': 'i', 'start': 21, 'end': 22}, {'entity_group': 'VERB', 'score': 0.998, 'word': 'live', 'start': 23, 'end': 27}, {'entity_group': 'ADP', 'score': 0.999, 'word': 'in', 'start': 28, 'end': 30}, {'entity_group': 'PROPN', 'score': 0.999, 'word': 'london', 'start': 31, 'end': 37}]
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    This token recognition pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"ner"` (for predicting the classes of tokens in a sequence: person, organisation, location or miscellaneous).

    The models that this pipeline can use are models that have been fine-tuned on a token classification task. See the
    up-to-date list of available models on
    [huggingface.co/models](https://huggingface.co/models?filter=token-classification).
    Ú	sequencesFTc                 ó¨   •— t        ‰| �  di |¤Ž | j                  | j                  dk(  rt        nt
        «       t        d¬«      | _        || _        y )NÚtfF)Údo_lower_caser.   )	ÚsuperÚ__init__Úcheck_model_typeÚ	frameworkr   r   r   Ú_basic_tokenizerÚ_args_parser)r$   Úargs_parserr%   Ú	__class__s      €r'   rC   z$TokenClassificationPipeline.__init__�   sL   ø€ Ü‰ÑÑ"˜6Ò"à×Ñà�~‰~ Ò%õ <ä=ô	
ô !/¸UÔ CˆÔØ'ˆÕr)   NÚgrouped_entitiesÚignore_subwordsÚaggregation_strategyr   r   Ústrider   c	                 ó"  — i }	||	d<   |r	|€dn||	d<   |�||	d<   i }
|€|�p|r|rt         j                  }n%|r|st         j                  }nt         j                  }|�t	        j
                  d|› d�«       |�t	        j
                  d|› d�«       |�~t        |t        «      rt         |j                  «          }|t         j                  t         j                  t         j                  hv r!| j                  j                  st        d«      ‚||
d	<   |�||
d
<   |�s|| j                  j                  k\  rt        d«      ‚|t         j                  k(  rt        d|› d�«      ‚| j                  j                  rdd|dœ}||	d<   nt        d«      ‚|	i |
fS )Nr   ú r   r   zl`grouped_entities` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="z"` instead.zk`ignore_subwords` is deprecated and will be removed in version v5.0.0, defaulted to `aggregation_strategy="z{Slow tokenizers cannot handle subwords. Please set the `aggregation_strategy` option to `"simple"` or use a fast tokenizer.rL   Úignore_labelszl`stride` must be less than `tokenizer.model_max_length` (or even lower if the tokenizer adds special tokens)zI`stride` was provided to process all the text but `aggregation_strategy="z&"`, please select another one instead.T)Úreturn_overflowing_tokensÚpaddingrM   Útokenizer_paramszm`stride` was provided to process all the text but you're using a slow tokenizer. Please use a fast tokenizer.)r0   r8   r7   r6   ÚwarningsÚwarnr   r    Úupperr:   r9   Ú	tokenizerÚis_fastr#   Úmodel_max_length)r$   rP   rJ   rK   rL   r   r   rM   r   Úpreprocess_paramsÚpostprocess_paramsrS   s               r'   Ú_sanitize_parametersz0TokenClassificationPipeline._sanitize_parametersœ   s  € ð ÐØ3FÐÐ/Ñ0áØ4=Ð4E©SÈ9Ð˜kÑ*àÐ%Ø2@ÐÐ.Ñ/àÐØÐ'¨?Ð+FÙ¡OÜ':×'@Ñ'@Ñ$Ù!©/Ü':×'AÑ'AÑ$ä':×'?Ñ'?Ð$àÐ+Ü—‘ð/Ø/CÐ.DÀKðQôð Ð*Ü—‘ð/Ø/CÐ.DÀKðQôð
  Ð+ÜÐ.´Ô4Ü':Ð;O×;UÑ;UÓ;WÑ'XÐ$à$Ü'×-Ñ-Ô/B×/FÑ/FÔH[×HcÑHcÐdñeàŸ™×.Ò.ä ð>óð ð :NÐÐ5Ñ6ØÐ$Ø2?Ð˜Ñ/ØÐØ˜Ÿ™×8Ñ8Ò8Ü ð Cóð ð $Ô':×'?Ñ'?Ò?Ü ðØ,Ð-Ð-SðUóð ð
 —>‘>×)Ò)à59Ø#'Ø"(ñ(Ð$ð
 =MÐ%Ð&8Ò9ä$ð8óð ð ! "Ð&8Ð8Ð8r)   r   r%   Úreturnc                  ó   — y ©Nr.   ©r$   r   r%   s      r'   r(   z$TokenClassificationPipeline.__call__ì   s   € ØLOr)   c                  ó   — y r_   r.   r`   s      r'   r(   z$TokenClassificationPipeline.__call__ï   s   € ØX[r)   c                 óÀ   •—  | j                   |fi |¤Ž\  }}}}||d<   ||d<   |r#t        d„ |D «       «      st        ‰| �  |gfi |¤ŽS |r||d<   t        ‰| �  |fi |¤ŽS )a  
        Classify each token of the text(s) given as inputs.

        Args:
            inputs (`str` or `List[str]`):
                One or several texts (or one list of texts) for token classification. Can be pre-tokenized when
                `is_split_into_words=True`.

        Return:
            A list or a list of list of `dict`: Each result comes as a list of dictionaries (one for each token in the
            corresponding input, or each entity if this pipeline was instantiated with an aggregation_strategy) with
            the following keys:

            - **word** (`str`) -- The token/word classified. This is obtained by decoding the selected tokens. If you
              want to have the exact string in the original sentence, use `start` and `end`.
            - **score** (`float`) -- The corresponding probability for `entity`.
            - **entity** (`str`) -- The entity predicted for that token/word (it is named *entity_group* when
              *aggregation_strategy* is not `"none"`.
            - **index** (`int`, only present when `aggregation_strategy="none"`) -- The index of the corresponding
              token in the sentence.
            - **start** (`int`, *optional*) -- The index of the start of the corresponding entity in the sentence. Only
              exists if the offsets are available within the tokenizer
            - **end** (`int`, *optional*) -- The index of the end of the corresponding entity in the sentence. Only
              exists if the offsets are available within the tokenizer
        r   r   c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­wr_   )r   r   )Ú.0Úinputs     r'   ú	<genexpr>z7TokenClassificationPipeline.__call__.<locals>.<genexpr>  s   è ø€ Ò*WÀu¬:°e¼T×+BÑ*Wùs   ‚r   )rG   ÚallrB   r(   )r$   r   r%   Ú_inputsr   r   r   rI   s          €r'   r(   z$TokenClassificationPipeline.__call__ò   sŠ   ø€ ð: CTÀ$×BSÑBSÐTZÑBeÐ^dÑBeÑ?ˆÐ$ n°iØ(;ˆÐ$Ñ%Ø'ˆˆ{ÑÙ¤sÑ*WÐPVÔ*WÔ'WÜ‘7Ñ# V HÑ7°Ñ7Ð7ÙØ'5ˆFÐ#Ñ$ä‰wÑ Ñ1¨&Ñ1Ð1r)   c           
   +   ó‚  K  — |j                  di «      }| j                  j                  xr | j                  j                  dkD  }d }|d   }|r�|d   }t        |t        «      st        d«      ‚|}	|j                  |	«      }g }t        |«      }
d}|	D ]2  }|j                  ||t        |«      z   f«       |t        |«      |
z   z  }Œ4 |	}d|d<   nt        |t        «      st        d«      ‚|} | j                  |f| j                  |d| j                  j                  dœ|¤Ž}|r!| j                  j                  st        d	«      ‚|j                  d
d «       t        |d   «      }t        |«      D ]¾  }| j                  dk(  r;|j                  «       D ��ci c]  \  }}|t        j                  ||   d«      “Œ! }}}n5|j                  «       D ��ci c]  \  }}|||   j!                  d«      “Œ }}}|�||d<   |dk(  r|nd |d<   ||dz
  k(  |d<   |�|j#                  |«      |d<   ||d<   |–— ŒÀ y c c}}w c c}}w ­w)NrS   r   r   r   zEWhen `is_split_into_words=True`, `sentence` must be a list of tokens.TzKWhen `is_split_into_words=False`, `sentence` must be an untokenized string.)Úreturn_tensorsÚ
truncationÚreturn_special_tokens_maskÚreturn_offsets_mappingz@is_split_into_words=True is only supported with fast tokenizers.Úoverflow_to_sample_mappingÚ	input_idsr@   r   Úsentencer   Úis_lastÚword_idsÚword_to_chars_map)ÚpoprW   rY   r   r   r#   Újoinr   Úappendr    rE   rX   ÚrangeÚitemsr@   Úexpand_dimsÚ	unsqueezerr   )r$   rp   r   rZ   rS   rk   rs   r   r   ÚwordsÚdelimiter_lenÚchar_offsetÚwordÚtext_to_tokenizer   Ú
num_chunksÚiÚkÚvÚmodel_inputss                       r'   Ú
preprocessz&TokenClassificationPipeline.preprocess  se  è ø€ Ø,×0Ñ0Ð1CÀRÓHÐØ—^‘^×4Ñ4Ò\¸¿¹×9XÑ9XÐ[\Ñ9\ˆ
à ÐØ/Ð0EÑFÐÙØ)¨+Ñ6ˆIÜ˜h¬Ô-Ü Ð!hÓiÐiØˆEØ —~‘~ eÓ,ˆHà "ÐÜ 	›NˆMØˆKØò 9�Ø!×(Ñ(¨+°{ÄSÈÃYÑ7NÐ)OÔPØœs 4›y¨=Ñ8Ñ8‘ð9ð
  %ÐØ6:ÐÐ2Ò3ä˜h¬Ô,Ü Ð!nÓoÐoØ'Ðà�—‘Øð
àŸ>™>Ø!Ø'+Ø#'§>¡>×#9Ñ#9ñ
ð ñ
ˆñ  t§~¡~×'=Ò'=ÜÐ_Ó`Ð`à�
‰
Ð/°Ô6Ü˜ Ñ,Ó-ˆ
ä�zÓ"ò 	ˆAØ�~‰~ Ò%ØGMÇ|Á|Ã~×V¹t¸qÀ! ¤2§>¡>°!°A±$¸Ó#:Ñ :ÐV�ÒVàAGÇÁÃ×P¹¸¸A  1 Q¡4§>¡>°!Ó#4Ñ 4ÐP�ÑPØÐ)Ø1?�Ð-Ñ.à34¸²6¡x¸tˆL˜Ñ$Ø&'¨:¸©>Ñ&9ˆL˜Ñ#Ø Ð,Ø+1¯?©?¸1Ó+=�˜ZÑ(Ø4E�Ð0Ñ1àÓñ	ùãVùãPùs   ‚FH?Æ$H3Æ:H?ÇH9Ç0AH?c                 óŽ  — |j                  d«      }|j                  dd «      }|j                  d«      }|j                  d«      }|j                  dd «      }|j                  dd «      }| j                  dk(  r | j                  di |¤Žd   }n, | j                  di |¤Ž}	t        |	t        «      r|	d	   n|	d   }|||||||d
œ|¥S )NÚspecial_tokens_maskr   rp   rq   rr   rs   r@   r   Úlogits)rˆ   r‡   r   rp   rq   rr   rs   r.   )rt   rE   Úmodelr   Údict)
r$   r„   r‡   r   rp   rq   rr   rs   rˆ   Úoutputs
             r'   Ú_forwardz$TokenClassificationPipeline._forwardT  sè   € à*×.Ñ.Ð/DÓEÐØ%×)Ñ)Ð*:¸DÓAˆØ×#Ñ# JÓ/ˆØ×"Ñ" 9Ó-ˆØ×#Ñ# J°Ó5ˆØ(×,Ñ,Ð-@À$ÓGÐà�>‰>˜TÒ!Ø�T—Z‘ZÑ/ ,Ñ/°Ñ2‰Fà�T—Z‘ZÑ/ ,Ñ/ˆFÜ)3°F¼DÔ)A�V˜HÒ%ÀvÈaÁyˆFð Ø#6Ø,Ø ØØ Ø!2ñ	
ð ð	
ð 		
r)   c                 ó  — |€dg}g }|d   j                  d«      }|D �]À  }| j                  dk(  rf|d   d   j                  t        j                  t        j
                  fv r4|d   d   j                  t        j                  «      j                  «       }n|d   d   j                  «       }|d   d   }|d   d   }	|d   �|d   d   nd }
|d	   d   j                  «       }|j                  d
«      }t        j                  |dd¬«      }t        j                  ||z
  «      }||j                  dd¬«      z  }| j                  dk(  r$|	j                  «       }	|
�|
j                  «       nd }
| j                  ||	||
||||¬«      }| j                  ||«      }|D �cg c],  }|j                  dd «      |vr|j                  dd «      |vr|‘Œ. }}|j                  |«       �ŒÃ t!        |«      }|dkD  r| j#                  |«      }|S c c}w )NÚOr   rs   Úptrˆ   rp   ro   r   r‡   rr   éÿÿÿÿT)ÚaxisÚkeepdimsr@   )rr   rs   ÚentityÚentity_groupr   )r   rE   ÚdtypeÚtorchÚbfloat16Úfloat16ÚtoÚfloat32ÚnumpyÚnpr5   ÚexpÚsumÚgather_pre_entitiesÚ	aggregateÚextendr   Úaggregate_overlapping_entities)r$   Úall_outputsrL   rP   Úall_entitiesrs   Úmodel_outputsrˆ   rp   ro   r   r‡   rr   ÚmaxesÚshifted_expÚscoresÚpre_entitiesrJ   r“   Úentitiesr€   s                        r'   Úpostprocessz'TokenClassificationPipeline.postprocessn  sF  € ØÐ Ø ˜EˆMØˆð (¨™N×.Ñ.Ð/BÓCÐà(ó (	*ˆMØ�~‰~ Ò%¨-¸Ñ*AÀ!Ñ*D×*JÑ*JÌuÏ~É~Ô_d×_lÑ_lÐNmÑ*mØ& xÑ0°Ñ3×6Ñ6´u·}±}ÓE×KÑKÓM‘à& xÑ0°Ñ3×9Ñ9Ó;�à" 1‘~ jÑ1ˆHØ% kÑ2°1Ñ5ˆIà6CÐDTÑ6UÐ6a�Ð.Ñ/°Ò2Ðgkð ð #0Ð0EÑ"FÀqÑ"I×"OÑ"OÓ"QÐØ$×(Ñ(¨Ó4ˆHä—F‘F˜6¨°TÔ:ˆEÜŸ&™& ¨%¡Ó0ˆKØ  ;§?¡?¸ÀT ?Ó#JÑJˆFà�~‰~ Ò%Ø%ŸO™OÓ-�	Ø;IÐ;U ×!5Ñ!5Ô!7Ð[_�à×3Ñ3ØØØØØ#Ø$Ø!Ø"3ð 4ó 	ˆLð  $Ÿ~™~¨lÐ<PÓQÐð /öàØ—:‘:˜h¨Ó-°]ÑBØ—J‘J˜~¨tÓ4¸MÑIò ðˆHð ð ×Ñ Ö)ðQ(	*ôR ˜Ó%ˆ
Ø˜Š>Ø×>Ñ>¸|ÓLˆLØÐùòs   Æ1Hc                 ó4  — t        |«      dk(  r|S t        |d„ ¬«      }g }|d   }|D ]\  }|d   |d   cxk  r|d   k  r3n n0|d   |d   z
  }|d   |d   z
  }||kD  s||k(  sŒ;|d   |d   kD  sŒG|}ŒJ|j                  |«       |}Œ^ |j                  |«       |S )Nr   c                 ó   — | d   S )NÚstartr.   )Úxs    r'   ú<lambda>zLTokenClassificationPipeline.aggregate_overlapping_entities.<locals>.<lambda>§  s
   € °!°G±*€ r)   ©Úkeyr®   ÚendÚscore)r   Úsortedrv   )r$   rª   Úaggregated_entitiesÚprevious_entityr“   Úcurrent_lengthÚprevious_lengths          r'   r¢   z:TokenClassificationPipeline.aggregate_overlapping_entities¤  sÎ   € Üˆx‹=˜AÒØˆOÜ˜(Ñ(<Ô=ˆØ ÐØ" 1™+ˆØò 	)ˆFØ˜wÑ'¨6°'©?ÔS¸_ÈUÑ=SÕSØ!'¨¡°¸±Ñ!@�Ø"1°%Ñ"8¸?È7Ñ;SÑ"S�à" _Ò4Ø%¨Ó8Ø˜w™¨/¸'Ñ*BÓBà&,‘Oà#×*Ñ*¨?Ô;Ø"(‘ð	)ð 	×"Ñ" ?Ô3Ø"Ð"r)   rp   ro   r¨   r‡   rr   rs   c	                 ól  — g }	t        |«      D �]¢  \  }
}||
   rŒ| j                  j                  t        ||
   «      «      }|��L||
   \  }}|�|�||
   }|�||   \  }}||z  }||z  }t	        |t        «      s/| j
                  dk(  r |j                  «       }|j                  «       }||| }t        | j                  dd«      rCt        | j                  j                  j                  dd«      rt        |«      t        |«      k7  }n_|t        j                  t        j                  t        j                  hv rt        j                   dt"        «       |dkD  xr d||dz
  |dz    v}t        ||
   «      | j                  j$                  k(  r|}d	}nd}d}d	}|||||
|d
œ}|	j'                  |«       �Œ¥ |	S )zTFuse various numpy arrays into dicts with all the information needed for aggregationNr�   Ú
_tokenizerÚcontinuing_subword_prefixz?Tokenizer does not support real words, using fallback heuristicr   rO   r   F)r~   r¨   r®   r³   ÚindexÚ
is_subword)Ú	enumeraterW   Úconvert_ids_to_tokensÚintr   rE   ÚitemÚgetattrr»   r‰   r   r0   r8   r9   r:   rT   rU   ÚUserWarningÚunk_token_idrv   )r$   rp   ro   r¨   r   r‡   rL   rr   rs   r©   ÚidxÚtoken_scoresr~   Ú	start_indÚend_indÚ
word_indexÚ
start_charÚ_Úword_refr¾   Ú
pre_entitys                        r'   rŸ   z/TokenClassificationPipeline.gather_pre_entitiesº  së  € ð ˆÜ!*¨6Ó!2ó 9	,ÑˆC�à" 3Ò'Øà—>‘>×7Ñ7¼¸IÀc¹NÓ8KÓLˆDØÑ)Ø%3°CÑ%8Ñ"�	˜7ð Ð'Ð,=Ð,IØ!)¨#¡�JØ!Ð-Ø(9¸*Ñ(E™˜
 AØ! ZÑ/˜	Ø :Ñ-˜ä! )¬SÔ1Ø—~‘~¨Ò-Ø$-§N¡NÓ$4˜	Ø")§,¡,£.˜Ø# I¨gÐ6�Ü˜4Ÿ>™>¨<¸Ô>Ä7Ø—N‘N×-Ñ-×3Ñ3Ð5PÐRVôDô
 "% T£¬c°(«mÑ!;‘Jð ,Ü+×1Ñ1Ü+×3Ñ3Ü+×/Ñ/ð0ñ ô
 !Ÿ™Ø]Ü'ôð "+¨Q¡Ò!e°3¸hÀyÐSTÁ}ÐW`ÐcdÑWdÐ>eÐ3e�Jä�y ‘~Ó&¨$¯.©.×*EÑ*EÒEØ#�DØ!&‘Jà �	Ø�Ø"�
ð Ø&Ø"ØØØ(ñˆJð ×Ñ 
Ö+ðs9	,ðt Ðr)   r©   c                 óŽ  — |t         j                  t         j                  hv rlg }|D ]d  }|d   j                  «       }|d   |   }| j                  j
                  j                  |   ||d   |d   |d   |d   dœ}|j                  |«       Œf n| j                  ||«      }|t         j                  k(  r|S | j                  |«      S )Nr¨   r½   r~   r®   r³   )r“   r´   r½   r~   r®   r³   )
r0   r6   r7   Úargmaxr‰   ÚconfigÚid2labelrv   Úaggregate_wordsÚgroup_entities)r$   r©   rL   rª   rÎ   Ú
entity_idxr´   r“   s           r'   r    z%TokenClassificationPipeline.aggregate  sÝ   € ØÔ$7×$<Ñ$<Ô>Q×>XÑ>XÐ#YÑYØˆHØ*ò (�
Ø'¨Ñ1×8Ñ8Ó:�
Ø" 8Ñ,¨ZÑ8�à"Ÿj™j×/Ñ/×8Ñ8¸ÑDØ"Ø'¨Ñ0Ø& vÑ.Ø'¨Ñ0Ø% eÑ,ñ�ð —‘ Õ'ñ(ð ×+Ñ+¨LÐ:NÓOˆHàÔ#6×#;Ñ#;Ò;ØˆOØ×"Ñ" 8Ó,Ð,r)   rª   c                 ó(  — | j                   j                  |D �cg c]  }|d   ‘Œ	 c}«      }|t        j                  k(  rA|d   d   }|j	                  «       }||   }| j
                  j                  j                  |   }nó|t        j                  k(  rLt        |d„ ¬«      }|d   }|j	                  «       }||   }| j
                  j                  j                  |   }n”|t        j                  k(  rvt        j                  |D �cg c]  }|d   ‘Œ	 c}«      }t        j                  |d¬«      }	|	j	                  «       }
| j
                  j                  j                  |
   }|	|
   }nt        d«      ‚||||d   d   |d	   d
   dœ}|S c c}w c c}w )Nr~   r   r¨   c                 ó(   — | d   j                  «       S )Nr¨   )r5   )r“   s    r'   r°   z<TokenClassificationPipeline.aggregate_word.<locals>.<lambda>!  s   € ¸&ÀÑ:J×:NÑ:NÓ:P€ r)   r±   )r‘   zInvalid aggregation_strategyr®   r�   r³   )r“   r´   r~   r®   r³   )rW   Úconvert_tokens_to_stringr0   r8   rÐ   r‰   rÑ   rÒ   r:   r5   r9   rœ   ÚstackÚnanmeanr#   )r$   rª   rL   r“   r~   r¨   rÆ   r´   Ú
max_entityÚaverage_scoresrÕ   Ú
new_entitys               r'   Úaggregate_wordz*TokenClassificationPipeline.aggregate_word  s|  € Ø�~‰~×6Ñ6ÐU]Ö7^È6¸¸v»Ò7^Ó_ˆØÔ#6×#<Ñ#<Ò<Ø˜a‘[ Ñ*ˆFØ—-‘-“/ˆCØ˜3‘KˆEØ—Z‘Z×&Ñ&×/Ñ/°Ñ4‰FØ!Ô%8×%<Ñ%<Ò<Ü˜XÑ+PÔQˆJØ Ñ)ˆFØ—-‘-“/ˆCØ˜3‘KˆEØ—Z‘Z×&Ñ&×/Ñ/°Ñ4‰FØ!Ô%8×%@Ñ%@Ò@Ü—X‘X¸hÖG°F˜v hÓ/ÒGÓHˆFÜŸZ™Z¨°QÔ7ˆNØ'×.Ñ.Ó0ˆJØ—Z‘Z×&Ñ&×/Ñ/°
Ñ;ˆFØ" :Ñ.‰EäÐ;Ó<Ð<àØØØ˜a‘[ Ñ)Ø˜B‘< Ñ&ñ
ˆ
ð Ðùò7 8_ùò Hs   šF
ÄFc                 ó>  — |t         j                  t         j                  hv rt        d«      ‚g }d}|D ]C  }|€|g}Œ	|d   r|j	                  |«       Œ |j	                  | j                  ||«      «       |g}ŒE |�!|j	                  | j                  ||«      «       |S )zú
        Override tokens from a given word that disagree to force agreement on word boundaries.

        Example: micro|soft| com|pany| B-ENT I-NAME I-ENT I-ENT will be rewritten with first strategy as microsoft|
        company| B-ENT I-ENT
        z;NONE and SIMPLE strategies are invalid for word aggregationNr¾   )r0   r6   r7   r#   rv   rÞ   )r$   rª   rL   Úword_entitiesÚ
word_groupr“   s         r'   rÓ   z+TokenClassificationPipeline.aggregate_words7  sº   € ð  Ü×$Ñ$Ü×&Ñ&ð$
ñ 
ô ÐZÓ[Ð[àˆØˆ
Øò 	&ˆFØÐ!Ø$˜X‘
Ø˜Ò%Ø×!Ñ! &Õ)à×$Ñ$ T×%8Ñ%8¸ÐEYÓ%ZÔ[Ø$˜X‘
ð	&ð Ð!Ø× Ñ  ×!4Ñ!4°ZÐAUÓ!VÔWØÐr)   c                 ó@  — |d   d   j                  dd«      d   }t        j                  |D �cg c]  }|d   ‘Œ	 c}«      }|D �cg c]  }|d   ‘Œ	 }}t        j                  |«      | j                  j                  |«      |d   d   |d   d	   d
œ}|S c c}w c c}w )zª
        Group together the adjacent tokens with the same entity predicted.

        Args:
            entities (`dict`): The entities predicted by the pipeline.
        r   r“   ú-r   r�   r´   r~   r®   r³   )r”   r´   r~   r®   r³   )Úsplitrœ   rÚ   ÚmeanrW   rØ   )r$   rª   r“   r¨   Útokensr”   s         r'   Úgroup_sub_entitiesz.TokenClassificationPipeline.group_sub_entitiesS  sª   € ð ˜!‘˜XÑ&×,Ñ,¨S°!Ó4°RÑ8ˆÜ—‘¸8ÖD°˜V G›_ÒDÓEˆØ/7Ö8 V�&˜“.Ð8ˆÐ8ð #Ü—W‘W˜V“_Ø—N‘N×;Ñ;¸FÓCØ˜a‘[ Ñ)Ø˜B‘< Ñ&ñ
ˆð Ðùò EùÚ8s   ¯BÁBÚentity_namec                 ó‚   — |j                  d«      rd}|dd  }||fS |j                  d«      rd}|dd  }||fS d}|}||fS )NzB-ÚBr   zI-ÚI)Ú
startswith)r$   rè   ÚbiÚtags       r'   Úget_tagz#TokenClassificationPipeline.get_tagh  sk   € Ø×!Ñ! $Ô'ØˆBØ˜a˜b�/ˆCð �3ˆwˆð ×#Ñ# DÔ)ØˆBØ˜a˜b�/ˆCð �3ˆwˆð ˆBØˆCØ�3ˆwˆr)   c                 óh  — g }g }|D ]†  }|s|j                  |«       Œ| j                  |d   «      \  }}| j                  |d   d   «      \  }}||k(  r|dk7  r|j                  |«       Œd|j                  | j                  |«      «       |g}Œˆ |r |j                  | j                  |«      «       |S )z³
        Find and group together the adjacent tokens with the same entity predicted.

        Args:
            entities (`dict`): The entities predicted by the pipeline.
        r“   r�   rê   )rv   rï   rç   )	r$   rª   Úentity_groupsÚentity_group_disaggr“   rí   rî   Úlast_biÚlast_tags	            r'   rÔ   z*TokenClassificationPipeline.group_entitiesv  sÍ   € ð ˆØ Ðàò 	/ˆFÙ&Ø#×*Ñ*¨6Ô2Øð —l‘l 6¨(Ñ#3Ó4‰GˆB�Ø $§¡Ð-@ÀÑ-DÀXÑ-NÓ OÑˆG�Xà�hŠ 2¨¢9à#×*Ñ*¨6Õ2ð ×$Ñ$ T×%<Ñ%<Ð=PÓ%QÔRØ'- hÑ#ð'	/ñ( à× Ñ  ×!8Ñ!8Ð9LÓ!MÔNàÐr)   )NNNNNFNNr_   )NN)'r*   r+   r,   r-   Údefault_input_namesÚ_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   rC   r   Úboolr0   r   r   rÁ   r    r\   r   r   rŠ   r(   r   r…   rŒ   r6   r«   r¢   rœ   ÚndarrayrŸ   r    rÞ   rÓ   rç   rï   rÔ   Ú__classcell__)rI   s   @r'   r=   r=   B   s  ø„ ñF"ðH &Ðà€OØ!ÐØ#ÐØ€Oá#EÓ#Gõ 
(ð Ø+/Ø*.Ø>BØ:>Ø$)Ø $Ø#'ñN9ð # 4™.ðN9ð " $™ð	N9ð
 'Ð':Ñ;ðN9ð !  e¨C°¨H¡oÑ!6Ñ7ðN9ð "ðN9ð ˜‘ðN9ð ˜C‘=óN9ð` ØO˜sÐO¨cÐO°d¸4ÀÀSÀ¹>Ñ6JÒOó ØOàØ[˜t C™yÐ[°CÐ[¸DÀÀdÈ3ÐPSÈ8ÁnÑAUÑ<VÒ[ó Ø[ð%2Ø˜C  c¡˜NÑ+ð%2Ø7:ð%2à	ˆt�D˜˜c˜‘NÑ# T¨$¨t°C¸°H©~Ñ*>Ñ%?Ð?Ñ	@õ%2óN9òv
ð4 =P×<TÑ<TÐdhó 4òl#ð< 37Ø=AñGàðGð —:‘:ðGð —
‘
ð	Gð
 !  e¨C°¨H¡oÑ!6Ñ7ðGð  ŸZ™ZðGð 2ðGð ˜4 ¨¡Ñ.Ñ/ðGð $ D¨¨s°C¨x©Ñ$9Ñ:ðGð 
ˆd‰óGðR- d¨4¡jð -ÐH[ð -Ð`dÐeiÑ`jó -ð, t¨D¡zð ÐI\ð Ðaeó ð<¨¨T©
ð ÐJ]ð ÐbfÐgkÑbló ð8¨4°©:ð ¸$ó ð* 3ð ¨5°°c°©?ó ð# t¨D¡zð #°d¸4±j÷ #r)   r=   ) r!   rT   Útypingr   r   r   r   r›   rœ   Úmodels.bert.tokenization_bertr   Úutilsr	   r
   r   r   Úbaser   r   r   r   Ú
tensorflowr@   Úmodels.auto.modeling_tf_autor   r–   Úmodels.auto.modeling_autor   r   r0   r=   ÚNerPipeliner.   r)   r'   ú<module>r     sš   ðÛ Û ß 1Ó 1ã å :÷ó ÷ TÓ Sñ ÔÛå^ÙÔÛåXôF¨ô Fô:˜,ô ñ Ù¨4Ô0ðnó!ôDu -ó uóE!ðDuðp *�r)   