Ñò
rùMc           @   sS  d  Z  y d d k l Z Wn# e j
 o d d k l Z n Xd d k l Z l Z d d k l	 Z	 d d k
 l
 Z
 d d k Z d „  Z d „  Z d	 „  Z d
 „  Z h  h  h  e d „ Z d h  e d „ Z h  d d „ Z d „  Z e d „ Z e d „ Z d „  Z e d „ Z e d „ Z d „  Z d „  Z e d „ Z h  e d „ Z e d „ Z e h  e d „ Z d e d „ Z! e d „ Z" d „  Z# d „  Z$ d  „  Z% d! „  Z& d" „  Z' d# „  Z( d$ „  Z) d% „  Z* d& „  Z+ d' „  Z, d( „  Z- e. d) j o[ d d k/ Z/ e/ i0 d* e d+ e/ i1 e/ i2 Bƒ \ Z3 Z4 e4 o e3 o d, e4 GHn e ƒ  n d S(-   sc  
	Bachelor AI project 2009. Andreas van Cranenburgh 0440949.
	Two-word stage dialogue simulator using a corpus of exemplars.

	Interactive usage:
	$ python model.py

	Other uses:
		- Re-enact dialogue in 01.cha (should be a text file with one utterance
		  per line, with each line preceeded by *MOT or *CHI, as in the CHILDES
		  corpus):

		  $ python
#	  	  >>> import model
#		  >>> model.evaluate(open('01.cha').read())
		  ...

		- Talk to self for 25 utterances:

#		  >>> model.selftalk(25)
		  ...

		- Demonstrate some sample utterances that work (but not from corpus):

#		  >>> model.test()
		  ...
iÿÿÿÿ(   t   choice(   t	   uppercaset	   lowercase(   t   defaultdict(   t   pprintNc          C   s~   d Gd GHHd GHt  ƒ  i ƒ  GHHt ƒ  }  |  i d ƒ xC t o; d Gt ƒ  } | d j o Pn |  i | ƒ } d G| GHq7 Wd S(   s%    Interactive textual user interface. s   Child dialogue simulator.s+   Enter parent utterance (or `quit' to quit).s   Utterances in corpus: s   Parent: t   quits   Child: N(   t   getexemplarst   keyst   dialoguet   sendt   Nonet   Truet	   raw_input(   t   dt   uttt   reply(    (    s    /home/andreas/ai/baproj/model.pyt   main3   s     	 	c          G   s"   d i  t d „  |  Dƒ ƒ ƒ GHd S(   s    Print debug output (also see nodbg).
		Takes a variable number of arguments of any type.

		>>> dbg('choices', range(3))
		choices [0, 1, 2]
	t    c         s   s%   x |  ] } t  | ƒ i ƒ  Vq Wd  S(   N(   t   strt
   expandtabs(   t   .0t   a(    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>O   s   	 N(   t   joint   list(   t   m(    (    s    /home/andreas/ai/baproj/model.pyt   dbgH   s    c          G   s   d  S(   N(    (   R   (    (    s    /home/andreas/ai/baproj/model.pyt   nodbgP   s    c         C   sc   d d k  l } l } y | t |  d d ƒ ƒ SWn+ | ƒ  } | | t |  d d ƒ ƒ | SXd  S(   Niÿÿÿÿ(   t   dumpt   loads   .picklet   rbt   wb(   t   cPickleR   R   t   open(   t   itemt   funR   R   t   temp(    (    s    /home/andreas/ai/baproj/model.pyt   loadorgenerateR   s    	c         #   sò  ˆ p t  d t ƒ ‰ n ˆ  p t  d ‡ f d †  ƒ ‰  n | p t  d ‡  ‡ f d †  ƒ } n d Gˆ  GHd g d d } } } xht o`| V} t | | ˆ ˆ  ƒ } d | j o- t | ƒ d	 j o t |	 |
 | | ˆ ƒ n | }	 | d
 |	 ƒ t ob | ˆ j oU |	 oN |	 t | |	 ˆ  ƒ j o' | d ƒ |	 ˆ | <t ˆ ˆ  ƒ } q_| d ƒ n t	 |	 ˆ ƒ }
 | d |
 ƒ t
 |
 ˆ | ˆ  ƒ } | i | |	 f ƒ | i | |
 f ƒ | |	 j o | p | o t | ƒ p d } q† q† Wd S(   s£    Co-routine for dialogue. Use send(utterance) to receive a reply.
		Initial input: exemplars
		Further input: one utterance at a time
		Output: one reply at a timet	   exemplarst   lexiconc              s   t  ˆ  t ƒ S(    (   t   inferlexicont   False(    (   R%   (    s    /home/andreas/ai/baproj/model.pyt   <lambda>d   s    t   constructionsc              s   t  ˆ ˆ  ƒ S(    (   t   inferconstructions(    (   R&   R%   (    s    /home/andreas/ai/baproj/model.pyR)   g   s    s!   Lexicon (distilled from corpus): t    t   reinforcementi   s   	interpretation:s   added utterance to exemplarss/   generation check failed, not added to exemplarss
   	reaction:N(   R,   R,   (   R$   R   R   t	   interprett   lent	   reinforceR(   t   lexiconcheckR+   t   responset   express2t   appendt	   findtopic(   R%   R*   R&   t   debugt	   discourset   topict   reactionuttR   t
   newmeaningt   meaningt   reaction(    (   R&   R%   s    /home/andreas/ai/baproj/model.pyR   [   s>    	  

i   c         C   s+  | p t  ƒ  } n t | i ƒ  ƒ } d \ } } t | g ƒ } g  } t | ƒ } | i d ƒ x² t |  ƒ D]¤ }	 | i d | | f ƒ | | d ƒ | | } } | i | ƒ }
 | |
 j oJ | d j o= | d ƒ t t	 t | i ƒ  ƒ | ƒ ƒ } | i
 | ƒ qm |
 } qm Wd Gd i | ƒ GHd S(	   sº    Talk to oneself, picking a random utterance from the exemplars when
		repetition is detected.
		Input: number of utterances to generate, exemplars
		Output: transcript of conversation
	s   *MOTs   *CHIs   %s: %siÿÿÿÿs   picking random utterances   
N(   s   *MOTs   *CHI(   R   R    R   t   setR   R	   R
   t   rangeR4   R   t   addR   (   t   ut   eR6   R   t   speakingt	   listeningt   saidt   cR   t   xt   r(    (    s    /home/andreas/ai/baproj/model.pyt   selftalk…   s(     
"
i   c         C   s  | p t  ƒ  } n t | ƒ } | i d ƒ x» t | ƒ D]­ } g  } xž |  i d ƒ D] } y | i d d ƒ \ } } Wn t j
 o
 qV n Xd | j oE | GHd | i | ƒ }	 |	 GHx% | |	 f D] }
 | i |
 ƒ qÈ WqV qV Wq: WHd i d „  | Dƒ ƒ S(   s   When presented with a corpus fragment, feed parent utterances to model 
		and generate model responses in place of child utterances.
		n = number of iterations to perform
		Input: string with transcript of corpus
		Output: same transcript with replies from model instead of child
	s   
t   :i   t   MOTs   *CHI:  c         s   s   x |  ] } t  | ƒ Vq Wd  S(   N(   R   (   R   R   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>Â   s   	 N(	   R   R   R	   R
   R>   t   splitt
   ValueErrorR4   R   (   t   convR%   t   nR   R   t	   modelconvt   bt   whoR   t   ansRF   (    (    s    /home/andreas/ai/baproj/model.pyt   evaluate¥   s,    	   c         C   s«   d i  d „  t d ƒ Dƒ ƒ } | d d | j o d Gn d G|  d | | | d d <d	 G| | d d GH| | j o d Gn d Gd
 G| d | | | <| | GHd S(   sÔ    Strengthen connection between last two utterances by adding 
		a random identifier and adding or updating the resulting exemplar.
		Input: two utterances with meanings
		Side-effect: updated or added exemplars
	R,   c         s   s   x |  ] } t  t ƒ Vq Wd  S(   N(   R    R   (   R   R   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>Ê   s   	 i   iÿÿÿÿi    t   updatedt   addedR   s   reinforcement:t   withN(   R   R>   (   R;   R<   R9   R7   R%   t   randomid(    (    s    /home/andreas/ai/baproj/model.pyR0   Ä   s    c            sˆ   d „  ‰ |  d d i  ƒ  |  d d i  ƒ  ‰  ‰ t ˆ  d ˆ ƒ } | o8 ˆ ‡ ‡ ‡  f d †  | Dƒ ƒ d } | d | ƒ | Sd S(   s²    Look for a recurring element in the last two utterances,
		if found, this becomes the new topic.
		Input: discourse (list of utterances and meanings)
		Output: clause, or None
	c         S   sE   t  |  ƒ }  y d „  |  Dƒ i ƒ  SWn t j
 o t |  ƒ SXd  S(   Nc         s   s6   x/ |  ]( \ } } | d  j o | | f Vq q Wd S(   i   N(    (   R   R   RP   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>ã   s   	 (   R   t   nextt   StopIterationt   min(   t   seq(    (    s    /home/andreas/ai/baproj/model.pyt   minprefertwoà   s
    iÿÿÿÿi   iþÿÿÿc         3   sC   x< |  ]5 } ˆ ˆ i  | ƒ | f ˆ  i  | ƒ | f f ƒ Vq Wd  S(   N(   t   index(   R   R   (   t   m2R\   t   m1(    s    /home/andreas/ai/baproj/model.pys	   <genexpr>î   s   	s   	topic:N(   RK   t	   intersect(   R7   R6   t   commonR8   (    (   R_   R^   R\   s    /home/andreas/ai/baproj/model.pyR5   Ù   s    		)c         C   sg  d „  } |  i  d d ƒ }  |  i ƒ  } g  } | i ƒ  D]% \ } }	 | | | | ƒ | |	 f q8 ~ }
 t |
 ƒ d } | o> g  } |
 D]& } | d | j o | | d qˆ qˆ ~ }
 n d Sg  } x³ |
 D]« \ } } g  } | D] }	 |	 | j o | |	 qã qã ~ } t | | | | t ƒ \ } } | oH | i | t t |  | | ƒ | ƒ d t	 t
 | ƒ ƒ | | f f ƒ qÌ qÌ W| oÝ t | ƒ d } | d	 ƒ | d
 | ƒ t | | d t |	 ƒ ƒ } | o
 | }	 n | d }	 g  } | D]( } | | d i ƒ  j o | | qëqë~ } | d t d i | ƒ ƒ ƒ | o t | |	 | | | ƒ d S|	 Sd Sd S(   sö    Return semantic representation for linguistic utterance.
		This function is in charge of backtracking over initial exemplars
		and picking the best result, the rest is done by interpretwith()
		Input: utterance
		Output: meaning representation
	c         S   s   t  t |  | i ƒ  ƒ ƒ S(   N(   R/   R`   RK   (   R   RP   (    (    s    /home/andreas/ai/baproj/model.pyt   matchû   s    s   [= s   [=i    i   R,   g      ð?iÿÿÿÿs   initial exemplar:s   	s   words left:R   N(   t   replaceRK   t   itemst   maxt   interpretwithR   R4   t	   edit_distR1   R/   t   tokensRZ   t
   conciliatet
   varclausest   reprR   (   t	   utteranceR8   R%   R&   R6   Rb   t   wordst   _[1]R   RP   t   initialRN   t   _[2]t   it   exuttt	   exmeaningt   _[3]t   wR;   RE   t   _[4](    (    s    /home/andreas/ai/baproj/model.pyR.   ô   sB    	?> 2


<c            sr   d „  ‰ t  ˆ ƒ ‰  d i ‡ ‡ f d †  |  i ƒ  Dƒ ƒ ‰ ‡ ‡  f d †  ‰ d i ‡ f d †  | i ƒ  Dƒ ƒ S(   Nc         S   s.   |  i  d |  d d !|  d d !ƒ i ƒ  i ƒ  S(   Ns   [=%s]i   iÿÿÿÿ(   Rc   t   stript   lower(   R   (    (    s    /home/andreas/ai/baproj/model.pyt   elimB  s    R   c         3   s:   x3 |  ], } ˆ  | ƒ ˆ j o ˆ ˆ  | ƒ Vq q Wd  S(   N(    (   R   RP   (   Ry   t   lex(    s    /home/andreas/ai/baproj/model.pys	   <genexpr>F  s   	 c            s(   |  ˆ j p |  ˆ  j p |  d d j S(   NiÿÿÿÿRI   (    (   R   (   t   uttlext   revlex(    s    /home/andreas/ai/baproj/model.pyt   checkK  s    c         3   s*   x# |  ] } ˆ  | ƒ o	 | Vq q Wd  S(   N(    (   R   R   (   R}   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>M  s   	 (   t   revdictR   RK   (   R   R;   Rz   (    (   R|   Ry   R{   Rz   R}   s    /home/andreas/ai/baproj/model.pyR1   >  s
    	(c         C   se  t  |  ƒ } |  od |  d d d j oO |  d d i | i ƒ  ƒ i ƒ  j o% | d t |  d ƒ ƒ |  d }  qw n | o'g  } |  D] } d | j o | | q‰ q‰ ~ oô g  } |  D] } d | j o | | q¼ q¼ ~ d d d !i ƒ  i ƒ  }	 |	 | j od t | |	 | d | ƒ }
 |
 o |
 } | d | ƒ qkd S| d c d | |	 7<| d
 | ƒ n g  } |  D]" } | d d j o | | qvqv~ }  n x§t	 |  ƒ D]™} d i | ƒ } x| D]y} | | j oft
 | ƒ i | i ƒ  ƒ oJ| }
 g  } | D]" }	 |	 | j o | | |	 qq~ } t | | | | d	 g | ƒ }
 |
 oã g  } |  D] } | | j o | | qkqk~ } g  } |  D] } | | j o | | qq~ }  | d t d i | ƒ ƒ d t | ƒ ƒ | d t | | ƒ ƒ | d t |
 ƒ ƒ t |  |
 | | | ƒ \ } } d | | f SqÎqÎWq²W|  o d Sd | f S(   s‘    Interpretation helper function called by interpret(), work out meaning
		of remaining words by stitching together the best matching fragments.
	i    t   [R   t   skippingi   i   iÿÿÿÿs   demonstrative dereferenced:R,   s   appended demonstrative:t   	t   ins   	ands   	matchesN(   i    R,   (   i    R,   (   R/   R   R   RK   Rk   Rw   Rx   Ri   R
   t   substr_iterR=   t   issubsetRf   (   Rm   t   partialmeaningR%   R&   R6   t   lRn   Ru   Rp   t   wordt   pmRt   R   t   subt	   substringRP   Rv   t   substt   _[5]RE   t   matchest   _[6]R   t   p(    (    s    /home/andreas/ai/baproj/model.pyRf   P  sH    &:I:  )6 22(c      
      sð  d „  ‰  d |  j o d |  i  d ƒ d Sd |  j o t d ƒ } | Sd	 |  j oƒd |  j o d
 Sd |  i  d d ƒ d } ‡  f d †  } g  } | i ƒ  D] } | | | | ƒ | f q£ ~ } t | ƒ d d j  oó g  }	 | D]& } | d d j  o |	 | d qê qê ~	 }
 | d t |
 ƒ ƒ |
 o t |
 ƒ } n d
 St | | t | ƒ ƒ } | oh t | | ƒ } | o) t |
 ƒ d j o | d | ƒ n | St |
 ƒ d j o | d | ƒ n | Sqì| Sn d |  j o |  Sd
 S(   s    Transform a meaning into a response using adjacency pairs of speech
		acts.
		Input: meaning representation
		Output: meaning representation
	c         S   s<   t  g  } t |  ƒ D] } t | ƒ o | | q q ~ ƒ S(   N(   R/   Rh   t   var(   R   Rn   R   (    (    s    /home/andreas/ai/baproj/model.pyt   varsŽ  s    t
   imperatives   acknowledgement RI   i   t
   ynquestiont	   agreementt   denialt
   whquestionR,   s
   assertion:c            s'   t  t |  ƒ t | ƒ ƒ d ˆ  | ƒ S(   Ng      à?(   Rg   Rh   (   R   R   (   R‘   (    s    /home/andreas/ai/baproj/model.pyt   scorež  s    i    s   possible reactions:s   choice:t	   assertion(   R”   R•   (	   RK   R    t   valuesRZ   t   sortedRi   Rj   t   unifiesR/   (   R;   R%   R6   t   opR   R—   Rn   R   R   Rp   t   choicesRG   RN   (    (   R‘   s    /home/andreas/ai/baproj/model.pyR2   ˆ  s@    	6:	c   	      C   sÛ   |  | i  ƒ  j o t t | ƒ |  ƒ Sd „  } g  } | i ƒ  D]" \ } } | | |  | ƒ | f qB ~ } t | ƒ d t |  ƒ j  oI t g  } | D], } | d t | ƒ d j o | | q˜ q˜ ~ ƒ d Sd Sd S(   s;    Express `meaning' by returning the most similar exemplar. c         S   s   t  |  i ƒ  | i ƒ  ƒ S(   N(   Rg   RK   (   R   R   (    (    s    /home/andreas/ai/baproj/model.pyt   distÇ  s    i    i   t   0N(   R™   R    R~   Rd   RZ   R/   (	   R;   R%   R&   Rž   Rn   R   RP   R   Rp   (    (    s    /home/andreas/ai/baproj/model.pyt   expressÀ  s    	<Ic            sE  ‡ f d †  ‰ ˆ  | j o" d i  ‡ f d †  | ˆ  Dƒ ƒ Sˆ  ˆ i ƒ  j o t t ˆ ƒ ˆ  ƒ St ‡  f d †  ˆ i ƒ  Dƒ ƒ oC t g  } ˆ i ƒ  D]$ \ } } ˆ  | j o | | q q ~ ƒ St ‡  f d †  ˆ i ƒ  Dƒ ƒ oC t g  } ˆ i ƒ  D]$ \ } } | ˆ  j o | | qq~ ƒ St ˆ  | ˆ | ƒ S(   sD    Express `meaning' by returning a matching exemplar or construction c            s   |  d i  ˆ  i ƒ  ƒ j S(   NR   (   R   R   (   R   (   R*   (    s    /home/andreas/ai/baproj/model.pyR}   Ò  s    R   c         3   s*   x# |  ] } ˆ  | ƒ o	 | Vq q Wd  S(   N(    (   R   R   (   R}   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>Õ  s   	 c         3   s   x |  ] } ˆ  | j Vq Wd  S(   N(    (   R   t   clauses(   R;   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>Ú  s   	 c         3   s   x |  ] } | ˆ  j Vq Wd  S(   N(    (   R   R¡   (   R;   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>Ý  s   	 (   R   R™   R    R~   t   anyRd   R3   (   R;   R%   R*   R&   Rn   R   R¡   Rp   (    (   R;   R*   R}   s    /home/andreas/ai/baproj/model.pyt   expressmultiÏ  s    "# ## #c            s«  ‡  f d †  } ˆ | i  ƒ  j o. t t | ƒ ˆ ƒ } | d | ƒ | | ƒ Sˆ | i  ƒ  j o. t t | ƒ ˆ ƒ } | d | ƒ | | ƒ St ‡ f d †  | i  ƒ  Dƒ ƒ o\ t g  } | i ƒ  D]$ \ } } ˆ | j o | | qÈ qÈ ~ ƒ } | d | ƒ | | ƒ St ˆ  ƒ }	 d ˆ j o ˆ i ƒ  ‰ n ˆ |	 j o	 |	 ˆ Sd St ˆ d ƒ p t ˆ d ƒ d t j o ˆ d }
 n d S| d |
 ƒ x. ˆ d D]" } | |	 j o t |	 | ƒ SqªW|
 |	 j o |	 |
 i ƒ  St ˆ d ƒ } | oF | d t j o5 t	 ˆ ƒ d	 j o t
 ˆ d	 ƒ } qIˆ d } n t
 ˆ d ƒ } g  } ˆ  i ƒ  D]$ \ } } | | j o | | qjqj~ } | o[ t g  } ˆ  i ƒ  D]$ \ } } | | j o | | q²q²~ ƒ } | d
 | d | ƒ n d } t d d g ƒ o | Sg  } ˆ  i ƒ  D]1 \ } } | | j o | | j o | | q(q(~ } | o1 t | ƒ } | d | } | d | d | ƒ n | d | ƒ | S(   sÕ    Transform a meaning into a two word utterance using exemplars,
		constructions or the lexicon. Filter result according to lexical
		knowledge.
		Input: meaning representation
		Output: one or two word utterance
	c            s;   d i  ‡  f d †  |  i ƒ  Dƒ ƒ } | d  j o d S| S(   NR   c         3   s0   x) |  ]" } | i  ƒ  ˆ  j o	 | Vq q Wd  S(   N(   Rx   (   R   R   (   R&   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>ì  s   	 R,   (   R   RK   R
   (   R   RG   (   R&   (    s    /home/andreas/ai/baproj/model.pyt   reduceë  s    %s   reduced:c         3   s   x |  ] } ˆ  | j Vq Wd  S(   N(    (   R   R¡   (   R;   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>ý  s   	 RI   R,   i   s   trying to express:i    i   s   comment:R‚   R   s   topic:s   topic not expressible?(   R™   R    R~   R¢   Rd   RK   t   argR   t   popR/   t   pred(   R;   R%   R*   R&   R6   R¤   R   Rn   R¡   R|   t   clauseR   R8   t   commentRp   RP   t
   candidatesRt   Rv   (    (   R&   R;   s    /home/andreas/ai/baproj/model.pyR3   ã  sd    # $	- >DKc            sz  | d ƒ t  t ƒ } t | ƒ } xê |  i ƒ  D]Ü \ } } xÍ t | i ƒ  ƒ D]¹ } t | ƒ d j o Pn d i d „  | Dƒ ƒ ‰  d ˆ  j o qN n t ‡  f d †  | Dƒ ƒ pQ t g  }	 |  D] }
 ˆ  |
 j o |	 |
 qÄ qÄ ~	 ƒ } | o | | ˆ  <qqN qN Wq/ WxCt t	 | ƒ i ƒ  ƒ D])\ } } | d } | | j o q(n x? | i
 ƒ  D]1 } | | j o | | j o | i | ƒ q_q_Wt | |  | t ƒ } | o. | d | t | ƒ t | ƒ ƒ | | | <q(g  } |  i ƒ  D]$ \ } } | | j o | | qïqï~ } d | } d	 t | ƒ f } | d
 t | ƒ | | ƒ q(W| t | ƒ j o | St |  | ƒ S(   s«    Build corpus of constructions from exemplars and lexicon.
		Input: exemplars & lexicon.
		Output: constructions, dictionary of pairings between substrings
		and clauses. s   distilling constructionsi   R   c         s   s:   x3 |  ], } t  d  „  | Dƒ ƒ o | i ƒ  Vq q Wd S(   c         s   s%   x |  ] } | i  ƒ  t j Vq Wd  S(   N(   Rx   R   (   R   RE   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>H  s   	 N(   t   allRx   (   R   RP   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>H  s   	 c         3   s   x |  ] } ˆ  | j Vq Wd  S(   N(    (   R   RP   (   Rm   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>L  s   	 i    s   construction (score %d)s
   (score %d)s   not found in candidates %ss   meaning of construction(   R   t   intR/   Rd   Rƒ   RK   R   R¢   Rš   R~   R   R¦   t   findmeaningR   Rk   R   R+   (   R%   R&   R*   R6   t   scoresRN   R   R;   R   Rn   RE   R—   t   formRp   t   mst	   candidate(    (   Rm   s    /home/andreas/ai/baproj/model.pyR+   9  sH    
  8 
  >
c            s„  g  } | i  ƒ  D]$ \ } } ˆ | j o | | q q ~ ‰ t | ƒ ‰ t ˆ ƒ d j og  } ˆ D] } | | i ƒ  qh ~ }	 t t |	 d |	 d ƒ }	 |	 o6 g  }
 ‡ ‡ f d †  ‰  d i ‡  f d †  |	 Dƒ ƒ St ‡ f d †  ˆ Dƒ ƒ d St t	 ƒ } xJ ˆ D]B } x9 t
 | i ƒ  ƒ D]% } d i | ƒ }	 | |	 c d 7<q%WqWt t | ƒ ƒ d d Sn ˆ o ˆ i ƒ  Sd S(   sr    Given the words in a construction, find the most common meaning
		associated with it in the corpus of exemplars. i   i    c            sM   |  ˆ  j o& t  ‡ f d †  ˆ  |  Dƒ ƒ o |  S|  d d j o d S|  S(   Nc         3   s   x |  ] } | ˆ  j Vq Wd  S(   N(    (   R   RP   (   R¯   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>‚  s   	 i    t   (s   (X)(   R¢   (   R   (   R|   R¯   (    s    /home/andreas/ai/baproj/model.pyt   abstract  s
    .R   c         3   s   x |  ] } ˆ  | ƒ Vq Wd  S(   N(    (   R   R   (   R³   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>‰  s   	 c         3   s(   x! |  ] } ˆ  i  | ƒ | f Vq Wd  S(   N(   t   count(   R   R   (   R°   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>  s   	 N(   Rd   R~   R/   RK   R¤   R`   R   Re   R   R¬   Rƒ   R¦   (   R¯   R%   R&   R6   Rn   R   R;   Rp   R   R   RN   R®   (    (   R³   R|   R°   R¯   s    /home/andreas/ai/baproj/model.pyR­   r  s(    >'  c            s"  t  d „  ˆ  i ƒ  Dƒ ƒ } t | ƒ } xó ˆ  i ƒ  D]å \ } } d | j oÌ t | | i d ƒ d | i d ƒ !ƒ } | | i d ƒ  i ƒ  d } | | | i d ƒ d i ƒ  d 7} | i ƒ  | i ƒ  }	 | | i d ƒ  | | i d ƒ d ˆ  | <|	 | j o | | |	 <qq5 q5 Wxt d i ˆ  i	 ƒ  ƒ i ƒ  ƒ t | i	 ƒ  ƒ D]]}	 |	 i ƒ  }	 g  }
 | i	 ƒ  D]* } |	 | i ƒ  j o |
 | i ƒ  qsqs~
 } g  } | i ƒ  D]0 \ } } |	 | i ƒ  j o | | i ƒ  q·q·~ } x– t
 t t | | d ƒ ƒ D]x } |	 | j oe t | ƒ d j o | | |	 <n@ ‡  f d †  ‰ ‡ f d	 †  | i ƒ  Dƒ } t | ƒ d | |	 <PqqWt o¡ |	 | j o” t t | | d ƒ } t | t | i	 ƒ  ƒ ƒ d j oW t t | | d ƒ | |	 <t | |	 ƒ d j o d
 | |	 <q-| |	 i ƒ  | |	 <q1n t | ƒ t | ƒ j o t | d ƒ j n oH t | ƒ t | d ƒ j o
 d j n o | d d | |	 <q­qPqPW| oE t d i | i	 ƒ  ƒ i ƒ  ƒ t | i	 ƒ  ƒ } | d | d ƒ n t | ƒ | j o t ˆ  | ƒ S| S(   st    Infer lexicon from corpus of exemplars.
		Input: exemplars.
		Output: lexicon, dictionary of word-clause pairings. c         s   s+   x$ |  ] \ } } | i  ƒ  | f Vq Wd  S(   N(   Rx   (   R   R   RP   (    (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>¢  s   	 R   i   t   ]iÿÿÿÿi    R   c            s"   d i  ˆ  i ƒ  ƒ i t |  ƒ ƒ S(   NR   (   R   R™   R´   R§   (   RP   (   R%   (    s    /home/andreas/ai/baproj/model.pyt   freq»  s    c         3   s%   x |  ] } ˆ  | ƒ | f Vq Wd  S(   N(    (   R   RP   (   R¶   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>½  s   	 R,   s   lexicon: not found:s   
(   t   dictRd   R/   R¬   R]   RK   Rx   R=   R   R   R   R¤   R`   RZ   R(   R¦   R'   (   R%   t   verboseR&   R6   t   exlcaseRN   R   R   R¨   R‡   Rn   R   t
   utterancesRp   RP   t   meaningsRª   t   notfound(    (   R%   R¶   s    /home/andreas/ai/baproj/model.pyR'   ž  sT     )%,2 DJ 	#4.1c      
   C   sn  | } | d j o | i ƒ  } n | d j o |  S|  } t St } x|  i ƒ  D]} t | ƒ d | j o"t | ƒ ot t | ƒ ƒ p t | ƒ | j oä t | ƒ t | ƒ i t | ƒ ƒ d } t g  } | D] }	 | t | ƒ |	 j qÚ ~ j oƒ | d t	 j o9 | d | d t | ƒ ƒ | i
 | t | ƒ ƒ } } qƒ| d t | ƒ d | ƒ | i
 | t | ƒ ƒ } } q‡n t } n t | ƒ t	 j o | Sn g  }
 | i ƒ  D]* }	 t |	 ƒ t	 j o |
 t |	 ƒ q½q½~
 } | oc d | j oV t | ƒ | j oC | i
 | d	 t | ƒ ƒ } | d
 t | ƒ ƒ t |  | | | ƒ SqR W| o | Sd S(   se   Test whether meaning can be made comptabible with partialmeaning,
		by looking for a family resemblance with partialmeanings, 
		if found, perform a substitution if necessary. Returns empty string on failure.

		>>> conciliate('question: animal(bunny) do(X)', 'assertion: point(dog) animal(dog)')
		substituted (bunny) for (dog)
		'assertion: point(bunny) animal(bunny)'
		>>> conciliate('assertion: do(hop) animal(bunny)', 'assertion: animal(bunny) do(X)')
		instantiated (X) with (hop)
		'assertion: animal(bunny) do(hop)'
		>>> conciliate('assertion: do(hop) animal(bunny)', 'assertion: point(bunny)')
		''
		R,   R²   i   t   instantiatedRV   t   substitutedt   forRI   i    s$   instantiated variable predicate withN(   R
   RK   R   R(   R§   R¥   R   Rh   R]   R   Rc   Ri   (   R;   R…   R‹   R6   Rˆ   t   successt   partt   oldargRn   R   Rp   R‘   (    (    s    /home/andreas/ai/baproj/model.pyRi   ×  sB     4)4 $
D'c         C   sß   | } | d j o |  S|  } t  Sx¶ t | i ƒ  |  i ƒ  ƒ D]™ \ } } | | j o q> q> t t | ƒ ƒ ob t |  ƒ t |  ƒ i t | ƒ ƒ d } | d | d t | ƒ ƒ | i t | ƒ | ƒ } } q> d Sq> W| S(   sù   succeed if everything in "partialmeaning" is compatible with "meaning",
		substituting along the way. Similar to conciliate() but operates on the
		whole meaning instead of looking at individual clauses, and does not
		perform substitution, merely instantiation.

		>>> unifies('assertion: do(hop) animal(bunny)', 'assertion: do(X) animal(bunny)')
		substituted (hop) for (X)
		'assertion: do(hop) animal(bunny)'
		>>> unifies('assertion: do(hop) animal(bunny)', 'assertion: animal(bunny) do(X)')
		''

	R,   i   R¾   R¿   (	   R   t   zipRK   R   R¥   Rh   R]   R§   Rc   (   R;   R…   R6   Rˆ   t   part1t   part2t   newarg(    (    s    /home/andreas/ai/baproj/model.pyR›     s     ) c          C   s  y d d k  l }  Wn t j
 o n	 X|  ƒ  Sh! d d 6d d 6d d 6d d	 6d d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d  d! 6d" d# 6d$ d% 6d& d' 6d& d( 6d) d* 6d+ d, 6d- d. 6d/ d0 6d1 d2 6d3 d4 6d5 d6 6d7 d8 6d9 d: 6d; d< 6d= d> 6d? d@ 6S(A   sl    Obtain corpus, either by importing from module `exemplars,'
		or by falling back to a small sample corpus. iÿÿÿÿ(   R   R,   t   uht   eht   acknowledgementt   aht   confirmationt   okt   yest   yeahR•   t   noR-   s   that's right !s    assertion: point(ball) toy(ball)t   balls   imperative: throw[0](X) toy(X)s   throw it to mes(   imperative: kick(football) toy(football)s   you kick the football !s   whquestion: point(X) Y(X)s   what is this ?s"   whquestion: point(cow) animal(cow)s   is that a cow ?s   whquestion: do(X) animal(bunny)s   what does a bunny do ?s   whquestion: do(X) animal(cow)s   what does a cow say ?s!   whquestion: do(X) animal(duck[3])s   what does a duckie say ?s   whquestion: animal(X) do(woof)s   what animal does woof woof ?s    whquestion: do(X) animal(cat[2])s   what's a kitty say ?s$   assertion: point(cat) animal(cat[2])s   that's a kittys'   assertion: point(donkey) animal(donkey)t   donkeys   that's a donkeys    assertion: do(meouw) animal(cat)t   meouws!   assertion: do(quack) animal(duck)t   quacks   assertion: do(moo) animal(cow)t   moos    assertion: animal(bunny) do(hop)t   bunnys   assertion: animal(dog) do(woof)t   dogs    assertion: do(hop) animal(bunny)t   hops   assertion: do(woof) animal(dog)s	   woof woofs#   ynquestion: want(juice) food(juice)s   want some juice ?s   assertion: food(chocolate)t	   chocolates   whquestion: inside(X) toy(box)s   what's inside ?s    assertion: inside(book) toy(box)t   books   assertion: point(book)s   book !(   R%   R   t   ImportError(   R   (    (    s    /home/andreas/ai/baproj/model.pyR   <  sL    
c          C   sw   d d d d d d d d d	 d
 d g }  t  ƒ  } t | ƒ } x4 |  D], } | GHt | d | | t ƒ } d G| GHqC Wd S(   s#    Interpret some sample utterances. s   what does a bunny do ?s
   kitty do ?s   throw the balls   catch the balls   where lives birdie ?s   that's a balls   can you throw it ?s   what does a duckie say ?s!   what does this animal [=cow] do ?s'   do you want to play with that [=ball] ?s   what does a ball say ?R,   s   interpretation:N(   R   R'   R.   R   (   t   testR%   R&   R   RP   (    (    s    /home/andreas/ai/baproj/model.pyRÛ   o  s$    		 c            sƒ  ‡  ‡ f d †  } d „  } g  } t  t |  ƒ d ƒ D] ‰ | d g t | ƒ d q6 ~ } x$ t | ƒ D] \ } } | | d <qj Wx, t  t | d ƒ ƒ D] } | | d | <q› Wd | d d <x³ t |  ƒ D]¥ \ } ‰  x– t | ƒ D]ˆ \ } ‰ | | | d | | ƒ }	 | | | | | ƒ | ƒ  }
 | | d | | | ƒ } t |
 |	 | f ƒ | | d | d <qç WqÎ W| d d S(   s*   Edit distance of two sequences. Non-standard features are that all
		mutations have a cost of 1 (so as not to favor insertions or deletions
		over substitutions), as well as a decay based on index
		(mutations with higher indices weigh less). 
		
		>>> edit_dist('foo', 'bar')
		2.4890507991136213c              s   ˆ  ˆ j o d Sd Sd  S(   Ni    i   (    (    (   R   RP   (    s    /home/andreas/ai/baproj/model.pyt	   substcost‘  s    c         S   s   t  i d |  S(   Ngš™™™™™É¿(   t   mathRA   (   RN   (    (    s    /home/andreas/ai/baproj/model.pyt   decay—  s    i   i    iÿÿÿÿ(   R>   R/   t	   enumerateRZ   (   t   sourcet   targetRÜ   RÞ   Rn   t   distanceRq   t   rowt   jt   insct   substct   delc(    (   R   RP   s    /home/andreas/ai/baproj/model.pyRg   ‰  s&    	B    -c         c   s[   xT t  t |  ƒ d d ƒ D]: } x1 t  | t |  ƒ ƒ D] } |  | | | d !Vq5 Wq Wd S(   s‹    Return all substrings of a sequence, in descending order of length.
		
		>>> list(substr_iter('abc'))
		['abc', 'ab', 'bc', 'a', 'b', 'c']iÿÿÿÿi   N(   R>   R/   (   R[   R   RP   (    (    s    /home/andreas/ai/baproj/model.pyRƒ   ¯  s
      c            s    t  ‡  f d †  ˆ  i ƒ  Dƒ ƒ S(   s¿    Reverse dictionary, ie. swap values and keys; since a value may occur
		with multiple keys, return a list of all keys associated with a value.

		>>> revdict({0: 1, 1: 2})
		{1: [0], 2: [1]}c         3   sW   xP |  ]I } | g  } ˆ  i  ƒ  D]$ \ } } | | j o | | q  q  ~ f Vq Wd  S(   N(   Rd   (   R   R   Rn   RP   RE   (   R   (    s    /home/andreas/ai/baproj/model.pys	   <genexpr>À  s   	 (   R·   R™   (   R   (    (   R   s    /home/andreas/ai/baproj/model.pyR~   ¸  s    c         C   s@   g  } |  d i  ƒ  D]$ } t t | ƒ ƒ o | | q q ~ S(   s1    >>> varclauses(['foo(bar) zed(X)'])
		['zed(X)']i    (   RK   R   R¥   (   R   Rn   R   (    (    s    /home/andreas/ai/baproj/model.pyRj   Â  s    c         C   s'   d |  j o |  |  i  d ƒ  S|  Sd S(   s    >>> pred('foo(bar)')
		'foo'R²   N(   R]   (   t   token(    (    s    /home/andreas/ai/baproj/model.pyR§   Ç  s    c         C   s'   d |  j o |  |  i  d ƒ Sd Sd S(   s   	>>> arg('foo(bar)')
		'(bar)'R²   R,   N(   R]   (   Rè   (    (    s    /home/andreas/ai/baproj/model.pyR¥   Î  s    c         C   s&   |  o |  d t  j p |  d t  j S(   se    Test whether an argument string is variable:
		
		>>> var('(bar)')
		False
		>>> var('(X)')
		True
	i    i   (   R   (   R¥   (    (    s    /home/andreas/ai/baproj/model.pyR   Õ  s    c         C   s   |  i  d d ƒ i ƒ  S(   s©    Turn meaning into a list of operator, predicates and arguments.
		
		>>> tokens('whquestion: do(X) animal(bunny)')
		['whquestion:', 'do', '(X)', 'animal', '(bunny)']
	R²   s    ((   Rc   RK   (   R   (    (    s    /home/andreas/ai/baproj/model.pyRh   ß  s    c         C   s   t  |  ƒ i t  | ƒ ƒ S(   s,    >>> intersect([1,2,3], [2,3])
		set([2, 3])(   R=   t   intersection(   R   RP   (    (    s    /home/andreas/ai/baproj/model.pyR`   ç  s    t   __main__R¸   t   optionflagss   %d doctests succeeded!(5   t   __doc__t	   free_willR    RÚ   t   randomt   stringR   R   t   collectionsR   R   RÝ   R   R   R   R$   R   RH   RS   R0   R5   R.   R1   Rf   R2   R    R£   R3   R+   R­   R(   R'   R
   Ri   R›   R   RÛ   Rg   Rƒ   R~   Rj   R§   R¥   R   Rh   R`   t   __name__t   doctestt   testmodt   NORMALIZE_WHITESPACEt   ELLIPSISt   failt	   attempted(    (    (    s    /home/andreas/ai/baproj/model.pyt   <module>   s^   					* 	J	88		V9,9F	3		&			
				
		