o
    rZh+"                     @   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   Z
nltk.parser   r   
isinstancesplitlistparseappend)inputsgrammarr	   r   r   r   cpZparsessenttokenssyntrees r   </var/www/auris/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$   Zinterpretationsr*   r   r   r'   g   s    z"evaluate_sents.<locals>.<listcomp>)r(   )r   r   r,   r+   r	   r   r*   r   evaluate_sents[   s   r-   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)Zjohnb1)Zmaryg1)Zsuzieg2)Zfidod1)Ztessd2)Znoosangirlr/   r0   boyr.   b2dogr1   r2   Zbarkwalk>   r0   r.   r1   Zchase>   r.   r/   )r/   r1   )r6   r/   )r0   r2   Zsee>   )r6   r2   )r0   r3   )r2   r.   r/   r.   r9   in>   )r2   r3   )r6   r3   )r.   r3   with>   r9   )r1   r.   r:   )r.   r1   )r   Z	ValuationdomainZModelm0Z
Assignmentg0)vvaldomr   r   r   demo_model0p   s,   








rC   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   )lenrG   r   r   r   r'          c                 S   s   g | ]
}|d  dks|qS )r   #r   rG   r   r   r   r'      rK   )codecsopen)filenameencodingfpsentsr   r   r   
read_sents   s   rS   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********************Zhellor)   r&   zoutput: N)r
   r   Z
fromstringr   r(   )r   greadingr%   r)   r   r   r   demo_legacy_grammar   s   rV   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--demor^   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)actionra   rb   z-bz--no-beta-reductionr[   zdon't carry out beta-reductionz-tz
--syntracecountr\   z.set syntactic tracing on; requires '-e' optionz-Tz
--semtracer]   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optparserW   set_defaults
add_option
parse_argsrC   r_   r   r,   execrS   r>   r?   r   r-   r]   r(   r\   	enumerater   r   dictsetkeys)rk   rW   rX   optsoptionsargsZSPACERrR   ZgramfileZ	sentsfiler,   rT   ZevaluationsZsemrepsir   r3   r    Zsemrepvaluer   r   r   r^      s   


	


r^   __main__rI   )r   )r   r   )rD   )__doc__rM   Znltk.semr   r   r#   r(   r-   rC   rS   rV   r^   __name__r   r   r   r   <module>   s   




!
~
