JL idZddlZddlZddlmZddlmZmZddlm Z m Z m Z m Z m Z eGdde ZdZd ZdOd ZGd d eeZGd deeZdPdZdZdPdZdZdPdZdZ dQdZdZdZdPdZdZ GddZ!e!Z" dRdZ#Gdde$Z%d Z&d!Z'd"Z(d#Z)d$Z*d%Z+d&Z,d'Z-d(Z.d)Z/d*Z0d+Z1dSd,Z2d-Z3d.Z4d/Z5Gd0d1e Z6Gd2d3e6e7Z8Gd4d5e6e9Z:Gd6d7e6e9Z;Gd8d9e6e7ZGd=d>e>Z?Gd?d@e>Z@e?dAddABZAe>dCdDEZBeGdFdGZCGdHdIZDdTdJZEdUdKZFdUdLZGeHdMk(reGgdNZIy)VaY Basic data classes for representing feature structures, and for performing basic operations on those feature structures. A feature structure is a mapping from feature identifiers to feature values, where each feature value is either a basic value (such as a string or an integer), or a nested feature structure. There are two types of feature structure, implemented by two subclasses of ``FeatStruct``: - feature dictionaries, implemented by ``FeatDict``, act like Python dictionaries. Feature identifiers may be strings or instances of the ``Feature`` class. - feature lists, implemented by ``FeatList``, act like Python lists. Feature identifiers are integers. Feature structures are typically used to represent partial information about objects. A feature identifier that is not mapped to a value stands for a feature whose value is unknown (*not* a feature without a value). Two feature structures that represent (potentially overlapping) information about the same object can be combined by unification. When two inconsistent feature structures are unified, the unification fails and returns None. Features can be specified using "feature paths", or tuples of feature identifiers that specify path through the nested feature structures to a value. Feature structures may contain reentrant feature values. A "reentrant feature value" is a single feature value that can be accessed via multiple feature paths. Unification preserves the reentrance relations imposed by both of the unified feature structures. In the feature structure resulting from unification, any modifications to a reentrant feature value will be visible using any of its feature paths. Feature structure variables are encoded using the ``nltk.sem.Variable`` class. The variables' values are tracked using a bindings dictionary, which maps variables to their values. When two feature structures are unified, a fresh bindings dictionary is created to track their values; and before unification completes, all bound variables are replaced by their values. Thus, the bindings dictionaries are usually strictly internal to the unification process. However, it is possible to track the bindings of variables if you choose to, by supplying your own initial bindings dictionary to the ``unify()`` function. When unbound variables are unified with one another, they become aliased. This is encoded by binding one variable to the other. Lightweight Feature Structures ============================== Many of the functions defined by ``nltk.featstruct`` can be applied directly to simple Python dictionaries and lists, rather than to full-fledged ``FeatDict`` and ``FeatList`` objects. In other words, Python ``dicts`` and ``lists`` can be used as "light-weight" feature structures. >>> from nltk.featstruct import unify >>> unify(dict(x=1, y=dict()), dict(a='a', y=dict(b='b'))) # doctest: +SKIP {'y': {'b': 'b'}, 'x': 1, 'a': 'a'} However, you should keep in mind the following caveats: - Python dictionaries & lists ignore reentrance when checking for equality between values. But two FeatStructs with different reentrances are considered nonequal, even if all their base values are equal. - FeatStructs can be easily frozen, allowing them to be used as keys in hash tables. Python dictionaries and lists can not. - FeatStructs display reentrance in their string representations; Python dictionaries and lists do not. - FeatStructs may *not* be mixed with Python dictionaries and lists (e.g., when performing unification). - FeatStructs provide a number of useful methods, such as ``walk()`` and ``cyclic()``, which are not available for Python dicts and lists. In general, if your feature structures will contain any reentrances, or if you plan to use them as dictionary keys, it is strongly recommended that you use full-fledged ``FeatStruct`` objects. N)total_ordering)raise_unorderable_typesread_str) ExpressionLogicalExpressionException LogicParserSubstituteBindingsIVariableceZdZdZdZ d"fd ZdZdZdZd#dZ dZ d Z d Z d Z d Zd ZdZdZdZdZd$dZdZdZdZdZdZdZdZdZdZd%dZdZd&dZdZ d Z!d!Z"xZ#S)' FeatStructa A mapping from feature identifiers to feature values, where each feature value is either a basic value (such as a string or an integer), or a nested feature structure. There are two types of feature structure: - feature dictionaries, implemented by ``FeatDict``, act like Python dictionaries. Feature identifiers may be strings or instances of the ``Feature`` class. - feature lists, implemented by ``FeatList``, act like Python lists. Feature identifiers are integers. Feature structures may be indexed using either simple feature identifiers or 'feature paths.' A feature path is a sequence of feature identifiers that stand for a corresponding sequence of indexing operations. In particular, ``fstruct[(f1,f2,...,fn)]`` is equivalent to ``fstruct[f1][f2]...[fn]``. Feature structures may contain reentrant feature structures. A "reentrant feature structure" is a single feature structure object that can be accessed via multiple feature paths. Feature structures may also be cyclic. A feature structure is "cyclic" if there is any feature path from the feature structure to itself. Two feature structures are considered equal if they assign the same values to all features, and have the same reentrancies. By default, feature structures are mutable. They may be made immutable with the ``freeze()`` method. Once they have been frozen, they may be hashed, and thus used as dictionary keys. Fc |tur|tjtfi|St|rtjt|fi|S|r t dt |t rUtjj|rtjt|fi|Stjt|fi|St|rtjt|St dt|||fi|S)a Construct and return a new feature structure. If this constructor is called directly, then the returned feature structure will be an instance of either the ``FeatDict`` class or the ``FeatList`` class. :param features: The initial feature values for this feature structure: - FeatStruct(string) -> FeatStructReader().read(string) - FeatStruct(mapping) -> FeatDict(mapping) - FeatStruct(sequence) -> FeatList(sequence) - FeatStruct() -> FeatDict() :param morefeatures: If ``features`` is a mapping or None, then ``morefeatures`` provides additional features for the ``FeatDict`` constructor. zLKeyword arguments may only be specified if features is None or is a mapping.z&Expected string or mapping or sequence) r FeatDict__new__ _is_mapping TypeError isinstancestrFeatStructReader_START_FDICT_REmatchFeatList _is_sequencesuper)clsfeatures morefeatures __class__s U/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/featstruct.pyrzFeatStruct.__new__s* * ''ALAAX&''(KlKK;(C(#3399(C#++HhO,OO#++HhO,OOh'''(;; HII7?3ALA Act)zNReturn an iterable of the feature identifiers used by this FeatStruct.NotImplementedErrorselfs r_keyszFeatStruct._keys "##rct)zUReturn an iterable of the feature values directly defined by this FeatStruct.r!r#s r_valueszFeatStruct._valuesr&rct)zReturn an iterable of (fid,fval) pairs, where fid is a feature identifier and fval is the corresponding feature value, for all features defined by this FeatStruct.r!r#s r_itemszFeatStruct._itemss "##rc\|j||tttS)aB Return True if ``self`` and ``other`` assign the same value to to every feature. In particular, return true if ``self[p]==other[p]`` for every feature path *p* such that ``self[p]`` or ``other[p]`` is a base value (i.e., not a nested feature structure). :param check_reentrance: If True, then also return False if there is any difference between the reentrances of ``self`` and ``other``. :note: the ``==`` is equivalent to ``equal_values()`` with ``check_reentrance=True``. _equalset)r$othercheck_reentrances r equal_valueszFeatStruct.equal_valuess"{{5"2CE35#%HHrc\|j|dtttS)a Return true if ``self`` and ``other`` are both feature structures, assign the same values to all features, and contain the same reentrances. I.e., return ``self.equal_values(other, check_reentrance=True)``. :see: ``equal_values()`` Tr,r$r/s r__eq__zFeatStruct.__eq__s!{{5$suce<>**U__-E-EE Et9s5z) )rc|js td |jS#t$r-|j t |_|jcYSwxYw)zu If this feature structure is frozen, return its hash value; otherwise, raise ``TypeError``. z5FeatStructs must be frozen before they can be hashed.)_frozenr_hashAttributeError_calculate_hashvaluer.r#s r__hash__zFeatStruct.__hash__ sS ||VW W ::  22359DJ::  s %3AAc||ury|j|jk7ryt|t|k7ryt|jt|jk7ry|r2t ||vs t ||vr1t |t |f|vSt |t |f|vry|j t ||j t ||j t |t |f|j D]9\}}||}t|tr|j|||||r1y||k7s9yy)a Return True iff self and other have equal values. :param visited_self: A set containing the ids of all ``self`` feature structures we've already visited. :param visited_other: A set containing the ids of all ``other`` feature structures we've already visited. :param visited_pairs: A set containing ``(selfid, otherid)`` pairs for all pairs of feature structures we've already visited. TF) rr<r.r%idaddr*rr r-) r$r/r0 visited_self visited_other visited_pairsfname self_fval other_fvals rr-zFeatStruct._equalsI 5= >>U__ , t9E " tzz| EKKM 2 2 $x<'2e9 +E4"U), ==4"U)$ 5 D""U)$2d8RY/0!% ! E9uJ)Z0 ''$ !! ! *  ! rcNt||vry|jt|d}t|jD]^\}}|dz}|t |z }|dz}t |t r||j|z }n|t |z }t|dz}`|S)z Return a hash value for this feature structure. :require: ``self`` must be frozen. :param visited: A set containing the ids of all feature structures we've already visited while hashing. i%i) rErFsortedr*hashrr rBint)r$visitedhashvalrJfvals rrBzFeatStruct._calculate_hashvalue]s d8w  BtH!$++-0 0KE4 rMG tE{ "G rMG$ +444W==4:%'J./G 0r'Frozen FeatStructs may not be modified.cP|jry|jty)a Make this feature structure, and any feature structures it contains, immutable. Note: this method does not attempt to 'freeze' any feature value that is not a ``FeatStruct``; it is recommended that you use only immutable feature values. N)r?_freezer.r#s rfreezezFeatStruct.freeze~s <<  SUrc|jS)a$ Return True if this feature structure is immutable. Feature structures can be made immutable with the ``freeze()`` method. Immutable feature structures may not be made mutable again, but new mutable copies can be produced with the ``copy()`` method. )r?r#s rfrozenzFeatStruct.frozens||rct||vry|jt|d|_t|j D]'\}}t |t s|j|)y)z Make this feature structure, and any feature structure it contains, immutable. :param visited: A set containing the ids of all feature structures we've already visited while freezing. NT)rErFr?rPr*rr rX)r$rSrJrUs rrXzFeatStruct._freezes^ d8w   BtH !$++-0 &KE4$ + W% &rcR|rtj|S|j|S)z Return a new copy of ``self``. The new copy will not be frozen. :param deep: If true, create a deep copy; if false, create a shallow copy. )copydeepcopyr)r$deeps rr^zFeatStruct.copys% ==& &>>$' 'rctr6r!)r$memos r __deepcopy__zFeatStruct.__deepcopy__s !##rc<|jit|S)zH Return True if this feature structure contains itself. )_find_reentrancesrEr#s rcycliczFeatStruct.cyclics%%b)"T(33rc4|jtS)z Return an iterator that generates this feature structure, and each feature structure it contains. Each feature structure will be generated exactly once. )_walkr.r#s rwalkzFeatStruct.walks zz#%  rct)z Return an iterator that generates this feature structure, and each feature structure it contains. :param visited: A set containing the ids of all feature structures we've already visited while freezing. r!)r$rSs rrhzFeatStruct._walks "##rc#Kt||vry|jt|||jD],}t|ts|j |Ed{.y7wr6)rErFr(rr rh)r$rSrUs rrhzFeatStruct._walks^ d8w   BtH LLN /D$ +::g... /.sAA1A1'A/(A1ct||vrd|t|<|Sd|t|<|jD]$}t|ts|j |&|S)z Return a dictionary that maps from the ``id`` of each feature structure contained in ``self`` (including ``self``) to a boolean value, indicating whether it is reentrant or not. TF)rEr(rr re)r$ reentrancesrUs rrezFeatStruct._find_reentrancessm d8{ "$(K4 !%*K4 !  8dJ/**;7 8rct||S)z/:see: ``nltk.featstruct.substitute_bindings()``)substitute_bindingsr$bindingss rrozFeatStruct.substitute_bindingss"422rct||S)z,:see: ``nltk.featstruct.retract_bindings()``)retract_bindingsrps rrszFeatStruct.retract_bindingssh//rct|S)z*:see: ``nltk.featstruct.find_variables()``)find_variablesr#s r variableszFeatStruct.variabless d##rct||||S)z,:see: ``nltk.featstruct.rename_variables()``)rename_variables)r$vars used_varsnew_varss rrxzFeatStruct.rename_variablessdIx@@rct|S)z Return the feature structure that is obtained by deleting any feature whose value is a ``Variable``. :rtype: FeatStruct )remove_variablesr#s rr}zFeatStruct.remove_variabless %%rc"t||||||Sr6unify)r$r/rqtracefail rename_varss rrzFeatStruct.unifysT5(E4EErct||S)z Return True if ``self`` subsumes ``other``. I.e., return true If unifying ``self`` with ``other`` would result in a feature structure equal to ``other``. )subsumesr3s rrzFeatStruct.subsumess e$$rcD|j|jiiS)z Display a single-line representation of this feature structure, suitable for embedding in other representations. )_reprrer#s r__repr__zFeatStruct.__repr__#s zz$004b99rct)a Return a string representation of this feature structure. :param reentrances: A dictionary that maps from the ``id`` of each feature value in self, indicating whether that value is reentrant or not. :param reentrance_ids: A dictionary mapping from each ``id`` of a feature value to a unique identifier. This is modified by ``repr``: the first time a reentrant feature value is displayed, an identifier is added to ``reentrance_ids`` for it. r!)r$rmreentrance_idss rrzFeatStruct._repr*s "##rr6F)T)Nr7N)NFNT)$r; __module__ __qualname____doc__r?rr%r(r*r1r4r8r=rCr-rB _FROZEN_ERRORrYr[rXr^rcrfrirhrerorsrvrxr}rrrr __classcell__)rs@rr r ns@G+Bh$ $ $I =!* EN>>M &( ($4 !$/030$A&F%: $rr rVz' %sIf self is frozen, raise ValueError.crfd}j|_jxsdt|zz|_|S)z Given a method function, return a new method function that first checks if ``self._frozen`` is true; and if so, raises ``ValueError`` with an appropriate message. Otherwise, call the method and return its result. cR|jrtt|g|i|Sr6)r? ValueErrorr)r$argskwargsmethods rwrappedz_check_frozen..wrappedFs* <<]+ +$000 0r)r;r_FROZEN_NOTICE)rindentrs` r _check_frozenr>s51 G~~+0GHGO NrceZdZdZddZdZdZddZdZdZ d Z d Z e e jZe e jZe e j Ze e j"Zdd Zd Zd ZdZdZdZdZdZy)ra A feature structure that acts like a Python dictionary. I.e., a mapping from feature identifiers to feature values, where a feature identifier can be a string or a ``Feature``; and where a feature value can be either a basic value (such as a string or an integer), or a nested feature structure. A feature identifiers for a ``FeatDict`` is sometimes called a "feature name". Two feature dicts are considered equal if they assign the same values to all features, and have the same reentrances. :see: ``FeatStruct`` for information about feature paths, reentrance, cyclic feature structures, mutability, freezing, and hashing. Nc t|tr-tj|||jdi|y|j|fi|y)a3 Create a new feature dictionary, with the specified features. :param features: The initial value for this feature dictionary. If ``features`` is a ``FeatStruct``, then its features are copied (shallow copy). If ``features`` is a dict, then a feature is created for each item, mapping its key to its value. If ``features`` is a string, then it is processed using ``FeatStructReader``. If ``features`` is a list of tuples ``(name, val)``, then a feature is created for each tuple. :param morefeatures: Additional features for the new feature dictionary. If a feature is listed under both ``features`` and ``morefeatures``, then the value from ``morefeatures`` will be used. Nr7)rrr fromstringupdate)r$rrs r__init__zFeatDict.__init__fsG h $   ) )(D 9 DKK ', ' DKK 1L 1rz'Expected feature name or path. Got %r.c>t|ttfrtj ||St|t r' |}|D]}t|t st||}|St|j|z#ttf$r}t||d}~wwxYw)zkIf the feature with the given name or path exists, return its value; otherwise, raise ``KeyError``.N) rrFeaturedict __getitem__tupler KeyError IndexErrorr _INDEX_ERRORr$ name_or_pathvalfides rrzFeatDict.__getitem__s lS'N 3##D,7 7  e , 4'#C%c:6&c(C# D-- <= =j) 4|,!3 4s%A<<B BBc0 ||S#t$r|cYSwxYw)zkIf the feature with the given name or path exists, return its value; otherwise, return ``default``.r)r$rdefaults rgetz FeatDict.gets&  % % N s  c. ||y#t$rYywxYw)>l3B/0!&*5"<00<+,D-- <= =rcl|jrttt|tt frt j|||St|trEt|dk(r td||dd}t|ts t||||d<yt|j|zzSet the value for the feature with the given name or path to ``value``. If ``name_or_path`` is an invalid path, raise ``KeyError``.rrNr)r?rrrrrr __setitem__rr<r rrrr$rvaluers rrzFeatDict.__setitem__s <<]+ + lS'N 3##D,> >  e ,< A% !=>>l3B/0!&*5"<00+0|B'(D-- <= =rc |jrtt|d}nLt|dr&t |j r|j }nt|dr|}n td|D]+\}}t |ttfs td|||<-|j D]+\}}t |ttfs td|||<-y)Nr7items__iter__z"Expected mapping or list of tupleszFeature names must be strings) r?rrhasattrcallablerrrrr)r$rrrkeyrs rrzFeatDict.updates <<]+ +  E Xw 'HX^^,DNN$E Xz *EAB B HCcC>2 ?@@DI %**, HCcC>2 ?@@DI rc|jx|t|<}|jD]2\}}tj|||tj||<4|Sr6)rrEr*r^r_)r$rbselfcopyrrs rrczFeatDict.__deepcopy__s[$(NN$44RX  JHC15sD1IHT]]3- . Jrc"|jSr6)keysr#s rr%zFeatDict._keyssyy{rc"|jSr6)valuesr#s rr(zFeatDict._valuess{{}rc"|jSr6)rr#s rr*zFeatDict._itemsszz|rcbdj|j|jiiS)zz Display a multi-line representation of this feature dictionary as an FVM (feature value matrix).  )join_strrer#s r__str__zFeatDict.__str__s) yy4#9#9"#=rBCCrc Vg}d}d}|t|r2t||vsJtt|dz|t|<t|j D]\}}t |dd}t||vr$|j |d|t|dE|dk(r|st|ttfrd|z}h|dk(r1|s/t|trd |jz}d t|z}t|tr!|j |d |j|d ur|j d |z|d ur|j d|zt|tr|j |d|d+t|ts!|j |d t|\|j||} |j |d | |t|rd|t|d|}dj|dj||S)NrrNdisplayz->()prefix%sslashz/%s=Tz+%sFz-%sz=<>(z{}[{}]{}, )rEreprr<rPrgetattrappendrr rnamerr rformatr) r$rmrsegmentsrsuffixrJrUr fval_reprs rrzFeatDict._repr s r$x d8>1 11'+C,?!,C'DN2d8 $"$**,/ 8KE4eY5G$x>)5'^BtH-E,Fa HI8#Fz$SV7XG#FdH-"TYY.F"T$Z/FD(+5'499+ 67 . .D*-5'D6 34j15'4:, 78 JJ{NC 5'9+ 673 86 r$x 412!F8z FeatDict._str..Ps=A#dQh-=srz = z = (rrr z =Nc32K|]}t|ywr6r)rlines rrz FeatDict._str..s14SY1sz[ {}{} ]z(%s) )rErr<maxrrPrljustrr rrrrrrrpopr)r$rmr maxfnamelenlinesrJrUr fval_lineslnamelinemaxlenridstridlines rrz FeatDict._str5s" r$x d8>1 11'+C,?!,C'DN2d8 $ t9>2d8$!N2d8$<<==v === "$**,/+ !KE4E\((5E$) wc$))56D*- wd4&23D(+ JJ{NC  wc$y/):;<h/ wc$t*67D^+ weN2d8,D+EQGH U2Y"_LL$"YY{NC FPPskAo6!;P P Oa/A5DL:h#7 a8I#JJ8$ # R W+ !\ 9? IIK1511QVW""4T0B)CDWW r$x nRX66E5:;cCJ&!+;E;%j1n*F!E&M#e*,$??E&M ;Q(X $$>$>& $** %E  !CDLL)Gt/J2D*FX\rrceZdZdZd dZdZdZdZdZe e jZ e e jZ e e jZ e e jZe e jZe e j Ze e j"Ze e j$Ze e j&ZdZdZd Zd Zd Zy )ray A list of feature values, where each feature value is either a basic value (such as a string or an integer), or a nested feature structure. Feature lists may contain reentrant feature values. A "reentrant feature value" is a single feature value that can be accessed via multiple feature paths. Feature lists may also be cyclic. Two feature lists are considered equal if they assign the same values to all features, and have the same reentrances. :see: ``FeatStruct`` for information about feature paths, reentrance, cyclic feature structures, mutability, freezing, and hashing. ct|trtj||ytj ||y)aZ Create a new feature list, with the specified features. :param features: The initial list of features for this feature list. If ``features`` is a string, then it is paresd using ``FeatStructReader``. Otherwise, it should be a sequence of basic values and nested feature structures. N)rrrrlistr)r$rs rrzFeatList.__init__s/ h $   ) )(D 9 MM$ )rz&Expected int or feature path. Got %r.c2t|trtj||St|tr' |}|D]}t|t st ||}|St|j|z#t tf$r}t ||d}~wwxYwr6) rrRrrrr rrrrrs rrzFeatList.__getitem__s lC (##D,7 7  e , 4'#C%c:6&c(C# D-- <= =j) 4|,!3 4s%A66B BBcf|jrttt|tt frt j||St|trCt|dk(r td||dd}t|ts t|||d=yt|j|zr)r?rrrrRslicerrrr<r rrrrs rrzFeatList.__delitem__s <<]+ + lS%L 1##D,7 7  e ,< A% !=>>l3B/0!&*5"<00<+,D-- <= =rcl|jrttt|tt frt j|||St|trEt|dk(r td||dd}t|ts t||||d<yt|j|zr)r?rrrrRrrrrr<r rrrrs rrzFeatList.__setitem__s <<]+ + lS%L 1##D,> >  e ,< A% !=>>l3B/0!&*5"<00+0|B'(D-- <= =rcx|jxt|<}|jfd|D|S)Nc3JK|]}tj|ywr6)r^r_)rrUrbs rrz(FeatList.__deepcopy__..sCd dD1Cs #)rrEextend)r$rbrs ` rrczFeatList.__deepcopy__s3$(NN$44RXCdCCrc<ttt|Sr6)rranger<r#s rr%zFeatList._keys sE#d)$%%rc|Sr6r7r#s rr(zFeatList._valuess rct|Sr6) enumerater#s rr*zFeatList._itemss rc|t|rDt||vsJtt|dz|t|<d|t|z}nd}g}|D]}t||vr!|jd|t|z1t |t r|j|j ]t |tr|jd|zt |tr"|j|j|||jdt|zdj|dj|S)NrN(%s)rz->(%s)rz{}[{}]r) rErr<rrr rrr rrr)r$rmrrrrUs rrzFeatList._reprs r$x d8>1 11'+C,?!,C'DN2d8 $nRX66FF 3D$x>)>"T(+C CDD(+ *D*-t ,D*- ; GHtDz 12 3vtyy':;;rN)r7)r;rrrrrrrrrr__iadd____imul__rr insertrremovereversesortrcr%r(r*rr7rrrrs *" >$>*T]]+HT]]+H 4;; 'F 4;; 'F 4;; 'F  !C 4;; 'FDLL)G  #D & any) :param bindings: A dictionary mapping from variables to values. r)_default_fs_classr^r__substitute_bindingsr.)fstructrqfs_classs rroro8s99$W-mmG$G(Hce< Nrct||vry|jt|t|r|j}n"t |r t |}n t d|D]x\}}t|tr#||vr||x}||<t|tr||vrt||rt||||Tt|tse|j|||<zyNExpected mapping or sequence) rErFrrrrrrr rr ro)rrqrrSrrJrUs rrrJs '{g KK7 7  g '"788@ tx(TX-=$,TN 2D75>x(TX-= dH % x7 C 1 2!55h?GEN @rc |dk(r t|}tj||f\}}|j||j Dcic]\}}t ||}}}t |||t|Scc}}w)a Return the feature structure that is obtained by replacing each feature structure value that is bound by ``bindings`` with the variable that binds it. A feature structure value must be identical to a bound value (i.e., have equal id) to be replaced. ``bindings`` is modified to point to this new feature structure, rather than the original feature structure. Feature structure values in ``bindings`` may be modified if they are contained in ``fstruct``. r)rr^r_rrrE_retract_bindingsr.)rrqr new_bindingsvarr inv_bindingss rrsrs_s{9$W-"mmWh,?@Wl OOL!3;>>3CDZc3BsGSLDLDg|Xsu= NEsBcVt||vry|jt|t|r|j}n"t |r t |}n t d|D]>\}}t||st||vr|t|||<t||||@yr) rErFrrrrrrr")rr%rrSrrJrUs rr"r"ts '{g KK7 7  g '"788E t dH %$x<'!-bh!7 dL(G D Erc^|dk(r t|}t|t|tS)za :return: The set of variables used by this feature structure. :rtype: set(Variable) r)r _variablesr.rrs rrurus+ 9$W- gsuh 66rct||vry|jt|t|r|j}n"t |r t |}n t d|D]r\}}t|tr|j|(t||rt||||Ct|tsT|j|jt|Sr) rErFrrrrrrr r(r rrv)rryrrSrrJrUs rr(r(s '{g KK7 7  g '"788* t dH % HHTN h ' tT8W 5 1 2 KK( ) * Krc |dk(r t|}|i}| t||}n t|}t||j|}t t j |||||tS)a Return the feature structure that is obtained by replacing any of this feature structure's variables that are in ``vars`` with new variables. The names for these new variables will be names that are not used by any variable in ``vars``, or in ``used_vars``, or in this feature structure. :type vars: set :param vars: The set of variables that should be renamed. If not specified, ``find_variables(fstruct)`` is used; i.e., all variables will be given new names. :type used_vars: set :param used_vars: A set of variables whose names should not be used by the new variables. :type new_vars: dict(Variable -> Variable) :param new_vars: A dictionary that is used to hold the mapping from old variables to new variables. For each variable *v* in this feature structure: - If ``new_vars`` maps *v* to *v'*, then *v* will be replaced by *v'*. - If ``new_vars`` does not contain *v*, but ``vars`` does contain *v*, then a new entry will be added to ``new_vars``, mapping *v* to the new variable that is used to replace it. To consistently rename the variables in a set of feature structures, simply apply rename_variables to each one, using the same dictionary: >>> from nltk.featstruct import FeatStruct >>> fstruct1 = FeatStruct('[subj=[agr=[gender=?y]], obj=[agr=[gender=?y]]]') >>> fstruct2 = FeatStruct('[subj=[agr=[number=?z,gender=?y]], obj=[agr=[number=?z,gender=?y]]]') >>> new_vars = {} # Maps old vars to alpha-renamed vars >>> fstruct1.rename_variables(new_vars=new_vars) [obj=[agr=[gender=?y2]], subj=[agr=[gender=?y2]]] >>> fstruct2.rename_variables(new_vars=new_vars) [obj=[agr=[gender=?y2, number=?z2]], subj=[agr=[gender=?y2, number=?z2]]] If new_vars is not specified, then an empty dictionary is used. r)rrur.union_rename_variablesr^r_)rryrzr{rs rrxrxs~X9$W- |gx04yw177 BI  gi8SU rc t||vry|jt|t|r|j}n"t |r t |}n t d|D]\}}t|tr>||vr ||||<#||vs(t||||<||||<|j||Tt||rt||||||qt|ts|jD]/} | |vs| |vs t| ||| <|j|| 1|j|||<|Sr)rErFrrrrrrr _rename_variabler-r rvro) rryrzr{rrSrrJrUr$s rr-r-sB '{g KK7 7  g '"788@ t dH %x!)$!1$ !B!)$ htn- h ' dD)Xx Q 1 2~~' 1$;3h#6$4S)$DHSMMM(3-0 1 "55h?GEN'@( Nrctjdd|jd}}|sd}t|||vr|dz }t|||vrt||S)Nz\d+$rr?rN)resubrr )r$rzrns rr/r/slffWb#((+Q!D  dVA3< I - Q dVA3< I - tfQCL !!rcr|dk(r t|}ttj||t S)z :rtype: FeatStruct :return: The feature structure that is obtained by deleting all features whose values are ``Variables``. r)r_remove_variablesr^r_r.r)s rr}r}s0 9$W- T]]73Xsu EErcft||vry|jt|t|rt|j }n+t |rtt |}n td|D]3\}}t|tr||=t||s't|||5|Sr) rErFrrrrrrrr r6)rrrSrrJrUs rr6r6s '{g KK7 7W]]_% g Yw'(7887 t dH % h ' dHg 6 7 NrceZdZdZy)_UnificationFailurecy)Nz"nltk.featstruct.UnificationFailurer7r#s rrz_UnificationFailure.__repr__5s3rN)r;rrrr7rrr9r94s4rr9Fc |dk(r$t|}t||k7r tdt||sJt||sJ|du}|i}tj|||f\}} } |j | |r0t ||} t | |} t| | | i|ti} |r td||  t|| || |||d}|tur |y||| dSt|| |t}|r t| |t|t!|||t|r t#d||r t%d||S#t$rYywxYw)a! Unify ``fstruct1`` with ``fstruct2``, and return the resulting feature structure. This unified feature structure is the minimal feature structure that contains all feature value assignments from both ``fstruct1`` and ``fstruct2``, and that preserves all reentrancies. If no such feature structure exists (because ``fstruct1`` and ``fstruct2`` specify incompatible values for some feature), then unification fails, and ``unify`` returns None. Bound variables are replaced by their values. Aliased variables are replaced by their representative variable (if unbound) or the value of their representative variable (if bound). I.e., if variable *v* is in ``bindings``, then *v* is replaced by ``bindings[v]``. This will be repeated until the variable is replaced by an unbound variable or a non-variable value. Unbound variables are bound when they are unified with values; and aliased when they are unified with variables. I.e., if variable *v* is not in ``bindings``, and is unified with a variable or value *x*, then ``bindings[v]`` is set to *x*. If ``bindings`` is unspecified, then all variables are assumed to be unbound. I.e., ``bindings`` defaults to an empty dict. >>> from nltk.featstruct import FeatStruct >>> FeatStruct('[a=?x]').unify(FeatStruct('[b=?x]')) [a=?x, b=?x2] :type bindings: dict(Variable -> any) :param bindings: A set of variable bindings to be used and updated during unification. :type trace: bool :param trace: If true, generate trace output. :type rename_vars: bool :param rename_vars: If True, then rename any variables in ``fstruct2`` that are also used in ``fstruct1``, in order to avoid collisions on variable names. rzGMixing FeatStruct objects with Python dicts and lists is not supported.Nr7)rrrr^r_rrur-r._trace_unify_start_destructively_unify_UnificationFailureErrorUnificationFailure_apply_forwards_apply_forwards_to_bindings_resolve_aliasesr_trace_unify_succeed_trace_bindings)fstruct1fstruct2rqrrrr user_bindings fstruct1copy fstruct2copy bindings_copyvars1vars2forwardresults rrrDsj9$X. X &( 24  h )) ) h )) )D(M37-- 8X&3/\<  OOM"|X6|X6,ub(CEJG 2|\:% ,'5$RT ## < lB7 7VWh >F#GX6X8SU; R( H% M5 $s?E EEceZdZdZy)r>zmAn exception that is used by ``_destructively_unify`` to abort unification when a failure is encountered.N)r;rrrr7rrr>r>s2rr>c  ||ur|r t|||S||t|<t|rt|r|D],}t|dd|j ||j .|D],}t|dd|j ||j .t |jD],\}} ||vrt|||| |||||||fz ||<(| ||<.|St|rct|rXt|t|k7rtStt|D]"} t| || || ||||||| fz || <$|St|s t|rt|s t|rtStd)aC Attempt to unify ``fstruct1`` and ``fstruct2`` by modifying them in-place. If the unification succeeds, then ``fstruct1`` will contain the unified value, the value of ``fstruct2`` is undefined, and forward[id(fstruct2)] is set to fstruct1. If the unification fails, then a _UnificationFailureError is raised, and the values of ``fstruct1`` and ``fstruct2`` are undefined. :param bindings: A dictionary mapping variables to values. :param forward: A dictionary mapping feature structures ids to replacement structures. When two feature structures are merged, a mapping from one to the other will be added to the forward dictionary; and changes will be made only to the target of the forward dictionary. ``_destructively_unify`` will always 'follow' any links in the forward dictionary for fstruct1 and fstruct2 before actually unifying them. :param trace: If true, generate trace output :param path: The feature path that led us to this unification step. Used for trace output. rNzExpected mappings or sequences)_trace_unify_identityrErrrrrPr_unify_feature_valuesrr<r?r r) rErFrqrMrrrpathrJfval2findexs rr=r=s68  !$ 1 %GBxL8X!6 :Eui.:##E5==9 : :Eui.:##E5==9 :#8>>#34 (LE5 "7UOE8O ##( (  h L$: x=CM )% %CM* F4  y  HV   x K$9X+h"7!! 4 55rc |r t|||t||vr|t|}t||vrt||vr|t|}t||vrdx} } t|tr ||vr|} ||}t|tr||vrt|tr ||vr|} ||}t|tr||vrt||r t||rt ||||||||} n\t|trt|tr||k7r|||<|} n.t|tr |||<|} nt|tr|||<|} nt||s t||rt } nt|t r|j|||} nt|tr[|j|} t|trj| |j|k7rVtd|d|d| d|j|t|tr|j|} n||k(r|} nt } | t ur| | || <| } | | | k7r| || <| } | t ur+| ||||} |rt|dd| | t urtt| |rt| ||t} |r t|| |rt| |r t!||| S)a Attempt to unify ``fval1`` and and ``fval2``, and return the resulting unified value. The method of unification will depend on the types of ``fval1`` and ``fval2``: 1. If they're both feature structures, then destructively unify them (see ``_destructively_unify()``. 2. If they're both unbound variables, then alias one variable to the other (by setting bindings[v2]=v1). 3. If one is an unbound variable, and the other is a value, then bind the unbound variable to the value. 4. If one is a feature structure, and the other is a base value, then fail. 5. If they're both base values, then unify them. By default, this will succeed if they are equal, and fail otherwise. NzCustomFeatureValue objects z and z# disagree about unification value: z vs. r)r<rErr r=r?runify_base_valuesCustomFeatureValuerAssertionError_trace_unify_failr>r@r.rCrD) rJfval1rTrqrMrrrfpathfvar1fvar2rNs rrRrR*s& 5%/ U)w 5 " U)w  U)w 5 " U)w EE UH %%8*; UH %%8*; UH %%8*; UH %%8*; %"z%'B% 5(GUD(E  E8 $E8)D E>#HUO E8 $ E8 $ E8 $ 5((C# eW %,,UE8DF 1 2[['F%!345;;uCU9U$eVU[[-?A 1 2[['F~+ + + "( Ue^"(##  %.F  eCRj& 1 ' '* *&(# (CEB UF+ FH-x( Mrc|jD]3\}}t||vr|t|}t||vr|||<5y) Replace any feature structure that has a forward pointer with the target of its forward pointer (to preserve reentrancy). N)rrE)rMrqr$rs rrArAsO nn& Ui7"BuI&Ei7" rct||vr|t|}t||vrt||vry|jt|t|r|j}n"t |r t |}n t d|D]N\}}t||st||vr|t|}t||vr|||<t||||P|S)r`Nr ) rErFrrrrrrr@)rrMrrSrrJrUs rr@r@s W+ "W+& W+  '{g KK7 7  g '"788> t dH %T(g%r$x(T(g%!GEN D'8W => Nrc|jD];\}}t|ts||vs||x}||<t|ts7||vr =y)z Replace any bound aliased vars with their binding; and replace any unbound aliased vars with their representative var. N)rrr )rqr$rs rrBrBsT nn&4 U)ex.?$,UO 3EHSM)ex.?4rc|dk(r tdn[djd|D}tddt|dz zzdztddt|dz zzd |zztddt|zzd zt|ztddt|zzd zt|zy) Nr7z Unification trace:.c3&K|] }d|z ywrr7)rr4s rrz%_trace_unify_start..s3D1H3s | rN|z| Unify feature: %sz / z|\ )printrr<_trace_valrepr)rSr[rTfullnames rr<r<s rz $%883d33 dVs4y1}--34 dVs4y1}--0E0PPQ $#d)# #e +nU.C CD $#d)# #f ,~e/D DErctddt|zzdztddt|zzdztddt|zzdztddt|zzdzt|zy)Nrfrgrhz| (identical objects)+-->rir<rrSr[s rrQrQsx $#d)# #c )* $#d)# #&= => $#d)# #c )* $#d)# #f ,tE{ :;rc|turd}nd}tddt|zzdztddt|zzdz|zy)Nrz (nonfatal)rfrgz| |zX zX X <-- FAIL)r?rir<)rSrNresumes rrZrZsQ ## $#d)# #g -. $#d)# #&6 6 ?@rctddt|zzdztddt|zzdzt|zy)Nrfrgrhrmrnros rrCrCsA $#d)# #c )* $#d)# #f ,tE{ :;rct|dkDrXt|jd}ddjd|Dz}t ddt|zzd z|zyy) Nrc |djS)Nrrvs rz!_trace_bindings..s1Q499r)r{%s}rc3BK|]\}}|dt|yw)z: N)rj)rr$rs rrz"_trace_bindings..s*% 0:cse2nS)* +% srfrgz Bindings: )r<rPrrri)rSrq binditemsbindstrs rrDrDsl 8}q8>>+1DE 499% >G%    dVc$i''*::WDE rcHt|trd|zSdt|zS)Nr)rr r)rs rrjrjs%#x czd3irc |t||k(S)z Return True if ``fstruct1`` subsumes ``fstruct2``. I.e., return true if unifying ``fstruct1`` with ``fstruct2`` would result in a feature structure equal to ``fstruct2.`` :rtype: bool r)rErFs rrr s uXx0 00rc4gfd}t||||S)z Return a list of the feature paths of all features which are assigned incompatible values by ``fstruct1`` and ``fstruct2``. :rtype: list(tuple) c*j||Sr6)r)r[rTrS conflict_lists r add_conflictzconflicts..add_conflictsT" r)rrr)rErFrrrs @r conflictsrs%M (H.Ls. c#67S]]_% s35)rr sum)r$rs rrvz$SubstituteBindingsSequence.variablesJs@#Az#x'@AC    E   As66c j|j|Dcgc]}|j||c}Scc}wr6)rsubst)r$rqrws rroz.SubstituteBindingsSequence.substitute_bindingsTs*~~E1tzz!X6EFFEs0cht|tr|j|S|j||Sr6)rr ror)r$rwrqs rrz SubstituteBindingsSequence.substWs/ a, -((2 2<<1% %rN)r;rrrrvrorr7rrrrDs  G&rrceZdZdZdZy)FeatureValueTuplea A base feature value that is a tuple of other base feature values. FeatureValueTuple implements ``SubstituteBindingsI``, so it any variable substitutions will be propagated to the elements contained by the set. A ``FeatureValueTuple`` is immutable. cVt|dk(ryddjd|DzS)Nrz()rrc3"K|]}| ywr6r7rbs rrz-FeatureValueTuple.__repr__..is!7QQC&!7 )r<rr#s rrzFeatureValueTuple.__repr__fs* t9> !7$!7777rN)r;rrrrr7rrrr^s 8rrceZdZdZdZeZy)FeatureValueSeta  A base feature value that is a set of other base feature values. FeatureValueSet implements ``SubstituteBindingsI``, so it any variable substitutions will be propagated to the elements contained by the set. A ``FeatureValueSet`` is immutable. cht|dk(ryddjtd|DzS)Nrz{/}ryrc3"K|]}| ywr6r7rs rrz+FeatureValueSet.__repr__..ys(>AA3(>r)r<rrPr#s rrzFeatureValueSet.__repr__ts1 t9> &(>(>">???rN)r;rrrrrr7rrrrls@GrrceZdZdZdZdZy)FeatureValueUnionzp A base feature value that represents the union of two or more ``FeatureValueSet`` or ``Variable``. ct|t}td|Ddk(rt|t}t|St |dk(rt |dSt j||S)Nc3<K|]}t|tywr6rr rrws rrz,FeatureValueUnion.__new__..71z!X&7rrN)_flattenrrrr<r frozensetrrrs rrzFeatureValueUnion.__new__sl&"34 77 71 <fo6F"6* * v;! <? "  f--rcJddjtd|DzS)Nry+c3"K|]}| ywr6r7rs rrz-FeatureValueUnion.__repr__..s'=11#'=r)rrPr#s rrzFeatureValueUnion.__repr__s#'='=!=>>>rNr;rrrrrr7rrrr~s ."?rrceZdZdZdZdZy)FeatureValueConcatzz A base feature value that represents the concatenation of two or more ``FeatureValueTuple`` or ``Variable``. ct|t}td|Ddk(rt|t}t|St |dk(rt |dSt j||S)Nc3<K|]}t|tywr6rrs rrz-FeatureValueConcat.__new__..rrrrN)rrrrr<rrrrs rrzFeatureValueConcat.__new__sk&"45 77 71 <f&78F$V, , v;! <? "}}S&))rc8ddjd|DzS)Nrrc3"K|]}| ywr6r7rs rrz.FeatureValueConcat.__repr__..s 6AA3 6r)rr#s rrzFeatureValueConcat.__repr__s 6 6666rNrr7rrrrs *"7rrcvg}|D]1}t||r|j|!|j|3|S)z} Helper function -- return a copy of list, with all elements of type ``cls`` spliced in rather than appended in. )rr r)lstrrNrs rrrsA F c3  MM#  MM#   MrcreZdZdZddZedZedZedZdZ dZ d Z d Z d Z d Zd Zy)rzi A feature identifier that's specialized to put additional constraints, default values, etc. Nc|dvsJ||_||_||_|jdk(rd|jf|_y|jdk(rd|jf|_yd|jf|_y)N)NrrrrrrNr)_name_default_display_sortkey)r$rrrs rrzFeature.__init__sl3333   ==H $,DM ]]g % ODM ODMrc|jS)zThe name of this feature.)rr#s rrz Feature.nameszzrc|jS)zDefault value for this feature.)rr#s rrzFeature.default}}rc|jS)z1Custom display location: can be prefix, or slash.)rr#s rrzFeature.displayrrc d|jzS)Nz*%s*rur#s rrzFeature.__repr__s !!rct|tryt|ts td|||j|jkS)NT<)rrrrrr3s rr=zFeature.__lt__s8 eS !%) #Cu 5}}u~~--rcft|t|k(xr|j|jk(Sr6)typerr3s rr4zFeature.__eq__s'DzT%[(FTZZ5;;-FFrc||k( Sr6r7r3s rr8zFeature.__ne__r9rc,t|jSr6)rQrr#s rrCzFeature.__hash__sDJJrc(|j|||Sr6) read_valuer$spositionrmparsers rrzFeature.read_values  Hk::rc||k(r|StS)zp If possible, return a single value.. If not, return the value ``UnificationFailure``. )r?)r$r[rTrqs rrWzFeature.unify_base_values s E>L% %r)NN)r;rrrrpropertyrrrrr=r4r8rCrrWr7rrrrsl ,".G! ;&rrceZdZdZy) SlashFeaturec(|j|||Sr6 read_partialrs rrzSlashFeature.read_values""1h <> q6CF?% % rN)r;rrr2compilerrrWr7rrrrsrzz,-H; rrr)rrrr)rc.eZdZdZdZdZdZdZdZy)rXa An abstract base class for base values that define a custom unification method. The custom unification method of ``CustomFeatureValue`` will be used during unification if: - The ``CustomFeatureValue`` is unified with another base value. - The ``CustomFeatureValue`` is not the value of a customized ``Feature`` (which defines its own unification method). If two ``CustomFeatureValue`` objects are unified with one another during feature structure unification, then the unified base values they return *must* be equal; otherwise, an ``AssertionError`` will be raised. Subclasses must define ``unify()``, ``__eq__()`` and ``__lt__()``. Subclasses may also wish to define ``__hash__()``. ctd)z If this base value unifies with ``other``, then return the unified value. Otherwise, return ``UnificationFailure``. zabstract base classr!r3s rrzCustomFeatureValue.unifyJs ""788rctSr6NotImplementedr3s rr4zCustomFeatureValue.__eq__Qrc||k( Sr6r7r3s rr8zCustomFeatureValue.__ne__Tr9rctSr6rr3s rr=zCustomFeatureValue.__lt__WrrcFtd|jjz)Nz%s objects or unhashable)rrr;r#s rrCzCustomFeatureValue.__hash__Zs2T^^5L5LLMMrN) r;rrrrr4r8r=rCr7rrrXrX6s!$9!NrrXc eZdZeefeedfdZd8dZe jdZ e jdZ e jdZ e jdZe jdZe jd Ze jd Ze jd Ze jd Ze jd ej(de j(dej(dej(d Zd9dZd8dZdZdZdZdZdZdZde fde jdfde jdfde jd fd!e jd"fd#e jd$fd%e jd&fd'e jd(fd)e jd*fg Zd+Zd,Z d-Z!d.Z"dd/d0d1Z#d2Z$d3Z%d4Z&d5Z'd6Z(d7Z)y):rNc|Dcic]}|j|c}|_||_||_d|_d|_|D]]}|j dk(r|j r td||_|j dk(s@|jr td||__|Dcgc]}|j|c}|_ | t}||_ ycc}wcc}w)Nrz"Multiple features w/ display=slashrz#Multiple features w/ display=prefix) r _features _fdict_class _flist_class_prefix_feature_slash_featurerrr_features_with_defaultsr _logic_parser)r$r fdict_class flist_class logic_parserffeatures rrzFeatStructReader.__init__ds.66!&&!)6''#" /G')&&$%IJJ&-#(*''$%JKK'.$ /$,( w/JG( $  &=L)'7( sC#C5Cc|j}|j|di|\}}|t|k7r|j|d||S)aK Convert a string representation of a feature structure (as displayed by repr) into a ``FeatStruct``. This process imposes the following restrictions on the string representation: - Feature names cannot contain any of the following: whitespace, parentheses, quote marks, equals signs, dashes, commas, and square brackets. Feature names may not begin with plus signs or minus signs. - Only the following basic feature value are supported: strings, integers, variables, None, and unquoted alphanumeric strings. - For reentrant values, the first mention must specify a reentrance identifier and a value; and any subsequent mentions must use arrows (``'->'``) to reference the reentrance identifier. rz end of string)striprr<_error)r$rrrrs rrzFeatStructReader.fromstringsJ& GGI++Aq"g>x s1v  KK?H 5 rz$\s*(?:\((\d+)\)\s*)?(\??[\w-]+)?(\[)z\s*]\s*/z&\s*([+-]?)([^\s\(\)<>"\'\-=\[\],]+)\s*z\s*->\s*z\s*\((\d+)\)\s*z\s*=\s*z\s*,\s*z$\s*(?:\((\d+)\)\s*)?(\??[\w-]+\s*)()rz)|(z\s*(z\s*(=|->)|[+-]z|\]))c|i} |j||||S#t$r@}t|jdk7r|j|g|jYd}~yd}~wwxYw)a Helper function that reads in a feature structure. :param s: The string to read. :param position: The position in the string to start parsing. :param reentrances: A dictionary from reentrance ids to values. Defaults to an empty dictionary. :return: A tuple (val, pos) of the feature structure created by parsing and the position where the parsed feature structure ends. :rtype: bool Nr) _read_partialrr<rr)r$rrrmrrs rrzFeatStructReader.read_partialsh  K $%%a;H H $166{a DKK #AFF # # $s A#6AA#cZ|=|jj||r|j}n|j}|jj||}|s*|j j||}|s t d||j}|jdr5|jd}||vrt d|jd|||<t|tr%|j|j|||||S|dd=|j|||||S)Nopen bracket or identifierrNznew identifier)rrrr_START_FSTRUCT_RE_BARE_PREFIX_RErrrstartrrr_read_partial_featdict_read_partial_featlist)r$rrrmrr identifiers rrzFeatStructReader._read_partials ?##))!X6++-++-&&,,Q9((..q(;E !=xHH99; ;;q>QJ[( !15;;q>BB&-K # gx ( MMO..q(E;PWX X ..q(E;PWX Xrcd|jdr td|jds td|t|kr^|jj ||}|||j fS|j j ||}|r|j }|jj ||}|s td||jd}||vr td||j }|j||n(|jd|||\}}|j||jj ||r#|jj ||}| td||j }|t|kr^td |) Nrz open bracketrrrNbound identifierrcomma close bracket) rrr<_END_FSTRUCT_RErr_REENTRANCE_RE _TARGET_REr _read_value _COMMA_RE)r$rrrrmrtargetrs rrz'FeatStructReader._read_partial_featlists ;;q>^, ,{{1~^, ,Q((..q(;E  ++''--a:E 99;--a:$\8<<Q,$%7BB 99;{623#'"2"21a;"Oxu%##))!X6NN((H5E} (33yy{HAQF(33rc|jdrq|jtd|jd|jdj }|j dr t |}|||j<|jds"|j||j||S|t|krvdx}}|jj||}|"|j||j||S|jj||}| td||jd}|j}|ddk(rC|ddk(r;|jj|d d}|td |jd||vrtd |jd|jd d k(rd }|jd dk(rd}||jj||}|p|j}|j j||}|s td||jd } | |vr td||j}|| }|R|j"j||}|r(|j}|j%||||\}}n td||||<|jj||r;|j&j||}| td||j}|t|krvtd|)Nrrr1rz feature namer*rrNzknown special featureznew namerT-Frrz equals signrr)rrrrr startswithr _finalizerr<rr_FEATURE_NAME_RErrrr _ASSIGN_REr r ) r$rrrrmr prefixvalrrr s rrz'FeatStructReader._read_partial_featdict s ;;q>##+ !=u{{1~NN A,,.I##C($Y/ ,5GD(( ){{1~>>!UYY[+wG GQ D5((..q(;E ~~ak7KK))//8Dyy{HAw#~$r(c/~~))$q*5<$%$$yy{H OO11!X>E (x@@"[[^F[0();XFF$yy{H'/E}--a:$yy{H&*&6&6tQ+&VOE8%]H=="GDM##))!X6NN((H5E} (33yy{HGQL(33rc|jj||}|r6|j}|j|||j |\}}|||<||fS)zw Called when we see the close brace -- checks for a slash feature, and adds in default values. ) _SLASH_RErrr r)r$rposrmrrrrws rrzFeatStructReader._finalizek s] $$Q, &&D%%dAuyy{KHFAsGDM |rcpt|tr|j||||S|j|||Sr6)rrr)r$rrrrms rr zFeatStructReader._read_value| s4 dG $??1h TB B??1h < read_logic_valuez <(.*?)(?read_set_value{read_tuple_valuez\(c(|j|||Sr6rr$rrrmrs rr!z#FeatStructReader.read_fstruct_value s  Hk::rct||Sr6)rr,s rr#zFeatStructReader.read_str_value s8$$rcTt|j|jfSr6)rRrrr,s rr$zFeatStructReader.read_int_value s5;;=!599;..rcTt|j|jfSr6)r rrr,s rr"zFeatStructReader.read_var_value s & 33rTF)NoneTrueFalsec~|j|j}}|jj|||fSr6)rr _SYM_CONSTSr)r$rrrmrrrs rr%zFeatStructReader.read_sym_value s5;;=%))+S##C-s22rc~|jjd|jddz|jfS)z%Mainly included for backwards compat.z%s(%s)rr)rparserrr,s rr&zFeatStructReader.read_app_value s4!!''5;;q!3D(DEuyy{RRrc |jj|jd}||j fS#t$r }t|d}~wwxYw#t$r!}t d|j d|d}~wwxYw)NrNzlogic expression)rr6rrrrr)r$rrrmrexprrs rr'z!FeatStructReader.read_logic_value s H ())// A?$ $. ( a' ( H/Q@a G Hs2*?A AAAA B A<<Bc @|j||||dttS)Nr)_read_seq_valuerrr,s rr*z!FeatStructReader.read_tuple_value s&## xeS2CEW  rc @|j||||dttS)N})r:rrr,s rr(zFeatStructReader.read_set_value s%## xeS/CT  rctj|}|j}tjd|zj ||} | r|| jfSg} d} tjd|zj ||} | r2| r|| | jfS|| | jfS|j |||\} }| j | tjd|zj ||} | std|z|| jddk(rd} | j}) zN Helper function used by read_tuple_value and read_set_value. z \s*/?\s*%sFTz\s*%sz\s*(,|\+|(?=%s))\s*z',' or '+' or '%s'rNr) r2escaperrrrrrr) r$rrrmr close_paren seq_class plus_classcprr seen_plusrs rr:z FeatStructReader._read_seq_value s3 YY{ #99; JJ}r) * 0 0H = ;' '  8b=)//8K3SH  "rrcV d|zjd}d|zjd}t|t|kDr-ddt|ddz zzdz}||gt|zz }n,ddt|ddz zzdz}||gt|zz }t||D]\}}t|zdz|ztd t|dzzdzd t|dzzt|ddzd z td j ztd j ztd j ztdj zi}|j ||} | tdj z| Stdj  fdd| zjdD|r?t|jdkDr#tt|j | S)Nrr[rrr]z rrz| |z+-----UNIFY-----+rhVz(FAILED)c3FK|]}|jzywr6)center)rrrlinelens rrz&display_unification..' sVQfqxx00Vs!) rr<ziprirLrrbound_variablesr) fs1fs2r fs1_lines fs2_lines blanklinefs1_linefs2_linerqrNrMs ` @rdisplay_unificationrW s""4(I""4(I 9~I&#Yq\!2Q!677#= i[3y>11 #Yq\!2Q!677#= i[3y>11 !)Y74( fx%'(234 &3Yq\** *U 2S3y|;L5L LM)A,!#a'G &&--g6 67 &&--g6 67 &3::g& &' &3::g& &'H YYsH %F ~ fz((112 M  IIV4&=:O:OPT:UV V  H4467!; $x.''0 1 Mrcddl}ddl}d}tdtd|jj gd}t t |Dcgc]}|t||f}}d} d}t ||kDrt|j||} n|} tdtd || ddg} d D]\} }| | td | t |fzd  |jj j} | dvry| dvr| }td|zf| dvrt|t | z| dvr ||t| dz } || d| |<t| ||r| dj| dd}nt| d| d}|?|D]\}}t|t|k(sn|jt ||ftd|jj j} | dvrycc}w#tdYxxYw)Nrz 1-%d: Select the corresponding feature structure q: Quit t: Turn tracing on or off l: List all feature structures ?: Help a This demo will repeatedly present you with a list of feature structures, and ask you to choose two for unification. Whenever a new feature structure is generated, it is added to the list of choices that you can pick from. However, since this can be a large number of feature structures, the demo will only print out a random subset for you to choose between at a given time. If you want to see the complete lists, type "l". For a list of valid commands, type "?". zPress "Enter" to continue...z [agr=[number=sing, gender=masc]]z[agr=[gender=masc, person=3]]z[agr=[gender=fem, person=3]]z[subj=[agr=(1)[]], agr->(1)]z[obj=?x]z [subj=?x]z[/=None]z[/=NP]z[cat=NP]z[cat=VP]z[cat=PP]z/[subj=[agr=[gender=?y]], obj=[agr=[gender=?y]]]z[gender=masc, agr=?C]z%[gender=?S, agr=[gender=?S,person=3]]c|D]Q\}}td|zjd}td|dz|dfz|ddD]}td|zSty)Nrrz%3d: %srNrz )rir)fstructsirrrs r list_fstructsz'interactive_demo..list_fstructs^ sp" &JAw GG^**40E )q1ueAh// 0ab  &gn% &  & rK___________________________________________________________________________z'Choose two feature structures to unify:))Firstr)SecondrNz%%s feature structure (1-%d,q,t,l,?): r)r)qQxX)tTz Trace = %s)hHr1)rLrNzBad sentence number)rz3 Type "Enter" to continue unifying; or "q" to quit.)randomsysristdinreadliner r<r rPsamplerrRrrWrr)rrkrlHELPfstruct_stringsr\ all_fstructsr] MAX_CHOICESr[selectednthinputnumrNrs rinteractive_demorx. s D    ()IIO$6;3;O5P01Jq) *+L  | { *fmmL+FGH#H h 78h$<3 FC1+%?L 123 II..0668E 44 *$) o56 /dS]23  *%l3 e*q.C".s"3A"6HQKG11+% < a[&&x{!&r=rRrAr@rBr<rQrZrCrDrjrrrrrrrrrrrrrrrrrDrErXrrWrxr{r;__all__r7rrrsBPd $<G$$G$G$V: ;0{z4{F W