ó <¿CVc@s1dZddlmZmZddlmZddlZddlZddlZddl m Z ddl m Z m Z ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZdefd „ƒYZd efd „ƒYZ d „Z!d „Z"d„Z#d„Z$e de%fd„ƒYƒZ&ej'dƒZ(ej'dƒZ)ej'dej*ƒZ+d„Z,dd„Z.e de%fd„ƒYƒZ/e de0fd„ƒYƒZ1dZ2dd„Z3e4dd„Z5dd„Z6dd„Z7d dd!„Z8e9d"kr-e8d#d$d ƒndS(%uK This module provides data structures for representing first-order models. iÿÿÿÿ(tprint_functiontunicode_literals(tpformatN(t decorator(t string_typestpython_2_unicode_compatible(tAbstractVariableExpressiont AllExpressiont Expressiont AndExpressiontApplicationExpressiontEqualityExpressiontExistsExpressiont IffExpressiont ImpExpressiontIndividualVariableExpressiontLambdaExpressiontNegatedExpressiont OrExpressiontVariablet is_indvartErrorcBseZRS((t__name__t __module__(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyR"st UndefinedcBseZRS((RR(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyR$scOsvtj|ƒ}tt|d|ƒƒ}|jddƒritƒx%|jƒD]}td|ƒqNWn|||ŽS(Niutraceu%s => %s(tinspectt getargspectdicttziptpoptNonetprinttitems(tftargstkwtargspectdtitem((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyttrace&scCsht|ƒdkrtStd„|DƒƒrTtt|ƒƒtt|ƒƒkrTtStd|ƒ‚dS(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 icss|]}t|tƒVqdS(N(t isinstancettuple(t.0tel((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pys ;su.Set %r contains sequences of different lengthsN(tlentTruetalltmaxtmint ValueError(ts((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytis_rel/s :cCsrtƒ}xb|D]Z}t|tƒr8|j|fƒqt|tƒr]|jt|ƒƒq|j|ƒqW|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 (tsetR(Rtaddtinttstr(R2tnewtelem((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytset2rel@s  cCs*t|ƒdkrdStt|ƒdƒS(ue Check the arity of a relation. :type rel: set of tuples :rtype: int of tuple of str i(R,tlist(trel((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytarityWst ValuationcBsVeZdZd„Zd„Zd„Zed„ƒZed„ƒZe 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. cCs¦tt|ƒjƒxŒ|D]„\}}t|tƒsDt|tƒrQ|||t__init__R(RtboolR4R:ttextwraptfillR1(tselftxstsymtvaltmsg((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyRAns  cCs0||krtj||ƒStd|ƒ‚dS(NuUnknown expression: '%s'(Rt __getitem__R(REtkey((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyRJ~s cCs t|ƒS(N(R(RE((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyt__str__„scCs‘g}x~|jƒD]p}t|tƒr8|j|ƒqt|tƒs|jg|D]%}|D]}|dk r^|^q^qTƒqqWt|ƒS(u7Set-theoretic domain of the value-space of a Valuation.N(tvaluesR(RtappendRBtextendRR4(REtdomRHttuple_R9((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytdomain‡s@cCst|jƒƒS(u9The non-logical constants which the Valuation recognizes.(tsortedtkeys(RE((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytsymbols’scCs t|ƒS(N(tread_valuation(tclsR2((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyt fromstring—s( RRt__doc__RARJRLtpropertyRRRUt classmethodRX(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyR>bs     u \s*=+>\s*u\s*,\s*ug\s* (\([^)]+\)) # tuple-expression \s*cCsÅtj|ƒ}|d}|d}|jdƒr»|dd!}tj|ƒ}|rg}xO|D]5}|dd!}ttj|ƒƒ}|j|ƒqaWntj|ƒ}t|ƒ}n||fS(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 iiu{iÿÿÿÿ( t _VAL_SPLIT_REtsplitt startswitht _TUPLES_REtfindallR)t_ELEMENT_SPLIT_RERNR4(R2tpiecestsymboltvaluet tuple_stringst set_elementsttstelement((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyt_read_valuation_line¥s     cCs¿|dk r|j|ƒ}ng}xŽt|jƒƒD]z\}}|jƒ}|jdƒs7|dkrpq7ny|jt|ƒƒWq7tk r°td||fƒ‚q7Xq7Wt |ƒ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#uuUnable to parse line %s: %sN( Rtdecodet enumeratet splitlineststripR^RNRiR1R>(R2tencodingt statementstlinenumtline((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyRVÇs   t AssignmentcBsSeZdZdd„Zd„Zd„Zdd„Zd„Zd„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 cCs¡tt|ƒjƒ||_|rŠxe|D]Z\}}||jks]td||jfƒ‚t|ƒsytd|ƒ‚|||­sN(R(R tuncurryRRƒR)tfunctiontargumentRttermR tfirsttsecondRRR R RRyRRR5tvariabletnametFalseR-R Rti(RER†R…R'RŠt argumentstfunvaltargvalstargvaltnew_gtutcfRwRH((R…REsc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyRƒ˜s`     !cCsZ|jj|jjkr)|j|jjSt|tƒrF||jjStd|ƒ‚dS(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 uCan't find a value for %sN(RRR€RUR(RR(RER†R…R'((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyR’âs icCs›d}|||}g}t|tƒr8t|ƒ} n|} | |jƒkr~|r|tƒt||d||fƒnxæ|jD]Û} |jƒ} | j| j| ƒ|rÊ|dkrÊ|d} nd} |j || | ƒ} |rt|d| ƒn| t kr0|rat|d|| fƒqaq†|j | ƒ|r†t|d|| | fƒq†q†Wt d„|Dƒƒ}nt 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 %siiu(trying assignment %s)uvalue of '%s' under %s is Falseuvalue of '%s' under %s is %scss|] }|VqdS(N((R*tc((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pys -su%s is not free in %s(R(RRtfreeRRRRyR5RRƒR‘RNR4R(RER†tvarexR…R'tnestingtspacertindentt candidatesRwR˜R—tlowtraceRdtresult((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyt satisfiersüs8     "N( RRRYRAR‚RLRR‡RƒR‘R’R£(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pyR`s     J icCs8tdtfdtfdtfgƒatgƒatttƒattƒa t ƒt dt ƒt dƒt dt ƒt dƒt ƒt dtƒt dt ƒdd d d d d dddddddddddg}xQ|D]I}|rt ƒtj |t |ƒqçt d|tj |t ƒfƒqçWdS(u!Example of a propositional model.uPuQuRu*uPropositional 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)uThe value of '%s' is: %sN( R>R-R‘tval1R4tdom1Rtm1Rrtg1RtmultR‡(R't sentencestsent((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytpropdemo@sD'       c CsIddd dtddgƒfd tdd gƒfd tdgƒfd td!d"d#d$gƒfgattƒatjatttƒattd%d&gƒa |sEt ƒt dt ƒt dƒt dt ƒt dd'dtƒt dt ƒdd d dd ddg}g|D]}t j |ƒ^q}t ƒxT|D]L}y$t d|tj|t ƒfƒWq=tk rˆt d|ƒq=Xq=Wd(dd)fd d*fd d+fg}x‘|D]†\}}yRtjt j |ƒt ƒ} td„|Dƒƒ} t d||| | kfƒWq¸tk r=t d||fƒq¸Xq¸WndS(,uExample of a first-order model.uadamub1ubettyug1ufidoud1ugirlug2uboyub2udoguloveuxuyu*u Models Demou Model m2: u-iu uVariable assignment = uwalksuzu&The interpretation of '%s' in m2 is %su-The interpretation of '%s' in m2 is Undefinedcss*|] }tjtj|ƒtƒVqdS(N(tm2R’RRXtg2(R*Rˆ((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pys ”su%s(%s) evaluates to %su%s(%s) evaluates to UndefinedN(uadamub1(ubettyug1(ufidoud1(ub1ug1(ub2ug2(ug1ub1(ug2ub1(uxub1(uyug2u--------------(uboyuadam(uadam(uadamuy(uyuadam(R4tv2R>tval2RRtdom2RR¬RrR­RR¨RRXR’RR)( tquietR'texprstet parsed_exprsR†t applicationstfunR"R”targsval((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytfolmodelos< <!    " $ $! cCsÑtdtƒtƒtdtƒtdƒtdtƒdddddd d d d d ddddddddg}xT|D]L}tjƒ|r©tj|t|ƒq}td|tj|tƒfƒq}WdS(uF Interpretation of closed expressions in a first-order model. R±u*uFOL Formulas Demoulove (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))uexists z1. boy(z1)u!exists x. (boy(x) & -(x = adam))u&exists x. (boy(x) & all y. love(y, x))uall x. (boy(x) | girl(x))u1all x. (girl(x) -> exists y. boy(y) & love(x, y))u3exists x. (boy(x) & all y. (girl(y) -> love(y, x)))u3exists x. (boy(x) & all y. (girl(y) -> love(x, y)))uall x. (dog(x) -> - girl(x))u-exists x. exists y. (love(x, y) & love(x, y))uThe value of '%s' is: %sN(R¸R-RR¨R­R{R¬R‡(R'tformulastfmla((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytfoldemoœs8     cCstƒtdtƒtdƒtdtƒtdtƒdddddd d d d d dddddddd dg}|rŒttƒnx%|D]}t|ƒtj|ƒq“Wg|D]}tj|ƒ^q»}x>|D]6}tjƒtd|tj |dt|ƒfƒqÝWdS(u5Satisfiers of an open formula in a first order model.u*uSatisfiers DemoR±uboy(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)uexists z22. love(x, z22)uexists y. love(y, x)uall y. (girl(y) -> love(x, y))uall 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))uall y. (dog(y) -> (x = y))u&exists y. (love(adam, y) & love(y, x))uThe satisfiers of '%s' are: %suxN( RR¨R¸R-R¬RRXR­R{R£(R'R¹RºR†tp((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytsatdemoÈsB      "  icCsqitd6td6td6td6}y||d|ƒWn3tk rlx#|D]}||d|ƒqNWnXdS(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 iiiiR'N(R«R¸R»R½tKeyError(tnumR'tdemos((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytdemoös    u__main__iR'(:RYt __future__RRtpprintRRRCtretnltk.decoratorsRt nltk.compatRRtnltk.sem.logicRRRR R R R R RRRRRRRt ExceptionRRR'R3R:R=RR>tcompileR\RatVERBOSER_RiRRVRrtobjectRR¨R«R‘R¸R»R½RÁR(((sc/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/evaluate.pytsD   d   < " €Û /- , .