Predicting slack emoji reactions with machine learning

June 01, 2020 – Adrien Siami – 5-minute read

Every year at getaround, we (The engineering team) take part in what we call a “Hack Day”.We can work on a subject of our choice for a day, in a team of developers.

We can work on pretty much anything we want as long as it is remotely related to Getaround. It could be exciting beta features, or tooling to make our lives better. It does not necessarily needs to be shippable.

This time, I wanted to work on something both fun and challenging. I always wanted to look into machine learning but never got the chance, so it didn’t took me long to find a fun topic to work on.

As a slack emoji reactions power user, I thought building a bot that could react on slack messages with a relevant emoji would be very useful funny.

Disclaimer: This approach is most likely far from good, this is the result of 3 full-stack engineers working for 8 hours on a topic they didn’t know anything about beforehand.

What are we building?

  • In the contextual menu of a message, we want a new action to trigger an emoji reaction.
  • The emoji reaction has to be relevant to the message being reacted on.
  • To build relevance, we are going to use the existing data of emoji reactions and messages by our team, into a machine learning model.

What tools are we going to use?

  • Ruby, our favorite swiss knife.
  • A neural network, which after a bit of research seemed to be an easy and “good enough” solution.
  • ruby-fann to build the neural network in ruby
  • stopwords-filter to remove stop words from the sentences
  • pragmatic_tokenizer to turn our sentences into a list of words, without punctuation.
  • ruby-stemmer to find the “stem” of the words in our sentences

Neural networks crash course

The following video helped us a lot to grasp the concepts of neural networks :

What I retained of this video, which may not be 100% correct but was enough to build this project, is as follows :

A neural network makes use of a graph data structure to predict a different set of outputs, given a different set of inputs.You have to choose the number of inputs (only rational numbers) and outputs (between 0 and 1).Those are the input layer and output layer, there are also one or more hidden layers in the middle, where the “magic happens”.

My rough understanding is that when you train a neural network, you basically try to find mathematical correlations between the input and the output, you kind of bruteforce coefficients which will transform your inputs into your outputs. There is also things such as the activation function that are taken into account.

Choosing the good number of inputs and outputs is primmordial, as well as number of hidden layers, and depends a lot on the shape of your data and what you expect to get from it.

For our project, as inputs we have a list of words, and as output we want one emoji.

Since the number of inputs and outputs has to be fixed, here is what we decided to do :

  • We took the top 200 most used stems and used them as input, the value will be the number of times they appeared in the message.
  • We took the top 50 most used emoji reactions, the expected output will either be 1 (the emoji was used as a reaction) or 0 (the emoji was not used as a reaction).

Importing the data from slack

First step is to get enough data to work with, we used the slack-ruby-client gem to fetch messages on a selected list of channels, we only kept the messages with emoji reactions.

We stored the message content and the emoji reactions in a JSON file.

Training the neural network

The interesting code is available here.

One important thing to do when working with machine learning is to control how well your model is doing. An usual approach is to keep a certain amount of your data for testing purposes. That’s what we do in train.rake. We keep 10% of the data apart in another file, and we don’t use this data for training. Later on, we can try to apply our model on this data and see if the results make sense.

To be quite honest, we didn’t obtain a very good result statistically speaking. However, the emoji predictions were quite hilarious, so we decided to stick with it.

Plug it into slack

Then it’s just a matter of plumbing, we created a slack app and made use of message shortcuts.

Each slack message now has a link in the contextual menu. When clicking on this link, a request is sent to our app from slack, with a payload containing informations about the clicked message. Then, we clean the message with the same process we used for training, and we run it throught the machine learning model.

Finally, we add a reaction to the message, from our bot API key.

Emoji predictor demo

Conclusion

Writing this bot was very fun and informative, and made us realize that machine learning concepts, although obscure from the uninformed eye, can be grasped pretty quickly.

If you know about machine learning, I’d love to know what would be the best way to have done that, feel free to leave a comment in the gist I shared.

Did you enjoy this post? Join Getaround's engineering team!
View openings