TextBlob

Simple, Pythonic, text processing--Sentiment analysis, part-of-speech tagging, noun phrase extraction, translation, and more. https://textblob.readthedocs.io/

GitHub Link: https://github.com/sloria/TextBlob


In [1]:
!pip install -U TextBlob


Collecting TextBlob
  Downloading textblob-0.13.0-py2.py3-none-any.whl (631kB)
    100% |████████████████████████████████| 634kB 678kB/s ta 0:00:01
Requirement already up-to-date: nltk>=3.1 in /Users/sampathm/miniconda3/lib/python3.6/site-packages (from TextBlob)
Collecting six (from nltk>=3.1->TextBlob)
  Downloading six-1.11.0-py2.py3-none-any.whl
Installing collected packages: TextBlob, six
  Found existing installation: six 1.10.0
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.10.0:
      Successfully uninstalled six-1.10.0
Successfully installed TextBlob-0.13.0 six-1.11.0

In [2]:
!python -m textblob.download_corpora


[nltk_data] Downloading package brown to /Users/sampathm/nltk_data...
[nltk_data]   Package brown is already up-to-date!
[nltk_data] Downloading package punkt to /Users/sampathm/nltk_data...
[nltk_data]   Package punkt is already up-to-date!
[nltk_data] Downloading package wordnet to
[nltk_data]     /Users/sampathm/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /Users/sampathm/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-
[nltk_data]       date!
[nltk_data] Downloading package conll2000 to
[nltk_data]     /Users/sampathm/nltk_data...
[nltk_data]   Package conll2000 is already up-to-date!
[nltk_data] Downloading package movie_reviews to
[nltk_data]     /Users/sampathm/nltk_data...
[nltk_data]   Package movie_reviews is already up-to-date!
Finished.

In [3]:
from textblob import TextBlob

In [4]:
sample_text = 'Welcome to TextBlob! TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.'

In [5]:
blob = TextBlob(sample_text)

In [8]:
# lets check what all tokens it has
blob.words


Out[8]:
WordList(['Welcome', 'to', 'TextBlob', 'TextBlob', 'is', 'a', 'Python', '2', 'and', '3', 'library', 'for', 'processing', 'textual', 'data', 'It', 'provides', 'a', 'simple', 'API', 'for', 'diving', 'into', 'common', 'natural', 'language', 'processing', 'NLP', 'tasks', 'such', 'as', 'part-of-speech', 'tagging', 'noun', 'phrase', 'extraction', 'sentiment', 'analysis', 'classification', 'translation', 'and', 'more'])

In [7]:
blob.word_counts


Out[7]:
defaultdict(int,
            {'2': 1,
             '3': 1,
             'a': 2,
             'analysis': 1,
             'and': 2,
             'api': 1,
             'as': 1,
             'classification': 1,
             'common': 1,
             'data': 1,
             'diving': 1,
             'extraction': 1,
             'for': 2,
             'into': 1,
             'is': 1,
             'it': 1,
             'language': 1,
             'library': 1,
             'more': 1,
             'natural': 1,
             'nlp': 1,
             'noun': 1,
             'part-of-speech': 1,
             'phrase': 1,
             'processing': 2,
             'provides': 1,
             'python': 1,
             'sentiment': 1,
             'simple': 1,
             'such': 1,
             'tagging': 1,
             'tasks': 1,
             'textblob': 2,
             'textual': 1,
             'to': 1,
             'translation': 1,
             'welcome': 1})

In [9]:
blob.tags


Out[9]:
[('Welcome', 'VB'),
 ('to', 'TO'),
 ('TextBlob', 'VB'),
 ('TextBlob', 'NNP'),
 ('is', 'VBZ'),
 ('a', 'DT'),
 ('Python', 'NNP'),
 ('2', 'CD'),
 ('and', 'CC'),
 ('3', 'CD'),
 ('library', 'NN'),
 ('for', 'IN'),
 ('processing', 'VBG'),
 ('textual', 'JJ'),
 ('data', 'NNS'),
 ('It', 'PRP'),
 ('provides', 'VBZ'),
 ('a', 'DT'),
 ('simple', 'JJ'),
 ('API', 'NNP'),
 ('for', 'IN'),
 ('diving', 'VBG'),
 ('into', 'IN'),
 ('common', 'JJ'),
 ('natural', 'JJ'),
 ('language', 'NN'),
 ('processing', 'NN'),
 ('NLP', 'NNP'),
 ('tasks', 'NNS'),
 ('such', 'JJ'),
 ('as', 'IN'),
 ('part-of-speech', 'JJ'),
 ('tagging', 'NN'),
 ('noun', 'JJ'),
 ('phrase', 'NN'),
 ('extraction', 'NN'),
 ('sentiment', 'NN'),
 ('analysis', 'NN'),
 ('classification', 'NN'),
 ('translation', 'NN'),
 ('and', 'CC'),
 ('more', 'JJR')]

In [10]:
blob.sentiment


Out[10]:
Sentiment(polarity=0.21666666666666665, subjectivity=0.5261904761904762)

In [11]:
# Text-Correction - Spell check
blob.correct()


Out[11]:
TextBlob("Welcome to TextBlob! TextBlob is a Python (2 and 3) library for processing texture data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech lagging, noun phrase extraction, sentiment analysis, classification, translation, and more.")

In [12]:
print(sample_text)


Welcome to TextBlob! TextBlob is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

In [13]:
blob.parse()


Out[13]:
'Welcome/NNP/B-NP/O to/TO/B-PP/B-PNP TextBlob/NN/B-NP/I-PNP !/./O/O\nTextBlob/NN/B-NP/O is/VBZ/B-VP/O a/DT/B-NP/O Python/NNP/I-NP/O (/(/O/O 2/IN/B-PP/O and/CC/O/O 3/CD/O/O )/)/O/O library/NN/B-NP/O for/IN/B-PP/B-PNP processing/NN/B-NP/I-PNP textual/JJ/I-NP/I-PNP data/NNS/I-NP/I-PNP ././O/O\nIt/PRP/B-NP/O provides/VBZ/B-VP/O a/DT/B-NP/O simple/JJ/I-NP/O API/NNP/I-NP/O for/IN/B-PP/B-PNP diving/VBG/B-VP/I-PNP into/IN/B-PP/B-PNP common/JJ/B-NP/I-PNP natural/JJ/I-NP/I-PNP language/NN/I-NP/I-PNP processing/NN/I-NP/I-PNP (/(/O/O NLP/NN/B-NP/O )/)/O/O tasks/NNS/B-NP/O such/JJ/B-ADJP/O as/IN/B-PP/O part-of-speech/JJ/B-ADJP/O tagging/VBG/B-VP/O ,/,/O/O noun/NN/B-NP/O phrase/NN/I-NP/O extraction/NN/I-NP/O ,/,/O/O sentiment/NN/B-NP/O analysis/NN/I-NP/O ,/,/O/O classification/NN/B-NP/O ,/,/O/O translation/NN/B-NP/O ,/,/O/O and/CC/O/O more/JJR/B-ADJP/O ././O/O'

In [14]:
blob.translate(to="zh")


Out[14]:
TextBlob("欢迎来到TextBlob! TextBlob是一个用于处理文本数据的Python(2和3)库。它提供了一个简单的API,用于潜入常见的自然语言处理(NLP)任务,如词性标注,名词短语提取,情感分析,分类,翻译等。")