ó
ù`]c           @  sG  d  Z  d d l m Z m Z d d l m Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d	 e  f d
 „  ƒ  YZ! d e! f d „  ƒ  YZ" d „  Z# d „  Z$ d „  Z% d „  Z& e d e' f d „  ƒ  Yƒ Z( e j) d ƒ Z* e j) d ƒ Z+ e j) d e j, ƒ Z- d „  Z. d d „ Z0 e d e' f d „  ƒ  Yƒ Z1 e d e2 f d „  ƒ  Yƒ Z3 d Z4 d d „ Z5 e6 d d „ Z7 d d „ Z8 d d  „ Z9 d! d d" „ Z: e; d# k rCe: d$ d% d! ƒn  d S(&   uK   
This module provides data structures for representing first-order
models.
iÿÿÿÿ(   t   print_functiont   unicode_literals(   t   pformatN(   t   string_types(   t	   decorator(   t   python_2_unicode_compatible(   t   AbstractVariableExpressiont   AllExpressiont
   Expressiont   AndExpressiont   ApplicationExpressiont   EqualityExpressiont   ExistsExpressiont   IffExpressiont   ImpExpressiont   IndividualVariableExpressiont   LambdaExpressiont   NegatedExpressiont   OrExpressiont   Variablet	   is_indvart   Errorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR   0   s   t	   Undefinedc           B  s   e  Z RS(    (   R   R   (    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR   4   s   c         O  s›   t  j d d k r% t j |  ƒ } n t j |  ƒ } t t | d | ƒ ƒ } | j d d  ƒ rŽ t	 ƒ  x% | j
 ƒ  D] } t	 d | ƒ qs Wn  |  | | Ž  S(   Ni    i   u   traceu   %s => %s(   t   syst   version_infot   inspectt   getfullargspect
   getargspect   dictt   zipt   popt   Nonet   printt   items(   t   ft   argst   kwt   argspect   dt   item(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   trace8   s    c         C  sh   t  |  ƒ d k r t St d „  |  Dƒ ƒ rT t  t |  ƒ ƒ t  t |  ƒ ƒ k rT t St d |  ƒ ‚ d S(   u    
    Check whether a set represents a relation (of any arity).

    :param s: a set containing tuples of str elements
    :type s: set
    :rtype: bool
        i    c         s  s   |  ] } t  | t ƒ Vq d  S(   N(   t
   isinstancet   tuple(   t   .0t   el(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pys	   <genexpr>Q   s    u.   Set %r contains sequences of different lengthsN(   t   lent   Truet   allt   maxt   mint
   ValueError(   t   s(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   is_relE   s
    	:c         C  sr   t  ƒ  } xb |  D]Z } t | t ƒ r8 | j | f ƒ q t | t ƒ r] | j t | ƒ ƒ q | j | ƒ q W| S(   uR  
    Convert a set containing individuals (strings or numbers) into a set of
    unary tuples. Any tuples of strings already in the set are passed through
    unchanged.

    For example:
      - set(['a', 'b']) => set([('a',), ('b',)])
      - set([3, 27]) => set([('3',), ('27',)])

    :type s: set
    :rtype: set of tuple of str
    (   t   setR+   R   t   addt   intt   str(   R5   t   newt   elem(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   set2relW   s    	c         C  s*   t  |  ƒ d k r d St  t |  ƒ d ƒ S(   ue   
    Check the arity of a relation.
    :type rel: set of tuples
    :rtype: int of tuple of str
    i    (   R/   t   list(   t   rel(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   arityo   s    t	   Valuationc           B  sV   e  Z d  Z d „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e	 d „  ƒ Z
 RS(   uâ  
    A dictionary which represents a model-theoretic Valuation of non-logical constants.
    Keys are strings representing the constants to be interpreted, and values correspond
    to individuals (represented as strings) and n-ary relations (represented as sets of tuples
    of strings).

    An instance of ``Valuation`` will raise a KeyError exception (i.e.,
    just behave like a standard  dictionary) if indexed with an expression that
    is not in its list of symbols.
    c         C  s¦   t  t |  ƒ j ƒ  xŒ | D]„ \ } } t | t ƒ sD t | t ƒ rQ | |  | <q t | t ƒ rs t | ƒ |  | <q t j	 d | | f d d ƒ} t
 | ƒ ‚ q Wd S(   u=   
        :param xs: a list of (symbol, value) pairs.
        uG   Error in initializing Valuation. Unrecognized value for symbol '%s':
%st   widthiB   N(   t   superRA   t   __init__R+   R   t   boolR7   R=   t   textwrapt   fillR4   (   t   selft   xst   symt   valt   msg(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRD   ‡   s    	c         C  s0   | |  k r t  j |  | ƒ St d | ƒ ‚ d  S(   Nu   Unknown expression: '%s'(   R   t   __getitem__R   (   RH   t   key(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRM   š   s    c         C  s
   t  |  ƒ S(   N(   R   (   RH   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   __str__    s    c         C  s‘   g  } x~ |  j  ƒ  D]p } t | t ƒ r8 | j | ƒ q t | t ƒ s | j g  | D]% } | D] } | d k	 r^ | ^ q^ qT ƒ q q Wt | ƒ S(   u7   Set-theoretic domain of the value-space of a Valuation.N(   t   valuesR+   R   t   appendRE   t   extendR!   R7   (   RH   t   domRK   t   tuple_R<   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   domain£   s    :c         C  s   t  |  j ƒ  ƒ S(   u9   The non-logical constants which the Valuation recognizes.(   t   sortedt   keys(   RH   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   symbols°   s    c         C  s
   t  | ƒ S(   N(   t   read_valuation(   t   clsR5   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt
   fromstringµ   s    (   R   R   t   __doc__RD   RM   RO   t   propertyRU   RX   t   classmethodR[   (    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRA   z   s   			u	   \s*=+>\s*u   \s*,\s*ug   \s*
                                (\([^)]+\))  # tuple-expression
                                \s*c         C  sÅ   t  j |  ƒ } | d } | d } | j d ƒ r» | d d !} t j | ƒ } | r g  } xO | D]5 } | d d !} t t j | ƒ ƒ } | j | ƒ qa Wn t j | ƒ } t | ƒ } n  | | f S(   u  
    Read a line in a valuation file.

    Lines are expected to be of the form::

      noosa => n
      girl => {g1, g2}
      chase => {(b1, g1), (b2, g1), (g1, d1), (g2, d2)}

    :param s: input line
    :type s: str
    :return: a pair (symbol, value)
    :rtype: tuple
    i    i   u   {iÿÿÿÿ(	   t   _VAL_SPLIT_REt   splitt
   startswitht
   _TUPLES_REt   findallR,   t   _ELEMENT_SPLIT_RERQ   R7   (   R5   t   piecest   symbolt   valuet   tuple_stringst   set_elementst   tst   element(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   _read_valuation_lineÇ   s    

c         C  s¿   | d k	 r |  j | ƒ }  n  g  } xŽ t |  j ƒ  ƒ D]z \ } } | j ƒ  } | j d ƒ s7 | d k rp q7 n  y | j t | ƒ ƒ Wq7 t k
 r° t d | | f ƒ ‚ q7 Xq7 Wt	 | ƒ S(   u  
    Convert a valuation string into a valuation.

    :param s: a valuation string
    :type s: str
    :param encoding: the encoding of the input string, if it is binary
    :type encoding: str
    :return: a ``nltk.sem`` valuation
    :rtype: Valuation
    u   #u    u   Unable to parse line %s: %sN(
   R!   t   decodet	   enumeratet
   splitlinest   stripRa   RQ   Rl   R4   RA   (   R5   t   encodingt
   statementst   linenumt   line(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRY   ê   s    t
   Assignmentc           B  sS   e  Z d  Z d d „ Z d „  Z d „  Z d d „ Z d „  Z d „  Z	 d „  Z
 RS(	   ud  
    A dictionary which represents an assignment of values to variables.

    An assigment can only assign values from its domain.

    If an unknown expression *a* is passed to a model *M*\ 's
    interpretation function *i*, *i* will first check whether *M*\ 's
    valuation assigns an interpretation to *a* as a constant, and if
    this fails, *i* will delegate the interpretation of *a* to
    *g*. *g* only assigns values to individual variables (i.e.,
    members of the class ``IndividualVariableExpression`` in the ``logic``
    module. If a variable is not assigned a value by *g*, it will raise
    an ``Undefined`` exception.

    A variable *Assignment* is a mapping from individual variables to
    entities in the domain. Individual variables are usually indicated
    with the letters ``'x'``, ``'y'``, ``'w'`` and ``'z'``, optionally
    followed by an integer (e.g., ``'x0'``, ``'y332'``).  Assignments are
    created using the ``Assignment`` constructor, which also takes the
    domain as a parameter.

        >>> from nltk.sem.evaluate import Assignment
        >>> dom = set(['u1', 'u2', 'u3', 'u4'])
        >>> g3 = Assignment(dom, [('x', 'u1'), ('y', 'u2')])
        >>> g3 == {'x': 'u1', 'y': 'u2'}
        True

    There is also a ``print`` format for assignments which uses a notation
    closer to that in logic textbooks:

        >>> print(g3)
        g[u1/x][u2/y]

    It is also possible to update an assignment using the ``add`` method:

        >>> dom = set(['u1', 'u2', 'u3', 'u4'])
        >>> g4 = Assignment(dom)
        >>> g4.add('x', 'u1')
        {'x': 'u1'}

    With no arguments, ``purge()`` is equivalent to ``clear()`` on a dictionary:

        >>> g4.purge()
        >>> g4
        {}

    :param domain: the domain of discourse
    :type domain: set
    :param assign: a list of (varname, value) associations
    :type assign: list
    c         C  s¡   t  t |  ƒ j ƒ  | |  _ | rŠ xe | D]Z \ } } | |  j k s] t d | |  j f ƒ ‚ t | ƒ sy t d | ƒ ‚ | |  | <q) Wn  d  |  _ |  j ƒ  d  S(   Nu   '%s' is not in the domain: %su-   Wrong format for an Individual Variable: '%s'(	   RC   Ru   RD   RU   t   AssertionErrorR   R!   t   variantt   _addvariant(   RH   RU   t   assignt   varRK   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRD   9  s    		c         C  s0   | |  k r t  j |  | ƒ St d | ƒ ‚ d  S(   Nu"   Not recognized as a variable: '%s'(   R   RM   R   (   RH   RN   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRM   I  s    c         C  s    t  |  j ƒ } | j |  ƒ | S(   N(   Ru   RU   t   update(   RH   R;   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   copyO  s    c         C  s(   | r |  | =n
 |  j  ƒ  |  j ƒ  d S(   u¼   
        Remove one or all keys (i.e. logic variables) from an
        assignment, and update ``self.variant``.

        :param var: a Variable acting as a key for the assignment.
        N(   t   clearRx   R!   (   RH   Rz   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   purgeT  s
    


c         C  sD   d } t  |  j ƒ } x( | D]  \ } } | d | | f 7} q W| S(   uQ   
        Pretty printing for assignments. {'x', 'u'} appears as 'g[u/x]'
        u   gu   [%s/%s](   RV   Rw   (   RH   t   gstringRw   RK   Rz   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRO   b  s
    c         C  sK   g  } x5 |  j  ƒ  D]' } | d | d f } | j | ƒ q W| |  _ d S(   uK   
        Create a more pretty-printable version of the assignment.
        i   i    N(   R#   RQ   Rw   R!   (   RH   t   list_R)   t   pair(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRx   m  s    	c         C  s\   | |  j  k s( t d | |  j  f ƒ ‚ t | ƒ sD t d | ƒ ‚ | |  | <|  j ƒ  |  S(   uh   
        Add a new variable-value pair to the assignment, and update
        ``self.variant``.

        u   %s is not in the domain %su-   Wrong format for an Individual Variable: '%s'(   RU   Rv   R   Rx   (   RH   Rz   RK   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR8   x  s
    (

N(   R   R   R\   R!   RD   RM   R|   R~   RO   Rx   R8   (    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRu     s   4				t   Modelc           B  s\   e  Z d  Z d „  Z d „  Z d „  Z d	 d „ Z d	 d „ Z e	 d „ Z
 d	 d d „ Z RS(
   u[  
    A first order model is a domain *D* of discourse and a valuation *V*.

    A domain *D* is a set, and a valuation *V* is a map that associates
    expressions with values in the model.
    The domain of *V* should be a subset of *D*.

    Construct a new ``Model``.

    :type domain: set
    :param domain: A set of entities representing the domain of discourse of the model.
    :type valuation: Valuation
    :param valuation: the valuation of the model.
    :param prop: If this is set, then we are building a propositional    model and don't require the domain of *V* to be subset of *D*.
    c         C  sY   t  | t ƒ s t ‚ | |  _ | |  _ | j | j ƒ sU t d | j | f ƒ ‚ n  d  S(   NuD   The valuation domain, %s, must be a subset of the model's domain, %s(   R+   R7   Rv   RU   t	   valuationt
   issupersetR   (   RH   RU   Rƒ   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRD   ˜  s    		c         C  s   d |  j  |  j f S(   Nu   (%r, %r)(   RU   Rƒ   (   RH   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   __repr__¢  s    c         C  s   d |  j  |  j f S(   Nu   Domain = %s,
Valuation = 
%s(   RU   Rƒ   (   RH   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyRO   ¥  s    c         C  s“   yV t  j | ƒ } |  j | | d | ƒ} | rQ t ƒ  t d | | | f ƒ n  | SWn6 t k
 rŽ | rŠ t ƒ  t d | | f ƒ n  d SXd S(   uA  
        Read input expressions, and provide a handler for ``satisfy``
        that blocks further propagation of the ``Undefined`` error.
        :param expr: An ``Expression`` of ``logic``.
        :type g: Assignment
        :param g: an assignment to individual variables.
        :rtype: bool or 'Undefined'
        R*   u    '%s' evaluates to %s under M, %su   '%s' is undefined under M, %su	   UndefinedN(   R   R[   t   satisfyR"   R   (   RH   t   exprt   gR*   t   parsedRg   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   evaluate¨  s    	c           s  t  | t ƒ r  | j ƒ  \ } } t  | t ƒ rk ˆ j | ˆ  ƒ } t ‡  ‡ f d †  | Dƒ ƒ } | | k Sˆ j | j ˆ  ƒ } ˆ j | j ˆ  ƒ } | | Snrt  | t ƒ rÃ ˆ j | j	 ˆ  ƒ St  | t
 ƒ rú ˆ j | j ˆ  ƒ où ˆ j | j ˆ  ƒ St  | t ƒ r1ˆ j | j ˆ  ƒ p0ˆ j | j ˆ  ƒ St  | t ƒ riˆ j | j ˆ  ƒ phˆ j | j ˆ  ƒ St  | t ƒ r ˆ j | j ˆ  ƒ ˆ j | j ˆ  ƒ k St  | t ƒ r×ˆ j | j ˆ  ƒ ˆ j | j ˆ  ƒ k St  | t ƒ r9ˆ  j ƒ  }	 x@ ˆ j D]5 }
 |	 j | j j |
 ƒ ˆ j | j	 |	 ƒ süt SqüWt St  | t ƒ r›ˆ  j ƒ  }	 x@ ˆ j D]5 }
 |	 j | j j |
 ƒ ˆ j | j	 |	 ƒ r^t Sq^Wt St  | t ƒ rÿi  } | j j } x< ˆ j D]1 }
 ˆ j | j	 ˆ  j | |
 ƒ ƒ } | | |
 <qÆW| Sˆ j | ˆ  | ƒ Sd S(   u  
        Recursive interpretation function for a formula of first-order logic.

        Raises an ``Undefined`` error when ``parsed`` is an atomic string
        but is not a symbol or an individual variable.

        :return: Returns a truth value or ``Undefined`` if ``parsed`` is        complex, and calls the interpretation function ``i`` if ``parsed``        is atomic.

        :param parsed: An expression of ``logic``.
        :type g: Assignment
        :param g: an assignment to individual variables.
        c         3  s!   |  ] } ˆ j  | ˆ  ƒ Vq d  S(   N(   R†   (   R-   t   arg(   Rˆ   RH   (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pys	   <genexpr>Ó  s    N(   R+   R
   t   uncurryR   R†   R,   t   functiont   argumentR   t   termR	   t   firstt   secondR   R   R   R   R   R|   RU   R8   t   variablet   namet   FalseR0   R   R   t   i(   RH   R‰   Rˆ   R*   R   t	   argumentst   funvalt   argvalst   argvalt   new_gt   ut   cfRz   RK   (    (   Rˆ   RH   s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR†   ¾  sV    
(()((!c         C  sZ   | j  j |  j j k r) |  j | j  j St | t ƒ rF | | j  j St d | ƒ ‚ d S(   uÈ  
        An interpretation function.

        Assuming that ``parsed`` is atomic:

        - if ``parsed`` is a non-logical constant, calls the valuation *V*
        - else if ``parsed`` is an individual variable, calls assignment *g*
        - else returns ``Undefined``.

        :param parsed: an ``Expression`` of ``logic``.
        :type g: Assignment
        :param g: an assignment to individual variables.
        :return: a semantic value
        u   Can't find a value for %sN(   R’   R“   Rƒ   RX   R+   R   R   (   RH   R‰   Rˆ   R*   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR•     s
    i    c         C  s›  d } | | | } g  } t  | t ƒ r8 t | ƒ }	 n | }	 |	 | j ƒ  k r~| r| t ƒ  t | | d | | f ƒ n  xæ |  j D]Û }
 | j ƒ  } | j |	 j |
 ƒ | rÊ | d k rÊ | d } n d } |  j	 | | | ƒ } | r t | d | ƒ n  | t
 k r0| rat | d | | f ƒ qaq† | j |
 ƒ | r† t | d | | | f ƒ q† q† Wt d „  | Dƒ ƒ } n t d	 |	 j | f ƒ ‚ | S(
   u¥  
        Generate the entities from the model's domain that satisfy an open formula.

        :param parsed: an open formula
        :type parsed: Expression
        :param varex: the relevant free individual variable in ``parsed``.
        :type varex: VariableExpression or str
        :param g: a variable assignment
        :type g:  Assignment
        :return: a set of the entities that satisfy ``parsed``.
        u      u'   Open formula is '%s' with assignment %si   i    u   (trying assignment %s)u   value of '%s' under %s is Falseu   value of '%s' under %s is %sc         s  s   |  ] } | Vq d  S(   N(    (   R-   t   c(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pys	   <genexpr>V  s    u   %s is not free in %s(   R+   R   R   t   freeR"   RU   R|   R8   R“   R†   R”   RQ   R7   R   (   RH   R‰   t   varexRˆ   R*   t   nestingt   spacert   indentt
   candidatesRz   R›   Rš   t   lowtraceRg   t   result(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt
   satisfiers  sB    N(   R   R   R\   RD   R…   RO   R!   RŠ   R†   R”   R•   R¦   (    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyR‚   …  s   	
		Ei   c         C  s8  t  d t f d t f d t f g ƒ a t g  ƒ a t t t ƒ a t t ƒ a	 t
 ƒ  t
 d t ƒ t
 d ƒ t
 d t ƒ t
 d ƒ t
 ƒ  t
 d t ƒ t
 d t ƒ d d	 d
 d d d d d d d d d d d d d d g } xQ | D]I } |  rt
 ƒ  t j | t	 |  ƒ qç t
 d | t j | t	 ƒ f ƒ qç Wd S(   u!   Example of a propositional model.u   Pu   Qu   Ru   *u   Propositional Formulas Demou7   (Propositional constants treated as nullary predicates)u
   Model m1:
u   (P & Q)u   (P & R)u   - Pu   - Ru   - - Pu	   - (P & R)u   (P | R)u   (R | P)u   (R | R)u	   (- P | R)u	   (P | - P)u   (P -> Q)u   (P -> R)u   (R -> P)u	   (P <-> P)u	   (R <-> R)u	   (P <-> R)u   The value of '%s' is: %sN(   RA   R0   R”   t   val1R7   t   dom1R‚   t   m1Ru   t   g1R"   t   multRŠ   (   R*   t	   sentencest   sent(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   propdemof  sD    '

	c         C  sI  d d d  d t  d d g ƒ f d	 t  d d
 g ƒ f d t  d g ƒ f d t  d! d" d# d$ g ƒ f g a t t ƒ a t j a t t t ƒ a t t d% d& g ƒ a	 |  sEt
 ƒ  t
 d t ƒ t
 d ƒ t
 d t ƒ t
 d d' d t ƒ t
 d t	 ƒ d d	 d d d d d g } g  | D] } t j | ƒ ^ q} t
 ƒ  xT | D]L } y$ t
 d | t j | t	 ƒ f ƒ Wq=t k
 rˆt
 d | ƒ q=Xq=Wd( d d) f d d* f d d+ f g } x‘ | D]† \ } } yR t j t j | ƒ t	 ƒ }	 t d „  | Dƒ ƒ }
 t
 d | | |
 |	 k f ƒ Wq¸t k
 r=t
 d | | f ƒ q¸Xq¸Wn  d S(,   u   Example of a first-order model.u   adamu   b1u   bettyu   g1u   fidou   d1u   girlu   g2u   boyu   b2u   dogu   loveu   xu   yu   *u   Models Demou
   Model m2:
u   -i   u   
u   Variable assignment = u   walksu   zu&   The interpretation of '%s' in m2 is %su-   The interpretation of '%s' in m2 is Undefinedc         s  s*   |  ]  } t  j t j | ƒ t ƒ Vq d  S(   N(   t   m2R•   R   R[   t   g2(   R-   R‹   (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pys	   <genexpr>É  s    u   %s(%s) evaluates to %su   %s(%s) evaluates to UndefinedN(   u   adamu   b1(   u   bettyu   g1(   u   fidou   d1(   u   b1u   g1(   u   b2u   g2(   u   g1u   b1(   u   g2u   b1(   u   xu   b1(   u   yu   g2u   --------------(   u   boyu   adam(   u   adam(   u   adamu   y(   u   yu   adam(   R7   t   v2RA   t   val2RU   t   dom2R‚   R¯   Ru   R°   R"   R«   R   R[   R•   R   R,   (   t   quietR*   t   exprst   et   parsed_exprsR‰   t   applicationst   funR%   R—   t   argsval(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   folmodel—  sN    !	
"		!c         C  sÑ   t  d t ƒ t ƒ  t d t ƒ t d ƒ t d t ƒ d d d d d d	 d
 d d d d d d d d d d d g } xT | D]L } t j ƒ  |  r© t j | t |  ƒ q} t d | t j | t ƒ f ƒ q} Wd S(   uF   
    Interpretation of closed expressions in a first-order model.
    R´   u   *u   FOL Formulas Demou   love (adam, betty)u   (adam = mia)u   \x. (boy(x) | girl(x))u   \x. boy(x)(adam)u   \x y. love(x, y)u   \x y. love(x, y)(adam)(betty)u   \x y. love(x, y)(adam, betty)u   \x y. (boy(x) & love(x, y))u#   \x. exists y. (boy(x) & love(x, y))u   exists z1. boy(z1)u!   exists x. (boy(x) &  -(x = adam))u&   exists x. (boy(x) & all y. love(y, x))u   all x. (boy(x) | girl(x))u1   all x. (girl(x) -> exists y. boy(y) & love(x, y))u3   exists x. (boy(x) & all y. (girl(y) -> love(y, x)))u3   exists x. (boy(x) & all y. (girl(y) -> love(x, y)))u   all x. (dog(x) -> - girl(x))u-   exists x. exists y. (love(x, y) & love(x, y))u   The value of '%s' is: %sN(   R»   R0   R"   R«   R°   R~   R¯   RŠ   (   R*   t   formulast   fmla(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   foldemoÓ  s8    
	
c         C  s  t  ƒ  t  d t ƒ t  d ƒ t  d t ƒ t d t ƒ d d d d d d	 d
 d d d d d d d d d d d d g } |  rŒ t  t ƒ n  x% | D] } t  | ƒ t j | ƒ q“ Wg  | D] } t j | ƒ ^ q» } x> | D]6 } t j ƒ  t  d | t j	 | d t |  ƒ f ƒ qÝ Wd S(   u5   Satisfiers of an open formula in a first order model.u   *u   Satisfiers DemoR´   u   boy(x)u   (x = x)u   (boy(x) | girl(x))u   (boy(x) & girl(x))u   love(adam, x)u   love(x, adam)u   -(x = adam)u   exists z22. love(x, z22)u   exists y. love(y, x)u   all y. (girl(y) -> love(x, y))u   all y. (girl(y) -> love(y, x))u)   all y. (girl(y) -> (boy(x) & love(y, x)))u)   (boy(x) & all y. (girl(y) -> love(x, y)))u)   (boy(x) & all y. (girl(y) -> love(y, x)))u+   (boy(x) & exists y. (girl(y) & love(y, x)))u   (girl(x) -> dog(x))u   all y. (dog(y) -> (x = y))u&   exists y. (love(adam, y) & love(y, x))u   The satisfiers of '%s' are: %su   xN(
   R"   R«   R»   R0   R¯   R   R[   R°   R~   R¦   (   R*   R¼   R½   R‰   t   p(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   satdemoÿ  sB    
	
"
i    c         C  sq   i t  d 6t d 6t d 6t d 6} y | |  d | ƒ Wn3 t k
 rl x# | D] }  | |  d | ƒ qN Wn Xd S(   uO  
    Run exists demos.

     - num = 1: propositional logic demo
     - num = 2: first order model demo (only if trace is set)
     - num = 3: first order sentences demo
     - num = 4: satisfaction of open formulas demo
     - any other value: run all the demos

    :param trace: trace = 1, or trace = 2 for more verbose tracing
    i   i   i   i   R*   N(   R®   R»   R¾   RÀ   t   KeyError(   t   numR*   t   demos(    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   demo-  s    "u   __main__i   R*   (<   R\   t
   __future__R    R   t   pprintR   R   RF   t   reR   t   sixR   t   nltk.decoratorsR   t   nltk.compatR   t   nltk.sem.logicR   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   t	   ExceptionR   R   R*   R6   R=   R@   R   RA   t   compileR_   Rd   t   VERBOSERb   Rl   R!   RY   Ru   t   objectR‚   R«   R®   R”   R»   R¾   RÀ   RÄ   R   (    (    (    s0   lib/python2.7/site-packages/nltk/sem/evaluate.pyt   <module>   sJ   d				B	#Ü1<,.