Text Generator Bot

For this Bot project, I wanna make a bot that could interact with people. So I finished the example code of how to react when someone followed you, mentioned you. Then I feel I wanna do something more dynamic. Since I took the machine learning class this semester, I wonder it would be so cool if people mention the bot with some text, and the bot reply them with generated text. It’s kinda like the book of answer which would always have a reasonable answer for any question.

I looked up ml5 library, there are so many possibilities like you can generate an image with an input of just several words. For this time, I used text generation with LSTM. Starting with the seed text, predict what text might come next based on the pre-trained Ernest Hemingway model.

Then problem occurs. Because I received interactions informations and run those reactions like favorite, reply, reblog in my server side, but ml5 is a p5 library which means I have to run it in the front side, so I need to communicate and transmit data between front side and server side. There are several ways to do that and I choose to do that via http get and post.

But there is also an issue that my server side won’t receive result until I refresh the web page. Asked Dan, and we added an interval function to run the generate function every second. In fact, in my case, WebSocket would be better to do those real time communication.

In the process of making this bot, I learned a lot of server side coding and how to talk between server and front side, and also I gain a basic picture of how front end and back end works.

Besides, I had some difficulties in how to use regex to filter text. Cuz the content I get from mention information have a lot html tags and also when people mention me, they will type @chengchaozz and in return I would need to filter those out to generate a reply. I kinda know I should use regular expression, but I still tried a tons of time to successfully filter them out. Regex is powerful, I should pay more attention to it.

Finally, I made it, I’m glad someone find my bot and typed something to my bot.

There is also a problem that when I run the server too long, it is possible to have 2 replies, one is from last mention and one is from the new mention. I guess this is because I trigger the generate function too often.

Code on Github


Leave a Reply

Your email address will not be published. Required fields are marked *