Notice: Undefined variable: cats in /home/jlaudun/public_html/wordpress/wp-content/plugins/coins-metadata/coins-metadata.php on line 32
Following up on some previous explorations, I was curious about the relationship between the various sentiment libraries available in Python. The code below will let you compare a text for yourself, but the current list of three — Afinn, TextBlob, and Indico — is not exhaustive, but rather the three I used to draft out this bit of code, which is better than a lot of code I’ve written thus far but still probably quite crude to some.
#! /usr/bin/env python # Imports import matplotlib.pyplot as plt import seaborn # for more appealing plots from nltk import tokenize # Customizations seaborn.set_style("darkgrid") plt.rcParams['figure.figsize'] = 12, 8 import math import re import sys #reload(sys) #sys.setdefaultencoding('utf-8') # AFINN def afinn_sentiment(filename): from afinn import Afinn afinn = Afinn() with open (my_file, "r") as myfile: text = myfile.read().replace('\n', ' ') sentences = tokenize.sent_tokenize(text) sentiments =  for sentence in sentences: sentsent = afinn.score(sentence) sentiments.append(sentsent) return sentiments # TextBlob def textblob_sentiment(filename): from textblob import TextBlob with open (filename, "r") as myfile: text=myfile.read().replace('\n', ' ') blob = TextBlob(text) textsentiments =  for sentence in blob.sentences: sentsent = sentence.sentiment.polarity textsentiments.append(sentsent) return textsentiments # Indico def indico_sentiment(filename): import indicoio indicoio.config.api_key = 'yourkeyhere' with open (my_file, "r") as myfile: text = myfile.read().replace('\n', ' ') sentences = tokenize.sent_tokenize(text) indico_sent = indicoio.sentiment(sentences) return indico_sent def plot_sentiments(filename): fig = plt.figure() plt.title("Comparison of Sentiment Libraries") plt.plot(afinn_sentiment(filename), label = "Afinn") plt.plot(textblob_sentiment(filename), label = "TextBlob") plt.plot(indico_sentiment(filename), label = "Indico") plt.ylabel("Emotional Valence") plt.xlabel("Sentence #") plt.legend(loc='lower right') plt.annotate("Oral Legend LAU-14 Used", xy=(30, 2))
Once you’ve loaded this script, all you need to do is give it a file with which to work: