
    /h                       S r SSKrSSKrSSKJr  SSKJrJr  SSKJ	r	J
r
JrJrJr  \ " S S\5      5       rSrS	rSOS
 jr " S S\\5      r " S S\\5      rSPS jrS rSPS jrS rSPS jrS r SQS jrS rS rSPS jrS r  " S S5      r!\!" 5       r"      SRS jr# " S S\$5      r%S  r&S! r'S" r(S# r)S$ r*S% r+S& r,S' r-S( r.S) r/S* r0S+ r1SSS, jr2S- r3S. r4S/ r5 " S0 S1\5      r6 " S2 S3\6\75      r8 " S4 S5\6\95      r: " S6 S7\6\95      r; " S8 S9\6\75      r<S: r=\ " S; S<5      5       r> " S= S>\>5      r? " S? S@\>5      r@\?" SASSASB9rA\>" SCSDSE9rB\ " SF SG5      5       rC " SH SI5      rDSTSJ jrESUSK jrFSUSL jrG\HSM:X  a  \G" 5         / SNQrIg)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_typesread_str)
ExpressionLogicalExpressionExceptionLogicParserSubstituteBindingsIVariablec                      ^  \ rS rSrSrSr S$U 4S jjrS rS rS r	S%S jr
S	 rS
 rS rS rS rS rSrS rS rS rS&S jrS rS rS rS rS rS rS rS rS rS'S jrS rS(S jr S  r!S! r"S" r#S#r$U =r%$ ))
FeatStructn   a  
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                 >  > U [         L Ga  Uc  [        R                  " [        40 UD6$ [        U5      (       a  [        R                  " [        U40 UD6$ U(       a  [	        S5      e[        U[        5      (       a\  [        R                  R                  U5      (       a  [        R                  " [        U40 UD6$ [        R                  " [        U40 UD6$ [        U5      (       a  [        R                  [        U5      $ [	        S5      e[        TU ]  " X40 UD6$ )ao  
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
isinstancestrFeatStructReader_START_FDICT_REmatchFeatList_is_sequencesuper)clsfeaturesmorefeatures	__class__s      G/var/www/auris/envauris/lib/python3.13/site-packages/nltk/featstruct.pyr   FeatStruct.__new__   s    * *''ALAAX&&''(KlKK;  (C((#3399(CC#++HhO,OO#++HhO,OOh''''(;; HII 7?3ALAA    c                     [        5       e)zFReturn an iterable of the feature identifiers used by this
FeatStruct.NotImplementedErrorselfs    r   _keysFeatStruct._keys        "##r!   c                     [        5       e)zMReturn an iterable of the feature values directly defined
by this FeatStruct.r#   r%   s    r   _valuesFeatStruct._values   r)   r!   c                     [        5       e)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   _itemsFeatStruct._items   s     "##r!   c                 Z    U R                  X[        5       [        5       [        5       5      $ )a  
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``.
_equalset)r&   othercheck_reentrances      r   equal_valuesFeatStruct.equal_values   s     {{5CE35#%HHr!   c                 \    U R                  US[        5       [        5       [        5       5      $ )z
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()``
Tr1   r&   r4   s     r   __eq__FeatStruct.__eq__   s!     {{5$suce<<r!   c                     X:X  + $ N r9   s     r   __ne__FeatStruct.__ne__         r!   c                     [        U[        5      (       d-  U R                  R                  UR                  R                  :  $ [	        U 5      [	        U5      :  $ r=   )r   r   r   __name__lenr9   s     r   __lt__FeatStruct.__lt__   sC    %,, >>**U__-E-EEEt9s5z))r!   c                     U R                   (       d  [        S5      e U R                  $ ! [         a-    U R	                  [        5       5      U l        U R                  s $ f = f)z]
If this feature structure is frozen, return its hash value;
otherwise, raise ``TypeError``.
z5FeatStructs must be frozen before they can be hashed.)_frozenr   _hashAttributeError_calculate_hashvaluer3   r%   s    r   __hash__FeatStruct.__hash__	  sS    
 ||VWW	:: 	22359DJ::	s   * 4A! A!c                    XL a  gU R                   UR                   :w  a  g[        U 5      [        U5      :w  a  g[        U R                  5       5      [        UR                  5       5      :w  a  gU(       a8  [	        U 5      U;   d  [	        U5      U;   a  [	        U 5      [	        U5      4U;   $ O[	        U 5      [	        U5      4U;   a  gUR                  [	        U 5      5        UR                  [	        U5      5        UR                  [	        U 5      [	        U5      45        U R                  5        HC  u  pgX   n[        U[        5      (       a  UR                  UUUUU5      (       d    gM<  Xx:w  d  MC    g   g)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)
r   rD   r3   r'   idaddr.   r   r   r2   )	r&   r4   r5   visited_selfvisited_othervisited_pairsfname	self_fval
other_fvals	            r   r2   FeatStruct._equal  sQ    = >>U__,
 t9E
"tzz|EKKM 22 $x<'2e9+E4"U),== ,F 4"U)$5 	D""U)$2d8RY/0 !%EJ)Z00 ''$ !!  ! *  !.  r!   c                 \   [        U 5      U;   a  gUR                  [        U 5      5        Sn[        U R                  5       5       Hb  u  p4US-  nU[	        U5      -  nUS-  n[        U[        5      (       a  X$R                  U5      -  nOU[	        U5      -  n[        US-  5      nMd     U$ )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)	rO   rP   sortedr.   hashr   r   rK   int)r&   visitedhashvalrT   fvals        r   rK   FeatStruct._calculate_hashvalue]  s     d8wBtH!$++-0KErMGtE{"GrMG$
++44W==4:%'J./G 1 r!   'Frozen FeatStructs may not be modified.c                 Z    U R                   (       a  gU R                  [        5       5        g)z
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)rH   _freezer3   r%   s    r   freezeFeatStruct.freeze~  s     <<SUr!   c                     U R                   $ )z
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.
)rH   r%   s    r   frozenFeatStruct.frozen  s     ||r!   c                     [        U 5      U;   a  gUR                  [        U 5      5        SU l        [        U R	                  5       5       H-  u  p#[        U[        5      (       d  M  UR                  U5        M/     g)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)rO   rP   rH   r[   r.   r   r   rd   )r&   r^   rT   r`   s       r   rd   FeatStruct._freeze  sZ     d8wBtH!$++-0KE$
++W% 1r!   c                 ^    U(       a  [         R                  " U 5      $ U R                  U 5      $ )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.
)copydeepcopyr   )r&   deeps     r   rm   FeatStruct.copy  s%     ==&&>>$''r!   c                     [        5       er=   r#   )r&   memos     r   __deepcopy__FeatStruct.__deepcopy__  s    !##r!   c                 <    U R                  0 5      [        U 5         $ )z8
Return True if this feature structure contains itself.
)_find_reentrancesrO   r%   s    r   cyclicFeatStruct.cyclic  s     %%b)"T(33r!   c                 4    U R                  [        5       5      $ )z
Return an iterator that generates this feature structure, and
each feature structure it contains.  Each feature structure will
be generated exactly once.
)_walkr3   r%   s    r   walkFeatStruct.walk  s     zz#%  r!   c                     [        5       e)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&   r^   s     r   rz   FeatStruct._walk  s     "##r!   c              #      #    [        U 5      U;   a  g UR                  [        U 5      5        U v   U R                  5        H3  n[        U[        5      (       d  M  UR                  U5       S h  vN   M5     g  N	7fr=   )rO   rP   r+   r   r   rz   )r&   r^   r`   s      r   rz   r~     s[     d8wBtH
LLND$
++::g... #.s   AA;A;/A90
A;c                     [        U 5      U;   a  SU[        U 5      '   U$ SU[        U 5      '   U R                  5        H+  n[        U[        5      (       d  M  UR	                  U5        M-     U$ )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)rO   r+   r   r   rv   )r&   reentrancesr`   s      r   rv   FeatStruct._find_reentrances  sk     d8{"$(K4!  %*K4! dJ//**;7 ' r!   c                     [        X5      $ )z/:see: ``nltk.featstruct.substitute_bindings()``)substitute_bindingsr&   bindingss     r   r   FeatStruct.substitute_bindings  s    "422r!   c                     [        X5      $ )z,:see: ``nltk.featstruct.retract_bindings()``)retract_bindingsr   s     r   r   FeatStruct.retract_bindings  s    //r!   c                     [        U 5      $ )z*:see: ``nltk.featstruct.find_variables()``)find_variablesr%   s    r   	variablesFeatStruct.variables  s    d##r!   c                     [        XX#5      $ )z,:see: ``nltk.featstruct.rename_variables()``)rename_variables)r&   vars	used_varsnew_varss       r   r   FeatStruct.rename_variables  s    I@@r!   c                     [        U 5      $ )zz
Return the feature structure that is obtained by deleting
any feature whose value is a ``Variable``.

:rtype: FeatStruct
)remove_variablesr%   s    r   r   FeatStruct.remove_variables  s      %%r!   c                     [        XX#XE5      $ r=   unify)r&   r4   r   tracefailrename_varss         r   r   FeatStruct.unify  s    T(4EEr!   c                     [        X5      $ )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``.
)subsumesr9   s     r   r   FeatStruct.subsumes  s     $$r!   c                 D    U R                  U R                  0 5      0 5      $ )zr
Display a single-line representation of this feature structure,
suitable for embedding in other representations.
)_reprrv   r%   s    r   __repr__FeatStruct.__repr__#  s     
 zz$004b99r!   c                     [        5       e)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&   r   reentrance_idss      r   r   FeatStruct._repr*  s     "##r!   )rH   rI   r=   F)T)Nr>   N)NFNT)&rC   
__module____qualname____firstlineno____doc__rH   r   r'   r+   r.   r6   r:   r?   rE   rL   r2   rK   _FROZEN_ERRORre   rh   rd   rm   rs   rw   r{   rz   rv   r   r   r   r   r   r   r   r   r   __static_attributes____classcell__)r   s   @r   r   r   n   s    @ G+Bh$
$
$I 	=!*EN> >M	&(
($4!$/030$A&F%:$ $r!   r   rb   z'
%sIf self is frozen, raise ValueError.c                 ~   ^  U 4S jnT R                   Ul         T R                  =(       d    S[        U-  -   Ul        U$ )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.
c                 \   > U R                   (       a  [        [        5      eT" U /UQ70 UD6$ r=   )rH   
ValueErrorr   )r&   argskwargsmethods      r   wrapped_check_frozen.<locals>.wrappedF  s*    <<]++$0000r!    )rC   r   _FROZEN_NOTICE)r   indentr   s   `  r   _check_frozenr   >  s5    1 G~~+0GHGONr!   c                      \ rS rSrSrSS jrSrS rSS jrS r	S	 r
S
 rS r\" \R                  5      r\" \R                   5      r\" \R"                  5      r\" \R$                  5      rSS jrS rS rS rS rS rS rS rSrg)r   iV  az  
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                     [        U[        5      (       a,  [        5       R                  X5        U R                  " S0 UD6  gU R                  " U40 UD6  g)a  
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.
Nr>   )r   r   r   
fromstringupdate)r&   r   r   s      r   __init__FeatDict.__init__f  sD      h$$))(9KK',' KK1L1r!   z'Expected feature name or path.  Got %r.c                 ^   [        U[        [        45      (       a  [        R	                  X5      $ [        U[
        5      (       a-   U nU H"  n[        U[        5      (       d  [        eX#   nM$     U$ [        U R                  U-  5      e! [        [        4 a  n[        U5      UeSnAff = f)zcIf the feature with the given name or path exists, return
its value; otherwise, raise ``KeyError``.N)r   r   Featuredict__getitem__tupler   KeyError
IndexErrorr   _INDEX_ERRORr&   name_or_pathvalfides        r   r   FeatDict.__getitem__  s     lS'N33##D77e,,4'C%c:66&(C ( 
 D--<== j) 4|,!34s   +B B,B''B,c                 0     X   $ ! [          a    Us $ f = f)zcIf the feature with the given name or path exists, return its
value; otherwise, return ``default``.r   )r&   r   defaults      r   getFeatDict.get  s$    	%% 	N	s    c                 .     X     g! [          a     gf = f)<Return true if a feature with the given name or path exists.TFr   r&   r   s     r   __contains__FeatDict.__contains__  s"    	 		s    
c                 
    X;   $ )r   r>   r   s     r   has_keyFeatDict.has_key  s    ##r!   c                    U R                   (       a  [        [        5      e[        U[        [
        45      (       a  [        R                  X5      $ [        U[        5      (       aG  [        U5      S:X  a  [        S5      eXSS    n[        U[        5      (       d  [        U5      eX!S   	 g[        U R                  U-  5      ezcIf the feature with the given name or path exists, delete
its value; otherwise, raise ``KeyError``.r   The path () can not be setN)rH   r   r   r   r   r   r   __delitem__r   rD   r   r   r   r   r&   r   parents      r   r   FeatDict.__delitem__  s     <<]++lS'N33##D77e,,< A% !=>>3B/0!&*55"<00+,D--<==r!   c                    U R                   (       a  [        [        5      e[        U[        [
        45      (       a  [        R                  XU5      $ [        U[        5      (       aI  [        U5      S:X  a  [        S5      eXSS    n[        U[        5      (       d  [        U5      eX#US   '   g[        U R                  U-  5      ezSet the value for the feature with the given name or path
to ``value``.  If ``name_or_path`` is an invalid path, raise
``KeyError``.r   r   Nr   )rH   r   r   r   r   r   r   __setitem__r   rD   r   r   r   r   r&   r   valuer   s       r   r   FeatDict.__setitem__  s     <<]++lS'N33##D>>e,,< A% !=>>3B/0!&*55"<00+0|B'(D--<==r!   c                    U R                   (       a  [        [        5      eUc  SnO[[        US5      (       a+  [	        UR
                  5      (       a  UR                  5       nO[        US5      (       a  UnO[        S5      eU H/  u  pE[        U[        [        45      (       d  [        S5      eXPU'   M1     UR                  5        H/  u  pE[        U[        [        45      (       d  [        S5      eXPU'   M1     g )Nr>   items__iter__z"Expected mapping or list of tupleszFeature names must be strings)
rH   r   r   hasattrcallabler   r   r   r   r   )r&   r   r   r   keyr   s         r   r   FeatDict.update  s    <<]++EXw''HX^^,D,DNN$EXz**EABBHCcC>22 ?@@I  %**,HCcC>22 ?@@I -r!   c                     U R                  5       =U[        U 5      '   nU R                  5        H2  u  p4[        R                  " XA5      U[        R                  " X15      '   M4     U$ r=   )r   rO   r.   rm   rn   )r&   rr   selfcopyr   r   s        r   rs   FeatDict.__deepcopy__  sN    $(NN$44RXHC15s1IHT]]3-. &r!   c                 "    U R                  5       $ r=   )keysr%   s    r   r'   FeatDict._keys  s    yy{r!   c                 "    U R                  5       $ r=   )valuesr%   s    r   r+   FeatDict._values  s    {{}r!   c                 "    U R                  5       $ r=   )r   r%   s    r   r.   FeatDict._items  s    zz|r!   c                 b    SR                  U R                  U R                  0 5      0 5      5      $ )zb
Display a multi-line representation of this feature dictionary
as an FVM (feature value matrix).

)join_strrv   r%   s    r   __str__FeatDict.__str__  s)    
 yy4#9#9"#=rBCCr!   c           	         / nSnSnU[        U 5         (       a4  [        U 5      U;  d   e[        [        U5      S-   5      U[        U 5      '   [        U R	                  5       5       GH  u  pg[        USS 5      n[        U5      U;   a%  UR                  U SU[        U5          S35        MG  US:X  a)  U(       d"  [        U[        [        45      (       a  SU-  nMv  US:X  a=  U(       d6  [        U[        5      (       a  S	UR                  -  nM  S	[        U5      -  nM  [        U[        5      (       a"  UR                  U S
UR                   35        M  USL a  UR                  SU-  5        GM  USL a  UR                  SU-  5        GM(  [        U[        5      (       a  UR                  U SU S35        GMW  [        U[        5      (       d"  UR                  U S
[        U5       35        GM  UR                  X5      n	UR                  U S
U	 35        GM     U[        U 5         (       a  SU[        U 5          SU 3nSR                  USR                  U5      U5      $ )Nr   rY   displayz->()prefix%sslashz/%s=Tz+%sFz-%sz=<>(z{}[{}]{}, )rO   reprrD   r[   r   getattrappendr   r
   r   namer   r   r   formatr   )
r&   r   r   segmentsr  suffixrT   r`   r  	fval_reprs
             r   r   FeatDict._repr	  s    r$x d8>111'+C,?!,C'DN2d8$ "$**,/KEeY5G$x>)5'^BtH-E,Fa HI8#Fz$SV7X7XG#FdH--"TYY.F"T$Z/FD(++5'499+ 67..D*--5'D6 34j115'4:, 78 JJ{C	5'9+ 673 06 r$x 412!F8<F  8)<fEEr!   c                    U[        U 5         (       a4  [        U 5      U;  d   e[        [        U5      S-   5      U[        U 5      '   [        U 5      S:X  a(  U[        U 5         (       a  SU[        U 5         -  /$ S/$ [        S U R	                  5        5       5      n/ n[        U R                  5       5       GH  u  pVSU-  R                  U5      n[        U[        5      (       a"  UR                  U SUR                   35        MQ  [        U[        5      (       a  UR                  U SU S	35        M  [        U[        5      (       a2  UR                  X5      nUR                  U S[        U5       35        M  [        U[        5      (       d!  UR                  U S[        U5       35        M  [        U5      U;   a&  UR                  U S
U[        U5          S35        GM1  U(       a  US   S:w  a  UR                  S5        UR!                  X5      nU V	s/ s H  n	SUS-   -  U	-   PM     nn	[        U5      S-
  S-  n
US-   X   US-   S -   X'   XH-  nUR                  S5        GM     US   S:X  a  UR#                  5         [        S U 5       5      nU Vs/ s H$  nSR%                  USU[        U5      -
  -  5      PM&     nnU[        U 5         (       aX  SU[        U 5         -  nU V	s/ s H  n	S[        U5      -  U	-   PM     nn	[        U5      S-
  S-  nXU   [        U5      S -   XN'   U$ s  sn	f s  snf s  sn	f )a  
:return: A list of lines composing a string representation of
    this feature dictionary.
: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.
rY   r   z(%s) []z[]c              3   >   #    U  H  n[        S U-  5      v   M     g7fr  NrD   ).0ks     r   	<genexpr> FeatDict._str.<locals>.<genexpr>P  s     =A#dQh--s   r  z = z = <r  z -> (r  r   r          z =Nc              3   8   #    U  H  n[        U5      v   M     g 7fr=   r  )r  lines     r   r  r    s     154SYY5s   z[ {}{} ]z(%s) )rO   r  rD   maxr   r[   r   ljustr   r
   r  r  r   r   r   r   r  popr  )r&   r   r   maxfnamelenlinesrT   r`   r  
fval_lineslnamelinemaxlenr#  idstridlines                  r   r  FeatDict._str5  s    r$x d8>111'+C,?!,C'DN2d8$ t9>2d8$!N2d8$<<==v === "$**,/KEE\((5E$))wc$))56D*--wd4&23D(++ JJ{C	wc$y/):;<h//wc$t*67D^+ weN2d8,D+EQGH
 U2Y"_LL$ "YY{C
 FPPZskAo6!;Z
P  
Oa/A5DL:#7a8I#JJ $
 # R W 0\ 9?IIK 1511QVWQV""4T0B)CDQVW r$x nRX66E5:;UcCJ&!+UE;%j1n*F!&M#e*,$??EM; Q( X
 <s   !L>$+M9Mr>   r=   )rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   clearr&  popitem
setdefaultr   rs   r'   r+   r.   r  r   r  r   r>   r!   r   r   r   V  s    24 =L>$$>$>& $**%E

!CDLL)Gt/J2D*FX\r!   r   c                      \ rS rSrSrSS jrSrS rS rS r	\
" \R                  5      r\
" \R                  5      r\
" \R                  5      r\
" \R                  5      r\
" \R                   5      r\
" \R"                  5      r\
" \R$                  5      r\
" \R&                  5      r\
" \R(                  5      rS	 rS
 rS rS rS rSrg)r   i  aM  
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.
r>   c                     [        U[        5      (       a  [        5       R                  X5        g[        R                  X5        g)a*  
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)r   r   r   r   listr   )r&   r   s     r   r   FeatList.__init__  s.     h$$))(9MM$)r!   z&Expected int or feature path.  Got %r.c                 R   [        U[        5      (       a  [        R                  X5      $ [        U[        5      (       a-   U nU H"  n[        U[
        5      (       d  [        eX#   nM$     U$ [        U R                  U-  5      e! [        [        4 a  n[        U5      UeS nAff = fr=   )
r   r]   r5  r   r   r   r   r   r   r   r   s        r   r   FeatList.__getitem__  s    lC((##D77e,,4'C%c:66&(C ( 
 D--<== j) 4|,!34s   +B B&B!!B&c                    U R                   (       a  [        [        5      e[        U[        [
        45      (       a  [        R                  X5      $ [        U[        5      (       aG  [        U5      S:X  a  [        S5      eXSS    n[        U[        5      (       d  [        U5      eX!S   	 g[        U R                  U-  5      er   )rH   r   r   r   r]   slicer5  r   r   rD   r   r   r   r   r   s      r   r   FeatList.__delitem__  s     <<]++lS%L11##D77e,,< A% !=>>3B/0!&*55"<00+,D--<==r!   c                    U R                   (       a  [        [        5      e[        U[        [
        45      (       a  [        R                  XU5      $ [        U[        5      (       aI  [        U5      S:X  a  [        S5      eXSS    n[        U[        5      (       d  [        U5      eX#US   '   g[        U R                  U-  5      er   )rH   r   r   r   r]   r:  r5  r   r   rD   r   r   r   r   r   s       r   r   FeatList.__setitem__  s     <<]++lS%L11##D>>e,,< A% !=>>3B/0!&*55"<00+0|B'(D--<==r!   c                 z   ^ U R                  5       =T[        U 5      '   nUR                  U4S jU  5       5        U$ )Nc              3   R   >#    U  H  n[         R                  " UT5      v   M     g 7fr=   )rm   rn   )r  r`   rr   s     r   r  (FeatList.__deepcopy__.<locals>.<genexpr>  s     CdddD11ds   $')r   rO   extend)r&   rr   r   s    ` r   rs   FeatList.__deepcopy__  s3    $(NN$44RXCdCCr!   c                 <    [        [        [        U 5      5      5      $ r=   )r5  rangerD   r%   s    r   r'   FeatList._keys  s    E#d)$%%r!   c                     U $ r=   r>   r%   s    r   r+   FeatList._values  s    r!   c                     [        U 5      $ r=   )	enumerater%   s    r   r.   FeatList._items  s    r!   c                    U[        U 5         (       aF  [        U 5      U;  d   e[        [        U5      S-   5      U[        U 5      '   SU[        U 5         -  nOSn/ nU  H  n[        U5      U;   a"  UR                  SU[        U5         -  5        M4  [	        U[
        5      (       a  UR                  UR                  5        Mf  [	        U[        5      (       a  UR                  SU-  5        M  [	        U[        5      (       a"  UR                  UR                  X5      5        M  UR                  S[        U5      -  5        M     SR                  USR                  U5      5      $ )NrY   (%s)r   z->(%s)r  z{}[{}]r  )rO   r  rD   r  r   r
   r  r   r   r   r  r   )r&   r   r   r  r  r`   s         r   r   FeatList._repr  s    r$x d8>111'+C,?!,C'DN2d8$nRX66FFD$x>)>"T(+C CDD(++		*D*--t,D*--

; GHtDz 12  vtyy':;;r!   N)r>   )rC   r   r   r   r   r   r   r   r   r   r   r5  __iadd____imul__r  rA  insertr&  removereversesortrs   r'   r+   r.   r   r   r>   r!   r   r   r     s     *" <L> >$>* T]]+HT]]+H4;;'F4;;'F4;;'F

!C4;;'FDLL)G#D&<r!   r   c                 ~    US:X  a  [        U 5      n[        R                  " U 5      n [        XU[	        5       5        U $ )a  
Return the feature structure that is obtained by replacing each
variable bound by ``bindings`` with its binding.  If a variable is
aliased to a bound variable, then it will be replaced by that
variable's value.  If a variable is aliased to an unbound
variable, then it will be replaced by that variable.

:type bindings: dict(Variable -> any)
:param bindings: A dictionary mapping from variables to values.
r   )_default_fs_classrm   rn   _substitute_bindingsr3   )fstructr   fs_classs      r   r   r   8  s7     9$W-mmG$GHce<Nr!   c                 
   [        U 5      U;   a  g UR                  [        U 5      5        [        U 5      (       a  U R                  5       nO'[	        U 5      (       a  [        U 5      nO[        S5      eU H  u  pV[        U[        5      (       a)  Xa;   a$  X   =o`U'   [        U[        5      (       a  Xa;   a  M$  [        Xb5      (       a  [        XaX#5        Ma  [        U[        5      (       d  Mx  UR                  U5      X'   M     g NExpected mapping or sequence)rO   rP   r   r   r   rI  r   r   r
   rV  r	   r   )rW  r   rX  r^   r   rT   r`   s          r   rV  rV  J  s    	'{gKK77	g		'"788x((T-=$,N2D5> x((T-=d%% C122!55h?GN r!   c                    US:X  a  [        U 5      n[        R                  " X45      u  pUR                  U5        UR	                  5        VVs0 s H  u  pE[        U5      U_M     nnn[        XU[        5       5        U $ s  snnf )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   )rU  rm   rn   r   r   rO   _retract_bindingsr3   )rW  r   rX  new_bindingsvarr   inv_bindingss          r   r   r   _  sv     9$W-"mmW,?@WOOL!3;>>3CD3CZcBsGSL3CLDgXsu=N Es   Bc                 x   [        U 5      U;   a  g UR                  [        U 5      5        [        U 5      (       a  U R                  5       nO'[	        U 5      (       a  [        U 5      nO[        S5      eU HB  u  pV[        Xb5      (       d  M  [        U5      U;   a  U[        U5         X'   [        XaX#5        MD     g rZ  )	rO   rP   r   r   r   rI  r   r   r]  )rW  r`  rX  r^   r   rT   r`   s          r   r]  r]  t  s    	'{gKK77	g		'"788d%%$x<'!-bh!7d(D	 r!   c                 `    US:X  a  [        U 5      n[        U [        5       U[        5       5      $ )zU
:return: The set of variables used by this feature structure.
:rtype: set(Variable)
r   )rU  
_variablesr3   rW  rX  s     r   r   r     s+    
 9$W-gsuh66r!   c                    [        U 5      U;   a  g UR                  [        U 5      5        [        U 5      (       a  U R                  5       nO'[	        U 5      (       a  [        U 5      nO[        S5      eU H  u  pV[        U[        5      (       a  UR                  U5        M-  [        Xb5      (       a  [        XaX#5        MK  [        U[        5      (       d  Mb  UR                  UR                  5       5        M     U$ rZ  )rO   rP   r   r   r   rI  r   r   r
   rc  r	   r   r   )rW  r   rX  r^   r   rT   r`   s          r   rc  rc    s    	'{gKK77	g		'"788dH%%HHTN''t85122KK()  Kr!   c           	          US:X  a  [        U 5      nUc  0 nUc  [        X5      nO[        U5      n[        X5      R                  U5      n[	        [
        R                  " U 5      XX4[        5       5      $ )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   )rU  r   r3   union_rename_variablesrm   rn   )rW  r   r   r   rX  s        r   r   r     sv    X 9$W- |g04y w177	BI gSU r!   c           	         [        U 5      U;   a  g UR                  [        U 5      5        [        U 5      (       a  U R                  5       nO'[	        U 5      (       a  [        U 5      nO[        S5      eU H  u  px[        U[        5      (       a<  X;   a  X8   X'   M'  X;   a(  [        X5      X8'   X8   X'   UR                  X8   5        MT  MV  [        X5      (       a  [        XX#XE5        Mu  [        U[        5      (       d  M  UR                  5        H1  n	X;   d  M
  X;  d  M  [        X5      X9'   UR                  X9   5        M3     UR                  U5      X'   M     U $ rZ  )rO   rP   r   r   r   rI  r   r   r
   _rename_variablerh  r	   r   r   )
rW  r   r   r   rX  r^   r   rT   r`   r_  s
             r   rh  rh    s$   	'{gKK77	g		'"788dH%%!)!1$!B!)hn-  ''d)xQ122~~';3#6$4S$DHMMM(-0 (
 "55h?GN' ( Nr!   c                     [         R                  " SSU R                  5      Sp2U(       d  Sn[        U U 35      U;   a  US-  n[        U U 35      U;   a  M  [        U U 35      $ )Nz\d+$r   r!  ?rY   )resubr  r
   )r_  r   r  ns       r   rj  rj    sj    ffWb#((+Q!
dVA3<
 I
-	Q dVA3<
 I
-tfQCL!!r!   c                 v    US:X  a  [        U 5      n[        [        R                  " U 5      U[	        5       5      $ )z
:rtype: FeatStruct
:return: The feature structure that is obtained by deleting
    all features whose values are ``Variables``.
r   )rU  _remove_variablesrm   rn   r3   rd  s     r   r   r     s0     9$W-T]]73XsuEEr!   c                    [        U 5      U;   a  g UR                  [        U 5      5        [        U 5      (       a  [        U R	                  5       5      nO0[        U 5      (       a  [        [        U 5      5      nO[        S5      eU H<  u  pE[        U[        5      (       a  X	 M  [        XQ5      (       d  M0  [        XQU5        M>     U $ rZ  )rO   rP   r   r5  r   r   rI  r   r   r
   rq  )rW  rX  r^   r   rT   r`   s         r   rq  rq    s    	'{gKK77W]]_%	g		Yw'(788dH%%''dg6	 
 Nr!   c                       \ rS rSrS rSrg)_UnificationFailurei4  c                     g)Nz"nltk.featstruct.UnificationFailurer>   r%   s    r   r   _UnificationFailure.__repr__5  s    3r!   r>   N)rC   r   r   r   r   r   r>   r!   r   rt  rt  4  s    4r!   rt  Fc           
         US:X  a%  [        U 5      n[        U5      U:w  a  [        S5      e[        X5      (       d   e[        X5      (       d   eUSLnUc  0 n[        R                  " XU45      u  pn
UR                  U
5        U(       a-  [        X5      n[        X5      n[        XU0 U[        5       5        0 nU(       a  [        SX5         [        XX-X4US5      nU[        L a  Uc  gU" XS5      $ [        XU[        5       5      nU(       a  [        X5        [        U5        [!        XU[        5       5        U(       a  [#        SU5        U(       a  [%        SU5        U$ ! [         a     gf = f)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.
r   zGMixing FeatStruct objects with Python dicts and lists is not supported.Nr>   )rU  r   r   rm   rn   r   r   rh  r3   _trace_unify_start_destructively_unify_UnificationFailureErrorUnificationFailure_apply_forwards_apply_forwards_to_bindings_resolve_aliasesrV  _trace_unify_succeed_trace_bindings)fstruct1fstruct2r   r   r   r   rX  user_bindingsfstruct1copyfstruct2copybindings_copyvars1vars2forwardresults                  r   r   r   D  s~   j 9$X.X&(24  h))))h)))) D(M 37--	X&3/\
 OOM"|6|6,ub(CEJ G2|:%5RT
 ##<B77 Vh>F#G6 X8SU; R(H%M5 $ s   E# #
E0/E0c                       \ rS rSrSrSrg)rz  i  ziAn exception that is used by ``_destructively_unify`` to abort
unification when a failure is encountered.r>   N)rC   r   r   r   r   r   r>   r!   r   rz  rz    s    2r!   rz  c                 ~   XL a  U(       a  [        Xp5        U $ X[        U5      '   [        U 5      (       a  [        U5      (       a  U  H.  n[        USS5      c  M  UR	                  XR
                  5        M0     U H.  n[        USS5      c  M  U R	                  XR
                  5        M0     [        UR                  5       5       H*  u  pX;   a  [        UX   U	UUUUUXx4-   5	      X'   M&  XU'   M,     U $ [        U 5      (       ag  [        U5      (       aW  [        U 5      [        U5      :w  a  [        $ [        [        U 5      5       H  n
[        U
X
   X   UUUUUXz4-   5	      X
'   M!     U $ [        U 5      (       d  [        U 5      (       a&  [        U5      (       d  [        U5      (       a  [        $ [        S5      e)a  
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.
r   NzExpected mappings or sequences)_trace_unify_identityrO   r   r  r2  r   r[   r   _unify_feature_valuesr   rD   r{  rD  r   )r  r  r   r  r   r   rX  pathrT   fval2findexs              r   ry  ry    s   6 !$1 %BxL 8X!6!6Eui.:##E==9  Eui.:##E==9  #8>>#34LE "7O8O
# #( 5   
h		L$:$:x=CM)%% CM*F4  y 
 H +  x
 
 K$9$9X+h"7"7!! 4
55r!   c	                 ~   U(       a  [        XU5        [        U5      U;   a  U[        U5         n[        U5      U;   a  M  [        U5      U;   a  U[        U5         n[        U5      U;   a  M  S=p[        U[        5      (       a'  X;   a"  Un	X1   n[        U[        5      (       a  X;   a  M"  [        U[        5      (       a'  X#;   a"  Un
X2   n[        U[        5      (       a  X#;   a  M"  [        X5      (       a   [        X'5      (       a  [	        XX4XVXx5      nGO[        U[        5      (       a"  [        U[        5      (       a  X:w  a  XU'   UnGOX[        U[        5      (       a  X#U'   UnGO;[        U[        5      (       a  XU'   UnGO[        X5      (       d  [        X'5      (       a  [
        nO[        U [        5      (       a  U R                  XU5      nO[        U[        5      (       ae  UR                  U5      n[        U[        5      (       a>  XR                  U5      :w  a*  [        SU< SU< SU< SUR                  U5      < 35      eO5[        U[        5      (       a  UR                  U5      nOX:X  a  UnO[
        nU[
        La  U	b  XU	'   U	nU
b  X:w  a  XU
'   U
nU[
        L a1  Ub	  U" XU5      nU(       a  [        USS U5        U[
        L a  [        e[        X5      (       a  [        XU[        5       5      nU(       a  [        X5        U(       a  [        X5      (       a  [!        X5        U$ )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.
NzCustomFeatureValue objects z and z# disagree about unification value: z vs. r   )rx  rO   r   r
   ry  r{  r   unify_base_valuesCustomFeatureValuer   AssertionError_trace_unify_failrz  r|  r3   r  r  )rT   fval1r  r   r  r   r   rX  fpathfvar1fvar2r  s               r   r  r  *  s   & 5/ U)w
5	" U)w

U)w
5	" U)w
 E
UH
%
%%*; UH
%
%%*; UH
%
%%*; UH
%
%%*;
 %""z%'B'B%(U(

 
E8	$	$E8)D)D>#UO 
E8	$	$	E8	$	$ 
E	$	$
5(C(C#
 eW%%,,U8DF122[['F%!344;;uCU9U$ eVU[[-?A 
 122[['F ~+
 ++ "( U^"( ##%.FeCRj&1''** &## (CEBU+F--(Mr!   c                     UR                  5        H7  u  p#[        U5      U ;   a  U [        U5         n[        U5      U ;   a  M  X1U'   M9     g){
Replace any feature structure that has a forward pointer with
the target of its forward pointer (to preserve reentrancy).
N)r   rO   )r  r   r_  r   s       r   r}  r}    sF    
 nn&
i7"BuI&E i7" 'r!   c                    [        U 5      U;   a  U[        U 5         n [        U 5      U;   a  M  [        U 5      U;   a  gUR                  [        U 5      5        [        U 5      (       a  U R                  5       nO'[	        U 5      (       a  [        U 5      nO[        S5      eU HU  u  pV[        Xb5      (       d  M  [        U5      U;   a  U[        U5         n[        U5      U;   a  M  X`U'   [        XaX#5        MW     U $ )r  Nr[  )	rO   rP   r   r   r   rI  r   r   r|  )rW  r  rX  r^   r   rT   r`   s          r   r|  r|    s     W+
 "W+& W+
  
'{gKK77	g		'"788d%%T(g%r$x( T(g%!END8=  Nr!   c                     U R                  5        HI  u  p[        U[        5      (       d  M  X ;   d  M#  X   =o U'   [        U[        5      (       d  MB  X ;   a  M&  MK     g)zx
Replace any bound aliased vars with their binding; and replace
any unbound aliased vars with their representative var.
N)r   r   r
   )r   r_  r   s      r   r~  r~    sK    
 nn&
))e.?$,O3ESM ))e.? 'r!   c                    U S:X  a  [        S5        O[SR                  S U  5       5      n[        SS[        U 5      S-
  -  -   S-   5        [        SS[        U 5      S-
  -  -   S	U-  -   5        [        SS[        U 5      -  -   S
-   [        U5      -   5        [        SS[        U 5      -  -   S-   [        U5      -   5        g )Nr>   z
Unification trace:.c              3   ,   #    U  H
  nS U-  v   M     g7fr  r>   )r  ro  s     r   r  %_trace_unify_start.<locals>.<genexpr>  s     3dD1Hds     |   rY   |z| Unify feature: %sz / z|\ )printr   rD   _trace_valrepr)r  r  r  fullnames       r   rx  rx    s    rz$%883d33dVs4y1}--34dVs4y1}--0E0PPQ	$#d)#
#e
+nU.C
CD	$#d)#
#f
,~e/D
DEr!   c                    [        SS[        U 5      -  -   S-   5        [        SS[        U 5      -  -   S-   5        [        SS[        U 5      -  -   S-   5        [        SS[        U 5      -  -   S-   [        U5      -   5        g )Nr  r  r  z| (identical objects)+-->r  rD   r  r  r  s     r   r  r    sx    	$#d)#
#c
)*	$#d)#
#&=
=>	$#d)#
#c
)*	$#d)#
#f
,tE{
:;r!   c                     U[         L a  SnOSn[        SS[        U 5      -  -   S-   5        [        SS[        U 5      -  -   S-   U-   5        g )Nr   z (nonfatal)r  r  z|   |zX   zX   X <-- FAIL)r{  r  rD   )r  r  resumes      r   r  r    sQ    ##	$#d)#
#g
-.	$#d)#
#&6
6
?@r!   c                     [        SS[        U 5      -  -   S-   5        [        SS[        U 5      -  -   S-   [        U5      -   5        g )Nr  r  r  r  r  r  s     r   r  r    sA    	$#d)#
#c
)*	$#d)#
#f
,tE{
:;r!   c                     [        U5      S:  aU  [        UR                  5       S S9nSSR                  S U 5       5      -  n[	        SS[        U 5      -  -   S	-   U-   5        g g )
Nr   c                      U S   R                   $ )Nr   r  vs    r   <lambda>!_trace_bindings.<locals>.<lambda>  s    1Q499r!   )r   {%s}r  c              3   F   #    U  H  u  pU S [        U5       3v   M     g7f)z: N)r  )r  r_  r   s      r   r  "_trace_bindings.<locals>.<genexpr>  s&      %
>G
se2nS)*+is   !r  r  z    Bindings: )rD   r[   r   r   r  )r  r   	binditemsbindstrs       r   r  r    sl    
8}q8>>+1DE	499 %
>G%
 
 
 	dVc$i''*::WDE r!   c                 R    [        U [        5      (       a  SU -  $ S[        U 5      -  $ )Nr  )r   r
   r  )r   s    r   r  r    s(    #x  czd3ir!   c                     U[        X5      :H  $ )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   )r  r  s     r   r   r   
  s     uX000r!   c                 ,   ^ / mU4S jn[        XX2S9  T$ )z
Return a list of the feature paths of all features which are
assigned incompatible values by ``fstruct1`` and ``fstruct2``.

:rtype: list(tuple)
c                 *   > TR                  U5        U $ r=   )r  )r  r  r  conflict_lists      r   add_conflictconflicts.<locals>.add_conflict  s    T"r!   )r   r   r   )r  r  r   r  r  s       @r   	conflictsr    s!     M 
(<=r!   c                 @    [        U S5      =(       a    [        U S5      $ )Nr   r   )r   r  s    r   r   r   +  s    1n%<'!V*<<r!   c                 x    [        U S5      =(       a(    [        U S5      =(       a    [        U [        5      (       + $ )Nr   __len__)r   r   r   r  s    r   r   r   /  s+    1j!Vga&;VJqRUDV@VVr!   c                     [        U [        5      (       a  [        $ [        U [        [        45      (       a  [        [        4$ [	        SU R
                  R                  -  5      e)NzBTo unify objects of type %s, you must specify fs_class explicitly.)r   r   r   r5  r   r   rC   )objs    r   rU  rU  3  sT    #z""#d|$$d|#%(]]%;%;<
 	
r!   c                   *    \ rS rSrSrS rS rS rSrg)SubstituteBindingsSequenceiD  zw
A mixin class for sequence classes that distributes variables() and
substitute_bindings() over the object's elements.
c                     U  Vs/ s H  n[        U[        5      (       d  M  UPM     sn[        S U  5       / 5      -   $ s  snf )Nc              3      #    U  H5  n[        U[        5      (       d  M  [        UR                  5       5      v   M7     g 7fr=   )r   r	   r5  r   )r  elts     r   r  7SubstituteBindingsSequence.variables.<locals>.<genexpr>L  s2      Cc#67 &S]]_%%s   ? ?)r   r
   sum)r&   r  s     r   r   $SubstituteBindingsSequence.variablesJ  sD    #Atz#x'@tAC
 E
 
 	
As   ??c           	      l    U R                  U  Vs/ s H  o R                  X!5      PM     sn5      $ s  snf r=   )r   subst)r&   r   r  s      r   r   .SubstituteBindingsSequence.substitute_bindingsT  s*    ~~E1zz!6EFFEs   1c                 p    [        U[        5      (       a  UR                  U5      $ UR                  X5      $ r=   )r   r	   r   r   )r&   r  r   s      r   r   SubstituteBindingsSequence.substW  s0    a,--((22<<%%r!   r>   N)	rC   r   r   r   r   r   r   r  r   r>   r!   r   r  r  D  s    

G&r!   r  c                       \ rS rSrSrS rSrg)FeatureValueTuplei^  z
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.
c                 X    [        U 5      S:X  a  gSSR                  S U  5       5      -  $ )Nr   z()rL  r  c              3   &   #    U  H  o v   M	     g 7fr=   r>   r  bs     r   r  -FeatureValueTuple.__repr__.<locals>.<genexpr>i  s     !7$QC&$   )rD   r   r%   s    r   r   FeatureValueTuple.__repr__f  s*    t9>		!7$!7777r!   r>   N)rC   r   r   r   r   r   r   r>   r!   r   r  r  ^  s    8r!   r  c                   "    \ rS rSrSrS r\rSrg)FeatureValueSetil  z
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.
c                 j    [        U 5      S:X  a  gSSR                  [        S U  5       5      5      -  $ )Nr   z{/}r  r  c              3   &   #    U  H  o v   M	     g 7fr=   r>   r  s     r   r  +FeatureValueSet.__repr__.<locals>.<genexpr>y  s     (>A3r  )rD   r   r[   r%   s    r   r   FeatureValueSet.__repr__t  s1    t9> 		&(>(>">???r!   r>   N)rC   r   r   r   r   r   r  r   r>   r!   r   r  r  l  s    @ Gr!   r  c                   $    \ rS rSrSrS rS rSrg)FeatureValueUnioni~  zd
A base feature value that represents the union of two or more
``FeatureValueSet`` or ``Variable``.
c                     [        U[        5      n[        S U 5       5      S:X  a  [        U[        5      n[        U5      $ [	        U5      S:X  a  [        U5      S   $ [        R                  X5      $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr=   r   r
   r  r  s     r   r  ,FeatureValueUnion.__new__.<locals>.<genexpr>       71z!X&&   r   rY   )_flattenr  r  r  rD   r5  	frozensetr   r   r   s     r   r   FeatureValueUnion.__new__  sj    &"34 7771<fo6F"6** v;!<?"   --r!   c                 J    SSR                  [        S U  5       5      5      -  $ )Nr  +c              3   &   #    U  H  o v   M	     g 7fr=   r>   r  s     r   r  -FeatureValueUnion.__repr__.<locals>.<genexpr>  s     '=1#r  )r   r[   r%   s    r   r   FeatureValueUnion.__repr__  s#     '='=!=>>>r!   r>   NrC   r   r   r   r   r   r   r   r>   r!   r   r  r  ~  s    
."?r!   r  c                   $    \ rS rSrSrS rS rSrg)FeatureValueConcati  zn
A base feature value that represents the concatenation of two or
more ``FeatureValueTuple`` or ``Variable``.
c                     [        U[        5      n[        S U 5       5      S:X  a  [        U[        5      n[        U5      $ [	        U5      S:X  a  [        U5      S   $ [        R                  X5      $ )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7fr=   r  r  s     r   r  -FeatureValueConcat.__new__.<locals>.<genexpr>  r  r  r   rY   )r  r  r  r  rD   r5  r   r   r  s     r   r   FeatureValueConcat.__new__  si    &"45 7771<f&78F$V,, v;!<?" }}S))r!   c                 8    SSR                  S U  5       5      -  $ )NrL  r  c              3   &   #    U  H  o v   M	     g 7fr=   r>   r  s     r   r  .FeatureValueConcat.__repr__.<locals>.<genexpr>  s      6A3r  )r   r%   s    r   r   FeatureValueConcat.__repr__  s     6 6666r!   r>   Nr  r>   r!   r   r  r    s    
*"7r!   r  c                     / nU  H7  n[        X15      (       a  UR                  U5        M&  UR                  U5        M9     U$ )zq
Helper function -- return a copy of list, with all elements of
type ``cls`` spliced in rather than appended in.
)r   rA  r  )lstr   r  r  s       r   r  r    s=    
 FcMM#MM#	 
 Mr!   c                   |    \ rS rSrSrSS jr\S 5       r\S 5       r\S 5       r	S r
S	 rS
 rS rS rS rS rSrg)r   i  z]
A feature identifier that's specialized to put additional
constraints, default values, etc.
Nc                     US;   d   eXl         X l        X0l        U R                  S:X  a  SU R                   4U l        g U R                  S:X  a  SU R                   4U l        g SU R                   4U l        g )N)Nr  r	  r  r   r	  rY   r   )_name_default_display_sortkey)r&   r  r   r  s       r   r   Feature.__init__  sf    3333
==H$,DM]]g%

ODM

ODMr!   c                     U R                   $ )zThe name of this feature.)r  r%   s    r   r  Feature.name  s     zzr!   c                     U R                   $ )zDefault value for this feature.)r  r%   s    r   r   Feature.default       }}r!   c                     U R                   $ )z1Custom display location: can be prefix, or slash.)r  r%   s    r   r  Feature.display  r  r!   c                      SU R                   -  $ )Nz*%s*r  r%   s    r   r   Feature.__repr__  s    		!!r!   c                     [        U[        5      (       a  g[        U[        5      (       d  [        SX5        U R                  UR                  :  $ )NT<)r   r   r   r   r	  r9   s     r   rE   Feature.__lt__  s<    eS!!%))#C5}}u~~--r!   c                 p    [        U 5      [        U5      :H  =(       a    U R                  UR                  :H  $ r=   )typer  r9   s     r   r:   Feature.__eq__  s'    DzT%[(FTZZ5;;-FFr!   c                     X:X  + $ r=   r>   r9   s     r   r?   Feature.__ne__  rA   r!   c                 ,    [        U R                  5      $ r=   )r\   r  r%   s    r   rL   Feature.__hash__  s    DJJr!   c                 &    UR                  XU5      $ r=   )
read_valuer&   spositionr   parsers        r   r  Feature.read_value  s      k::r!   c                     X:X  a  U$ [         $ )zX
If possible, return a single value..  If not, return
the value ``UnificationFailure``.
)r{  )r&   r  r  r   s       r   r  Feature.unify_base_values	  s    
 >L%%r!   )r  r  r  r	  )NN)rC   r   r   r   r   r   propertyr  r   r  r   rE   r:   r?   rL   r  r  r   r>   r!   r   r   r     sl    
,      ".G! ;&r!   r   c                       \ rS rSrS rSrg)SlashFeaturei  c                 &    UR                  XU5      $ r=   read_partialr   s        r   r  SlashFeature.read_value  s    ""1<<r!   r>   N)rC   r   r   r   r  r   r>   r!   r   r)  r)    s    =r!   r)  c                   D    \ rS rSr\R
                  " S5      rS rS rSr	g)RangeFeaturei  z(-?\d+):(-?\d+)c                     U R                   R                  X5      nU(       d  [        SU5      e[        UR	                  S5      5      [        UR	                  S5      5      4UR                  5       4$ )NrD  rY   r!  )RANGE_REr   r   r]   groupend)r&   r!  r"  r   r#  ms         r   r  RangeFeature.read_value  sT    MM,Wh//AGGAJQWWQZ11557::r!   c                     Uc  U$ Uc  U$ [        US   US   5      [        US   US   5      4nUS   US   :  a  [        $ U$ )Nr   rY   )r$  minr{  )r&   r  r  r   rngs        r   r  RangeFeature.unify_base_values"  sX    =L=L%(E!H%s58U1X'>>q6CF?%%
r!   r>   N)
rC   r   r   r   rm  compiler1  r  r  r   r>   r!   r   r/  r/    s    zz,-H;r!   r/  r	  )r   r  r  r  )r  c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)
r  i6  a  
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__()``.
c                     [        S5      e)zv
If this base value unifies with ``other``, then return the
unified value.  Otherwise, return ``UnificationFailure``.
zabstract base classr#   r9   s     r   r   CustomFeatureValue.unifyJ  s    
 ""788r!   c                     [         $ r=   NotImplementedr9   s     r   r:   CustomFeatureValue.__eq__Q      r!   c                     X:X  + $ r=   r>   r9   s     r   r?   CustomFeatureValue.__ne__T  rA   r!   c                     [         $ r=   r?  r9   s     r   rE   CustomFeatureValue.__lt__W  rB  r!   c                 F    [        SU R                  R                  -  5      e)Nz%s objects or unhashable)r   r   rC   r%   s    r   rL   CustomFeatureValue.__hash__Z  s    2T^^5L5LLMMr!   r>   N)rC   r   r   r   r   r   r:   r?   rE   rL   r   r>   r!   r   r  r  6  s!    $9!Nr!   r  c                      \ rS rSr\\4\\S4S jrS:S jr	\
R                  " S5      r\
R                  " S5      r\
R                  " S5      r\
R                  " S5      r\
R                  " S	5      r\
R                  " S
5      r\
R                  " S5      r\
R                  " S5      r\
R                  " S5      r\
R                  " S\R*                  < S\R*                  < S\R*                  < S\R*                  < S3	5      rS;S jrS:S jrS rS rS rS rS rS rS\4S\
R                  " S5      4S\
R                  " S5      4S \
R                  " S!5      4S"\
R                  " S#5      4S$\
R                  " S%5      4S&\
R                  " S'5      4S(\
R                  " S)5      4S*\
R                  " S+5      4/	rS, r S- r!S. r"S/ r#SS0S1S2.r$S3 r%S4 r&S5 r'S6 r(S7 r)S8 r*S9r+g)<r   ic  Nc                    U Vs0 s H  oUR                   U_M     snU l        X l        X0l        S U l        S U l        U Hi  nUR                  S:X  a"  U R
                  (       a  [        S5      eX`l        UR                  S:X  d  MG  U R                  (       a  [        S5      eX`l        Mk     U Vs/ s H  ofR                  c  M  UPM     snU l	        Uc
  [        5       nX@l        g s  snf s  snf )Nr	  z"Multiple features w/ display=slashr  z#Multiple features w/ display=prefix)r  	_features_fdict_class_flist_class_prefix_feature_slash_featurer  r   r   _features_with_defaultsr   _logic_parser)r&   r   fdict_classflist_classlogic_parserffeatures          r   r   FeatStructReader.__init__d  s     .66X&&!)X6''#"G')&&$%IJJ&-#(*''$%JKK'.$   $,(
#+G8(
$ &=L)' 7(
s   C&0C+C+c                     UR                  5       nU R                  US0 U5      u  p4U[        U5      :w  a  U R                  USU5        U$ )a  
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.
r   zend of string)stripr,  rD   _error)r&   r!  rW  r   r"  s        r   r   FeatStructReader.fromstring  sH    & GGI++Aq"g>s1vKK?H5r!   z$\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*)()r  z)|(z\s*(z\s*(=|->)|[+-]z|\]))c                     Uc  0 n U R                  XX45      $ ! [         aA  n[        UR                  5      S:w  a  e U R                  " U/UR                  Q76    SnAgSnAff = f)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_partialr   rD   r   rZ  )r&   r!  r"  r   rW  r   s         r   r,  FeatStructReader.read_partial  sb     K	$%%a;HH 	$166{aKK#AFF##	$s    
A$7AA$c                    UcA  U R                   R                  X5      (       a  U R                  5       nOU R                  5       nU R                  R                  X5      nU(       d.  U R
                  R                  X5      nU(       d  [        SU5      eUR                  5       nUR                  S5      (       a5  UR                  S5      nXc;   a  [        SUR                  S5      5      eXCU'   [        U[        5      (       a#  UR                  5         U R                  XXSU5      $ US S 2	 U R                  XXSU5      $ )Nopen bracket or identifierrY   znew identifier)r   r   rL  rM  _START_FSTRUCT_RE_BARE_PREFIX_REr   r3  r2  startr   r   r0  _read_partial_featdict_read_partial_featlist)r&   r!  r"  r   rW  r   
identifiers          r   r^  FeatStructReader._read_partial  s   ?##))!66++-++- &&,,Q9((..q;E !=xHH99; ;;q>>QJ( !15;;q>BB&-
#gx((MMO..qEPWXX
..qEPWXXr!   c                    UR                  S5      (       a  [        S5      eUR                  S5      (       d  [        S5      eU[        U5      :  Gaj  U R                  R	                  X5      nUb  XSR                  5       4$ U R                  R	                  X5      nU(       a  UR                  5       nU R                  R	                  X5      nU(       d  [        SU5      eUR                  S5      nXd;  a  [        SU5      eUR                  5       nUR                  XF   5        O&U R                  SXU5      u  prUR                  U5        U R                  R	                  X5      (       a  GM.  U R                  R	                  X5      nUc  [        SU5      eUR                  5       nU[        U5      :  a  GMj  [        S	U5      e)
Nr!  zopen bracketr   rg  rY   bound identifierr   commaclose bracket)r2  r   rD   _END_FSTRUCT_REr   r3  _REENTRANCE_RE
_TARGET_REr  _read_value	_COMMA_RE)r&   r!  r"  r   r   rW  targetr   s           r   rf  'FeatStructReader._read_partial_featlist  s   ;;q>>^,,{{1~~^,, Q((..q;E 		++ ''--a:E 99;--a:$\8<<Q,$%7BB 99;{23 #'"2"21a;"Ou% ##))!66 NN((5E} (33yy{HA QF (33r!   c                    UR                  S5      (       av  U R                  c  [        SUR                  S5      5      eUR                  S5      R	                  5       nUR                  S5      (       a  [        U5      nXeU R                  '   UR                  S5      (       d   U R                  XR                  5       XE5      $ U[        U5      :  Ga  S =pxU R                  R                  X5      nUb   U R                  XR                  5       XE5      $ U R                  R                  X5      nUc  [        SU5      eUR                  S5      nUR                  5       nUS   S:X  aE  US   S:X  a<  U R                  R                  US	S 5      nUc  [        S
UR                  S5      5      eXu;   a  [        SUR                  S5      5      eUR                  S	5      S:X  a  SnUR                  S	5      S:X  a  SnUc  U R                  R                  X5      nUbt  UR                  5       nU R                   R                  X5      nU(       d  [        SU5      eUR                  S	5      n	X;  a  [        SU5      eUR                  5       nXI   nUcS  U R"                  R                  X5      nU(       a%  UR                  5       nU R%                  XqX$5      u  pO[        SU5      eXU'   U R                  R                  X5      (       a  GMJ  U R&                  R                  X5      nUc  [        SU5      eUR                  5       nU[        U5      :  a  GM  [        SU5      e)Nr!  ra  rl  r   zfeature namer   *r   rY   zknown special featureznew namer  T-Frg  rj  zequals signrk  rl  )r2  rN  r   rd  rY  
startswithr
   	_finalizer3  rD   rm  r   _FEATURE_NAME_RErK  r   rn  ro  
_ASSIGN_RErp  rq  )
r&   r!  r"  r   r   rW  	prefixvalr  r   rr  s
             r   re  'FeatStructReader._read_partial_featdict	  s   ;;q>>##+ !=u{{1~NNA,,.I##C(($Y/	,5D(() {{1~~>>!YY[+GG QD ((..q;E ~~akKK ))//<E} ::;;q>Dyy{H Aw#~$r(c/~~))$q*5<$%<ekk!nMM  U[[^<< {{1~${{1~$ }++11!>$$yy{H OO11!>E (x@@"[[^F0();XFF$yy{H'/E }--a:$yy{H&*&6&6t&VOE8 %]H== "DM ##))!66 NN((5E} (33yy{HG QL (33r!   c                     U R                   R                  X5      nU(       a3  U R                  nU R                  XaUR	                  5       U5      u  prXtU'   XB4$ )z_
Called when we see the close brace -- checks for a slash feature,
and adds in default values.
)	_SLASH_REr   rO  rp  r3  )r&   r!  posr   rW  r   r  r  s           r   rx  FeatStructReader._finalizek	  sS     $$Q,&&D%%duyy{KHFADM
 |r!   c                 t    [        U[        5      (       a  UR                  X#X@5      $ U R                  X#U5      $ r=   )r   r   r  )r&   r  r!  r"  r   s        r   rp  FeatStructReader._read_value|	  s1    dG$$??1BB??1<<r!   c                     U R                    H3  u  pEUR                  X5      nU(       d  M  [        X5      nU" XX65      s  $    [        SU5      e)Nr   )VALUE_HANDLERSr   r  r   )r&   r!  r"  r   handlerregexpr   handler_funcs           r   r  FeatStructReader.read_value	  sM    #22OGLL-Eu&t5#ADD	  3
 (++r!   c                     UR                  S5      nU[        US   5      :  a4  U[        UR                  S5      5      S-   -  nU[        US   5      :  a  M4  SUS   -   S-   SU-  -   S-   SU-  -   n[        U5      e)	Nr   r   rY   z$Error parsing feature structure
    z
    r  z^ zExpected %s)splitrD   r&  r   )r&   r!  expectedr"  r(  estrs         r   rZ  FeatStructReader._error	  s    U1X&EIIaL)A--H U1X& 4Ah Hn 	
 h&' 	 r!   read_fstruct_valueread_var_valuez\?[a-zA-Z_][a-zA-Z0-9_]*read_str_valuez[uU]?[rR]?(['"])read_int_valuez-?\d+read_sym_valuez[a-zA-Z_][a-zA-Z0-9_]*read_app_valuez0<(app)\((\?[a-z][a-z]*)\s*,\s*(\?[a-z][a-z]*)\)>read_logic_valuez<(.*?)(?<!-)>read_set_value{read_tuple_valuez\(c                 &    U R                  XU5      $ r=   r+  r&   r!  r"  r   r   s        r   r  #FeatStructReader.read_fstruct_value	  s      k::r!   c                     [        X5      $ r=   )r   r  s        r   r  FeatStructReader.read_str_value	  s    $$r!   c                 T    [        UR                  5       5      UR                  5       4$ r=   )r]   r2  r3  r  s        r   r  FeatStructReader.read_int_value	  s    5;;=!599;..r!   c                 T    [        UR                  5       5      UR                  5       4$ r=   )r
   r2  r3  r  s        r   r  FeatStructReader.read_var_value	  s    &		33r!   TF)NoneTrueFalsec                 z    UR                  5       UR                  5       peU R                  R                  XU5      U4$ r=   )r2  r3  _SYM_CONSTSr   )r&   r!  r"  r   r   r   r3  s          r   r  FeatStructReader.read_sym_value	  s1    ;;=%))+S##C-s22r!   c                 ~    U R                   R                  SUR                  SS5      -  5      UR                  5       4$ )z%Mainly included for backwards compat.z%s(%s)r!  r   )rQ  parser2  r3  r  s        r   r  FeatStructReader.read_app_value	  s4    !!''5;;q!3D(DEuyy{RRr!   c                 
     U R                   R                  UR                  S5      5      nXTR                  5       4$ ! [         a  n[        UeS nAff = f! [         a!  n[	        SUR                  S5      5      UeS nAff = f)NrY   zlogic expression)rQ  r  r2  r   r   r3  rd  )r&   r!  r"  r   r   exprr   s          r   r  !FeatStructReader.read_logic_value	  s    	H())//A? $$ . ( a'(  	H/Q@aG	Hs2   *> A 
AAAA 
B!A==Bc           	      <    U R                  XX4S[        [        5      $ )Nr  )_read_seq_valuer  r  r  s        r   r  !FeatStructReader.read_tuple_value	  s"    ##S2CEW
 	
r!   c           	      <    U R                  XX4S[        [        5      $ )N})r  r  r  r  s        r   r  FeatStructReader.read_set_value	  s!    ##S/CT
 	
r!   c                    [         R                  " U5      nUR                  5       n[         R                  " SU-  5      R	                  X5      n	U	(       a  U" 5       U	R                  5       4$ / n
Sn [         R                  " SU-  5      R	                  X5      n	U	(       a7  U(       a  U" U
5      U	R                  5       4$ U" U
5      U	R                  5       4$ U R                  XU5      u  pU
R                  U5        [         R                  " SU-  5      R	                  X5      n	U	(       d  [        SU-  U5      eU	R                  S5      S:X  a  SnU	R                  5       nM  )	z>
Helper function used by read_tuple_value and read_set_value.
z
\s*/?\s*%sFTz\s*%sz\s*(,|\+|(?=%s))\s*z',' or '+' or '%s'rY   r  )	rm  escaper3  r:  r   r  r  r   r2  )r&   r!  r"  r   r   close_paren	seq_class
plus_classcpr4  r   	seen_plusr   s                r   r   FeatStructReader._read_seq_value	  s)    YY{#99;JJ}r)*00=;''	

8b=)//<A%f-quuw66$V,aeeg55 !OOAEMCMM# 

1B67==aJA !5!:HEEwwqzS  	uuwH' r!   )rL  rK  rP  rM  rQ  rN  rO  r=   )r   NN),rC   r   r   r   SLASHTYPEr   r   r   r   rm  r:  rb  rm  r~  ry  rn  ro  rz  rq  rc  patternr   r,  r^  rf  re  rx  rp  r  rZ  r  r  r  r  r  r  r  r  r  r  r  r  r   r>   r!   r   r   r   c  s    *82 

#JKjj,O

4 Izz"KLZZ,N./JJ'J

:&Ijj!HIOjj ##%%$$$$	
O$*Y<,4\[4z"=,: 
01	2::&ABC	2::&9:;	2::h/0	2::&?@AJJNO	
 
RZZ(89:	2::d+,	RZZ./N";%/4  >K3SH



"r!   r   c                 b  ^^
 SU -  R                  S5      nSU-  R                  S5      n[        U5      [        U5      :  a,  SS[        US   5      S-
  -  -   S-   nXE/[        U5      -  -  nO+SS[        US   5      S-
  -  -   S-   nX5/[        U5      -  -  n[        X45       H  u  pg[        TU-   S-   U-   5        M     [        TS	[        US   5      -  -   S-   S	[        US   5      -  -   5        [        US   5      S-  S
-   m
[        TSR	                  T
5      -   5        [        TSR	                  T
5      -   5        [        TSR	                  T
5      -   5        [        TSR	                  T
5      -   5        0 nU R                  X5      n	U	c  [        TSR	                  T
5      -   5        U	$ [        SR                  UU
4S jSU	-  R                  S5       5       5      5        U(       a@  [        UR                  5       5      S:  a#  [        [        U5      R	                  T
5      5        U	$ )Nr  r   [r  r   r!  ]z   rv  r   z|               |z+-----UNIFY-----+r  Vz(FAILED)c              3   L   >#    U  H  nTUR                  T5      -   v   M     g 7fr=   )center)r  r*  r   linelens     r   r  &display_unification.<locals>.<genexpr>'
  s!     V:UQfqxx00:Us   !$)	r  rD   zipr  r  r   r   bound_variablesr  )fs1fs2r   	fs1_lines	fs2_lines	blanklinefs1_linefs2_liner   r  r  s     `       @r   display_unificationr  
  s   ""4(I""4(I
9~I&#Yq\!2Q!677#=	[3y>11	#Yq\!2Q!677#=	[3y>11	!)7fx%'(23 8	&3Yq\**
*U
2S3y|;L5L
LM)A,!#a'G	&&--g6
67	&&--g6
67	&3::g&
&'	&3::g&
&'HYYs%F~fz((112 M 	IIV4&=:O:OPT:UVV	
 H4467!;$x.''01Mr!   c                    SS K nSS KnSn[        S5        [        S5        UR                  R	                  5         / SQn[        [        U5      5       Vs/ s H  oU[        XE   5      4PM     nnS n Sn[        U5      U:  a  [        UR                  Xh5      5      n	OUn	[        S5        [        S	5        U" U	5        S S /n
S
 H  u  pX   b  M  [        SU[        U5      4-  SS9   UR                  R	                  5       R                  5       nUS;   a    g US;   a  U (       + n [        SU -  5        Mm  US;   a  [        U[        U	5      -  5        M  US;   a
  U" U5        M  [        U5      S-
  nXm   S   X'   [        5         X   c  M  M     U (       a  U
S   R                  U
S   SS9nO[        U
S   U
S   5      nUbA  U H  u  p_[        U5      [        U5      :X  d  M    O   UR                  [        U5      U45        [        S5        UR                  R	                  5       R                  5       nUS;   a  g GM  s  snf !   [        S5         GM  = f)Nr   z
    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 "?".
    zPress "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                     U  HS  u  p[        5         SU-  R                  S5      n[        SUS-   US   4-  5        USS   H  n[        SU-   5        M     MU     [        5         g )Nr  r   z%3d: %srY   r   z     )r  r  )fstructsirW  r(  r#  s        r   list_fstructs'interactive_demo.<locals>.list_fstructs^
  sd    "JAGG^**40E)q1ueAh//0ab	gn% "	 # 	r!      K___________________________________________________________________________z'Choose two feature structures to unify:))Firstr   )SecondrY   z%%s feature structure (1-%d,q,t,l,?): r  )r3  )qQxX)tTz   Trace = %s)hHrl  )r*  LrY   zBad sentence number)r   z3
Type "Enter" to continue unifying; or "q" to quit.)randomsysr  stdinreadlinerD  rD   r   r[   samplerY  r]   r   r  r  r  )r   r  r  HELPfstruct_stringsr  all_fstructsr  MAX_CHOICESr  selectednthinputnumr  rW  s                   r   interactive_demor  .
  sh   D 
		 

()IIO$ 6;3;O5P5PJ)*+5P   |{*fmmLFGH#Hh78h$<3FC+%?L 123 II..0668E 44
*$)	o56 /dS]23 
*%l3 e*q.C"."3A"6HKG1 +% 4< a[&&x{!&<F(!hqkBF*
<4=0 + ##S%6$?@DE		""$**,((u j/0s*   I*.II7II&!IIc                     / SQnU Vs/ s H  n[        U5      PM     nnU H,  nU H#  n[        SU< SU< S[        XE5      < 35        M%     M.     gs  snf )z
Just for testing
r  z
*******************
fs1 is:
z


fs2 is:
z

result is:
N)r   r  r   )r   r  fssr  r  r  s         r   demor  
  sW    O  0??JsOL? CU3_.    @s   A__main__)r   r   r   r   r   r  r   r)  r/  r  r  r   )r   )r   )Nr>   Nr   )NFNTr   )r   )r  r   )Jr   rm   rm  	functoolsr   nltk.internalsr   r   nltk.sem.logicr   r   r   r	   r
   r   r   r   r   r   r   r5  r   r   rV  r   r]  r   rc  r   rh  rj  r   rq  rt  r{  r   	Exceptionrz  ry  r  r}  r|  r~  rx  r  r  r  r  r  r   r  r   r   rU  r  r   r  r  r  r  r  r  r   r)  r/  r  r  r  r   r  r  r  rC   __all__r>   r!   r   <module>r     sB  Pd  	 $ <  G$$ G$ G$V :;0{z4 {F
W<z4 W<~$@**E&7, ?H=@B"F24 4
 )* : 
	wt2y 2
d6Nvr@4F<A<F 1,=W	
"&!4 &482E 80) $?2I ?<73U 78& E& E& E&P=7 =
7 ( 	WeW=vx( $N $N $NXa aRDsl$N zFr!   