
    Ng+"                         d Z ddlZddlmZ ddZddZddZddZd	 ZddZ	d Z
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                    ddl m} ddlm}m} t          ||          r ||          }n |||          }g }| D ]M}|                                }	t          |                    |	                    }
|	                    |
           N|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syntreess              I/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/sem/util.pyparse_sentsr      s     ,+++++::::::::'>** /(([...F    (())hM    SEMc                     ddl m} |                                 }t          ||          sJ 	 ||         S # t          $ r& t          |d           t          d|z             Y nw x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   nodes       r   root_semrepr(   4   s     322222==??Dd122222BF| B B Bd7&@AAAAAB 
s   6 -A&%A&c                 @    fdt          | ||          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                 ,    g | ]}fd |D             S )c                 4    g | ]}|t          |          fS  )r(   ).0synr&   s     r   
<listcomp>z.interpret_sents.<locals>.<listcomp>.<listcomp>V   s(    ===S#{3''	(===r   r,   )r-   r   r&   s     r   r/   z#interpret_sents.<locals>.<listcomp>U   s=        	>===H===  r   r
   )r   )r   r   r&   r   s     ` r   interpret_sentsr0   I   s=       #FG5AAA   r   c                 D    f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                 0    g | ]}fd |D             S )c           	      R    g | ]#\  }}||                     d |z            f$S )z%sr
   r   )r-   r.   sem
assignmentmodelr   s      r   r/   z-evaluate_sents.<locals>.<listcomp>.<listcomp>h   sJ     	
 	
 	
c #u~~dSj*E~JJK	
 	
 	
r   r,   )r-   interpretationsr5   r6   r   s     r   r/   z"evaluate_sents.<locals>.<listcomp>g   sV       
 		
 	
 	
 	
 	
 	
-	
 	
 	
  r   )r0   )r   r   r6   r5   r   s     ```r   evaluate_sentsr8   [   sF         
  /vw??   r   c                      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        |           }|j        }t          j        ||          at          j        |          ad S )N)johnb1)maryg1)suzieg2)fidod1)tessd2)noosangirlr=   r?   boyr;   b2dogrA   rC   barkwalk>   r;   rA   r?   chase>   rH   r=   r=   rA   r?   rC   r;   r=   see>   rH   rC   rC   r;   r?   rE   rP   r=   r;   in>   r;   rE   rH   rE   rC   rE   with>   r;   rA   rA   r;   rP   rU   )r   	ValuationdomainModelm0
Assignmentg0)vvaldoms      r   demo_model0rf   p   s     		$	t	t	$	###$	JJJKQQQ	
 
6667	IIIJ%	A* 
Q

C
*C	S	!	!B		S	!	!BBBr   utf8c                     t          j        | d|          5 }d |D             }d d d            n# 1 swxY w Y   d |D             }d |D             }|S )Nrc                 6    g | ]}|                                 S r,   )rstripr-   ls     r   r/   zread_sents.<locals>.<listcomp>   s     ((((((r   c                 8    g | ]}t          |          d k    |S r   )lenrl   s     r   r/   zread_sents.<locals>.<listcomp>   s#    ,,,1Q!Qr   c                 *    g | ]}|d          dk    |S )r   #r,   rl   s     r   r/   zread_sents.<locals>.<listcomp>   s!    1111QqTS[[Q[[[r   )codecsopen)filenameencodingfpsentss       r   
read_sentsry      s    	XsH	-	- )((R((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) -,,,,E11111ELs   044c                      ddl m}  |                     d          }t          d|z             t          d           t	          dg|d          D ]+}|d         \  }}t                       t          d	|           ,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********************hellor4   )r&   zoutput: N)r   r   
fromstringr$   r0   )r   greadingr.   r4   s        r   demo_legacy_grammarr      s     ,+++++!!		 	A 

!
#$$$	(OOO"G9a>>>  1:Sj# r   c            	      2   dd l } ddlm} d} ||          }|                    ddddddd           |                    d	d
ddd           |                    ddddd           |                    ddddd           |                    ddddd           |                    ddd d!d"#           |                    d$d%d d&d'#           |                    d(d)d*d+d,#           |                    d-d.d*d/d0#           |                                \  }}d1}t                       g d2}d3}|j        r|j        }	|j        r|j        }|j	        rt          d4|j	        z             |t          |	          }t          }
t          }|j        rt          |||
||j        5          }nt#          |||j        5          }t'          |          D ]\  }}d6}t)          d7|z             t)          |           |j        rn||         D ]d\  }}}t+          |t,                    r!t/          |                                          }t)          d8||fz             t)          |           |d6z  }e||         D ]\  }}t)          d8||fz             |d6z  }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--modelr6   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-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optparser   set_defaults
add_option
parse_argsrf   r   r   r6   execry   r`   rb   r   r8   r   r0   r   	enumerater$   r   dictsetkeys)r   r   r   optsoptionsargsSPACERrx   gramfile	sentsfiler6   r}   evaluationssemrepsir   rE   r%   semrepvalues                       r   r   r      s   JJJ%%%%%%K <K000D     	OOQ     	OOhY-@#     	OO1     	OO1     	OO+     	OO-     	OO=     	OO&     oo''OWdFMMM  E 4H &%	 #?} 3!GM1222}9%% E
A K$UHeQgFVWWW!%9IJJJU##  4%&&&f 
	*5a.  &eT** .

--Ei1f+-...eQ $+1:  i1f+-...Q r   __main__ro   )r   )r   r   )rg   )__doc__rs   nltk.semr   r   r(   r0   r8   rf   ry   r   r   __name__r,   r   r   <module>r      s              6
 
 
 
*   $   *" " "B     0{ { {| zDFFF r   