Not Slytherin… a Sorting Hat for the Rest of Us

For the 2016 edition of #Hackillinois, I teamed up with Neel Mouleeswaran, and Chamila Amithirigala. And we created Sorting Hat — a sassy talking hat that sorts you into a Hogwarts house based on the conversation you have with it, just like in Harry Potter.

We won Facebook’s Favorite Hack on the competition and KCG’s Best use of an automated test. :)

To create the hat, we used the following

Hardware:

  • Raspberry Pi
  • Arduino Uno
  • Flora RGB Smart NeoPixel (2)
  • Anker USB Power Supply
  • Edimax Wi-Fi USB Dongle
  • 900m Servo Motor

Software:

  • Speech recognition with Google Speech Recognition API
  • Text-to-speech server with Apple Dictation Services
  • Audio streaming from Mac → Pi over Wi-Fi
  • Remote control of Pi with UDP
  • Chat bot built with NLTK
  • LED + Servo control with Arduino

Creating the hat’s personality

The chatbot was built from the NLTK library in Python. The bot responds reasonably well, very quickly because it doesn’t do particularly fancy computing to figure out a response. Given an input sentence, the bot will use regular expression matching (basically, just search for specific keywords and phrases), and respond accordingly. Since the Sorting Hat is like a psychologist, I based several of the Sorting Hat’s responses on Eliza the psychologist, the very first chatbot that people believed was real. Like a stereotypical psychologist, Eliza flipped her response right back at the subject. For example, if someone said “I like x,” Eliza might respond, “Why do you like x?” The Sorting Hat chatbot flips responses in a manner very similar to Eliza, but in Harry Potter themed ways. There are also additional Harry Potter specific keywords that will trigger related responses. It offers several pieces of wisdom about choices, love, happiness, and death. And definitely don’t try to swear at this Sorting Hat, it might send you to the dungeons or even refuse to sort you!

“Eyes are the windows to the soul”

And as that common saying goes, we realized the need to animate the eyes of our creation in addition to its conversational personality to make it seem alive.

void breath(){ 
strip.setPixelColor(0, 200, 200, 200);
strip.setPixelColor(1, 200, 200, 200);
strip.show();
for (int i=10; i<200; i++) {
strip.setBrightness(i);
strip.show();
delay(10);
}
for (int i=200; i>10; i — ) {
strip.setBrightness(i);
strip.show();
delay(10);
}
}

As for how everything else was wired…. it’s just magic ;)

Check out the blog that was written about our hack by Robert McGrath

And the video of it at on youtube:

“Oh, you may not think I’m pretty,
But don’t judge on what you see, 
I’ll eat myself if you can find
A smarter hat then me.
You can keep your bowlers black,
Your top hats sleek and tall,
For I’m the Hogwarts Sorting Hat 
And I can cap them all.
There’s nothing hidden in your head
The Sorting Hat can’t see,
So try me on and I will tell you
Where you ought to be…”