Attempting human-like speach: Markov chains

In orderto make the activitiy sentences in our memory more human-like, we can attempt to build a simple chatbot from the tex as well. A simple, and maybe naive, approach is to build a Markov chain.

First load some modules


In [14]:
import pensieve as pens
import textacy
from collections import defaultdict
from random import random

Define our markiv hcain functions. First to create the dics. First attempt only takes triplets of words a b c and adds {'a b':c} to the dictionary, takes a step forward. The inverse dicitnoary is also saved for some tests, using it to seed the chain.

The dictionaries are sampled with equal probability, could look into using frequency for relative weights.


In [25]:
def make_markov_chain(docs):
    my_dict = defaultdict(list)
    inverse_dict = defaultdict(list)
    for doc in docs:
        print("Reading ",doc)
        d = pens.Doc(doc)
        for p in d.paragraphs:
            for sent in p.doc.sents:
                #print(sent.text)
                bow = textacy.extract.words(sent)
                for i_word, word in enumerate(bow):
                    if i_word < 3:
                        continue
                    key = sent[i_word-2].text+' '+sent[i_word-1].text
                    value = sent[i_word].text
                    my_dict[key].append(value)
                    inverse_dict[value].append(key)
    return my_dict, inverse_dict

def sample_from_chain(mv_dict, key):
    options = len(mv_dict[key])
    x = 999
    while x > options-1:
        x = int(10*(random()/options)-1)
    #rint(x)
    #print(x,key, options)
    return(mv_dict[key][x])

def make_chain(mkv_chain, key):
    counter = 0
    chain = key
    while key in mkv_chain:
        #if counter > 5:
        #    return chain
        chain+=' '+sample_from_chain(mkv_chain,key)
        key = chain.split()[-2]+' '+chain.split()[-1]
        counter +=1
    return chain

In [21]:
all_books = ['../../clusterpot/book1.txt',
            '../../clusterpot/book2.txt',
            '../../clusterpot/book3.txt',
            '../../clusterpot/book4.txt',
            '../../clusterpot/book5.txt',
            '../../clusterpot/book6.txt',
            '../../clusterpot/book7.txt']

Load the books and buildthe dictionaries, and run some simple test for proof of principle.


In [26]:
mkv_chain, inv_chain = make_markov_chain(all_books)


Reading  ../../clusterpot/book1.txt
Reading  ../../clusterpot/book2.txt
Reading  ../../clusterpot/book3.txt
Reading  ../../clusterpot/book4.txt
Reading  ../../clusterpot/book5.txt
Reading  ../../clusterpot/book6.txt
Reading  ../../clusterpot/book7.txt

In [33]:
#print(mkv_chain)
for i in range(20):
    print('\n',make_chain(mkv_chain,'He said'))


 He said my blood would make Harry happy , after all he ’s done … all the letters , he could persuade ’em …

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said the giant ; he ’d be stiff if you ’d expect the real reason Ginny Weasley , eh ? ” repeated Professor McGonagall coldly , as Dudley

 He said my blood would make Harry look like a large , sticky chocolate cake with Happy Birthday

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said the giant ; he ’d be stiff if you ’d expect an unqualified wizard

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said my blood would make Harry happy , after all he ’s done … all the letters , he could persuade to

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said my blood would make Harry happy , after all he ’s done … all the letters , he could persuade to

 He said nothing , Molly , do n’t know about that , ” he grunted , swinging the steering wheel and his friends Crabbe and this is the safest place in the usual amount of neck , which lay silent and tidy under

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

 He said the giant ; he ’d be stiff if you ’d expect the dummy gave a great sniff and patted her on the dull light given off by the time Dudley

 He said my blood would make Harry look down at the little bundle ; Hagrid ’s shoulders shook , and one daughter

 He said my blood would make Harry happy , after all he ’s done … all right , thirty - seven then

 He said nothing , Molly , do n’t know about that , ” he grunted , as Dudley

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: