Sentiment analysis of text

7.3.1

Sentiment analysis of text

Last updated 2020-01-29 Read time 2 min

We will use the model in『SiEBERT - English-Language Sentiment Classification』 to classify the sentiment of English sentences. I would like to classify the sentiment of each English sentence by positive and negative.

Hartmann, Jochen and Heitmann, Mark and Siebert, Christian and Schamp, Christina, “More than a feeling: Accuracy and Application of Sentiment Analysis”, International Journal of Research in Marketing(2022)

Here we use the siebert/sentiment-roberta-large-english model on huggingface, if you want to use transformers on Google Colab you need to install transformers beforehand.

1
2
3
4
5
6
7
import numpy as np
from transformers import pipeline
from IPython.display import HTML

sentiment_pipeline = pipeline(
    "sentiment-analysis", model="siebert/sentiment-roberta-large-english"
)

Analyze the sentiment of each sentence in the text #

The entire text is separated by “.” to separate each sentence.

Here, Petrobras Webcast – 3rd Quarter Results 2022 November 5, 2022 transcription data is used.

1
2
3
transcript = """Hello!Hello!Hello!Hello!Hello!"""
ts_list = [ts for ts in transcript.split(".") if len(ts) > 20]
scores = sentiment_pipeline(ts_list)

Visualize the results #

Visualize the results using positive and negative labels and their scores.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
for t, s in zip(ts_list, scores):
    score = np.round(float(s["score"]), 4)  # sentiment score
    font_weight = "bold" if score > 0.995 else "normal"  # thickness of the text

    # color display for each sentiment
    if s["label"] == "NEGATIVE":
        r = 255 - 10 * int(1000 - score * 1000)
        display(
            HTML(
                f"[score={score}] <span style='color:rgb({r},100,100);font-weight:{font_weight};'>{t}</span>"
            )
        )
    elif s["label"] == "POSITIVE":
        g = 255 - 10 * int(1000 - score * 1000)
        display(
            HTML(
                f"[score={score}] <span style='color:rgb(100,{g},100);font-weight:{font_weight};'>{t}</span>"
            )
        )

[score=0.9976] Hello!Hello!Hello!Hello!Hello!