3. Natural language processing with Python 5-1. Concept of sentiment analysis [AFINN-111]

⑴ Acquisition of AFINN-111

import pandas as pd

afn = pd.read_csv(r'https://raw.githubusercontent.com/fnielsen/afinn/master/afinn/data/AFINN-111.txt', 
                  names=['word_score'])

image.png

⑵ Convert AFINN-111 to dictionary type

afn["split"] = afn["word_score"].str.split("\t")
afn["word"] = afn["split"].str.get(0)
afn["score"] = afn["split"].str.get(1)

image.png

import numpy as np
afn_int = afn.assign(score_int = afn['score'].astype(np.int64))

image.png

afn_int = afn_int[["word", "score_int"]]

sentiment_dict = afn_int.set_index('word')['score_int'].to_dict()

image.png

(3) Calculation of emotional value by AFINN-111

`Gauche was in charge of playing the cello at the activity photo studio in the town. He had a reputation for not being very good, though. He was always bullied by the conductor, not because he wasn't good at it, but because he was actually the worst of his fellow musicians. ``

string = "Gauche was in charge of playing the cello at the town's activity photo studio. He had a reputation for not being a very good player. Not only was he not a good player, but he was actually the worst player among his fellow musicians, so he was always bullied by the head musician."
import nltk
from nltk.tokenize import *
nltk.download('punkt')

for s in sent_tokenize(string):
    words = word_tokenize(s.lower())
    score = sum(sentiment_dict.get(word, 0) for word in words)
    print(score)

image.png

for s in sent_tokenize(string):
    words = word_tokenize(s.lower())
    positive = 0
    negative = 0
    for word in words:
        score = sentiment_dict.get(word, 0)
        if score > 0:
            positive += score
        if score < 0:
            negative += score
    print(s)
    print("positive:", positive)
    print("negative:", negative)

image.png

string_2 = "Gauche was in charge of playing the cello at the town's activity photo studio. But he had a reputation for being terrible at it. Not only was he not a good player, but he was actually the worst player among his fellow musicians, so he was always bullied by the head musician."

for s in sent_tokenize(string_2):
    words = word_tokenize(s.lower())
    positive = 0
    negative = 0
    for word in words:
        score = sentiment_dict.get(word, 0)
        if score > 0:
            positive += score
        if score < 0:
            negative += score
    print(s)
    print("positive:", positive)
    print("negative:", negative)

image.png

⑷ Problems and precautions

Recommended Posts

3. Natural language processing with Python 5-1. Concept of sentiment analysis [AFINN-111]
3. Natural language processing with Python 4-1. Analysis for words with KWIC
3. Natural language processing with Python 5-2. Emotion intensity analysis tool VADER
3. Natural language processing with Python 2-1. Co-occurrence network
3. Natural language processing with Python 1-1. Word N-gram
Japanese Natural Language Processing Using Python3 (4) Sentiment Analysis by Logistic Regression
Python: Natural language processing
3. Natural language processing with Python 5-5. Emotion value analysis of Japanese sentences [Japanese evaluation polarity dictionary (words)]
3. Natural language processing with Python 5-3. Emotion value analysis of Japanese sentences [Word emotion polarity value correspondence table]
3. Natural language processing with Python 2-2. Co-occurrence network [mecab-ipadic-NEologd]
[Python] I played with natural language processing ~ transformers ~
3. Natural language processing with Python 5-4. Emotion value analysis of Japanese sentences [Japanese evaluation polarity dictionary (noun edition)]
100 Language Processing with Python Knock 2015
Sentiment analysis with Python (word2vec)
Natural language processing 1 Morphological analysis
[Chapter 5] Introduction to Python with 100 knocks of language processing
Building an environment for natural language processing with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
3. Natural language processing with Python 3-1. Important word extraction tool TF-IDF analysis [original definition]
3. Natural language processing with Python 3-3. A year of corona looking back at TF-IDF
Study natural language processing with Kikagaku
100 Language Processing Knock with Python (Chapter 1)
[Natural language processing] Preprocessing with Japanese
100 Language Processing Knock with Python (Chapter 3)
10 functions of "language with battery" python
■ [Google Colaboratory] Preprocessing of Natural Language Processing & Morphological Analysis (janome)
100 Language Processing Knock-59: Analysis of S-expressions
Sentiment analysis with natural language processing! I tried to predict the evaluation from the review text
3. Natural language processing with Python 3-4. A year of corona looking back on TF-IDF [Data creation]
Dockerfile with the necessary libraries for natural language processing in python
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Basics of binarized image processing with Python
Natural language processing of Yu-Gi-Oh! Card name-Yu-Gi-Oh!
100 Language Processing Knock with Python (Chapter 2, Part 2)
100 Language Processing Knock with Python (Chapter 2, Part 1)
Sentiment analysis of tweets with deep learning
Drawing with Matrix-Reinventor of Python Image Processing-
I tried natural language processing with transformers.
Rehabilitation of Python and NLP skills starting with "100 Language Processing Knock 2015" (Chapter 1)
3. Natural language processing with Python 1-2. How to create a corpus: Aozora Bunko
Getting started with Python with 100 knocks on language processing
Python: Deep Learning in Natural Language Processing: Basics
Static analysis of Python code with GitLab CI
Let's enjoy natural language processing with COTOHA API
Unbearable shortness of Attention in natural language processing
Python: Deep learning in natural language processing: Implementation of answer sentence selection system
Data analysis with python 2
Voice analysis with python
RNN_LSTM2 Natural language processing
Image processing with Python
Voice analysis with python
Data analysis with Python
Various processing of Python
100 natural language processing knocks Chapter 4 Morphological analysis (first half)
[Language processing 100 knocks 2020] Summary of answer examples by Python
Explanation of the concept of regression analysis using python Part 2
100 natural language processing knocks Chapter 4 Morphological analysis (second half)
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Chapter 6] Introduction to scikit-learn with 100 knocks of language processing