ó <¿CVc@s†dZddlmZddlZddlmZmZddlmZddl m Z ddl m Z ddl mZdd lmZdd lmZmZdd lmZdd lmZdd lmZddlmZdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZ d„Z!e"d„Z#e"d„Z$dd„Z%d„Z&e'dkr‚e&ƒndS(sô 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). iÿÿÿÿ(tprint_functionN(tand_tadd(treduce(tshow_cfg(t RegexpTagger(t load_parser(t MaltParser(tresolve_anaphoratAnaphoraResolutionException(tDrtGlue(t Expression(t MaceCommand(tProver9CommandtReadingCommandcBs,eZd„Zd„Zd„Zd„ZRS(cCs tƒ‚dS(sS :param sentence: the sentence to read :type sentence: str N(tNotImplementedError(tselftsentence((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytparse_to_readings@scCs|S(s9 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) ((Rtsentence_readings((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytprocess_threadGs cCs tƒ‚dS(s  :param readings: readings to combine :type readings: list(Expression) :return: one combined reading :rtype: Expression N(R(Rtreadings((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytcombine_readingsSscCs tƒ‚dS(sï 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 N(R(Rt expression((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytto_fol\s (t__name__t __module__RRRR(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR?s  tCfgReadingCommandcBs/eZdd„Zd„Zd„Zd„ZRS(cCs+|r |nd|_t|jƒ|_dS(sg :param gramfile: name of file where grammar can be loaded :type gramfile: str s%grammars/book_grammars/discourse.fcfgN(t _gramfileRt_parser(Rtgramfile((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyt__init__iscCsKddlm}|jƒ}|jj|ƒ}g|D]}||ƒ^q5S(s(:see: ReadingCommand.parse_to_readings()iÿÿÿÿ(t root_semrep(tnltk.semR tsplitRtparse(RRR ttokensttreesttree((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRqs cCs tt|ƒS(s':see: ReadingCommand.combine_readings()(RR(RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRxscCs|S(s:see: ReadingCommand.to_fol()((RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR|sN(RRtNoneRRRR(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRhs   tDrtGlueReadingCommandcBs>eZdedd„Zd„Zd„Zd„Zd„ZRS(cCsI|dkr'tjjdddƒ}ntd|d|d|ƒ|_dS(s¿ :param semtype_file: name of file where grammar can be loaded :param remove_duplicates: should duplicates be removed? :param depparser: the dependency parser tgrammarstsample_grammarssdrt_glue.semtypet semtype_filetremove_duplicatest depparserN(R'tostpathtjoinR t_glue(RR+R,R-((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR‚s   cCs|jj|ƒS(s(:see: ReadingCommand.parse_to_readings()(R1tparse_to_meaning(RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRscCs-y|j|ƒgSWntk r(gSXdS(s%:see: ReadingCommand.process_thread()N(RR (RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR“s cCstt|ƒ}t|jƒƒS(s':see: ReadingCommand.combine_readings()(RRRtsimplify(RRtthread_reading((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRšscCs |jƒS(s:see: ReadingCommand.to_fol()(tfol(RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRŸsN( RRR'tFalseRRRRR(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR(s    tDiscourseTestercBséeZdZddd„Zd„Zeed„Zed„Z d„Z d„Z d„Z d„Z dd „Zeed „Zdeeeed „Zdd „Zeed „Zdeed„Zed„Zd„Zed„ƒZRS(s3 Check properties of an ongoing discourse. cCsØ||_tgt|ƒD]\}}d||f^qƒ|_d|_i|_|r_|ntƒ|_i|_ i|_ |dk rËddl m }x#|D]}t ||ƒs t‚q W||_n g|_dS(s÷ Initialize a ``DiscourseTester``. :param input: the discourse sentences :type input: list of str :param background: Formulas which express background assumptions :type background: list(Expression) ss%siÿÿÿÿ(R N(t_inputtdictt enumeratet _sentencesR't_modelst _readingsRt_reading_commandt_threadst_filtered_threadstnltk.sem.logicR t isinstancetAssertionErrort _background(Rtinputtreading_commandt backgroundtitsentR te((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR¨s 8       cCs9x2t|jƒD]!}td||j|fƒqWdS(sI Display the list of sentences in the current discourse. s%s: %sN(tsortedR;tprint(Rtid((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyt sentencesÄsc CsF|rÎ|jdtƒxµt|jƒD]¡}g|j|ƒD]\}}|^q<}||j7}xc|j|ƒD]R}td|d|ƒ} | jƒrqt d|t |ƒfƒt d|ƒqqqqWq&Wn|j j |ƒt gt|j ƒD]\} } d| | f^qñƒ|_|rB|jdtƒ|jdtƒndS( sÊ 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``. tverbosetgoalt assumptionss!Sentence '%s' under reading '%s':s'Not informative relative to thread '%s'ss%stshowN(RR6RKR?texpand_threadsRDt _get_readingsR tproveRLtstrR8tappendR9R:R;tmodels( RRt informchkt consistchkttidtridtreadingRQt sent_readingttpRHRI((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyt add_sentenceËs (  ;cCs­y|jj|ƒWn*tk r@td|ƒ|jƒdSXtgt|jƒD]\}}d||f^qTƒ|_|j dt ƒ|r©tdƒ|jƒndS(s# 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. sJRetraction failed. The sentence '%s' is not part of the current discourse:ss%sROsCurrent sentences are N( R8tremovet ValueErrorRLRNR'R9R:R;RR6(RRRORHRI((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytretract_sentenceês   ; cCst|jjƒdS(sJ Print out the grammar in use for parsing input sentences N(RR>R(R((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytgrammarÿscCs|jj|ƒS(se Build a list of semantic readings for a sentence. :rtype: list(Expression) (R>R(RR((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRT scCs—i|_x‡t|jƒD]v}|j|}|j|ƒ}tgtt|dtƒƒD](\}}d||f|jƒf^qWƒ|j|RRpt Exceptiont __class__RRL( Rtfiltertshow_thread_readingstthreadsR[R\RR4RJ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyt _show_threads@s5cCse|jƒ|jƒ|s |r)t}n|ra|sH|jd|ƒqa|jd|d|ƒndS(s^ 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. RRxRyN(RgRoRkRsR{(RRtthreadedRORxRy((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRTs    cCs^|dkr|j}ng||D]7}|jdƒd D]}||j||f^q:q#S(sÄ 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 RtiN(R'R?R"R=(Rt thread_idRzR\Rf((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRSls  c CsHg}x;t|ƒD]-}g|j|d|ƒD]\}}|^q/}tt|jj|jj|ƒƒƒ}|r¥||j7}td|ddƒ} | j ƒ} nt } |j || fƒ|rt dƒtd|ƒt dƒ|rx|D]} t| ƒqóWt dƒn| r3t| jddƒƒq@tdƒqqW|S( NRzt max_modelsiiPsModel for Discourse Thread %stformattcookedsNo model found! (RKRStlisttmapR>RRRDR R't build_modelR6RWtspacerRLtmodel( RRzRRROtresultsR[R\R]RQtmbt modelfoundta((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRi€s,.'     c Cs7|jƒ|jƒ|r.i|j||6n|j}xù|j|d|d|ƒD]Ü\}}g||D] }|^qj}|sÚtd||fƒx7|j|ƒD]&\}} td|| jƒfƒq¦WtƒqStd||fƒx7|j|ƒD]&\}} td|| jƒfƒqþWtƒqSWdS(sÛ 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. RRROsInconsistent discourse: %s %s:s %s: %ssConsistent discourse: %s %s:N(RgRoR?RiRLRSRp( RR}RRRORzR[RˆR\tidlistR]((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRX›s  #( cCsddlm}xVt|ƒD]H\}}t||ƒs>t‚|rUtd|ƒn|jj|ƒqW|jƒ|j ƒdS(s0 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) iÿÿÿÿ(R s"Adding assumption %s to backgroundN( RAR R:RBRCRLRDRWRgRo(RRGROR tcountRJ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytadd_backgroundµs cCs(x!|jD]}tt|ƒƒq WdS(s: Show the current background assumptions. N(RDRLRV(RRJ((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRGÈscCsVg}xI|D]A}x8|D]0}g}||7}|j|ƒ|j|ƒqWq W|S(sÛ 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 (RW(t discourseRtresulttsublistRqtnew((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyRhÓs    N(RRt__doc__R'RRNR6R`RkRcRdRTRgRoRsR{RRSRiRXRŒRGt staticmethodRh(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR7¤s&         cCsžg}x‘t|jƒƒD]}\}}|jƒ}|jdƒs|dkrRqny|jtj|ƒƒWqtk r•td||fƒ‚qXqW|S(s  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) t#RusUnable to parse line %s: %s( R:t splitlineststript startswithRWR t fromstringRvRb(tst statementstlinenumtline((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytload_folïs   cCsùtddg|ƒ}|jƒtƒtƒ|jƒtƒ|jƒtƒ|jdtƒtƒ|jdƒ|jdƒtƒ|jƒtƒ|jdtƒtƒtddg|ƒ}tƒ|jdd tƒtƒ|jƒtƒ|jdd tƒtƒ|jƒtƒ|jd ƒtƒ|jd d tƒtd dddg|ƒ}|jdtƒddl}t j j dddƒ}|j j |ƒ}tƒ|j|d tƒ|jƒtƒ|jdtƒtƒ|jƒdS(s? Illustrate the various methods of ``DiscourseTester`` s A boxer walkssEvery boxer chases a girlR|td1sJohn is a boxersA student dancessEvery student is a personsNo person dancesRZROsA person dancesRYsVincent is a boxersFido is a boxersVincent is marrieds Fido barksRxiÿÿÿÿNR)t book_grammarssbackground.fol(R7RXRLRNRRkR`Rct nltk.dataR.R/R0tdatatloadRŒR6RG(RFtdttnltktbackground_fileRG((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytdiscourse_demos\                  cCsytddg|ƒ}|jƒtƒ|jƒtƒ|jƒtƒ|jdtƒtƒ|jdtdtƒdS(s? Illustrate the various methods of ``DiscourseTester`` severy dog chases a boyshe runsRyRxN(R7RXRLRNRRk(RFR¢((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytdrt_discourse_demo=s     icCstd|ƒdS(NRt(RL(tnum((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyR„NscCsNtƒtdddddgƒ}td |ƒ}ttd td |ƒƒdS(Ns^(chases|runs)$tVBs^(a)$tex_quants ^(every)$t univ_quants ^(dog|boy)$tNNs^(he)$tPRPttaggerR,R-(s^(chases|runs)$R¨(s^(a)$R©(s ^(every)$Rª(s ^(dog|boy)$R«(s^(he)$R¬(R¥RRR¦R(R6(R­R-((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pytdemoQs t__main__((R‘t __future__RR.toperatorRRt functoolsRRŸRtnltk.tagRt nltk.parseRtnltk.parse.maltRt nltk.sem.drtRR t nltk.sem.glueR RAR tnltk.inference.maceR tnltk.inference.prover9R tobjectRRR(R7RœR'R¥R¦R„R®R(((sj/private/var/folders/cc/xm4nqn811x9b50x1q_zpkmvdjlphkp/T/pip-build-FUwmDn/nltk/nltk/inference/discourse.pyt,s2 )#ÿL  6