Ë
    T^(h?i  ã                   ó   — 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)ÚListÚOptionalÚTupleÚUnioné   )Ú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                 óÒ  — |�;t        |t        t        f«      r%t        |«      dkD  rt        |«      }t        |«      }nUt        |t        «      r|g}d}n?t
        �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 )Nr   r   zAt least one input is required.Úoffset_mappingz;offset_mapping should have the same batch size as the input)
Ú
isinstanceÚlistÚtupleÚlenÚstrr   ÚtypesÚGeneratorTypeÚ
ValueErrorÚget)Úselfr   ÚkwargsÚ
batch_sizer   s        úi/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/token_classification.pyÚ__call__z+TokenClassificationArgumentHandler.__call__    sÔ   € ØÐ¤*¨V´d¼E°]Ô"CÌÈFËÐVWÊÜ˜&“\ˆFÜ˜V›‰JÜ˜¤Ô$Ø�XˆFØ‰JÜÐ ¤Z°¼Ô%@ÄJÈvÔW\×WjÑWjÔDkØ˜4�<ÐäÐ>Ó?Ð?àŸ™Ð$4Ó5ˆÙÜ˜.¬$Ô/´J¸~ÈaÑ?PÔRWÔ4XØ"0Ð!1�Ü�>Ó" jÒ0Ü Ð!^Ó_Ð_Ø�~Ð%Ð%ó    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r&   © r'   r%   r   r      s    „ ñð&˜u S¨$¨s©) ^Ñ4ô &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%   r.   r.   5   s   „ ÙNà€DØ€FØ€EØ€GØ
�Cr'   r.   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 e«       fˆ fd„	Z	 	 	 	 	 	 d dee   dee   dee	   dee
eeef         d	ee   f
d
„Zdeee
e   f   fˆ fd„Zd!d„Zd„ Ze	j&                  dfd„Zd„ Zdedej.                  dej.                  dee
eeef         dej.                  de	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).
    Ú	sequencesc                 ó¨   •— t        ‰| �  |i |¤Ž | j                  | j                  dk(  rt        nt
        «       t        d¬«      | _        || _        y )NÚtfF)Údo_lower_case)	ÚsuperÚ__init__Úcheck_model_typeÚ	frameworkr   r   r   Ú_basic_tokenizerÚ_args_parser)r"   Úargs_parserÚargsr#   Ú	__class__s       €r%   rA   z$TokenClassificationPipeline.__init__ˆ   sN   ø€ Ü‰Ñ˜$Ð) &Ò)Ø×Ñà�~‰~ Ò%õ <ä=ô	
ô !/¸UÔ CˆÔØ'ˆÕr'   NÚgrouped_entitiesÚignore_subwordsÚaggregation_strategyr   Ústridec                 ó  — i }|�||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   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.rK   Ú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ÚpaddingrL   Útokenizer_paramszm`stride` was provided to process all the text but you're using a slow tokenizer. Please use a fast tokenizer.)r.   r6   r5   r4   ÚwarningsÚwarnr   r   Úupperr8   r7   Ú	tokenizerÚis_fastr    Úmodel_max_length)
r"   rN   rI   rJ   rK   r   rL   Úpreprocess_paramsÚpostprocess_paramsrQ   s
             r%   Ú_sanitize_parametersz0TokenClassificationPipeline._sanitize_parameters“   sã  € ð ÐØÐ%Ø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   c                 ó^   •—  | j                   |fi |¤Ž\  }}|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.

        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   )rE   r@   r&   )r"   r   r#   Ú_inputsr   rH   s        €r%   r&   z$TokenClassificationPipeline.__call__Ü   sD   ø€ ð4 #4 $×"3Ñ"3°FÑ"E¸fÑ"EÑˆ�ÙØ'5ˆFÐ#Ñ$ä‰wÑ Ñ1¨&Ñ1Ð1r'   c           
   +   ó¼  K  — |j                  di «      }| j                  j                  r| j                  j                  dkD  rdnd} | j                  |f| j                  |d| j                  j                  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<   |–— Œ¥ y c c}
}	w c c}
}	w ­w)NrQ   r   TF)Úreturn_tensorsÚ
truncationÚreturn_special_tokens_maskÚreturn_offsets_mappingÚoverflow_to_sample_mappingÚ	input_idsr>   r   Úsentencer   Úis_last)ÚpoprU   rW   rC   rV   r   ÚrangeÚitemsr>   Úexpand_dimsÚ	unsqueeze)r"   rd   r   rX   rQ   r_   r   Ú
num_chunksÚiÚkÚvÚmodel_inputss               r%   Ú
preprocessz&TokenClassificationPipeline.preprocessü   s^  è ø€ Ø,×0Ñ0Ð1CÀRÓHÐØ!Ÿ^™^×<Ò<ÀÇÁ×A`ÑA`ÐcdÒAd‘TÐjoˆ
Ø�—‘Øð
àŸ>™>Ø!Ø'+Ø#'§>¡>×#9Ñ#9ñ
ð ñ
ˆð 	�
‰
Ð/°Ô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˜Ñ#àÓñ
	ùãVùãPùs   ‚CEÃ$EÃ2EÄ	EÄ(4Ec                 óB  — |j                  d«      }|j                  dd «      }|j                  d«      }|j                  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   rd   re   r>   r   Úlogits)rs   rr   r   rd   re   r,   )rf   rC   Úmodelr   Údict)r"   ro   rr   r   rd   re   rs   Úoutputs           r%   Ú_forwardz$TokenClassificationPipeline._forward  s¼   € à*×.Ñ.Ð/DÓEÐØ%×)Ñ)Ð*:¸DÓAˆØ×#Ñ# JÓ/ˆØ×"Ñ" 9Ó-ˆØ�>‰>˜TÒ!Ø�T—Z‘ZÑ/ ,Ñ/°Ñ2‰Fà�T—Z‘ZÑ/ ,Ñ/ˆFÜ)3°F¼DÔ)A�V˜HÒ%ÀvÈaÁyˆFð Ø#6Ø,Ø Øñ
ð ð
ð 	
r'   c           	      óÄ  — |€dg}g }|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                  «       }
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ÚOÚptrs   r   rd   rc   r   rr   éÿÿÿÿT)ÚaxisÚkeepdimsr>   ÚentityÚentity_groupr   )rC   ÚdtypeÚtorchÚbfloat16Úfloat16ÚtoÚfloat32ÚnumpyÚnpr3   ÚexpÚsumÚgather_pre_entitiesÚ	aggregater!   Úextendr   Úaggregate_overlapping_entities)r"   Úall_outputsrK   rN   Úall_entitiesÚmodel_outputsrs   rd   rc   r   rr   ÚmaxesÚshifted_expÚscoresÚpre_entitiesrI   r~   Úentitiesrk   s                      r%   Úpostprocessz'TokenClassificationPipeline.postprocess+  s  € ØÐ Ø ˜EˆMØˆØ(ó  	*ˆ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Ðä—F‘F˜6¨°TÔ:ˆEÜŸ&™& ¨%¡Ó0ˆKØ  ;§?¡?¸ÀT ?Ó#JÑJˆFà�~‰~ Ò%Ø%ŸO™OÓ-�	Ø;IÐ;U ×!5Ñ!5Ô!7Ð[_�à×3Ñ3Ø˜) V¨^Ð=PÐRfóˆLð  $Ÿ~™~¨lÐ<PÓQÐð /öàØ—:‘:˜h¨Ó-°]ÑBØ—J‘J˜~¨tÓ4¸MÑIò ðˆHð ð ×Ñ Ö)ðA 	*ôB ˜Ó%ˆ
Ø˜Š>Ø×>Ñ>¸|ÓLˆLØÐùòs   Å31Gc                 ó:  — t        |«      dk(  r|S t        |d„ ¬«      }g }|d   }|D ]_  }|d   |d   cxk  r|d   k  r6n n3|d   |d   z
  }|d   |d   z
  }||kD  r|}Œ8||k(  sŒ>|d   |d   kD  sŒJ|}ŒM|j                  |«       |}Œa |j                  |«       |S )Nr   c                 ó   — | d   S )NÚstartr,   )Úxs    r%   ú<lambda>zLTokenClassificationPipeline.aggregate_overlapping_entities.<locals>.<lambda>X  s
   € °!°G±*€ r'   ©Úkeyr™   ÚendÚscore)r   ÚsortedÚappend)r"   r•   Úaggregated_entitiesÚprevious_entityr~   Úcurrent_lengthÚprevious_lengths          r%   r�   z:TokenClassificationPipeline.aggregate_overlapping_entitiesU  sÓ   € Üˆx‹=˜AÒØˆOÜ˜(Ñ(<Ô=ˆØ ÐØ" 1™+ˆØò 
	)ˆFØ˜wÑ'¨6°'©?ÔS¸_ÈUÑ=SÕSØ!'¨¡°¸±Ñ!@�Ø"1°%Ñ"8¸?È7Ñ;SÑ"S�Ø! OÒ3Ø&,‘OØ# Ó6¸6À'¹?È_Ð]dÑMeÓ;eØ&,‘Oà#×*Ñ*¨?Ô;Ø"(‘ð
	)ð 	×"Ñ" ?Ô3Ø"Ð"r'   rd   rc   r“   rr   Úreturnc                 ó2  — g }t        |«      D �]…  \  }}	||   rŒ| j                  j                  t        ||   «      «      }
|��/||   \  }}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 aggregationNrz   Ú
_tokenizerÚcontinuing_subword_prefixz?Tokenizer does not support real words, using fallback heuristicr   ú r   F)Úwordr“   r™   rž   ÚindexÚ
is_subword)Ú	enumeraterU   Úconvert_ids_to_tokensÚintr   rC   ÚitemÚgetattrr¨   rt   r   r.   r6   r7   r8   rR   rS   ÚUserWarningÚunk_token_idr¡   )r"   rd   rc   r“   r   rr   rK   r”   ÚidxÚtoken_scoresr«   Ú	start_indÚend_indÚword_refr­   Ú
pre_entitys                   r%   rŠ   z/TokenClassificationPipeline.gather_pre_entitiesi  s¬  € ð ˆÜ!*¨6Ó!2ó 0	,ÑˆC�à" 3Ò'Øà—>‘>×7Ñ7¼¸IÀc¹NÓ8KÓLˆDØÑ)Ø%3°CÑ%8Ñ"�	˜7Ü! )¬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ð ×Ñ 
Ö+ða0	,ðb Ð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ž   )
r.   r4   r5   Úargmaxrt   ÚconfigÚid2labelr¡   Úaggregate_wordsÚgroup_entities)r"   r”   rK   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“   )r3   )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ž   )rU   Úconvert_tokens_to_stringr.   r6   r¼   rt   r½   r¾   r8   r3   r7   r‡   ÚstackÚnanmeanr    )r"   r•   rK   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­   )r.   r4   r5   r    r¡   rÊ   )r"   r•   rK   Úword_entitiesÚ
word_groupr~   s         r%   r¿   z+TokenClassificationPipeline.aggregate_wordsÛ  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Æ   ÚmeanrU   rÄ   )r"   r•   r~   r“   Útokensr   s         r%   Úgroup_sub_entitiesz.TokenClassificationPipeline.group_sub_entities÷  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_tag  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Ö   )r¡   rÛ   rÓ   )	r"   r•   Úentity_groupsÚentity_group_disaggr~   rÙ   rÚ   Úlast_biÚlast_tags	            r%   rÀ   z*TokenClassificationPipeline.group_entities  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'   )NNNNNN)N)!r(   r)   r*   r+   Údefault_input_namesr   rA   r   Úboolr.   r   r   r°   rZ   r   r   r&   rp   rw   r4   r–   r�   r‡   Úndarrayru   rŠ   r‹   rÊ   r¿   rÓ   rÛ   rÀ   Ú__classcell__)rH   s   @r%   r;   r;   ?   sö  ø„ ñF"ðH &Ðá#EÓ#Gõ 	(ð Ø+/Ø*.Ø>BØ:>Ø $ñG9ð # 4™.ðG9ð " $™ð	G9ð
 'Ð':Ñ;ðG9ð !  e¨C°¨H¡oÑ!6Ñ7ðG9ð ˜‘óG9ðR2˜u S¨$¨s©) ^Ñ4õ 2ó@ò4
ð* =P×<TÑ<TÐdhó (òT#ð(<àð<ð —:‘:ð<ð —
‘
ð	<ð
 !  e¨C°¨H¡oÑ!6Ñ7ð<ð  ŸZ™Zð<ð 2ð<ð 
ˆd‰ó<ð|- 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   rR   Ú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   r.   r;   ÚNerPipeliner,   r'   r%   ú<module>rí      s˜   ðÛ Û ß /Ó /ã å :÷ó ÷ TÓ Sñ ÔÛå^ÙÔÛåXô&¨ô &ô4˜,ô ñ Ù¨4Ô0ðnó!ôD\ -ó \óE!ðD\ð~ *�r'   