# How can I improve this word-prediction AI?

I'm relatively new to AI, and I've tried to create one that "speaks". Here's how it works:

1. Get training data e.g 'Jim ran to the shop to buy candy'
2. The data gets split into overlapping 'chains' of three e.g ['Jim ran to', 'ran to the', 'to the shop', 'the shop to'...]
3. User enters two words
4. Looks through the chains to find if the two words have been seen before.
5. If they have, finds out which word followed it and how many times.
6. Work out the probability e.g: if 'or' followed the two words 3 times, 'because' followed the two words 1 time and 'but' followed it 1 time it would be 0.6, 0.2 and 0.2
7. Generate a random decimal
8. If the random decimal is in the range of the first word (0 - 0.6) pick that one or if it's in the range of the second word (0.6 - 0.8) pick that word or if it's in the range of the third (0.8 - 1) pick that word
9. Output the word picked
10. Repeat from 4 but with the new last two words e.g if the last words had been 'to be' and it picked 'or' the new last two words would be 'be or'.

It does work, but it doesn't stick to a particular topic. For example, after training with 800 random Wikipedia articles:

In the early 1990s the frequency had a plastic pickguard and separate hardtail bridge with the council hoped that the bullet one replaced with the goal of educating the next orders could revert to the north island or string of islands in a new urban zone close to the west.

As you can see the topic changes many times mid-sentence. I thought of increasing the number of words it considered from two to three or four, but I thought it might start simply quoting the articles. If I'm wrong please tell me.

Any help is greatly appreciated. If I haven't explained clearly enough or you have any questions please ask.

• Welcome to AI! (That is very elegant output, imo, nonsensicality aside;) – DukeZhou Jan 26 '18 at 20:41
• How come you pick one at "random"? It seems like that be less accurate than just picking the one with the highest decimal – Emil Wormbs Jan 26 '18 at 21:10