ó <¿CVc@s¬dZddlmZmZddlZddlmZdd„Zdd„Zddd „Z dd „Z d „Z d d „Z d„Z d„Zedkr¨e ƒndS(uà Utility functions for batch-processing sentences: parsing and extraction of the semantic representation of the root node of the the syntax tree, followed by evaluation of the semantic representation in a first-order model. iÿÿÿÿ(tprint_functiontunicode_literalsN(tevaluateic CsŸddlm}ddlm}m}t||ƒrD||ƒ}n||d|ƒ}g}x<|D]4}|jƒ} t|j| ƒƒ} |j | ƒqcW|S(u Convert input sentences into syntactic trees. :param inputs: sentences to be parsed :type inputs: list of str :param grammar: ``FeatureGrammar`` or name of feature-based grammar :rtype: dict :return: a mapping from input sentences to a list of ``Tree``s iÿÿÿÿ(tFeatureGrammar(tFeatureChartParsert load_parserttrace( t nltk.grammarRt nltk.parseRRt isinstancetsplittlisttparsetappend( tinputstgrammarRRRRtcptparsestsentttokenstsyntrees((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyt parse_sentss   uSEMcCsvddlm}|jƒ}t||ƒs1t‚y ||SWn/tk rnt|ddƒtd|ƒnX‚dS(u Find the semantic representation at the root of a tree. :param syntree: a parse ``Tree`` :param semkey: the feature label to use for the root semantics in the tree :return: the semantic representation at the root of a ``Tree`` :rtype: sem.Expression iÿÿÿÿ(tFeatStructNonterminaltendu u'has no specification for the feature %sN(RRtlabelR tAssertionErrortKeyErrortprint(tsyntreetsemkeyRtnode((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyt root_semrep3s    cCsHgt||d|ƒD].}g|D]}|t||ƒf^q#^qS(uB Add the semantic representation to each syntactic parse tree of each input sentence. :param inputs: a list of sentences :param grammar: ``FeatureGrammar`` or name of feature-based grammar :return: a mapping from sentences to lists of pairs (parse-tree, semantic-representations) :rtype: dict R(RR(RRRRRtsyn((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pytinterpret_sentsGs c CsXgt||ƒD]D}g|D]1\}}|||jd||d|ƒf^q^qS(uu Add the truth-in-a-model value to each semantic representation for each syntactic parse of each input sentences. :param inputs: a list of sentences :param grammar: ``FeatureGrammar`` or name of feature-based grammar :return: a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model) :rtype: dict u%sR(R!R(RRtmodelt assignmentRtinterpretationsR tsem((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pytevaluate_sentsTs cCs-ddddddd tddgƒfdtddgƒfdtdd gƒfdtdd gƒfdtdddgƒfdtdddd gƒfdtd!d"d#d$d%gƒfdtd&d'd(gƒfdtd)d*d+d,gƒfg}tj|ƒ}|j}tj||ƒatj|ƒadS(-Nujohnub1umaryug1usuzieug2ufidoud1utessud2unoosaunugirluboyub2udogubarkuwalkuchaseuseeuinuwith(ujohnub1(umaryug1(usuzieug2(ufidoud1(utessud2(unoosaun(ub1ug1(ub2ug1(ug1ud1(ug2ud2(ub1ug1(ub2ud2(ug1ub1(ud2ub1(ug2un(ub1un(ub2un(ud2un(ub1ug1(ug1ub1(ud1ub1(ub1ud1(tsetRt ValuationtdomaintModeltm0t Assignmenttg0(tvtvaltdom((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyt demo_model0cs&! uutf8cCs•tj|d|ƒ&}g|D]}|jƒ^q}WdQXg|D]}t|ƒdkrD|^qD}g|D]}|ddkso|^qo}|S(Nuriu#(tcodecstopentrstriptlen(tfilenametencodingtfptltsents((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyt read_sents€s %+)cCs‚ddlm}|jdƒ}td|ƒtd ƒxDtdg|dd ƒD]*}|d \}}tƒtd |ƒqPWd S(už Check that interpret_sents() is compatible with legacy grammars that use a lowercase 'sem' feature. Define 'test.fcfg' to be the following iÿÿÿÿ(Ru1 % start S S[sem=] -> 'hello' uReading grammar: %su*iuhelloRusemiuoutput: Nu********************(RRt fromstringRR!(RtgtreadingR R%((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pytdemo_legacy_grammar‰s  cBsRddl}ddlm}d}|d|ƒ}|jdededdd dd d d d dd ƒ|jddddddddƒ|jddddddddƒ|jdddddddd ƒ|jd!d"dd#dd$dd%ƒ|jd&d'd(d)dd*dd+ƒ|jd,d-d(d)dd.dd/ƒ|jd0d1d(d2dd3dd4ƒ|jd5d6d(d2dd7dd8ƒ|jƒ\}}d9d:}eƒd;d<d=d>d?d@g}dA}|jrË|j} n|j rà|j }n|j rûdB|j dUn|dkre | ƒ}ne } e} |jrLe||| | dC|jƒ} ne||dC|jƒ} xçe|ƒD]Ù\}}dD}edE|ƒe|ƒ|jrx£| |D][\}}}e|eƒrâe|jƒƒ}nedF||fƒe|ƒ|dD7}q¯Wqqx6| |D]*\}}edF||fƒ|dD7}qWqqWdS(GNiÿÿÿÿ(t OptionParseru, Parse and evaluate some sentences. t descriptionRtbetatsyntraceitsemtracetdemoudefaultRut sentencesu-du--demotdestudemothelpuBchoose demo D; omit this for the default demo, or specify 'chat80'tmetavaruDu-gu--gramugrammaruread in grammar GuGu-mu--modelumodelu"import model M (omit '.py' suffix)uMu-su --sentencesu sentencesu"read in a file of test sentences SuSu-eu --no-evaltactionu store_falseuevaluateujust do a syntactic analysisu-bu--no-beta-reductionubetaudon't carry out beta-reductionu-tu --syntraceucountusyntraceu.set syntactic tracing on; requires '-e' optionu-Tu --semtraceusemtraceuset semantic tracing onu-iuFido sees a boy with MaryuJohn sees Maryuevery girl chases a doguevery boy chases a girluJohn walks with a girl in Noosau who walksu"grammars/sample_grammars/sem2.fcfguimport %s as modelRiu Sentence: %su%d: %s(tsystoptparseR@t set_defaultstTruet add_optiont parse_argsR1RFRR"tNoneR;R+R-RR&RDR!RCt enumerateRR tdictR'tkeys(RKR@RAtoptstoptionstargstSPACERR:tgramfilet sentsfileR"R=t evaluationstsemrepstiRtnRtsemreptvalue((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyREžsv               !   u__main__(t__doc__t __future__RRR2tnltk.semRRRR!R&R1R;R?REt__name__(((s_/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/sem/util.pyts       U