§
    'ê[ff#  ã                   ó�   — 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              úA/var/www/piapp/venv/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=   ø€ ðð ð ð å# F¨G¸5Ð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—~’~ d¨S¡j°*À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   øøø€ ðð ð ð ð ð õ
  /¨v°wÑ?Ô?ðñ ô ð 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Ñ	!Ô	!€B€B€Br   Ú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Ð1�1 Q q¤T¨S¢[ [ˆQ [ [ [r   )ÚcodecsÚopen)ÚfilenameÚencodingÚfpÚsentss       r   Ú
read_sentsry   ‘   s¨   € Ý	Œ�X˜s HÑ	-Ô	-ð )°Ø(Ð( RÐ(Ñ(Ô(ˆð)ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ð -Ð,˜Ð,Ñ,Ô,€EØ1Ð1˜Ð1Ñ1Ô1€EØ€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õ 
Ð
 !Ñ
#Ñ$Ô$Ð$Ý	ˆ(�O„O€OÝ" G 9¨a¸Ð>Ñ>Ô>ð ð ˆØ˜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¬  € Ø€J€J€JØ%Ð%Ð%Ð%Ð%Ð%ð€Kð ˆ< KÐ0Ñ0Ô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ˆdà€Få�M„M€Mðð ð €Eð 4€HàÔð &ØÔ%ˆ	Ø„ð #Ø”?ˆØ„}ð 3ÝÐ! G¤MÑ1Ñ2Ô2Ð2à€}Ý˜9Ñ%Ô%ˆõ €EÝ
€AàÔð KÝ$ U¨H°e¸QÀgÔFVÐWÑWÔWˆˆå! %¨¸Ô9IÐJÑJÔJˆå˜UÑ#Ô#ð ð ‰ˆˆ4ØˆÝÐ Ñ%Ñ&Ô&Ð&Ýˆf‰ŒˆØÔð 	à,7¸¬Nð ð Ñ(�˜& %Ý˜e¥TÑ*Ô*ð .Ý §
¢
¡¤Ñ-Ô-�EÝ�i 1 f +Ñ-Ñ.Ô.Ð.Ý�e‘”�Ø�Q‘��ðð &-¨Q¤Zð ð Ñ!�˜&Ý�i 1 f +Ñ-Ñ.Ô.Ð.Ø�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ÒÐØ€D�F„F€FØÐÑÔÐÐÐð Ðr   