ó <¿CVc@slddlmZddlZddlmZdefd„ƒYZddlm Z defd„ƒYZ dS( iÿÿÿÿ(tprint_functionN(tFeaturetBrillTemplateIcBs eZdZd„Zd„ZRS(sÏ An interface for generating lists of transformational rules that apply at given sentence positions. ``BrillTemplateI`` is used by ``Brill`` training algorithms to generate candidate rules. cCs t‚dS(sú Return a list of the transformational rules that would correct the *i*th subtoken's tag in the given token. In particular, return a list of zero or more rules that would change *tokens*[i][1] to *correctTag*, if applied to *token*[i]. If the *i*th token already has the correct tag (i.e., if tagged_tokens[i][1] == correctTag), then ``applicable_rules()`` should return the empty list. :param tokens: The tagged tokens being tagged. :type tokens: list(tuple) :param i: The index of the token whose tag should be corrected. :type i: int :param correctTag: The correct tag for the *i*th token. :type correctTag: any :rtype: list(BrillRule) N(tNotImplementedError(tselfttokenstit correctTag((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pytapplicable_rulesscCs t‚dS(s¬ Returns the set of indices *i* such that ``applicable_rules(token, i, ...)`` depends on the value of the *index*th token of *token*. This method is used by the "fast" Brill tagger trainer. :param token: The tokens being tagged. :type token: list(tuple) :param index: The index whose neighborhood should be returned. :type index: int :rtype: set N(R(Rttokentindex((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pytget_neighborhood.s(t__name__t __module__t__doc__RR (((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyRs (tRuletTemplatecBsteZdZgZd„Zd„Zd„Zd„Zd„Ze d e d„ƒZ e d„ƒZ e d„ƒZRS( sÊ A tbl Template that generates a list of L{Rule}s that apply at a given sentence position. In particular, each C{Template} is parameterized by a list of independent features (a combination of a specific property to extract and a list C{L} of relative positions at which to extract it) and generates all Rules that: - use the given features, each at its own independent position; and - are applicable to the given token. cGs·td„|Dƒƒr"||_nft|dtƒr|td„|dDƒƒr|g|dD]}|d|Œ^qZ|_n tdƒ‚djt|jƒƒ|_|jj |ƒdS(s„ Construct a Template for generating Rules. Takes a list of Features. A C{Feature} is a combination of a specific property and its relative positions and should be a subclass of L{nltk.tbl.feature.Feature}. An alternative calling convention (kept for backwards compatibility, but less expressive as it only permits one feature type) is Template(Feature, (start1, end1), (start2, end2), ...) In new code, that would be better written Template(Feature(start1, end1), Feature(start2, end2), ...) #For instance, importing some features >>> from nltk.tbl.template import Template >>> from nltk.tag.brill import Word, Pos #create some features >>> wfeat1, wfeat2, pfeat = (Word([-1]), Word([1,2]), Pos([-2,-1])) #Create a single-feature template >>> Template(wfeat1) Template(Word([-1])) #or a two-feature one >>> Template(wfeat1, wfeat2) Template(Word([-1]),Word([1, 2])) #or a three-feature one with two different feature types >>> Template(wfeat1, wfeat2, pfeat) Template(Word([-1]),Word([1, 2]),Pos([-2, -1])) #deprecated api: Feature subclass, followed by list of (start,end) pairs #(permits only a single Feature) >>> Template(Word, (-2,-1), (0,0)) Template(Word([-2, -1]),Word([0])) #incorrect specification raises TypeError >>> Template(Word, (-2,-1), Pos, (0,0)) Traceback (most recent call last): File "", line 1, in File "nltk/tag/tbl/template.py", line 143, in __init__ raise TypeError( TypeError: expected either Feature1(args), Feature2(args), ... or Feature, (start1, end1), (start2, end2), ... :type features: list of Features :param features: the features to build this Template on css|]}t|tƒVqdS(N(t isinstanceR(t.0tf((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pys ‡sicss|]}t|tƒVqdS(N(Rttuple(Rta((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pys ‰siscexpected either Feature1(args), Feature2(args), ... or Feature, (start1, end1), (start2, end2), ...s{0:03d}N( tallt _featurest issubclassRt TypeErrortformattlent ALLTEMPLATEStidtappend(Rtfeaturesttp((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyt__init__Qs6 -- cCs9d|jjdjg|jD]}t|ƒ^qƒfS(Ns%s(%s)t,(t __class__R tjoinRtstr(RR((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyt__repr__‘scCsv||d|krgS|j||ƒ}ttj|Œƒ}g|D],}t|j||d|t|ƒƒ^qFS(Ni(t_applicable_conditionstlisttittproductRRR(RRR t correct_tagtapplicable_conditionstxstx((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyR”s cCs˜g}x‹|jD]€}|jgƒxj|jD]_}d||koTt|ƒkns_q-n|j|||ƒ}|dj||fƒq-WqW|S(sn :returns: A set of all conditions for rules that are applicable to C{tokens[index]}. iiÿÿÿÿ(RRt positionsRtextract_property(RRR t conditionstfeaturetpostvalue((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyR'¡s &c Cs¸t|gƒ}dgg|jD]}|jD] }|^q,q}t|ƒt|ƒ}}td|| ƒ} t|| dt|ƒƒ} x$t| | ƒD]} |j| ƒqW|S(Nii(tsetRR/tmintmaxRtrangetadd( RRR t neighborhoodtfeattpt allpositionststarttendtsteR((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyR ±s3c #sï‡fd†}tƒ}xÐ||ƒD]Â}x¹tj|ŒD]¨‰t‡fd†tˆƒDƒƒriq;n|r—t‡fd†tˆƒDƒƒr—q;n|tˆƒŒ}t|ƒ}||krÑ|jƒq;n|j|ƒ|Vq;Wq%WdS(sÞ Factory method to mass generate Templates from a list L of lists of Features. #With combinations=(k1, k2), the function will in all possible ways choose k1 ... k2 #of the sublists in L; it will output all Templates formed by the Cartesian product #of this selection, with duplicates and other semantically equivalent #forms removed. Default for combinations is (1, len(L)). The feature lists may have been specified manually, or generated from Feature.expand(). For instance, >>> from nltk.tbl.template import Template >>> from nltk.tag.brill import Word, Pos #creating some features >>> (wd_0, wd_01) = (Word([0]), Word([0,1])) >>> (pos_m2, pos_m33) = (Pos([-2]), Pos([3-2,-1,0,1,2,3])) >>> list(Template.expand([[wd_0], [pos_m2]])) [Template(Word([0])), Template(Pos([-2])), Template(Pos([-2]),Word([0]))] >>> list(Template.expand([[wd_0, wd_01], [pos_m2]])) [Template(Word([0])), Template(Word([0, 1])), Template(Pos([-2])), Template(Pos([-2]),Word([0])), Template(Pos([-2]),Word([0, 1]))] #note: with Feature.expand(), it is very easy to generate more templates #than your system can handle -- for instance, >>> wordtpls = Word.expand([-2,-1,0,1], [1,2], excludezero=False) >>> len(wordtpls) 7 >>> postpls = Pos.expand([-3,-2,-1,0,1,2], [1,2,3], excludezero=True) >>> len(postpls) 9 #and now the Cartesian product of all non-empty combinations of two wordtpls and #two postpls, with semantic equivalents removed >>> templates = list(Template.expand([wordtpls, wordtpls, postpls, postpls])) >>> len(templates) 713 will return a list of eight templates Template(Word([0])), Template(Word([0, 1])), Template(Pos([-2])), Template(Pos([-1])), Template(Pos([-2]),Word([0])), Template(Pos([-1]),Word([0])), Template(Pos([-2]),Word([0, 1])), Template(Pos([-1]),Word([0, 1]))] #Templates where one feature is a subset of another, such as #Template(Word([0,1]), Word([1]), will not appear in the output. #By default, this non-subset constraint is tightened to disjointness: #Templates of type Template(Word([0,1]), Word([1,2]) will also be filtered out. #With skipintersecting=False, then such Templates are allowed WARNING: this method makes it very easy to fill all your memory when training generated templates on any real-world corpus :param featurelists: lists of Features, whose Cartesian product will return a set of Templates :type featurelists: list of (list of Features) :param combinations: given n featurelists: if combinations=k, all generated Templates will have k features; if combinations=(k1,k2) they will have k1..k2 features; if None, defaults to 1..n :type combinations: None, int, or (int, int) :param skipintersecting: if True, do not output intersecting Templates (non-disjoint positions for some feature) :type skipintersecting: bool :returns: generator of Templates cs…ˆ}|dkr(dtˆƒdfn4t|tƒrG||dfn|d|ddf}tjj‡fd†t|ŒDƒƒS(Niic3s!|]}tjˆ|ƒVqdS(N(R)t combinations(Rtr(R-(sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pys s(tNoneRRtintR)tchaint from_iterableR8(R-tkt combrange(RB(R-sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pytnonempty_powerset s "c3sI|]?\}}tˆƒD]&\}}||ko=|j|ƒVqqdS(N(t enumeratet issuperset(RRR.tjty(tpick(sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pys s c3sI|]?\}}tˆƒD]&\}}||ko=|j|ƒVqqdS(N(RKt intersects(RRR.RMRN(RO(sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pys s N( R5R)R*tanyRKtsortedR%t _poptemplateR9( tclst featurelistsRBtskipintersectingRJt seentemplatestpickst thistemplatetstrpick((RBROsc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pytexpandÂs"K     cCs g|_dS(N(R(RT((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyt_cleartemplates+scCs|jr|jjƒSdS(N(RtpopRD(RT((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyRS/sN(R R RRR!R&RR'R t classmethodRDtTrueR[R\RS(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyRBs  @   h( t __future__Rt itertoolsR)tnltk.tbl.featureRtobjectRt nltk.tbl.ruleRR(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/tbl/template.pyt s  /