o
    i+"                     @   s~   d Z ddlZddlmZ dddZdddZdd	d
ZdddZdd ZdddZ	dd Z
dd Zedkr=e  e
  dS dS )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evaluatec                 C   sn   ddl m} ddlm}m} t||r||}n|||d}g }| D ]}| }	t||	}
|	|
 q"|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.
    r   FeatureGrammar)FeatureChartParserload_parsertrace)
nltk.grammarr   
nltk.parser   r   
isinstancesplitlistparseappend)inputsgrammarr	   r   r   r   cpparsessenttokenssyntrees r   H/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nltk/sem/util.pyparse_sents   s   

r   SEMc                 C   sX   ddl m} |  }t||sJ z|| W S  ty+   t|dd td|  Y  w )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
   r   labelr   KeyErrorprint)syntreesemkeyr   noder   r   r   root_semrep4   s   	
r%   c                    s    fddt | ||dD S )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)))
    c                    s   g | ]} fd d|D qS )c                    s   g | ]	}|t | fqS r   )r%   ).0synr#   r   r   
<listcomp>V   s    z.interpret_sents.<locals>.<listcomp>.<listcomp>r   )r&   r   r(   r   r   r)   U   s    z#interpret_sents.<locals>.<listcomp>r   )r   )r   r   r#   r	   r   r(   r   interpret_sentsI   s   
r*   c                    s    fddt | |D S )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)))
    c                    s"   g | ]} fd d|D qS )c              	      s*   g | ]\}}||j d |  dfqS )z%sr   r   )r&   r'   sem
assignmentmodelr	   r   r   r)   h   s    z-evaluate_sents.<locals>.<listcomp>.<listcomp>r   )r&   interpretationsr,   r   r   r)   g   s    z"evaluate_sents.<locals>.<listcomp>)r*   )r   r   r.   r-   r	   r   r,   r   evaluate_sents[   s   r0   c                  C   s   ddddddddd	hfd
ddhfdddhfdddhfdh dfdh dfdh dfdh dfdh dfg} t | }|j}t ||at |ad S )N)johnb1)maryg1)suzieg2)fidod1)tessd2)noosangirlr4   r6   boyr2   b2dogr8   r:   barkwalk>   r2   r8   r6   chase>   r?   r4   r4   r8   r6   r:   r2   r4   see>   r?   r:   r:   r2   r6   r<   rG   r4   r2   in>   r2   r<   r?   r<   r:   r<   with>   r2   r8   r8   r2   rG   rL   )r   	ValuationdomainModelm0
Assignmentg0)vvaldomr   r   r   demo_model0p   s,   








r]   utf8c                 C   s\   t | d|}dd |D }W d    n1 sw   Y  dd |D }dd |D }|S )Nrc                 S   s   g | ]}|  qS r   )rstripr&   lr   r   r   r)      s    zread_sents.<locals>.<listcomp>c                 S   s   g | ]
}t |d kr|qS r   )lenra   r   r   r   r)          c                 S   s   g | ]
}|d  dks|qS )r   #r   ra   r   r   r   r)      re   )codecsopen)filenameencodingfpsentsr   r   r   
read_sents   s   rm   c                  C   s`   ddl m}  | d}td|  td tdg|ddD ]}|d \}}t  td	| qd
S )z
    Check that interpret_sents() is compatible with legacy grammars that use
    a lowercase 'sem' feature.

    Define 'test.fcfg' to be the following

    r   r   z1
    % start S
    S[sem=<hello>] -> 'hello'
    zReading grammar: %sz********************hellor+   r(   zoutput: N)r
   r   
fromstringr!   r*   )r   greadingr'   r+   r   r   r   demo_legacy_grammar   s   rr   c               	   C   s  dd l } ddlm} d}||d}|jdddddddd |jd	d
dddd |jdddddd |jdddddd |jdddddd |jddd d!d"d# |jd$d%d d&d'd# |jd(d)d*d+d,d# |jd-d.d*d/d0d# | \}}d1}t  g d2}d3}|jr|j}	|jr|j}|j	rt
d4|j	  |d u rt|	}t}
t}|jrt|||
||jd5}nt|||jd5}t|D ]N\}}d6}td7|  t| |jr|| D ] \}}}t|trt| }td8||f  t| |d67 }qq|| D ]\}}td8||f  |d67 }qqd S )9Nr   )OptionParserz,
    Parse and evaluate some sentences.
    )descriptionTdefault )r   betasyntracesemtracedemor   	sentencesz-dz--demorz   zBchoose demo D; omit this for the default demo, or specify 'chat80'D)desthelpmetavarz-gz--gramr   zread in grammar GGz-mz--modelr.   z"import model M (omit '.py' suffix)Mz-sz--sentencesr{   z"read in a file of test sentences SSz-ez	--no-evalstore_falser   zjust do a syntactic analysis)actionr}   r~   z-bz--no-beta-reductionrw   zdon't carry out beta-reductionz-tz
--syntracecountrx   z.set syntactic tracing on; requires '-e' optionz-Tz
--semtracery   zset semantic tracing onz------------------------------)zFido sees a boy with MaryzJohn sees Maryzevery girl chases a dogzevery boy chases a girlzJohn walks with a girl in Noosaz	who walksz"grammars/sample_grammars/sem2.fcfgzimport %s as modelr      z
Sentence: %sz%d:  %s)sysoptparsers   set_defaults
add_option
parse_argsr]   r{   r   r.   execrm   rW   rY   r   r0   ry   r*   rx   	enumerater!   r   dictsetkeys)r   rs   rt   optsoptionsargsSPACERrl   gramfile	sentsfiler.   rp   evaluationssemrepsir   r<   r"   semrepvaluer   r   r   rz      s   


	


rz   __main__rc   )r   )r   r   )r^   )__doc__rg   nltk.semr   r   r%   r*   r0   r]   rm   rr   rz   __name__r   r   r   r   <module>   s   




!
~
