a
     ˜ëj+"  ã                   @   sz   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rveƒ  e
ƒ  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||ƒr0||ƒ}n|||d�}g }| D ]$}| ¡ }	t| |	¡ƒ}
| 	|
¡ qD|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   úL/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/sem/util.pyÚparse_sents   s    

r   ÚSEMc                 C   s^   ddl m} |  ¡ }t||ƒs"J ‚z
|| W S  tyV   t|dd� td| ƒ Y n0 ‚ dS )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 %sN)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   ó    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ÚnZgirlr0   r1   Zboyr/   Úb2Zdogr2   r3   ZbarkÚwalk>   r1   r2   r/   Zchase>   ©r/   r0   )r1   r3   )r0   r2   )r5   r0   Zsee>   )r3   r/   ©r0   r/   )r1   r4   r7   )r5   r3   Úin>   )r5   r4   )r3   r4   )r/   r4   Úwith>   )r/   r2   )r2   r/   r7   r8   )r   Z	ValuationÚdomainZModelÚm0Z
AssignmentÚg0)ÚvÚvalÚdomr   r   r   Údemo_model0p   s,    





þ

î
rA   Úutf8c                 C   s\   t  | d|¡�}dd„ |D ƒ}W d   ƒ n1 s20    Y  dd„ |D ƒ}dd„ |D ƒ}|S )NÚrc                 S   s   g | ]}|  ¡ ‘qS r   )Úrstrip©r$   Úlr   r   r   r'   “   r(   zread_sents.<locals>.<listcomp>c                 S   s   g | ]}t |ƒd kr|‘qS )r   )ÚlenrE   r   r   r   r'   –   r(   c                 S   s   g | ]}|d  dks|‘qS )r   ú#r   rE   r   r   r   r'   —   r(   )ÚcodecsÚopen)ÚfilenameÚencodingÚfpÚsentsr   r   r   Ú
read_sents‘   s
    ,rO   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   Ú
fromstringr   r)   )r   ÚgÚreadingr%   r*   r   r   r   Údemo_legacy_grammar›   s    ÿrS   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	�r.t
d4|j	 ƒ |d u �r@t|	ƒ}t}
t}|j�rft|||
||jd5�}nt|||jd5�}t|ƒD ]¦\}}d6}td7| ƒ t|ƒ |j�rö|| D ]D\}}}t|tƒ�rÐt| ¡ ƒ}td8||f ƒ t|ƒ |d67 }�q®n,|| D ]"\}}td8||f ƒ |d67 }�qþ�q~d 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)Úactionr^   r_   z-bz--no-beta-reductionrX   zdon't carry out beta-reductionz-tz
--syntraceÚcountrY   z.set syntactic tracing on; requires '-e' optionz-Tz
--semtracerZ   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ÚoptparserT   Úset_defaultsÚ
add_optionÚ
parse_argsrA   r\   r   r-   ÚexecrO   r<   r=   r   r.   rZ   r)   rY   Ú	enumerater   r   ÚdictÚsetÚkeys)rh   rT   rU   ÚoptsÚoptionsÚargsZSPACERrN   ZgramfileZ	sentsfiler-   rQ   ZevaluationsZsemrepsÚir   r4   r    ZsemrepÚvaluer   r   r   r[   ³   sÂ    
ù
û
ÿûûûûûû	
r[   Ú__main__)r   )r   )r   r   )r   )rB   )Ú__doc__rI   Znltk.semr   r   r#   r)   r.   rA   rO   rS   r[   Ú__name__r   r   r   r   Ú<module>	   s   



!

~