Working With TextBlob In NLP


Some Properties of the Blob

  • words

  • sentences

  • parts-of-speech tags

  • noun_phrases

  • sentiment

  • polarity

  • subjectivity


NOTE: Will need to install NLTK Data to analyze properties.


import nltk
nltk.download('averaged_perceptron_tagger')

Download common corpora

python -m textblob.download_corpora
# Create a TextBlob object, call it what you want

blob = TextBlob(text)
blob.words
blob.sentences

Parts_of_Speech (a.k.a. tags)

  • TextBlob uses a PatternTagger to determine parts-of-speech

  • Uses pattern library POS tagging

  • Pattern's 63 parts-of-speech tags

  • Samples patterns:

  • NN—a singular noun or **mass noun

  • NNS - a plural noun

  • NNP—a proper singular noun

  • VB - a verb, base form

  • VBZ—a third person singular present verb

  • DT—a determiner (the, an, that, this, my, their, etc.)

  • JJ—an adjective

  • IN— a subordinating conjunction or preposition

  • PRP - a personal pronoun

  • CC - a coordinating conjunction


Textbook links out of date:


blob.tags
blob.noun_phrases

Basic Sentiment with TextBlob

  • The sentiment property returns a namedtuple of the form Sentiment(polarity, subjectivity)

  • polarity ranges from -1.0 (negative) to + 1.0 (positive)

  • subjectivity ranges from 0.0 (objective) to 1.0 (subjective)

https://textblob.readthedocs.io/en/dev/quickstart.html#sentiment-analysis


# Overall sentiment
blob.sentiment
# Individial sentiment components
print(f'Polarity (-1 to 1 with 1 pos): {blob.polarity:.3f}\nSubjectivity (0 to 1 with 1 sub): {blob.subjectivity:.3f}')


Sentiment by Sentence

for sentence in blob.sentences:
 print(f'1 {sentence.sentiment}')
 print(f'2 {sentence.polarity:.2f}')
 print(f'3 {sentence.subjectivity:.2f}')
 print(f'4 {sentence}')
 print()
# Create sentiment dataframe
#pd.set_option('max_colwidth', 400)

= []
= []
txt = []
for sentence in blob.sentences:
    p.append(sentence.sentiment.polarity)
    s.append(sentence.sentiment.subjectivity)
    txt.append(str(sentence))

df_sent = pd.DataFrame(p,columns=['polarity'])
df_sent['subjectivity'] = s
df_sent['text'] = txt

df_sent.sort_values('polarity', ascending=True, inplace = True) 


df_sent.head(10)
# Some stats

df_sent.describe()
# correlation
df_sent.corr()
# matplotlib scatter plot

print(blob.sentiment)
sns.scatterplot(x='polarity', y='subjectivity', data=df)

# control x and y limits
plt.ylim(0, 1)
plt.xlim(-1, 1)
# filter by scores
for sentence in blob.sentences:
 if sentence.polarity < -.8:
 if sentence.subjectivity >.8:
 print(f'{sentence} (p:{sentence.polarity:.2f}, s:{sentence.subjectivity:.2f})')
 print()
# Plot in Plotly

import plotly.graph_objects as go
import plotly.express as px
fig = px.scatter(df_sent,
                 x = 'polarity' ,
                 y = 'subjectivity',
                 hover_data = ['text']
                )
fig.show()

WordCloud

  • Be sure you've installed WordCloud

  • imageoio should be installed with Anaconda (docs

from wordcloud import WordCloud
import imageio
import numpy as np

mask_image = imageio.imread('mask_star.png')
cloud = WordCloud(width=600,height=600,background_color='white', 
                  mask=mask_image, max_words=50,
                 contour_width=3, contour_color='red')
cloud = cloud.generate(text)
plt.imshow(cloud)

Create word cloud with WordCloud

loud = WordCloud(background_color='white', max_words=50)
cloud = cloud.generate(text)
#cloud = cloud.to_file(outfile)
# show image
plt.imshow(cloud)
# magic to set precision
%precision 3


Contact us to get any NLP project help at: contact@codersarts.com

Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram