Home For Fiction – Blog

for thinking people

Patreon LogoPatreon

December 28, 2020

BardBot, a Shakespearean Chatbot

Programming

bardbot, javascript, programming, Shakespeare, sonnet

Remember my Shakespearean sonnet mixer? I was thinking, wouldn’t it be great if we could actually talk to the Bard and have him respond in his unique way to our questions? Well, of course it would have been, but alas, he’s dead. The next best thing – aren’t I modest? – is this BardBot; a JavaScript Shakespearean chatbot that takes the user input and returns a relevant line from the Bard’s sonnets.

The program takes the last word provided and returns either a direct reference (if the word exists in the sonnets database) or a rhyme (if it doesn’t).

In fact, I attempted to use more than just the last word (and I may attempt again in the future), but I wasn’t happy with the result. But even in this basic implementation, the BardBot results are pretty funny!

bardbot
Would the Bard approve? Probably not, but who cares!

BardBot: the Program

It’s best to start with the BardBot program itself, as you’re likely more interested in trying it than me talking about it. So, let’s take a look at it, and then I’ll offer you some background.

Click to run the program

Note that the program links to raw.githack. This means that, as a free service, 100% uptime cannot be guaranteed. If it seems unresponsive, please try later.

Like most of my programs, it should be considered as something funny, casual, and alpha-stage. I’m sure you could come up with something better – and feel free to grab the code at the bottom of the page and try!

So, the way it works is simple. As I mentioned, it detects the last word provided by the user and searches either for a direct match or for a rhyme. Therefore, a question such as “Do you like snow?” would return either a line from Shakespeare’s sonnets containing the word “snow”, or, if none is found, it would attempt to return a rhyme – e.g. a line ending with “bow” or “flow”.

But why only the Last Word?

If you’re wondering why I made it so that BardBot only works with the last word, the answer is: simplicity. As I mentioned in the introduction, I did attempt working with the entire sentence provided by the user. However, I didn’t like the results.

The biggest problem was the inability of having a clear separation between parts of speech. My original goal was to detect nouns and verbs. In a user input such as “Can you describe the scent of roses?”, my plan was to have the code work with “describe”, “scent”, and “roses”, with particular focus on the latter two.

And yet, I noticed a problem: “can” would be taken into consideration, and so would “you”. As a result, the code would return irrelevant results, because it would return things relevant to such generic words. I did attempt some workarounds, but in the end I opted for simplicity.

After all, most questions or requests in this context place the noun at the end: “Do you like snow?” “Do you enjoy poetry?” “Tell me about love”, etc.

home for fiction

BardBot: the Code

The code for this little JavaScript project can be found on my GitHub page. Like many other of my programming examples, this one, too, uses the excellent RiTa library.

And if you’re interested in other similar programs, why don’t you take a look at my iambic pentameter generator, or my rhyming anapest generator.