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.