§
    íóÉjé"  ã                   ó”   — d Z ddlm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.
é    )Úopen©Ú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              úp/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/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)   3   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>U   s(   ø€ Ð=Ð=Ð=¨Sˆ#�{˜3 Ñ'Ô'Ð	(Ð=Ð=Ð=r   r-   )r.   r   r'   s     €r   r0   z#interpret_sents.<locals>.<listcomp>T   s=   ø€ ð ð ð àð 	>Ð=Ð=Ð=°HÐ=Ñ=Ô=ðð ð r   r   )r   )r   r   r'   r   s     ` r   Úinterpret_sentsr1   H   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   r0   z-evaluate_sents.<locals>.<listcomp>.<listcomp>g   sJ   ø€ ð 	
ð 	
ð 	
á��cð �#�u—~’~ d¨S¡j°*ÀE�~ÑJÔJÐKð	
ð 	
ð 	
r   r-   )r.   Úinterpretationsr6   r7   r   s     €€€r   r0   z"evaluate_sents.<locals>.<listcomp>f   sV   ø€ ð ð ð ð
 ð		
ð 	
ð 	
ð 	
ð 	
ð 	
à-ð	
ñ 	
ô 	
ðð ð r   )r1   )r   r   r7   r6   r   s     ```r   Úevaluate_sentsr9   Z   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ÚdogrB   rD   ÚbarkÚwalk>   r<   rB   r@   Úchase>   ©rI   r>   ©r>   rB   ©r@   rD   ©r<   r>   Úsee>   ©rI   rD   ©rD   r<   ©r@   rF   rQ   ©r>   r<   Úin>   ©r<   rF   ©rI   rF   ©rD   rF   Úwith>   ©r<   rB   ©rB   r<   rQ   rV   )r   Ú	ValuationÚdomainÚModelÚm0Ú
AssignmentÚg0)ÚvÚvalÚdoms      r   Údemo_model0rg   o   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          | d|¬¦  «        5 }d„ |D ¦   «         }d d d ¦  «         n# 1 swxY w Y   d„ |D ¦   «         }d„ |D ¦   «         }|S )NÚr)Úencodingc                 ó6   — g | ]}|                      ¦   «         ‘ŒS r-   )Úrstrip©r.   Úls     r   r0   zread_sents.<locals>.<listcomp>“   s    € Ð(Ð(Ð( �—’‘”Ð(Ð(Ð(r   c                 ó8   — g | ]}t          |¦  «        d k    ¯|‘ŒS ©r   )Úlenrn   s     r   r0   zread_sents.<locals>.<listcomp>–   s#   € Ð,Ð,Ð,�1¥ Q¡¤¨!¢ ˆQ   r   c                 ó*   — g | ]}|d          dk    °|‘ŒS )r   ú#r-   rn   s     r   r0   zread_sents.<locals>.<listcomp>—   s!   € Ð1Ð1Ð1�1 Q q¤T¨S¢[ [ˆQ [ [ [r   )Ú_secure_open)Úfilenamerk   ÚfpÚsentss       r   Ú
read_sentsry   �   s©   € å	�h ¨hÐ	7Ñ	7Ô	7ð )¸2Ø(Ð( RÐ(Ñ(Ô(ˆð)ð )ð )ñ )ô )ð )ð )ð )ð )ð )ð )øøøð )ð )ð )ð )ð -Ð,˜Ð,Ñ,Ô,€EØ1Ð1˜Ð1Ñ1Ô1€Eà€Ls   “,¬0³0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********************Úhellor5   )r'   zoutput: N)r   r   Ú
fromstringr%   r1   )r   ÚgÚreadingr/   r5   s        r   Údemo_legacy_grammarr   œ   s¤   € ð ,Ð+Ð+Ð+Ð+Ð+à×!Ò!ð	ñ	ô 	€Aõ 
Ð
 !Ñ
#Ñ$Ô$Ð$Ý	ˆ(�O„O€OÝ" G 9¨a¸Ð>Ñ>Ô>ð ð ˆØ˜1”:‰ˆˆSÝ‰ŒˆÝˆj˜#ÑÔÐÐðð r   c            	      ó.  — 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| 
                    d4¦  «         |€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--modelr7   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.fcfgzW--model is currently unsupported in demo(); the CLI always uses the built-in demo modelr   é   z
Sentence: %sz%d:  %s)ÚsysÚoptparser�   Úset_defaultsÚ
add_optionÚ
parse_argsrg   r‰   r   r7   Úerrorry   ra   rc   r   r9   r‡   r1   r†   Ú	enumerater%   r   ÚdictÚsetÚkeys)r•   r�   r‚   ÚoptsÚoptionsÚargsÚSPACERrx   ÚgramfileÚ	sentsfiler7   r}   ÚevaluationsÚsemrepsÚir   rF   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àÔð &ØÔ%ˆ	Ø„ð #Ø”?ˆà„}ð 
Ø�
Š
Øeñ	
ô 	
ð 	
ð €}Ý˜9Ñ%Ô%ˆõ €EÝ
€AàÔð KÝ$ U¨H°e¸QÀgÔFVÐWÑWÔWˆˆå! %¨¸Ô9IÐJÑJÔJˆå˜UÑ#Ô#ð ð ‰ˆˆ4ØˆÝÐ Ñ%Ñ&Ô&Ð&Ýˆf‰ŒˆØÔð 
	Ø*5°a¬.ð ð Ñ&�˜ Ý˜e¥TÑ*Ô*ð .Ý §
¢
¡¤Ñ-Ô-�EÝ�i 1 f +Ñ-Ñ.Ô.Ð.Ý�e‘”�Ø�Q‘��ðð $+¨1¤:ð ð ‘�˜Ý�i 1 f +Ñ-Ñ.Ô.Ð.Ø�Q‘��ððð r   Ú__main__Nrq   )r   )r   r   )rh   )Ú__doc__Únltk.pathsecr   ru   Únltk.semr   r   r)   r1   r9   rg   ry   r   rˆ   Ú__name__r-   r   r   ú<module>r¯      s   ððð ð .Ð -Ð -Ð -Ð -Ð -Ø Ð Ð Ð Ð Ð ðð ð ð ð6
ð 
ð 
ð 
ð*ð ð ð ð$ð ð ð ð*"ð "ð "ðB	ð 	ð 	ð 	ðð ð ð0~ð ~ð ~ðB ˆzÒÐØ€D�F„F€FØÐÑÔÐÐÐð Ðr   