ó <¿CVc@sRdZddlmZmZddlmZddlmZddlm Z ddl m Z ddl m Z mZmZmZmZmZmZmZmZdefd „ƒYZd efd „ƒYZejd efd „ƒYƒZded„ZedkrNxedƒD]Ze eƒqWe ƒx!edƒD]Ze eƒq7WndS(u An implementation of the Hole Semantics model, following Blackburn and Bos, Representation and Inference for Natural Language (CSLI, 2005). The semantic representations are built by the grammar hole.fcfg. This module contains driver code to read in sentences and parse them according to a hole semantics grammar. After parsing, the semantic representation is in the form of an underspecified representation that is not easy to read. We use a "plugging" algorithm to convert that representation into first-order logic formulas. iÿÿÿÿ(tprint_functiontunicode_literals(treduce(tcompat(t load_parser(t skolemize( t AllExpressiont AndExpressiontApplicationExpressiontExistsExpressiont IffExpressiont ImpExpressiontLambdaExpressiontNegatedExpressiont OrExpressiont ConstantscBsŽeZdZdZdZdZdZdZdZdZ dZ d Z d Z id „e6d „e6e e6ee6ee6ee6ee6ee 6ZRS( uALLuEXISTSuNOTuANDuORuIMPuIFFuPREDuLEQuHOLEuLABELcCst|j|ƒS(N(Rtvariable(tvte((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt8scCst|j|ƒS(N(R R(RR((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyR9s(t__name__t __module__tALLtEXISTStNOTtANDtORtIMPtIFFtPREDtLEQtHOLEtLABELR RRR R RtMAP(((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyR+s&  t HoleSemanticscBsƒeZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z d „Z d „Zd „ZRS(ur This class holds the broken-down components of a hole semantics, i.e. it extracts the holes, labels, logic formula fragments and constraints out of a big conjunction of such as produced by the hole semantics grammar. It then provides some operations on the semantics dealing with holes, labels and finding legal ways to plug holes with labels. cCs\tƒ|_tƒ|_i|_tƒ|_|j|ƒ|jƒ|_|jƒ|_ dS(uB Constructor. `usr' is a ``sem.Expression`` representing an Underspecified Representation Structure (USR). A USR has the following special predicates: ALL(l,v,n), EXISTS(l,v,n), AND(l,n,n), OR(l,n,n), IMP(l,n,n), IFF(l,n,n), PRED(l,v,n,v[,v]*) where the brackets and star indicate zero or more repetitions, LEQ(n,n), HOLE(n), LABEL(n) where l is the label of the node described by the predicate, n is either a label or a hole, and v is a variable. N( tsettholestlabelst fragmentst constraintst _break_downt_find_top_most_labelsttop_most_labelst_find_top_holettop_hole(tselftusr((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt__init__Js     cCs||j|jBkS(ue Return true if x is a node (label or hole) in this semantic representation. (R%R$(R-tx((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pytis_nodedscCs3t|tƒr2|j|jƒ|j|jƒnýt|tƒr|jƒ\}}|jjt j krŒ|j j t |d|dƒƒq/|jjt jkr¸|jj |dƒq/|jjt jkrä|jj |dƒq/|d}||jkst‚||df|j|tremoveRFt_sanity_check_pluggingR,tappendRI( R-tholet ancestors0RNtpotential_labels0t plug_acc0RJRRtlRPRO((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyRLÄs       cCs=x6|jD]+}|j|kr |j|kr5tSq q WtS(u· Return True if the `label' cannot be placed underneath the holes given by the set `ancestors' because it would violate the constraints imposed on it. (R'tlhstrhstTruetFalse(R-R;RRtc((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyRUîs  cCsÑ||jkr)|g|}||}n|}||jksDt‚x8|jD]-}|j|krN|j|ks{t‚qNqNW|j|d}x:|D]2}|j|ƒr—|j|||g|ƒq—q—WdS(uÏ Make sure that a given plugging is legal. We recursively go through each node and make sure that no constraints are violated. We also check that all holes have been filled. iN( R$R%R9R'R^R_R&R1RW(R-tpluggingRQRRR;RbR=RD((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyRWús   cCs|j||jƒS(u„ Return the first-order logic formula tree for this underspecified representation using the plugging given. (t _formula_treeR,(R-Rc((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt formula_treescCs‰||kr |j|||ƒS||jkr|j|\}}g|D]}|j||ƒ^qI}ttj|jj|ƒS|SdS(N(RdR&RRR!RR6(R-RcRQtpredR=RDtchildren((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyRds %(RRt__doc__R/R1R(RER)R+RKRIRLRURWReRd(((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyR"Bs       *  R8cBs;eZdZd„Zd„Zd„Zd„Zd„ZRS(u~ This class represents a constraint of the form (L =< N), where L is a label and N is a node (a label or a hole). cCs||_||_dS(N(R^R_(R-R^R_((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyR/&s cCs<|j|jkr4|j|jko3|j|jkStSdS(N(t __class__R^R_Ra(R-tother((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt__eq__*s"cCs ||k S(N((R-Rj((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt__ne__0scCstt|ƒƒS(N(thashtrepr(R-((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt__hash__3scCsd|j|jfS(Nu (%s < %s)(R^R_(R-((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt__repr__6s(RRRhR/RkRlRoRp(((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyR8 s     cCs|sd}n|r%td|ƒnt|ƒ}|jƒ}t|j|ƒƒ}|rotdt|ƒƒng}x—|D]}|jƒdjƒ}|r®td|ƒnxt|t ƒrÌ|j }q±Wt |ƒ} |rïtd| ƒnt | ƒ} |r’td| j ƒtd| jƒtd | jƒtd | jƒtd | jƒtd ƒx4| jjƒD] \} } td | | fƒqkWn| jƒ} tt| j| ƒƒ}|rþx5t|ƒD]'\}}tƒtd||fƒqÉWtƒn|j|ƒq|W|S(Nu"grammars/sample_grammars/hole.fcfguReading grammar fileuGot %d different parsesuSEMu Raw: u Skolemized:u Holes: u Labels: u Constraints: u Top hole: u Top labels: u Fragments:u %s: %su%d. %s(tprintRtsplittlisttparseRFR;tsimplifyR2R ttermRR"R$R%R'R,R*R&titemsRKtmapRet enumeratetextend(tsentencetgrammar_filenametverbosetparserttokensttreest all_readingsttreetsemt skolemizedthole_semR]RCRKtreadingstitr((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyt hole_readings:sL          u__main__u a dog barksuevery girl chases a dogN(!Rht __future__RRt functoolsRtnltkRt nltk.parseRtnltk.sem.skolemizeRtnltk.sem.logicRRRR R R R R RtobjectRR"tpython_2_unicode_compatibleR8tNoneRaR‰RRˆRq(((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/hole.pyts"@ Þ A