ó
    ´OÂf+"  ã                   óŠ   • 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              Ú//usr/lib/python3/dist-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   € ô $ F¸5ÒAôâAˆHñ 5=Ó=²H¨SŒ{˜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—~‘~ d¨S¡j°*�~Ð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‡   € Ü	�Š�X˜s HÔ	-°Ù%'Ó(¢R —‘–¡RˆÐ(÷ 
.ñ Ó,š�1¤ Q£¨!¡�Q™€EÐ,ÙÓ1š�1 q¡T¨S¡[�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Ü" G 9¨a¸Ô>ˆØ˜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  [        XxX«UR                   S59nO[#        XxUR$                  S59n['        U5       Hµ  u  pïS6n[)        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Ñ0€Dà×ÑØØØØØØØð ñ ð 	‡O�OØØØØQØð ñ ð 	‡O�OØˆh˜YÐ-@È#ð ñ ð 	‡O�OØØØØ1Øð ñ ð 	‡O�OØØØØ1Øð ñ ð 	‡O�OØØØØØ+ð ñ ð 	‡O�OØØØØØ-ð ñ ð 	‡O�OØØØØØ=ð ñ ð 	‡O�OØØØØØ&ð ñ ð —o‘oÓ'�O€Wà€Fä„Mò€Eð 4€Hà××Ø×%Ñ%ˆ	Ø‡‡Ø—?‘?ˆØ‡}‡}ÜÐ! G§M¡MÑ1Ô2à�}Ü˜9Ó%ˆô €EÜ
€Aà××Ü$ U°eÀg×FVÑFVÑW‰ä! %¸×9IÑ9IÑJˆä˜UÖ#‰ˆØˆÜÐ Ñ%Ô&ÜˆfŒØ××Ø*5°a¬.Ñ&�˜ Ü˜e¤T×*Ñ*Ü §
¡
£Ó-�EÜ�i 1 f +Ñ-Ô.Ü�e”Ø�Q‘’ó +9ð $+¨1¤:‘�˜Ü�i 1 f +Ñ-Ô.Ø�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   