Are you curious how the world feels right now? I am, so we will implement a pretty simple sentiment analysis using emojis in tweets. It will tell if there are more 😃 and less 😢 in tweets now than usually so the world looks happy or we see more 🎄 in December.

We use Scala, sbt, Akka Streams, Guice and Redis. I use IntelliJ IDEA, but you are free to use your favorite IDE.

I plan to take this journey in two or three posts. I keep the application simple for the sake of readability but feel free to extend the app to know how your country, people speaking your language feel right now or use the words in tweets to implement a more sophisticated sentiment analysis.

The tested source code is available on GitHub. I might make typos here, so check whether it works. Feel free to clone or fork it.

One more thing before we start. This is overengineered. It can be done in a few lines. Don’t waste Akka Streams to increment Redis counters in real life. Use it to orchestrate processes with time consuming steps. My intent is to give you an architecture that you can use to solve real problems with Akka Streams. I just found counting emojis simple and exciting enough to keep your attention during this process.

Start IntelliJ IDEA, click Create New Project , then choose Scala and SBT . On the next screen change the name to twitter-trends , then click on Finish .

Open build.sbt, it should look like this:

name := "emoji-trends" version := "0.1" scalaVersion := "2.12.4"

We use

akka streams,

Twitter4J to get the tweets,

emoji-java to extract emojis,

logback for logging,

scala-logging to make it convenient,

akka-slf4j for getting log from Akka,

scala-redis to connect Redis

and scala-guice for dependency injection.

Add the following snippet to build.sbt, save it and let IntelliJ refresh the project.

libraryDependencies ++= Seq(

"org.twitter4j" % "twitter4j-stream" % "4.0.6",

"com.vdurmont" % "emoji-java" % "4.0.0",

"ch.qos.logback" % "logback-classic" % "1.2.3",

"com.typesafe.scala-logging" %% "scala-logging" % "3.7.2",

"com.typesafe.akka" %% "akka-stream" % "2.5.6",

"com.typesafe.akka" %% "akka-slf4j" % "2.5.6",

"net.debasishg" %% "redisclient" % "3.4",

"net.codingwell" %% "scala-guice" % "4.1.0"

)

Our source directory is src/main/scala . The main package is com.acme.emojitrends .

Create a class named EmojiCounter in the stream package with the following content.

package com.acme.emojitrends.stream import akka.actor.ActorSystem

import akka.stream.ActorMaterializer

class EmojiCounter {

private implicit val system = ActorSystem("EmojiTrends")

private implicit val materializer = ActorMaterializer()

private implicit val executionContext = system.dispatcher

}

We need the system and the materializer since Akka streams are materialized to Akka Actors. The execution context is needed for asynchronous execution in Scala.