CoreML – Twitter sentiment analysis

Today a post about the new Apple framework – CoreML – a machine learning framework to use your trained data-model inside iOS apps.

https://developer.apple.com/machine-learning/core-ml/

Documentation: https://developer.apple.com/documentation/coreml


The example of today is about machine learning to identify the mood of a collection of tweets (#apple, @apple, #covid-19, #love, etc…).


Create your input data

First of all, you need to create and train your data model. To do this, prepare an excel (CSV file) with two columns:

  • class
  • text

Where class is the mood, like “Positive“, “Neutral“, “Negative” and the text is the tweet with the related mood.

More rows you add more precisely became your model. This is the manual phase for training.

Example of CSV file:


class;text
Positive;Now all @Apple has to do is get swype on the iphone and it will be crack. Iphone that is
Negative;What was @apple thinking making #Siri totally dependent on a network connection? Siri + @ATT = utter frustration.
Neutral;after being on hold with @apple for the last 30 mins i really like their music selection of on-hold music. White Stripes Ray Lamontagne
[…]

This is an extract of the CSV file.

Add the row and the phrases you prefer to create a new strong model.


Create your data-model

After creating your CSV file, you can now use the XCode Playground with the new framework CreateML used to generate data-models.

Your data model is now created! Import in your new project and you should see something like this:

Well done.


Create a demo app

To read the twitter timeline, I suggest the Swifter library. Clone it and add in your new project:

https://github.com/mattdonnelly/Swifter

The next step is to create a new Twitter App because you need the consumerKey and consumerSecret in order to access data.

A simple step, create a new API key from here: https://developer.twitter.com/en/apps

Now, instantiate your Swifter library with the new keys:

    let swifter = Swifter(
consumerKey: "<##KEY##>",
consumerSecret: "<##SECRET##>"
)

And make a test search to check the data:

swifter.searchTweet(
using: "@apple",
lang: "en",
count: 100,
tweetMode: .extended,
success: { results, metadata in
print( results )
}) { _ in }

Do you see a valid response? Cool!


Use your generated .mlmodel

Import your .mlmodel into project and instantiate.

Remember that the class name is the name of your generated model!

Example:

let sentimentClassifier = TweetSentimentClassifier()

Test the classifier:

let predition = try! sentimentClassifier.prediction(
text: "@Apple is a terrible company!"
)
print( predition.label )

Classify your tweets

As a result of the searchTweet() function, you receive an array of JSON data.

Is useful to store it in an array to analyze later:

var tweets = [TweetSentimentClassifierInput]()
for i in 0..<100 {
if let tweet = results[i]["full_text"].string {
let tweetForClassification = TweetSentimentClassifierInput(text: tweet)
tweets.append( tweetForClassification )
}
}

and later, use the array to analyzing all tweets

let predictions = try self.sentimentClassifier.predictions(inputs: tweets)
for prediction in predictions {
let sentiment = prediction.label
print( sentiment )
}

You can now play with your predictions and make the app you prefer!


Too lazy?

Full example code on github: https://github.com/elpsk/CoreML_Sentiment_Analyzer


enjoy predicting!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.