If you have data regardless of whether they are obtained from social media or other sources, the next step is to analyze the meaning of those data. However, it is difficult to interpret the natural language in terms of sentiment analysis. Fortunately, there are several ways to understand text data and even provide quantification.
TextBlob is one of the simplest ways to process textual data and you can find more detail information here. For instance, the library provides sentiment analysis of whether textual data are positive (positive value of polarity) or not (Tutorial of Textblob). Further information about sentiment analysis is here.
1. Preparation
In order to analyze data, install the library as follows:
pip install -U textblob
python -m textblob.download_corpora
2. TextBlob
Given that data is saved as a text file, you need to convert text into an object. Also, import TextBlob.
import os
import json
from textblob import TextBlob
Read data from the file and create the new file which will include the result of text processing.
f1 = open("name of data file","r")
f2 = open(“name of output file”, “w”)
Convert every line of f1 file to an object to interpret textual data. Also, only English sentences are set to be analyzed.
for line in f1
obj = json.loads(line)
if tweetobj['lang']!="en":
continue
tweet = obj['text'].strip()
Prior to applying TextBlob to objects, you might need to change or sort them out. Also, you have to define “Keyword” that you want to analyze before saving output. As you can see the following codes, the output of sentiment analysis consists of “Keyword”, polarity, and subjectivity of each tweet.
tweetBlob = TextBlob(tweet)
out = Keyword+","+str(tweetBlob.sentiment.polarity)+","+str(tweetBlob.sentiment.subjectivity)
fp2.write(out+"\n")
fp1.close()
fp2.close()
Now, you can see a new csv file in the directory that you designated like the one below. This will be used as the fundamental data for analysis in R.
