How do Markov chat bots work?

I was thinking of creating a chatbot using something like brand chains, but I'm not quite sure how to make it work. From what I understand, you create a table from the data with the given word, and then the words that follow. Is it possible to attach a bot to any probability or counter? Is that even a good idea?

The second part of the problem is related to keywords. Assuming I can already identify keywords from user input, how do I create a sentence that uses this keyword? I don’t always want to start a sentence with a keyword, so how can I sow a chain of stamps?

+58
probability artificial-intelligence chatbot markov-chains
Mar 15 2018-11-11T00:
source share
3 answers

I made a Markov chat chain for IRC in Python a few years ago and can shed some light on how I did it. The generated text does not necessarily make any sense, but it can be very interesting to read. Let's decompose it in stages. Assuming you have fixed input, a text file (you can use text input or chat text, or just use your imagination)

Scroll through the text and create a dictionary, i.e. a container with a key. And put all the couple words in the form of keys and the word following the meaning. For example: if you have the text "abcabk", you start with "ab" as the key and "c" as the value, then "bc" and "a" as the value ... the value should be a list or any collection hold 0 ..many 'items', as you can have more than one meaning for a given pair of words. In the above example, you will have “ab” two times, followed by “c” and then “k”. So in the end you will have a dictionary / hash that looks like this: {'a b': ['c','k'], 'b c': ['a'], 'c a': ['b']}

Now you have the necessary structure to create your funky text. You can start with a random key or fixed location! Therefore, given the structure that we have, we can start by storing "ab", and then randomly taking the next word from c or k, so the first save in the cycle "abk" (if "k" is selected) then you continue to move one step to the right, which in our case is “bk” and saves a random value for this pair if you don’t have in our case, so you exit the loop (or you can solve other things, such as starting over). When the loop is complete, you print the saved text string.

The larger the input, the more values ​​you will have for you (a couple of words), and then you will have a “smarter bot” so that you can “train” your bot by adding more text (maybe entering a chat?). If you have a book as input, you can create some good random suggestions. Please note that you do not need to take only one word that follows the pair as a value, you can take 2 or 10. The difference is that your text will be more accurate if you use longer building blocks. Start with a pair as a key and the next word as a value.

So, you see that basically you can have two steps: first create a structure in which you randomly choose a key to start with, then take this key and print a random value for this key and continue until you have a value or some or other condition. If you want, you can “seed” a couple of words from the chat input from your key value structure to get started. Its up to your imagination how to start your chain.

An example with real words:

 "hi my name is Al and i live in a box that i like very much and i can live in there as long as i want" "hi my" -> ["name"] "my name" -> ["is"] "name is" -> ["Al"] "is Al" -> ["and"] ........ "and i" -> ["live", "can"] ........ "i can" -> ["live"] ...... 

Now let's build a loop:

Choose a random key, say “hello my” and randomly select a value, only one here, so its “name”, (SAVING “hello my name”) .
Now move one step to the right, taking "my name" as the next key and select a random value ... "this", (SAVING "hello my name") .
Now go and take the "name" ... "Al" (SAVING "hello my name is AL") .
Now take "is Al" ... "and" (SAVING "Hello, my name is Al and") .

...

When you go to "and i", you arbitrarily choose a value, let's say "can", then the word "i can" is created, etc. .... when you come to your stop state or you have no values, type the constructed line in our case:

"Hi, my name is Al, and I can live there as long as I want."

If you have more values, you can go to any keys. The more values, the more combinations you have and the more random and interesting the text will be.

+125
Mar 15 '11 at 3:30
source share

The bot selects a random word from your input and generates a response, choosing another random word, which is considered the successor to its word. He then repeats this process, intending to find a successor to the word in turn and continue iteratively until he thinks he has said enough. He reaches this conclusion, dwelling on the word that was before the punctuation mark in the text of the training. Then it returns to input mode again so you can answer, etc.

This is not very realistic, but I urge everyone to do better in 71 lines of code! This is a big challenge for any budding pythonist, and I just want me to be able to open the task to a wider audience than the small number of visitors that I get on this blog. To encode a bot that is always guaranteed to be grammatical, it will probably be closer to a few hundred lines, I was very simplified by just trying to come up with the simplest rule to give the computer a simple punch to say something.

His answers are rather impressionistic, to say the least! You must also indicate what you are saying in single quotes.

I used War and Peace for my “corps”, which took a couple of hours for a training run, use a shorter file if you are impatient ...

here is the coach

 #lukebot-trainer.py import pickle b=open('war&peace.txt') text=[] for line in b: for word in line.split(): text.append (word) b.close() textset=list(set(text)) follow={} for l in range(len(textset)): working=[] check=textset[l] for w in range(len(text)-1): if check==text[w] and text[w][-1] not in '(),.?!': working.append(str(text[w+1])) follow[check]=working a=open('lexicon-luke','wb') pickle.dump(follow,a,2) a.close() 

Here is the bot:

 #lukebot.py import pickle,random a=open('lexicon-luke','rb') successorlist=pickle.load(a) a.close() def nextword(a): if a in successorlist: return random.choice(successorlist[a]) else: return 'the' speech='' while speech!='quit': speech=raw_input('>') s=random.choice(speech.split()) response='' while True: neword=nextword(s) response+=' '+neword s=neword if neword[-1] in ',?!.': break print response 

You tend to experience a strange feeling when it says something that seems partially meaningful.

+5
May 28 '14 at 1:12
source share

You can do the following: Order 1 markov chain generator using words, not letters. Every time someone posts something, what he has added is added to the bot database. Also, the bot will save when he goes to the chat, and when the guy sends the first message (10 times), he will save the time that the same guy was waiting to send again (10 times) ... This second part will be used to see when the guy leaves, so he joins the chat and after a while, based on the table, “after how many 10 seconds the guy sent after joining the chat,” he will continue to publish with the same table thinking “as there was a time that used to record a post that was published but after the posts, which he used X seconds to think and write "

0
May 02 '14 at 13:31
source share



All Articles