JL i+"~dZddlZddlmZd dZddZddZd dZdZdd Z d Z d Z e d k(re e yy)z 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. N)evaluatecddlm}ddlm}m}t ||r ||}n |||}g}|D]=}|j } t|j| } |j| ?|S)a Convert input sentences into syntactic trees. :param inputs: sentences to be parsed :type inputs: list(str) :param grammar: ``FeatureGrammar`` or name of feature-based grammar :type grammar: nltk.grammar.FeatureGrammar :rtype: list(nltk.tree.Tree) or dict(list(str)): list(Tree) :return: a mapping from input sentences to a list of ``Tree`` instances. rFeatureGrammar)FeatureChartParser load_parsertrace) nltk.grammarr nltk.parserr isinstancesplitlistparseappend) inputsgrammarr rrrcpparsessenttokenssyntreess S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/sem/util.py parse_sentsrso,:'>*  (  . F () h  Mcddlm}|j}t||sJ ||S#t$rt |dt d|zYwxYw)a 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 r)FeatStructNonterminal )endz'has no specification for the feature %s)r rlabelr KeyErrorprint)syntreesemkeyrnodes r root_semrepr&4s_3 ==?D d1 22 2BF| B d 7&@A Bs+$AAc t|||Dcgc]}|Dcgc]}|t||fc} c}}Scc}wcc}}w)a Add the semantic representation to each syntactic parse tree of each input sentence. :param inputs: a list of sentences :type inputs: list(str) :param grammar: ``FeatureGrammar`` or name of feature-based grammar :type grammar: nltk.grammar.FeatureGrammar :return: a mapping from sentences to lists of pairs (parse-tree, semantic-representations) :rtype: list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression))) r )rr&)rrr$r rsyns rinterpret_sentsr)IsG$FG5A  5==S#{3' (= = s >9 >>ct||Dcgc]/}|Dcgc]\}}|||jd|z||f!c}}1c}}}Scc}}wcc}}}w)a 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 :type inputs: list(str) :param grammar: ``FeatureGrammar`` or name of feature-based grammar :type grammar: nltk.grammar.FeatureGrammar :return: a mapping from sentences to lists of triples (parse-tree, semantic-representations, evaluation-in-model) :rtype: list(list(tuple(nltk.tree.Tree, nltk.sem.logic.ConstantExpression, bool or dict(str): bool))) z%sr )r)r)rrmodel assignmentr interpretationsr(sems revaluate_sentsr/[sh" /vw?    . c#u~~dSj*E~J K    s A$A A Acddddddddd hfd d d hfd ddhfdddhfdhdfdhdfdhdfdhdfdhdfg}tj|}|j}tj||atj |ay)N)johnb1)maryg1)suzieg2)fidod1)tessd2)noosangirlr4r6boyr2b2dogr8r:barkwalk>r2r8r6chase>r?r4r4r8r6r:r2r4see>r?r:r:r2r6r<rGr4r2in>r2r<r?r<r:r<with>r2r8r8r2rGrL)r ValuationdomainModelm0 Assignmentg0)vvaldoms r demo_model0r]ps  $ t  t  $ #$ JK  Q  67 IJ% A*   Q C **C S !B   S !Brc"tj|d|5}|Dcgc]}|j}}dddDcgc]}t|dkDs|}}|Dcgc] }|ddk(r |}}|Scc}w#1swYFxYwcc}wcc}w)Nrr#)codecsopenrstriplen)filenameencodingfplsentss r read_sentsrjs XsH -)%'((() ,1Q!Q ,E , 11QqTS[Q 1E 1 L ))) - 1s3A;A6A;BB B .B 6A;;Bcddlm}|jd}td|ztdt dg|dD] }|d\}}ttd |"y ) z Check that interpret_sents() is compatible with legacy grammars that use a lowercase 'sem' feature. Define 'test.fcfg' to be the following rrz1 % start S S[sem=] -> 'hello' zReading grammar: %sz********************hellor.)r$zoutput: N)r r fromstringr"r))rgreadingr(r.s rdemo_legacy_grammarrpsi,!!  A  ! #$ (O"G9a>1:S  j#rc ddl}ddlm}d}||}|jddddddd|j d d d d d |j ddddd|j ddddd|j ddddd|j ddd d!d"#|j d$d%d d&d'#|j d(d)d*d+d,#|j d-d.d*d/d0#|j \}}d1}t gd2}d3}|jr |j} |jr |j}|jrtd4|jz| t }t} t} |jrt||| | |j 5} nt#|||j$5} t'|D]\}}d6}t)d7|zt)||jrX |D]O\}}}t+|t,rt/|j1}t)d8||fzt)||d6z }Q |D]\}}t)d8||fz|d6z }y)9Nr) OptionParserz, Parse and evaluate some sentences. ) descriptionTdefault)rbetasyntracesemtracedemor sentencesz-dz--demoryzBchoose demo D; omit this for the default demo, or specify 'chat80'D)desthelpmetavarz-gz--gramrzread in grammar GGz-mz--modelr+z"import model M (omit '.py' suffix)Mz-sz --sentencesrzz"read in a file of test sentences SSz-ez --no-eval store_falserzjust do a syntactic analysis)actionr|r}z-bz--no-beta-reductionrvzdon't carry out beta-reductionz-tz --syntracecountrwz.set syntactic tracing on; requires '-e' optionz-Tz --semtracerxzset semantic tracing onz------------------------------)zFido sees a boy with MaryzJohn sees Maryzevery girl chases a dogzevery boy chases a girlzJohn walks with a girl in Noosaz who walksz"grammars/sample_grammars/sem2.fcfgzimport %s as modelr z Sentence: %sz%d: %s)sysoptparserr set_defaults add_option parse_argsr]rzrr+execrjrWrYrr/rxr)rw enumerater"r dictsetkeys)rrrrsoptsoptionsargsSPACERrigramfile sentsfiler+rn evaluationssemrepsirr<r#semrepvalues rryrys%K K 0D   OO   Q  OO hY-@# OO   1  OO   1  OO   +  OO   -  OO   =  OO   & oo'OWd FM E4H%% ??}} !GMM 12 }9% E A$UHeQgFVFVW !%9I9IJU#4  %& f   *5a. &eT* -Ei1f+-.e Q  $+1: i1f+-.Q r__main__)r)SEM)rr)utf8) __doc__ranltk.semrrr&r)r/r]rjrpry__name__rrrsW6 *$*"B0{| zFr