JL i+lddlmZmZddlmZGddeZej GddeZy) )ABCMetaabstractmethod)jsontagsc@eZdZdZdZd dZedZdZdZ dZ 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"||_ ||_yN) original_tagreplacement_tag)selfr r s S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/tbl/rule.py__init__zTagRule.__init__ s(B.FNc|ttt|}|Dcgc]}|j||s|}}|D]}||d|jf||<|Scc}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)listrangelenappliesr )r tokens positionsichanges r applyz TagRule.apply'sy"  U3v;/0I'B$,,vq*A!BB  =A1t';';    99;rr) metaclassc`eZdZdZdZdZdZedZdZ dZ dZ d Z d Z d Zd Zd Zy)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.RulecNtj|||||_||_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)rr _conditions templateid)r r2r r conditionss r rz Rule.__init__ss%" |_=%$rc`|j|j|j|jdS)N)r2original replacementr3)r2r r r1r's r encode_json_objzRule.encode_json_objs.//))//**   rc P||d|d|dtd|dDS)Nr2r5r6c32K|]}t|ywr tuple).0feats r z'Rule.decode_json_obj..s<$%+<r3r:)clsobjs r decode_json_objzRule.decode_json_objs9    O   <#l*;< <   rc||d|jk7ry|jD]K\}}|jD]6}d||zcxkrt|ksn|j |||z|k(s6Jyy)NFrT)r r1rrextract_property)r rrfeaturevalposs r rz Rule.appliess %= t00 0!,, LGS(( US[63v;6++FECK@CG   rc||uxsp|duxrj|j|jk(xrO|j|jk(xr4|j|jk(xr|j|jk(Sr ) __class__r r r1r s r r"z Rule.__eq__su}   64>>1 6!!U%7%77 6$$(=(== 6  E$5$55  rc||k( Sr rr s r r%z Rule.__ne__sEM""rc |jS#t$r(tt||_|jcYSwxYwr ) _Rule__hashAttributeErrorhashreprr's r r(z Rule.__hash__s: ;;  tDz*DK;;  s .??c N |jS#t$rdj|jj|j t |jt |jdjd|jD|_|jcYSwxYw)Nz{}('{}', {}, {}, [{}])z, c3FK|]\}}d|dt|dyw)(,)N)rP)r<fvs r r>z Rule.__repr__..s'L!QAaS$q'!,Ls!) _Rule__reprrNformatrJr)r2rPr r joinr1r's r __repr__z Rule.__repr__s ;;  299''T&&'T))* L4;K;KLLDK;;  s BB$#B$c d}dj|jDcgc]\}}|||c}}}|jd|jd|}|Scc}}w)Nc|dj|j|djd|jDS)z| Return a compact, predicate-logic styled string representation of the given condition. z {}:{}@[{}]rTc32K|]}t|ywr )str)r<ws r r>z._condition_to_logic..s;AQ;r?)rY PROPERTY_NAMErZr)rFvalues r _condition_to_logicz)Rule.__str__.._condition_to_logics;  &&%%;):):;; rz & z-> if )rZr1r r )r rcrVrWr3ss r __str__z Rule.__str__sh ZZ595E5E F6Aq A & F   !D$8$8#9j\ J GsA c|dk(r|jS|dk(r|jS|dk(r|jStd|)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 "", line 1, in 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_rPverbosezunknown rule format spec: )rfr[_verbose_format ValueError)r fmts r rYz Rule.formatsUF %<<<> ! F]==? " I '') )9#?@ @rcfdd|jd|j}|jrdnddjfd|jDz}||zS)z Return a wordy, human-readable string representation of the given rule. Not sure how useful this is. c^dj|j|j|S)Nzthe {} of {} is "{}")rYrar)rFrb range_to_strs r condition_to_strz.Rule._verbose_format..condition_to_strs0)00%%W../ rc(t|dk(r-|d}|dk(ry|dk(ry|dk(ry|dkrd| zS|dkDrd|zSyt|}t|}||z t|dz k(rd ||fzSd jd j d |DS) NrDrz this wordzthe preceding wordzthe following wordz word i-%dz word i+%dzwords i%+d...i%+dz words {{{}}}rTc3&K|] }d|z yw)zi%+dNr)r<ds r r>z=Rule._verbose_format..range_to_str..8s ?! ?s)rmaxminrYrZ)rpmxmns r rnz*Rule._verbose_format..range_to_str#s9~"aL6&7/!V/U&!++U&?*^^7c)nq00."b99)00 ?Y ??rz -> rdz, and c36K|]\}}||ywr r)r<rVrWros r r>z'Rule._verbose_format..<s$J '-1 Q "J s)r r r1rZ)r r6r3rorns @@r rizRule._verbose_formatsn  0**+40D0D/EF $ 0 0fbHMMJ 151A1AJ =  Z''rN)r)r*r+r,json_tagrr7 classmethodrBrr"r%r(r[rfrYrirrr r/r/]sV$H%*   , #$&*AX+(rr/N)abcrrnltkrr register_tagr/rrr rs@( H;H;V a(7a(a(r