
    /h+"                         S r SSKrSSKJr  SS jrSS jrSS jrSS jrS rSS	 jr	S
 r
S r\S:X  a  \" 5         \
" 5         gg)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                     SSK Jn  SSKJnJn  [        X5      (       a	  U" U5      nOU" XS9n/ nU  H>  nUR                  5       n	[        UR                  U	5      5      n
UR                  U
5        M@     U$ )ab  
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              E/var/www/auris/envauris/lib/python3.13/site-packages/nltk/sem/util.pyparse_sentsr      si     ,:'**(.F()h  M    c                     SSK Jn  U R                  5       n[        X25      (       d   e X1   $ ! [         a    [        USS9  [        SU-  5         e f = f)z
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==?Dd2222B| Bd7&@A	Bs   . "AAc                     [        XUS9 VVs/ s H   nU Vs/ s H  oU[        XR5      4PM     snPM"     snn$ s  snf s  snnf )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)))
r	   )r   r&   )r   r   r$   r
   r   syns         r   interpret_sentsr)   I   sJ     $F5AAH 5==HS{3'	(H=A =s   
>9>>c                     [        X5       VVVs/ s H-  nU VVs/ s H  u  pgXgUR                  SU-  X4S94PM     snnPM/     snnn$ s  snnf s  snnnf )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)))
z%sr	   )r)   r   )r   r   model
assignmentr
   interpretationsr(   sems           r   evaluate_sentsr/   [   si    "  /v?
  @O .	
-
 u~~dSj*~JK-	
  @ 	
s   A!A	A	Ac                  
   SSSSSSSSS	14S
SS14SSS14SSS14S1 Sk4S1 Sk4S1 Sk4S1 Sk4S1 Sk4/n [         R                  " U 5      nUR                  n[         R                  " X!5      q[         R
                  " U5      qg )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>   r2   r4   r?   r4   r4   r8   r6   r:   see>   rD   r?   r:   r:   r2   r4   r2   r6   r<   in>   r2   r<   r?   r<   r:   r<   with>   r2   r8   rD   r8   r2   rK   )r   	ValuationdomainModelm0
Assignmentg0)vvaldoms      r   demo_model0r]   p   s     		$	t	t	$	#$	JKQ	
 
67	IJ%	A* 

Q
C
**C		!B			S	!Br   c                 D   [         R                  " U SU5       nU Vs/ s H  o3R                  5       PM     nnS S S 5        W Vs/ s H  n[        U5      S:  d  M  UPM     nnU Vs/ s H  o3S   S:X  a  M  UPM     nnU$ s  snf ! , (       d  f       NT= fs  snf s  snf )Nrr   #)codecsopenrstriplen)filenameencodingfplsentss        r   
read_sentsrj      s    	XsH	-%'(RR( 
. ,1Q!QE,11qTS[QE1L ) 
.	- -1s3   BBBBB(B8BB
Bc                      SSK Jn   U R                  S5      n[        SU-  5        [        S5        [	        S/USS9 H   nUS   u  p4[        5         [        S	U5        M"     g
)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.   s        r   demo_legacy_grammarrp      sb     ,!!		A 

!
#$	(O"G9a>1:j# ?r   c            
         SS K n SSKJn  SnU" US9nUR                  SSSSSSSS9  UR	                  S	S
SSSS9  UR	                  SSSSSS9  UR	                  SSSSSS9  UR	                  SSSSSS9  UR	                  SSS S!S"S#9  UR	                  S$S%S S&S'S#9  UR	                  S(S)S*S+S,S#9  UR	                  S-S.S*S/S0S#9  UR                  5       u  pES1n[        5         / S2QnS3nUR                  (       a  UR                  n	UR                  (       a  UR                  nUR                  (       a  [        S4UR                  -  5        Uc  [        W	5      n[        n
[        nUR                  (       a  [        XxXUR                   S59nO[#        XxUR$                  S59n['        U5       H  u  pS6n[)        S7U-  5        [)        U5        UR                  (       a`  WU    HU  u  nnn[+        U[,        5      (       a  [/        UR1                  5       5      n[)        S8UU4-  5        [)        U5        US6-  nMW     M  WU    H  u  nn[)        S8UU4-  5        US6-  nM     M     g )9Nr   )OptionParserz,
    Parse and evaluate some sentences.
    )descriptionTdefault )r   betasyntracesemtracedemor   	sentencesz-dz--demory   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--sentencesrz   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-reductionrv   zdon't carry out beta-reductionz-tz
--syntracecountrw   z.set syntactic tracing on; requires '-e' optionz-Tz
--semtracerx   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optparserr   set_defaults
add_option
parse_argsr]   rz   r   r+   execrj   rW   rY   r   r/   rx   r)   rw   	enumerater"   r   dictsetkeys)r   rr   rs   optsoptionsargsSPACERri   gramfile	sentsfiler+   rn   evaluationssemrepsir   r<   r#   semrepvalues                       r   ry   ry      s   %K K0D   	OOQ   	OOhY-@#   	OO1   	OO1   	OO+   	OO-   	OO=   	OO&   oo'OWFME 4H%%	??}}!GMM12}9% E
A$UegFVFVW!%9I9IJU#%&f*5a.&eT**

-Ei1f+-.eQ +9 $+1:i1f+-.Q $. $r   __main__)r   )SEM)r   r   )utf8)__doc__ra   nltk.semr   r   r&   r)   r/   r]   rj   rp   ry   __name__ r   r   <module>r      sW     6
*$*"B0{| zF r   