How to analyse social sentiments for Bitcoin

blog
sentiment-analysis
data-analysis
bitcoin

#1

In this experiment we will use publicly available Twitter data and Node.js to create a simple sentiment analyses application. This application will be able to search and analyse tweets related to Bitcoin.

You can use this analysis to build all sorts of applications. Let’s begin…

Install Twit and Sentiment packages

npm install twit --save
npm install sentiment --save

Let the code begin

const Twit = require('twit');
const Sentiment = require('sentiment');
const analyst = new Sentiment();

// Get your twitter app details
// Use environment variables like these if deploying on the server or committing code to git
// You can export the variable like TWITTER_CONSUMER_KEY on unix like systems by 'export TWITTER_CONSUMER_KEY='twitterconsumerkey''
const twitterConfig = {
    consumer_key: process.env.TWITTER_CONSUMER_KEY,
    consumer_secret: process.env.TWITTER_CPNSUMER_SECRET,
    access_token: process.env.TWITTER_ACCESS_TOKEN,
    access_token_secret: process.env.TWITTER_AACCESS_TOKEN_SECRET,
    timeout_ms: 60*1000
};

// Initialise twitter api using the defined config object
let twitterAPI = new Twit(twitterConfig);

At this stage we are ready to search Twitter. Let’s define a function to search twitter and extract the text part of the tweet (tweet can have links).

// Use Twitter endpoint /search/tweets to query for the given string
async function getTweets(q, count) {
    try {
        let tweets = await twitterAPI.get('search/tweets', {q, count, 'tweet_mode': 'extended'});
        return tweets.data.statuses.map(getText);
    } catch(err) {
        return null;
    }
}

// Extract relatively clean text
function getText(tweet) {
    if (tweet.lang == 'en')  { // change this as you like
        let txt = tweet.retweeted_status ? tweet.retweeted_status.full_text : tweet.full_text;
        return txt.split(/ |\n/).filter(v => !v.startsWith('http')).join(' ');
    } else {
        return '';
    }
}

Create a function to get the data and analyse it to predict sentiment and print the results

async function analyse(keywords) {
    let count = 100;
    let tweets = await getTweets(keywords, count);    
    let combined = tweets.join(' ');
    if (combined) {
        // Analyse
        let score = analyst.analyze(combined);
        // Score is an object which contains the detailed analyses. For this experiments we are only interested in comparative score.
        let points = score.comparative;
        // That's it. Here we know the sentiment of the last 100 tweets with the hashtag #Bitcoin 
        // Let's print that on console.
        emojiPrinter(points);
    }
}

function emojiPrinter(points) {
    // You might need to adjust these checks
    if (points == 0) { // neutral
        console.log(':-|');
    } else if (points > 0) { // positive
        console.log(':-)');        
    } else { // negetive
        console.log(':-(');                
    }    
}

We are all set to execute and see the magic!

// Twitter has rate limits on search API. 
// We will be sending 1 request every 7 seconds. 
// This will keep Twitter happy and It's good enough for the live analyses.
setInterval(function() { 
    analyse("#Bitcoin");
}, 7000);

Hope you enjoyed reading. Give it a try and let me know if you have any questions.


#2

We have create a live implementation of what you shared! it uses the social data collected from different sources. Mainly by the Aaron’s Cat search engine.