JL iVBdZddlZddlmZmZddlmZddlmZm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZmZdd lmZdd lmZddlmZGddeZGddeZGddeZGddZ dZ!ddZ"ddZ#ddZ$dZ%e&dk(re%yy) a Module for incrementally developing simple discourses, and checking for semantic ambiguity, consistency and informativeness. Many of the ideas are based on the CURT family of programs of Blackburn and Bos (see http://homepages.inf.ed.ac.uk/jbos/comsem/book1.html). Consistency checking is carried out by using the ``mace`` module to call the Mace4 model builder. Informativeness checking is carried out with a call to ``Prover.prove()`` from the ``inference`` module. ``DiscourseTester`` is a constructor for discourses. The basic data structure is a list of sentences, stored as ``self._sentences``. Each sentence in the list is assigned a "sentence ID" (``sid``) of the form ``s``\ *i*. For example:: s0: A boxer walks s1: Every boxer chases a girl Each sentence can be ambiguous between a number of readings, each of which receives a "reading ID" (``rid``) of the form ``s``\ *i* -``r``\ *j*. For example:: s0 readings: s0-r1: some x.(boxer(x) & walk(x)) s0-r0: some x.(boxerdog(x) & walk(x)) A "thread" is a list of readings, represented as a list of ``rid``\ s. Each thread receives a "thread ID" (``tid``) of the form ``d``\ *i*. For example:: d0: ['s0-r0', 's1-r0'] The set of all threads for a discourse is the Cartesian product of all the readings of the sequences of sentences. (This is not intended to scale beyond very short discourses!) The method ``readings(filter=True)`` will only show those threads which are consistent (taking into account any background assumptions). N)ABCMetaabstractmethod)reduce)addand_)show_cfg) MaceCommand)Prover9Command) load_parser) MaltParser)AnaphoraResolutionExceptionresolve_anaphora)DrtGlue Expression) RegexpTaggercBeZdZedZdZedZedZy)ReadingCommandcy)zS :param sentence: the sentence to read :type sentence: str Nselfsentences ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/inference/discourse.pyparse_to_readingsz ReadingCommand.parse_to_readings?c|S)a9 This method should be used to handle dependencies between readings such as resolving anaphora. :param sentence_readings: readings to process :type sentence_readings: list(Expression) :return: the list of readings after processing :rtype: list(Expression) rrsentence_readingss rprocess_threadzReadingCommand.process_threadFs ! rcy)z :param readings: readings to combine :type readings: list(Expression) :return: one combined reading :rtype: Expression Nrrreadingss rcombine_readingszReadingCommand.combine_readingsRrrcy)z Convert this expression into a First-Order Logic expression. :param expression: an expression :type expression: Expression :return: a FOL version of the input expression :rtype: Expression Nrr expressions rto_folzReadingCommand.to_fol[rrN)__name__ __module__ __qualname__rrr!r%r)rrrrr>sA  !    rr) metaclassc&eZdZddZdZdZdZy)CfgReadingCommandNcN|r|nd|_t|j|_y)zg :param gramfile: name of file where grammar can be loaded :type gramfile: str z%grammars/book_grammars/discourse.fcfgN) _gramfiler _parser)rgramfiles r__init__zCfgReadingCommand.__init__hs% !H&M #4>>2 rcddlm}|j}|jj |}|Dcgc] }|| c}Scc}w)(:see: ReadingCommand.parse_to_readings()r) root_semrep)nltk.semr7splitr2parse)rrr7tokenstreestrees rrz#CfgReadingCommand.parse_to_readingsrs>(! ""6*.34d D!444sAc"tt|Sz':see: ReadingCommand.combine_readings())rrr#s rr%z"CfgReadingCommand.combine_readingszsdH%%rc|Sz:see: ReadingCommand.to_fol()rr's rr)zCfgReadingCommand.to_fol~srN)r*r+r,r4rr%r)rrrr/r/gs35&rr/c,eZdZddZdZdZdZdZy)DrtGlueReadingCommandNcp|!tjjddd}t||||_y)z :param semtype_file: name of file where grammar can be loaded :param remove_duplicates: should duplicates be removed? :param depparser: the dependency parser Ngrammarssample_grammarszdrt_glue.semtype) semtype_fileremove_duplicates depparser)ospathjoinr_glue)rrHrIrJs rr4zDrtGlueReadingCommand.__init__s<  77<<-/AL%/  rc8|jj|S)r6)rNparse_to_meaningrs rrz'DrtGlueReadingCommand.parse_to_readingsszz**844rcJ |j|gS#t$rgcYSwxYw)z%:see: ReadingCommand.process_thread())r%r rs rr!z$DrtGlueReadingCommand.process_threads0 ))*;<= =* I s  ""cTtt|}t|jSr?)rrrsimplify)rr$thread_readings rr%z&DrtGlueReadingCommand.combine_readingss"X. 7 7 9::rc"|jSrA)folr's rr)zDrtGlueReadingCommand.to_fols~~r)NFN)r*r+r,r4rr!r%r)rrrrDrDs 5;  rrDceZdZdZddZdZddZddZdZdZ d Z d Z dd Z dd Z dd ZddZddZddZddZdZedZy)DiscourseTesterz3 Check properties of an ongoing discourse. Nc"||_t|Dcic] \}}d|z| c}}|_d|_i|_|r|n t |_i|_i|_|#ddl m }|D]}t||rJ||_ yg|_ ycc}}w)z Initialize a ``DiscourseTester``. :param input: the discourse sentences :type input: list of str :param background: Formulas which express background assumptions :type background: list(Expression) s%sNrr) _input enumerate _sentences_models _readingsr/_reading_command_threads_filtered_threadsnltk.sem.logicr isinstance _background)rinputreading_command backgroundisentres rr4zDiscourseTester.__init__s :CE:JKwq$519d?K .O4E4G  !#  ! 1 1!!Z000 1)D !D LsB crt|jD]}t|d|j|!y)zI Display the list of sentences in the current discourse. : N)sortedr]print)rids r sentenceszDiscourseTester.sentencess9) 2B RD4??2./0 1 2rc ||r|jdt|jD]}|j|Dcgc]\}}| }}}||jz }|j |D]I}t ||} | js!td|dt|dtd|zK|jj|t|jD cic] \} } d| z|  c} } |_ |r%|jd|jd y y cc}}wcc} } w) a Add a sentence to the current discourse. Updates ``self._input`` and ``self._sentences``. :param sentence: An input sentence :type sentence: str :param informchk: if ``True``, check that the result of adding the sentence is thread-informative. Updates ``self._readings``. :param consistchk: if ``True``, check that the result of adding the sentence is thread-consistent. Updates ``self._readings``. Fverbose)goal assumptionsz Sentence 'z' under reading 'z':z'Not informative relative to thread '%s'rZ)showN)r$rnraexpand_threadsre _get_readingsr proverostrr[appendr\r]models) rr informchk consistchktidridreadingrv sent_readingtprirjs r add_sentencezDiscourseTester.add_sentences'  MM%M (dmm, O=A=P=PQT=UV>CwV Vt/// $($6$6x$@OL'\{SBxxz'\):<G#MNO O 8$:CDKK:PQwq$519d?Q  MM%M ( KKUK # WRs  D21D8cd |jj|t |jDcic] \}}d|z| c}}|_|jd|rtd|j yy#t$r!td|z|j YywxYwcc}}w)a# Remove a sentence from the current discourse. Updates ``self._input``, ``self._sentences`` and ``self._readings``. :param sentence: An input sentence :type sentence: str :param verbose: If ``True``, report on the updated list of sentences. zJRetraction failed. The sentence '%s' is not part of the current discourse:NrZFrszCurrent sentences are )r[remove ValueErrorrorqr\r]r$)rrrtrirjs rretract_sentencez DiscourseTester.retract_sentences  KK  x (;DDKK:PQwq$519d?Q e $  * + NN    \  NN   RsA?B,?'B)(B)cBt|jjy)zJ Print out the grammar in use for parsing input sentences N)rr`r1)rs rgrammarzDiscourseTester.grammars &&001rc8|jj|S)ze Build a list of semantic readings for a sentence. :rtype: list(Expression) )r`rrs rryzDiscourseTester._get_readingss $$66x@@rc.i|_t|jD]p}|j|}|j|}t t|t Dcic]\}}|d||j c}}|j|<rycc}}w)zV Use ``self._sentences`` to construct a value for ``self._readings``. )keyz-rN)r_rnr]ryr\r{rS)rsidrr$rrs r_construct_readingsz#DiscourseTester._construct_readings s $//* Cs+H))(3H%.fX3.G$H# C%r#!1!1!33#DNN3  #s Bcgg}t|jD]*}|j|t|j|},t|Dcic] \}}d|z| c}}|_i|_|j |j}|jjD]\}}|df|vs ||j |<ycc}}w)z Use ``self._readings`` to construct a value for ``self._threads`` and use the model builder to construct a value for ``self._filtered_threads`` zd%sTN)rnr_multiplyr\rarb_check_consistencyitems)r thread_listrrthreadconsistency_checkeds r_construct_threadsz"DiscourseTester._construct_threads.s d $..) RC-- VDNN3!#45c:L//@@JTTVW#"$ %#+t}}S1> B C!L%4q{{7K7K%KNLs+B9*,B>> C*C%%C*c|j|j|s|rd}|r)|s|j|y|j||yy)a^ Construct and show the readings of the discourse (or of a single sentence). :param sentence: test just this sentence :type sentence: str :param threaded: if ``True``, print out each thread ID and the corresponding thread. :param filter: if ``True``, only print out consistent thread IDs and threads. T)rrrN)rrrr)rrthreadedrtrrs rr$zDiscourseTester.readingsfs^   " ! )H ##X#6""!8L# rc| |j}||Dcgc]/}|jdddD]}||j||f1c}}Scc}}w)a Given a thread ID, find the list of ``logic.Expression`` objects corresponding to the reading IDs in that thread. :param thread_id: thread ID :type thread_id: str :param threads: a mapping from thread IDs to lists of reading IDs :type threads: dict :return: A list of pairs ``(rid, reading)`` where reading is the ``logic.Expression`` associated with a reading ID :rtype: list of tuple Nr)rar9r_)r thread_idrrrs rrxzDiscourseTester.expand_threadssp ?mmGy) yy~bq) $..%c* + +   s4Ac rg}t|D]}|j||Dcgc]\}}| }}}tt|jj |jj |}|r.||jz }td|d} | j} nd} |j|| f|stdtd|ztd|r|D] } t| td| rt jdtd "|Scc}}w) N)r) max_modelsFPzModel for Discourse Thread %scooked)formatzNo model found! )rnrxlistmapr`r)r!rer build_modelr|spacerromodel) rrrwrtresultsrrrrvmb modelfoundas rrz"DiscourseTester._check_consistencys.'? /C.2.A.A#w.A.W*S'K))00))88EK t///  {rB^^- " NNC, -r 5;<r (!a!2J"((((34-.; /<;s D3c >|j|j|r||j|in |j}|j|||D]\}}||Dcgc]}|}}|sUt d|d|d|j |D]$\}} t d|d| j &t ot d|d|d|j |D]$\}} t d|d| j &t ycc}w) z Call Mace4 to build a model for each current discourse thread. :param thread_id: thread ID :type thread_id: str :param show: If ``True``, display the model that has been found. )rwrtzInconsistent discourse:  :z rmzConsistent discourse: N)rrrarrorxr) rrrwrtrrrridlistrs rr}zDiscourseTester.modelss6   " !;D9dmmI67$--#66 $ 7  OC&-S\2cc2F20Qvha@A$($7$7$<?LCDR(9(9(;'<=>?.se1VHA>?$($7$7$<?LCDR(9(9(;'<=>? 3s! Dcddlm}t|D]>\}}t||sJ|rt d|z|j j |@|j|jy)a0 Add a list of background assumptions for reasoning about the discourse. When called, this method also updates the discourse model's set of readings and threads. :param background: Formulas which contain background information :type background: list(Expression) rrz"Adding assumption %s to backgroundN) rcrr\rdrorer|rr)rrhrtrcountrks radd_backgroundzDiscourseTester.add_backgroundsn .!*- 'HE1a, ,,:UBC    # #A &  '   " !rcN|jD]}tt|y)z: Show the current background assumptions. N)reror{)rrks rrhzDiscourseTester.backgrounds$!! A #a&M rcxg}|D]2}|D]+}g}||z }|j||j|-4|S)a Multiply every thread in ``discourse`` by every reading in ``readings``. Given discourse = [['A'], ['B']], readings = ['a', 'b', 'c'] , returns [['A', 'a'], ['A', 'b'], ['A', 'c'], ['B', 'a'], ['B', 'b'], ['B', 'c']] :param discourse: the current list of readings :type discourse: list of lists :param readings: an additional list of readings :type readings: list(Expression) :rtype: A list of lists )r|) discourser$resultsublistrnews rrzDiscourseTester.multiplysS  #G #w 1  c"  # #  r)NN)FF)TrB)NFTFF)NTF)F)r*r+r,__doc__r4rqrrrryrrrrr$rxrr}rrh staticmethodrrrrrXrXs">2 $D02A 5"6"C0" > . D8"(rrXc*g}t|jD]Q\}}|j}|jds|dk(r- |j t j |S|S#t$r}td|d||d}~wwxYw)a  Temporarily duplicated from ``nltk.sem.util``. Convert a file of first order formulas into a list of ``Expression`` objects. :param s: the contents of the file :type s: str :return: a list of parsed formulas. :rtype: list(Expression) #rzUnable to parse line rmN) r\ splitlinesstrip startswithr|r fromstringrr)s statementslinenumlinerks rload_folrsJ"1<<>2O zz| ??3 42:  O   j33D9 : O  O4WIRvFGQ N Os $A22 B;B  Bctddg|}|jtt|jt|j t|j dt|jd|j dt|jt|j dttddg|}t|j d d t|j t|j d d t|jt|j d t|j d d tgd|}|j dddl}tjjddd}|jj|}t|j|d |jt|j dt|jy)? Illustrate the various methods of ``DiscourseTester`` z A boxer walkszEvery boxer chases a girlT)rd1zJohn is a boxerzA student danceszEvery student is a personzNo person dances)rrszA person dances)r~)zVincent is a boxerzFido is a boxerzVincent is marriedz Fido barks)rrNrF book_grammarszbackground.folF)rXr}rorqr$rr nltk.datarKrLrMdataloadrrh)rgdtnltkbackground_filerhs rdiscourse_demor*s  56 BIIK G GLLN GKKM GKKK GIIdOOO%& GLLN GKKK G  89? B GOO&4O8 GKKM G*D9 GIIK GKK!" GOO%O6 U BKKtKggll:@PQO0J Gj%0MMO GKKtK GIIKrctddg|}|jt|jt|j t|j dt|j ddy)rzevery dog chases a boyzhe runsT)rrN)rXr}rorqr$)rgrs rdrt_discourse_demordsc 2I> PBIIK GLLN GKKM GKKTK* GKKt$K7rc td|zy)Nr)ro)nums rrrts #)rcvttgd}t|}tt d|y)N))z^(chases|runs)$VB)z^(a)$ex_quant)z ^(every)$ univ_quant)z ^(dog|boy)$NN)z^(he)$PRP)taggerF)rIrJ)rrr rrD)rrJs rdemorxs5  F&)IKr__main__rB))'rrKabcrr functoolsroperatorrrrrnltk.inference.macer nltk.inference.prover9r nltk.parser nltk.parse.maltr nltk.sem.drtr r nltk.sem.gluerrcrnltk.tagrrr/rDrXrrrrrr*rrrrs#J '+1"&F!%!& w& R8# N# LeeP 27t 8 $ zFr