Sentiment Quickstart

We will learn how to install and use the sentiment API library in Python.


Investor Sentiment Analysis Audience This tutorial is designed to let you quickly start exploring and developing applications with the BrainrexNatural Language API. This tutorial steps through a Natural Language API application using Python code. The purpose here is not to explain the Python client libraries, but to explain how to make calls to the Natural Language API. Applications in Java and Node.js are essentially similar. Consult the Natural Language API Samples for samples in other languages (including this sample within the tutorial).

Prerequisites

In order to run this tutorial you need to do the following:

  • You've installed the Brainrex Language Client for Python
  • You have signed up for the Brainrex Free tier.
  • You have created API keys
  • Store API keys as enviroment variables.
  • Basic familiarity with Python programming.
  • Set up your Python development environment. It is recommended that you have the latest version of Python 3.11s.0, pip, and -virtualenv installed on your system.
  • Now we're going to try out another header style.

Let's start by downloading the Brainrex library

!pip install brainrex
### Making your first call quickstart 
from __future__ import print_function
import time
import brainrex
from brainrex.rest import ApiException
from pprint import pprint
 
# Go to console.brainrex.com/signup > APIs > Subcribe to an API > Dashboard > API Key > Copy
# Configure API key authorization: APIKeyHeader
configuration = brainrex.Configuration()
configuration.api_key['x-api-key'] = 'INSERT_YOUR_API_KEY_HERE' # Paste key here
 
# create an instance of the API class
api_instance = brainrex.LanguageApi(brainrex.ApiClient(configuration))
text = brainrex.Text("bitcoin is the best") # Text | String of text to be analyze for investor sentiment.
 
try:
    # Sentiment analysis score using a model trained for buy signals.
    api_response = api_instance.language_get_price_sentiment(text)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling LanguageApi->language_get_price_sentiment: %s\n" % e)

Your first call to the General Sentiment API

try:
    # Sentiment analysis score using a model trained for buy signals.
    api_response = api_instance.language_get_general_sentiment(text)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling LanguageApi->language_get_price_sentiment: %s\n" % e)

Lets download a sample dataset we provide

import pandas as pd 
 
tweets_dataframe = pd.read_csv('https://s3-eu-west-1.amazonaws.com/brainrex.com/data/sample_text_data.csv')
tweetList= tweets_dataframe.Tweet.to_list()
tweetList[:5]
 
for index, tweet in enumerate(tweetList[344:366]): # Change the number of posts you want to analyze by changing this values. [start:end]
    try:
        text = brainrex.Text(tweet)
        investor_score = api_instance.language_get_price_sentiment(text)
        sentiment_score = api_instance.language_get_general_sentiment(text)
        # TODO add response
        print('Sentence {} investor score of {}, general sentiment score {}'.format(index, investor_score, sentiment_score))
 
    except ApiException as e:
        print("Exception when calling LanguageApi->language_get_price_sentiment: %s\n" % e)

Let's try the crypto sentiment algorithm

%matplotlib inline
import matplotlib.pyplot as plt
res = []
for tweet in tweetList[:30]: # Change the number of posts you want to analyze by changing this values. [start:end]
    try:
        text = brainrex.Text(tweet)
        api_response = api_instance.language_get_price_sentiment(text)
        res.append(api_response)
    except ApiException as e:
        print("Exception when calling LanguageApi->language_get_price_sentiment: %s\n" % e)
# Plot the results from the sentiment scores.
series = pd.DataFrame(res)
series=series.astype(float)
plt.style.use('ggplot')
series.plot(figsize=(20,4))