U
    ef#                     @   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   N/var/www/html/assets/scripts/venv/lib/python3.8/site-packages/nltk/sem/util.pyparse_sents   s    

r   SEMc                 C   s`   ddl m} |  }t||s"tz
|| W S  tk
rX   t|dd td|  Y nX  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   AssertionError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dd	dhfdddddhfddddddhfddddhfd ddd!d"h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girlr0   r1   boyr/   b2dogr2   r3   ZbarkwalkZchase)r/   r0   )r7   r0   )r0   r2   )r1   r3   Zsee)r7   r3   )r0   r/   )r3   r/   )r1   r4   in)r/   r4   )r7   r4   )r3   r4   with)r2   r/   )r/   r2   )r   Z	ValuationdomainZModelm0Z
Assignmentg0)vvaldomr   r   r   demo_model0p   s,    




rB   utf8c              	   C   sH   t | d|}dd |D }W 5 Q R X 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   )lenrF   r   r   r   r(      s      c                 S   s   g | ]}|d  dks|qS )r   #r   rF   r   r   r   r(      s      )codecsopen)filenameencodingfpsentsr   r   r   
read_sents   s
    rP   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    rT   c               	   C   s2  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  d2d3d4d5d6d7g}d8}|jr|j}	|jr |j}|j	r6t
d9|j	  |d krHt|	}t}
t}|jrnt|||
||jd:}nt|||jd:}t|D ]\}}d;}td<|  t| |jr|| D ]D\}}}t|trt| }td=||f  t| |d;7 }qn,|| D ]"\}}td=||f  |d;7 }qqd S )>Nr   )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-reductionrY   zdon't carry out beta-reductionz-tz
--syntracecountrZ   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optparserU   set_defaults
add_option
parse_argsrB   r]   r   r-   execrP   r=   r>   r   r.   r[   r)   rZ   	enumerater    r   dictsetkeys)ri   rU   rV   optsoptionsargsZSPACERrO   ZgramfileZ	sentsfiler-   rR   ZevaluationsZsemrepsir   r4   r!   Zsemrepvaluer   r   r   r\      s    

    	
r\   __main__)r   )r   )r   r   )r   )rC   )__doc__rJ   Znltk.semr   r   r$   r)   r.   rB   rP   rT   r\   __name__r   r   r   r   <module>	   s   



!

 