Ë
    çÍ:j+"  ã                   ó~   — 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
«        yy)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 ]=  }|j                  «       }	t        |j                  |	«      «      }
|j                  |
«       Œ? |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              úb/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/sem/util.pyÚparse_sentsr      so   € õ ,ß:ä�'˜>Ô*Ù Ó(‰á˜¨Ô.ˆØ€FØò  ˆØ—‘“ˆÜ˜Ÿ™ Ó(Ó)ˆØ�‰�hÕð ð €Mó    c                 óª   — ddl m} | j                  «       }t        ||«      sJ ‚	 ||   S # t        $ r t        |d¬«       t        d|z  «       Y ‚ 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à�=‰=‹?€DÜ�dÐ1Ô2Ð2Ð2ðBØ�F‰|ÐøÜò BÜˆd˜ÕÜÐ7¸&Ñ@ÕAØ	ðBús   ¦+ «$AÁAc                 óˆ   — t        | ||¬«      D ��cg c]  }|D �cg c]  }|t        ||«      f‘Œ c}‘Œ  c}}S c c}w c c}}w )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   sG   € ô $ F¨G¸5ÔA÷àð 5=Ö=¨Sˆ#Œ{˜3 Ó'Ò	(Ô=óð ùÚ=ùós   ’	>›9°>¹>c                 ó®   — t        | |«      D ���cg c]/  }|D ��cg c]  \  }}|||j                  d|z  ||¬«      f‘Œ! c}}‘Œ1 c}}}S c c}}w c c}}}w )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/   [   sh   € ô"  /¨v°wÓ?÷ð ð
 ð .÷	
á��cð �#�u—~‘~ d¨S¡j°*ÀE�~ÓJÒKõ	
ôð ùó	
ùôs   ‘
A›$A
¿AÁ
A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y )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>   ©r?   r4   ©r4   r8   ©r6   r:   ©r2   r4   Úsee>   ©r?   r:   ©r:   r2   ©r6   r<   rG   ©r4   r2   Úin>   ©r2   r<   ©r?   r<   ©r:   r<   Úwith>   ©r2   r8   ©r8   r2   rG   rL   )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ä	�‰˜˜SÓ	!€Bä	×	Ñ	˜SÓ	!�Br   c                 ó"  — t        j                  | d|«      5 }|D �cg c]  }|j                  «       ‘Œ }}d d d «       D �cg c]  }t        |«      dkD  sŒ|‘Œ }}|D �cg c]  }|d   dk(  rŒ|‘Œ }}|S c c}w # 1 sw Y   ŒFxY wc c}w c c}w )NÚrr   ú#)ÚcodecsÚopenÚrstripÚlen)ÚfilenameÚencodingÚfpÚlÚsentss        r   Ú
read_sentsrj   ‘   s�   € Ü	�‰�X˜s HÓ	-ð )°Ø%'Ö( �—‘•Ð(ˆÐ(÷)ð Ö,�1¤ Q£¨!£ŠQÐ,€EÐ,ØÖ1�1 Q q¡T¨S£[ŠQÐ1€EÐ1Ø€Lùò )÷)ð )üò -ùÚ1s3   ˜A;�A6´A;ÁBÁBÁ BÁ.BÁ6A;Á;Bc                  óÈ   — ddl m}  | j                  d«      }t        d|z  «       t        d«       t	        dg|d¬«      D ]   }|d   \  }}t        «        t        d	|«       Œ" y
)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   ›   si   € õ ,à×!Ñ!ð	ó	€Aô 
Ð
 !Ñ
#Ô$Ü	ˆ(„OÜ" G 9¨a¸Ô>ò ˆØ˜1‘:‰ˆˆSÜŒÜˆj˜#Õñr   c            	      óÈ  — dd l } ddlm} d} ||¬«      }|j                  ddddddd¬«       |j	                  d	d
ddd¬«       |j	                  ddddd¬«       |j	                  ddddd¬«       |j	                  ddddd¬«       |j	                  ddd d!d"¬#«       |j	                  d$d%d d&d'¬#«       |j	                  d(d)d*d+d,¬#«       |j	                  d-d.d*d/d0¬#«       |j                  «       \  }}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                  rX|   D ]O  \  }}}t+        |t,        «      rt/        |j1                  «       «      }t)        d8||fz  «       t)        |«       |d6z  }ŒQ Œ…|   D ]  \  }}t)        d8||fz  «       |d6z  }Œ Œ¨ y )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ˆdà€Fä„Mò€Eð 4€Hà×ÒØ×%Ñ%ˆ	Ø‡‚Ø—?‘?ˆØ‡}‚}ÜÐ! G§M¡MÑ1Ô2à€}Ü˜9Ó%ˆô €EÜ
€Aà×ÒÜ$ U¨H°e¸QÀg×FVÑFVÔW‰ä! %¨¸×9IÑ9IÔJˆä˜UÓ#ò ‰ˆˆ4ØˆÜÐ Ñ%Ô&ÜˆfŒØ×ÒØ*5°a©.ò Ñ&�˜ Ü˜e¤TÔ*Ü §
¡
£Ó-�EÜ�i 1 f +Ñ-Ô.Ü�e”Ø�Q‘‘ñð $+¨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   