Note: This notebook contains ALL the code for Sections 12.2.12.2.7
from textblob import TextBlob
text = 'Today is a beautiful day. Tomorrow looks like bad weather.'
blob = TextBlob(text)
blob
TextBlob
, Sentence
s and Word
s Support String Methods and Comparisons¶Sentence
s, Word
s and TextBlob
s inherit from BaseBlob
, which defines many common methods and propertiesBaseBlob
documentationblob.sentences
WordList
is a subclass of Python’s built-in list type with additional NLP methods. Word
objectsblob.words
blob
blob.tags
TextBlob
uses a PatternTagger
to determine parts-of-speechNN
—a singular noun or mass nounVBZ
—a third person singular present verbDT
—a determiner (the, an, that, this, my, their, etc.)JJ
—an adjectiveNNP
—a proper singular nounIN
—a subordinating conjunction or prepositionblob
blob.noun_phrases
Word
can represent a noun phrase with multiple words. blob
blob.sentiment
polarity
is the sentiment — from -1.0
(negative) to 1.0
(positive) with 0.0
being neutral. subjectivity
is a value from 0.0 (objective) to 1.0 (subjective). %precision
magic specifies the default precision for standalone float
objects and float
objects in built-in types like lists, dictionaries and tuples:%precision 3
blob.sentiment.polarity
blob.sentiment.subjectivity
0.85
) and one is negative (-0.6999999999999998
), which might explain why the entire TextBlob
’s sentiment
was close to 0.0
(neutral)for sentence in blob.sentences:
print(sentence.sentiment)
from textblob.sentiments import NaiveBayesAnalyzer
blob = TextBlob(text, analyzer=NaiveBayesAnalyzer())
blob
blob.sentiment
for sentence in blob.sentences:
print(sentence.sentiment)
blob
blob.detect_language()
spanish = blob.translate(to='es')
spanish
spanish.detect_language()
chinese = blob.translate(to='zh')
chinese
chinese.detect_language()
from_lang
keyword argument to the translate
methodchinese = blob.translate(from_lang='en', to='zh')
from_lang
and to
use iso-639-1 language codesspanish.translate()
chinese.translate()
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.