
    g+                     l    d dl mZmZ d dlmZ  G d de      Zej                   G d de             Zy)	    )ABCMetaabstractmethod)jsontagsc                   @    e Zd ZdZd Zd	dZed        Zd Zd Z	d Z
y)
TagRulea  
    An interface for tag transformations on a tagged corpus, as
    performed by tbl taggers.  Each transformation finds all tokens
    in the corpus that are tagged with a specific original tag and
    satisfy a specific condition, and replaces their tags with a
    replacement tag.  For any given transformation, the original
    tag, replacement tag, and condition are fixed.  Conditions may
    depend on the token under consideration, as well as any other
    tokens in the corpus.

    Tag rules must be comparable and hashable.
    c                 "    || _         	 || _        y N)original_tagreplacement_tag)selfr
   r   s      B/var/www/openai/venv/lib/python3.12/site-packages/nltk/tbl/rule.py__init__zTagRule.__init__    s    (B.F    Nc                     |t        t        t        |                  }|D cg c]  }| j                  ||      s| }}|D ]  }||   d   | j                  f||<    |S c c}w )a  
        Apply this rule at every position in positions where it
        applies to the given sentence.  I.e., for each position p
        in *positions*, if *tokens[p]* is tagged with this rule's
        original tag, and satisfies this rule's condition, then set
        its tag to be this rule's replacement tag.

        :param tokens: The tagged sentence
        :type tokens: list(tuple(str, str))
        :type positions: list(int)
        :param positions: The positions where the transformation is to
            be tried.  If not specified, try it at all positions.
        :return: The indices of tokens whose tags were changed by this
            rule.
        :rtype: int
        r   )listrangelenappliesr   )r   tokens	positionsichanges        r   applyzTagRule.apply'   su    " U3v;/0I 'BY$,,vq*A!YB
 A1t';';<F1I   Cs
   A"A"c                      y)a  
        :return: True if the rule would change the tag of
            ``tokens[index]``, False otherwise
        :rtype: bool
        :param tokens: A tagged sentence
        :type tokens: list(str)
        :param index: The index to check
        :type index: int
        N )r   r   indexs      r   r   zTagRule.appliesF   s    r   c                     t        d      )NzRules must implement __eq__()	TypeErrorr   others     r   __eq__zTagRule.__eq__S       788r   c                     t        d      )NzRules must implement __ne__()r   r    s     r   __ne__zTagRule.__ne__V   r#   r   c                     t        d      )NzRules must implement __hash__()r   r   s    r   __hash__zTagRule.__hash__Y   s    9::r   r	   )__name__
__module____qualname____doc__r   r   r   r   r"   r%   r(   r   r   r   r   r      s5    G> 	 	99;r   r   )	metaclassc                   `    e Zd ZdZdZd Zd Zed        Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zy)Rulea!  
    A Rule checks the current corpus position for a certain set of conditions;
    if they are all fulfilled, the Rule is triggered, meaning that it
    will change tag A to tag B. For other tags than A, nothing happens.

    The conditions are parameters to the Rule instance. Each condition is a feature-value pair,
    with a set of positions to check for the value of the corresponding feature.
    Conceptually, the positions are joined by logical OR, and the feature set by logical AND.

    More formally, the Rule is then applicable to the M{n}th token iff:

      - The M{n}th token is tagged with the Rule's original tag; and
      - For each (Feature(positions), M{value}) tuple:

        - The value of Feature of at least one token in {n+p for p in positions}
          is M{value}.
    znltk.tbl.Rulec                 N    t         j                  | ||       || _        || _        y)ak  
        Construct a new Rule that changes a token's tag from
        C{original_tag} to C{replacement_tag} if all of the properties
        specified in C{conditions} hold.

        :param templateid: the template id (a zero-padded string, '001' etc,
            so it will sort nicely)
        :type templateid: string

        :param conditions: A list of Feature(positions),
            each of which specifies that the property (computed by
            Feature.extract_property()) of at least one
            token in M{n} + p in positions is C{value}.
        :type conditions: C{iterable} of C{Feature}

        N)r   r   _conditions
templateid)r   r2   r
   r   
conditionss        r   r   zRule.__init__s   s%    " 	|_=%$r   c                 `    | j                   | j                  | j                  | j                  dS )N)r2   originalreplacementr3   )r2   r
   r   r1   r'   s    r   encode_json_objzRule.encode_json_obj   s.    //))//**	
 	
r   c           
      P     | |d   |d   |d   t        d |d   D                    S )Nr2   r5   r6   c              3   2   K   | ]  }t        |        y wr	   tuple).0feats     r   	<genexpr>z'Rule.decode_json_obj.<locals>.<genexpr>   s     <*;$%+*;   r3   r:   )clsobjs     r   decode_json_objzRule.decode_json_obj   s9    
O<#l*;<<	
 	
r   c                     ||   d   | j                   k7  ry| j                  D ]K  \  }}|j                  D ]6  }d||z   cxk  rt        |      k  sn |j	                  |||z         |k(  s6 J  y y)N   Fr   T)r
   r1   r   r   extract_property)r   r   r   featurevalposs         r   r   zRule.applies   s     %=t000 !,,LGS((US[63v;6++FECK@CG	 )  - r   c                     | |u xsp |d uxrj |j                   | j                   k(  xrO | j                  |j                  k(  xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r	   )	__class__r
   r   r1   r    s     r   r"   zRule.__eq__   s    u} 
 64>>16!!U%7%776 $$(=(==6   E$5$55	
r   c                     | |k(   S r	   r   r    s     r   r%   zRule.__ne__   s    EM""r   c                     	 | j                   S # t        $ r( t        t        |             | _         | j                   cY S w xY wr	   )_Rule__hashAttributeErrorhashreprr'   s    r   r(   zRule.__hash__   s:    	;; 	tDz*DK;;	s    .??c                 N   	 | j                   S # t        $ r dj                  | j                  j                  | j
                  t        | j                        t        | j                        dj                  d | j                  D                    | _         | j                   cY S w xY w)Nz{}('{}', {}, {}, [{}])z, c              3   F   K   | ]  \  }}d | dt        |       d  yw)(,)N)rP   )r<   fvs      r   r>   z Rule.__repr__.<locals>.<genexpr>   s*     L;K!QAaS$q'!,;Ks   !)_Rule__reprrN   formatrJ   r)   r2   rP   r
   r   joinr1   r'   s    r   __repr__zRule.__repr__   s    	;; 	299''T&&'T))* 		L4;K;KLLDK ;;	s    BB$#B$c           
          d }dj                  | j                  D cg c]  \  }} |||       c}}      }| j                   d| j                   d| }|S c c}}w )Nc                 |    dj                  | j                  |dj                  d | j                  D                    S )z|
            Return a compact, predicate-logic styled string representation
            of the given condition.
            z
{}:{}@[{}]rT   c              3   2   K   | ]  }t        |        y wr	   )str)r<   ws     r   r>   z<Rule.__str__.<locals>._condition_to_logic.<locals>.<genexpr>   s     ;):AQ):r?   )rY   PROPERTY_NAMErZ   r   )rF   values     r   _condition_to_logicz)Rule.__str__.<locals>._condition_to_logic   s;    
  &&%%;):):;; r   z & z-> if )rZ   r1   r
   r   )r   rc   rV   rW   r3   ss         r   __str__zRule.__str__   sn    		 ZZ595E5EF5E6Aq A&5EF

   !D$8$8#9j\J	 Gs   A
c                     |dk(  r| j                         S |dk(  r| j                         S |dk(  r| j                         S t        d|       )a  
        Return a string representation of this rule.

        >>> from nltk.tbl.rule import Rule
        >>> from nltk.tag.brill import Pos

        >>> r = Rule("23", "VB", "NN", [(Pos([-2,-1]), 'DT')])

        r.format("str") == str(r)
        True
        >>> r.format("str")
        'VB->NN if Pos:DT@[-2,-1]'

        r.format("repr") == repr(r)
        True
        >>> r.format("repr")
        "Rule('23', 'VB', 'NN', [(Pos([-2, -1]),'DT')])"

        >>> r.format("verbose")
        'VB -> NN if the Pos of words i-2...i-1 is "DT"'

        >>> r.format("not_found")
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "nltk/tbl/rule.py", line 256, in format
            raise ValueError("unknown rule format spec: {0}".format(fmt))
        ValueError: unknown rule format spec: not_found
        >>>

        :param fmt: format specification
        :type fmt: str
        :return: string representation
        :rtype: str
        r_   rP   verbosezunknown rule format spec: )rf   r[   _verbose_format
ValueError)r   fmts     r   rY   zRule.format   sU    F %<<<>!F]==?"I''))9#?@@r   c                     fdd | j                    d| j                   }| j                  rdnddj                  fd| j                  D              z   }||z   S )z
        Return a wordy, human-readable string representation
        of the given rule.

        Not sure how useful this is.
        c                 ^    dj                  | j                   | j                        |      S )Nzthe {} of {} is "{}")rY   ra   r   )rF   rb   range_to_strs     r   condition_to_strz.Rule._verbose_format.<locals>.condition_to_str  s0    )00%%W../ r   c                 (   t        |       dk(  r-| d   }|dk(  ry|dk(  ry|dk(  ry|dk  rd| z  S |dkD  rd|z  S y t        |       }t        |       }||z
  t        |       dz
  k(  rd	||fz  S d
j                  dj	                  d | D                    S )NrD   r   z	this wordzthe preceding wordzthe following wordz	word i-%dz	word i+%dzwords i%+d...i%+dzwords {{{}}}rT   c              3   &   K   | ]	  }d |z    yw)zi%+dNr   )r<   ds     r   r>   z=Rule._verbose_format.<locals>.range_to_str.<locals>.<genexpr>8  s      ?Y!Ys   )r   maxminrY   rZ   )r   pmxmns       r   rn   z*Rule._verbose_format.<locals>.range_to_str#  s    9~"aL6&7/!V/U&!++U&?*  ^^7c)nq00."b99)00 ?Y ?? r   z -> rd    z, and c              3   6   K   | ]  \  }} ||        y wr	   r   )r<   rV   rW   ro   s      r   r>   z'Rule._verbose_format.<locals>.<genexpr><  s#      J
1Av1Q"1As   )r
   r   r1   rZ   )r   r6   r3   ro   rn   s      @@r   ri   zRule._verbose_format  sn    		0 **+40D0D/EF $ 0 0fbHMM J
151A1AJ
 =
 

 Z''r   N)r)   r*   r+   r,   json_tagr   r7   classmethodrB   r   r"   r%   r(   r[   rf   rY   ri   r   r   r   r/   r/   ]   sV    $ H%*
 
 
,
#$&*AX+(r   r/   N)abcr   r   nltkr   r   register_tagr/   r   r   r   <module>r      s@    ( H; H;V 
a(7 a( a(r   