
    i85              	          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
mZ d dl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 dd
lmZ ddlmZmZ ddlmZmZ ddlm Z  ddlm!Z! ddl"m#Z# ddl$m%Z% eeee&e'f   ef      Z(ee'eee(   e(ee&f   f   Z)ee'eee&e'f   ee&e'f   f   f   Z*ee'ee'eee&e'f   ee&e'f   f   f   f   Z+dee    dee'ef   fdZ,d Z- G d de%      Z.de/de
e/e/f   fdZ0d Z1y)    N)Path)AnyCallableDictIterableListOptionalTupleUnion   )util)Errors)Language)Matcher)Scorer)IDS)DocSpan)normalize_token_attrsset_token_attrs)Example)SimpleFrozenList)Vocab   )Pipeexamplesreturnc                    d }i }|j                  t        j                  | dfi |       |j                  t        j                  | dfi |       |j                  t        j                  | dfd|i|       |j                  t        j                  | dfd|i|       |j                  t        j                  | dfi |       |S )Nc                 .    t        | |      j                  S N)getattrkey)tokenattrs     n/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/spacy/pipeline/attributeruler.pymorph_key_getterz/attribute_ruler_score.<locals>.morph_key_getter   s    ud#'''    tagposmorphgetterlemma)updater   score_token_attrscore_token_attr_per_feat)r   kwargsr&   resultss       r%   attribute_ruler_scorer2      s    ( GNN6**8UEfEFNN6**8UEfEFNN'U:JUfU NN((g	
&6	
:@	

 NN6**8WGGHNr'   c                      t         S r    )r2    r'   r%   make_attribute_ruler_scorerr5   /   s      r'   c                   F   e Zd ZdZ	 d%deddedededee	   dd	f
d
Z
d&dZd	d	d	d	ddee	g ee   f      dee   deee      dee   dee   dd	fdZdedefdZdefdZd Zdeeeeeef   eeef   f   f   dd	fdZdeeeeeeeef   eeef   f   f   f   dd	fdZ	 d'dee   dededd	fdZdee   dd	fdZede e   fd       Z! e"       fdee   de#fdZ$ e"       fd e#dee   dd fd!Z% e"       fd"ee&ef   dee   dd	fd#Z' e"       fd"ee&ef   dee   dd fd$Z(y	)(AttributeRulerzSet token-level attributes for tokens matched by Matcher patterns.
    Additionally supports importing patterns from tag maps and morph rules.

    DOCS: https://spacy.io/api/attributeruler
    F)validatescorervocabnamer8   r9   r   Nc                    || _         || _        t        | j                  |      | _        || _        g | _        g | _        g | _        || _        y)a  Create the AttributeRuler. After creation, you can add patterns
        with the `.initialize()` or `.add_patterns()` methods, or load patterns
        with `.from_bytes()` or `.from_disk()`. Loading patterns will remove
        any patterns you've added previously.

        vocab (Vocab): The vocab.
        name (str): The pipe name. Defaults to "attribute_ruler".
        scorer (Optional[Callable]): The scoring method. Defaults to
            Scorer.score_token_attr for the attributes "tag", "pos", "morph" and
            "lemma" and Scorer.score_token_attr_per_feat for the attribute
            "morph".

        RETURNS (AttributeRuler): The AttributeRuler component.

        DOCS: https://spacy.io/api/attributeruler#init
        r8   N)	r;   r:   r   matcherr8   attrs_attrs_unnormedindicesr9   )selfr:   r;   r8   r9   s        r%   __init__zAttributeRuler.__init__:   sH    0 	
tzzH= !#
+-"$r'   c                 z    t        | j                  | j                        | _        g | _        g | _        g | _        y)zReset all patterns.r=   N)r   r:   r8   r>   r?   r@   rA   rB   s    r%   clearzAttributeRuler.clear[   s.    tzzDMMB
!r'   )nlppatternstag_mapmorph_rulesget_examplesrG   rH   rI   rJ   c                    | j                          |r| j                  |       |r| j                  |       |r| j                  |       yy)a  Initialize the attribute ruler by adding zero or more patterns.

        Rules can be specified as a sequence of dicts using the `patterns`
        keyword argument. You can also provide rules using the "tag map" or
        "morph rules" formats supported by spaCy prior to v3.
        N)rF   add_patternsload_from_tag_mapload_from_morph_rules)rB   rK   rG   rH   rI   rJ   s         r%   
initializezAttributeRuler.initializeb   sE     	

h'""7+&&{3 r'   docc                     | j                         }	 | j                  |      }| j                  ||       |S # t        $ r } || j                  | |g|      cY d}~S d}~ww xY w)zApply the AttributeRuler to a Doc and set all attribute exceptions.

        doc (Doc): The document to process.
        RETURNS (Doc): The processed Doc.

        DOCS: https://spacy.io/api/attributeruler#call
        N)get_error_handlermatchset_annotations	Exceptionr;   )rB   rQ   error_handlermatcheses        r%   __call__zAttributeRuler.__call__y   s`     ..0	<jjoG  g.J 	< D3%;;	<s   $7 	A  AA A c           	          | j                  |dd      }|D cg c],  \  }}}t        | j                  j                  |         |||f. }}}}|j	                          |S c c}}}w )NTF)allow_missingas_spans)r>   intr:   stringssort)rB   rQ   rX   m_idsrY   s         r%   rT   zAttributeRuler.match   sr    ,,s$,G &
 
a ##D)*D!Q7
 
 	
s   1A"c                    |D ]I  \  }}}}t        ||||      }| j                  |   }| j                  |   }		 ||	   }
t        ||	   |       K y# t        $ rn t	        t
        j                  j                  | j                  j                  |j                        |D cg c]  }|j                   nc c}w c}|	            dw xY w)zModify the document in place)label)rH   spanindexN)r   r?   rA   
IndexError
ValueErrorr   E1001formatr>   getrd   textr   )rB   rQ   rX   attr_idmatch_idstartendre   r?   rf   r#   ts               r%   rU   zAttributeRuler.set_annotations   s    -4 	0)GXucUCx8DJJw'ELL)E U DK/%	0  	 !LL''!%!1!1$**!=.23aff33# (  	s   AACB32Cc                    |j                         D ]  \  }}d|ig}t        |      \  }}d|vrB| j                  j                  j	                  |      }| j                  j
                  |   |d<   nD| j                  j                  j	                  |d         }| j                  j
                  |   |d<   | j	                  |g|        y)zLoad attribute ruler patterns from a tag map.

        tag_map (dict): The tag map that maps fine-grained tags to
            coarse-grained tags and morphological features.

        DOCS: https://spacy.io/api/attributeruler#load_from_morph_rules
        TAGMORPHN)items_split_morph_attrsr:   
morphologyaddr_   )rB   rI   r(   r?   patternmorph_attrsr*   s          r%   rN   z AttributeRuler.load_from_tag_map   s     "--/ 		'JCs|nG!3E!:E;e#

--11+>!%!3!3E!:g

--11%.A!%!3!3E!:gHHgY&		'r'   c                    |D ]  }||   D ]  }||dg}||   |   }t        |      \  }}d|v rE| j                  j                  j                  |d         }| j                  j                  |   |d<   nC|rA| j                  j                  j                  |      }| j                  j                  |   |d<   | j                  |g|         y)a+  Load attribute ruler patterns from morph rules.

        morph_rules (dict): The morph rules that map token text and
            fine-grained tags to coarse-grained tags, lemmas and morphological
            features.

        DOCS: https://spacy.io/api/attributeruler#load_from_morph_rules
        )ORTHrs   rt   N)rv   r:   rw   rx   r_   )rB   rJ   r(   wordry   r?   rz   r*   s           r%   rO   z$AttributeRuler.load_from_morph_rules   s      	+C#C( 
+$(56#C(.%7%>"{e# JJ1155eGnEE%)ZZ%7%7%>E'N  JJ1155kBE%)ZZ%7%7%>E'N'E*
+	+r'   r?   rf   c                    t        t        | j                              }| j                  j	                  | j
                  j                  j	                  |      |       | j                  j                  |       t        | j
                  |      }| j                  j                  |       | j                  j                  |       y)aB  Add Matcher patterns for tokens that should be modified with the
        provided attributes. The token at the specified index within the
        matched span will be assigned the attributes.

        patterns (Iterable[List[Dict]]): A list of Matcher patterns.
        attrs (Dict): The attributes to assign to the target token in the
            matched span.
        index (int): The index of the token in the matched span to modify. May
            be negative to index from the end of the span. Defaults to 0.

        DOCS: https://spacy.io/api/attributeruler#add
        N)strlenr?   r>   rx   r:   r_   r@   appendr   rA   )rB   rH   r?   rf   r"   s        r%   rx   zAttributeRuler.add   s    " #djj/"++//4h?##E*%djj%8

% E"r'   c                 6    |D ]  } | j                   di |  y)ac  Add patterns from a list of pattern dicts with the keys as the
        arguments to AttributeRuler.add.
        patterns (Iterable[dict]): A list of pattern dicts with the keys
            as the arguments to AttributeRuler.add (patterns/attrs/index) to
            add as patterns.

        DOCS: https://spacy.io/api/attributeruler#add_patterns
        Nr4   )rx   )rB   rH   ps      r%   rM   zAttributeRuler.add_patterns   s"      	ADHHMqM	r'   c                    g }t        t        | j                              D ]c  }i }| j                  j	                  t        |            d   |d<   | j                  |   |d<   | j                  |   |d<   |j                  |       e |S )zAll the added patterns.r   rH   r?   rf   )	ranger   r?   r>   rk   r   r@   rA   r   )rB   all_patternsir   s       r%   rH   zAttributeRuler.patterns   s     s4::' 	#AA LL,,SV4Q7AjM--a0AgJaAgJ"	# r'   excludec                 X     i } fd|d<    fd|d<   t        j                  |      S )zSerialize the AttributeRuler to a bytestring.

        exclude (Iterable[str]): String names of serialization fields to exclude.
        RETURNS (bytes): The serialized object.

        DOCS: https://spacy.io/api/attributeruler#to_bytes
        c                  <    j                   j                         S N)r   )r:   to_bytes)r   rB   s   r%   <lambda>z)AttributeRuler.to_bytes.<locals>.<lambda>  s    TZZ%8%8%8%I r'   r:   c                  B    t        j                   j                        S r    )srslymsgpack_dumpsrH   rE   s   r%   r   z)AttributeRuler.to_bytes.<locals>.<lambda>  s    (;(;DMM(J r'   rH   )r   r   )rB   r   	serializes   `` r%   r   zAttributeRuler.to_bytes  s/     	I	' J	*}}Y00r'   
bytes_datac                 T      fd} fd|d}t        j                  ||        S )a'  Load the AttributeRuler from a bytestring.

        bytes_data (bytes): The data to load.
        exclude (Iterable[str]): String names of serialization fields to exclude.
        returns (AttributeRuler): The loaded object.

        DOCS: https://spacy.io/api/attributeruler#from_bytes
        c                 N    j                  t        j                  |              y r    )rM   r   msgpack_loads)brB   s    r%   load_patternsz0AttributeRuler.from_bytes.<locals>.load_patterns  s    e11!45r'   c                 >    j                   j                  |       S r   )r:   
from_bytes)r   r   rB   s    r%   r   z+AttributeRuler.from_bytes.<locals>.<lambda>#  s    tzz44Q4H r'   r:   rH   )r   r   )rB   r   r   r   deserializes   ` `  r%   r   zAttributeRuler.from_bytes  s/    	6 I%
 	
K9r'   pathc                 N      fd fdd}t        j                  ||       y)zSerialize the AttributeRuler to disk.

        path (Union[Path, str]): A path to a directory.
        exclude (Iterable[str]): String names of serialization fields to exclude.

        DOCS: https://spacy.io/api/attributeruler#to_disk
        c                 >    j                   j                  |       S r   )r:   to_diskr   r   rB   s    r%   r   z(AttributeRuler.to_disk.<locals>.<lambda>4  s    tzz11!W1E r'   c                 D    t        j                  | j                        S r    )r   write_msgpackrH   r   rB   s    r%   r   z(AttributeRuler.to_disk.<locals>.<lambda>5  s    %"5"5a"G r'   r   N)r   r   )rB   r   r   r   s   ` ` r%   r   zAttributeRuler.to_disk)  s%     FG
	 	T9g.r'   c                 T      fd} fd|d}t        j                  ||        S )a(  Load the AttributeRuler from disk.

        path (Union[Path, str]): A path to a directory.
        exclude (Iterable[str]): String names of serialization fields to exclude.
        RETURNS (AttributeRuler): The loaded object.

        DOCS: https://spacy.io/api/attributeruler#from_disk
        c                 N    j                  t        j                  |              y r    )rM   r   read_msgpackr   s    r%   r   z/AttributeRuler.from_disk.<locals>.load_patternsE  s    e0034r'   c                 >    j                   j                  |       S r   )r:   	from_diskr   s    r%   r   z*AttributeRuler.from_disk.<locals>.<lambda>I  s    tzz33Aw3G r'   r   )r   r   )rB   r   r   r   r   s   ` `  r%   r   zAttributeRuler.from_disk9  s/    	5 H%
 	t['2r'   )attribute_ruler)r   N)r   ))__name__
__module____qualname____doc__r2   r   r   boolr	   r   rC   rF   r   r   r   AttributeRulerPatternType
TagMapTypeMorphRulesTyperP   r   rZ   rT   rU   r   r   r^   rN   rO   MatcherPatternTyperx   rM   propertyr   rH   r   bytesr   r   r   r   r   r4   r'   r%   r7   r7   3   s    &
 %: 
  " 
B #'BF(,044xHW,=(=>?4 h	4
 8$=>?4 *%4 n-4 
4.<C <C <  0,'CeCHouS#X&F!GGH'	'*+T#tE#s(OU3PS8_4T/U*U%V VW+	+2 QR# !34#=A#JM#	#0
X.G%H 
T 
 	$89 	 	 1A0B 1 1u 1 ;K:L*23-	. @P?Q/$)$//7}/	/" @P?Q$)$/7}	r'   r7   r?   c                     i }i }| j                         D ]@  \  }}|dv s,|t        j                         v s|t        j                         v r|||<   <|||<   B ||fS )zSplit entries from a tag map or morph rules dict into to two dicts, one
    with the token-level features (POS, LEMMA) and one with the remaining
    features, which are presumed to be individual MORPH features._)ru   r   keysvalues)r?   other_attrsrz   kvs        r%   rv   rv   P  sf     KK 18qCHHJ!szz|*;KNKN	
 ##r'   c                 x    | dk(  r!t        j                  d      }|j                  S t        dt         d|        )Nmake_attribute_rulerzspacy.pipeline.factorieszmodule z has no attribute )	importlibimport_moduler   AttributeErrorr   )r;   modules     r%   __getattr__r   _  sA    %%(()CD***
78*,>tfE
FFr'   )2r   pathlibr   typingr   r   r   r   r   r	   r
   r   r    r   errorsr   languager   r>   r   r9   r   symbolsr   tokensr   r   tokens._retokenizer   r   trainingr   r   r:   r   piper   r^   r   r   r   r   r   r2   r5   r7   dictrv   r   r4   r'   r%   <module>r      s4     N N N         G  #  $uS#X345  t&');T3F	GG  #tE#s(OU38_<==>
c4T%S/5c?*J%K KLLMHW$5 DcN &!ZT Zz$d $uT4Z'8 $Gr'   