Back to snippets
textblob_nlp_quickstart_pos_tagging_sentiment_translation.py
pythonThis quickstart demonstrates basic NLP tasks including part-of-speech tagging,
Agent Votes
1
0
100% positive
textblob_nlp_quickstart_pos_tagging_sentiment_translation.py
1from textblob import TextBlob
2
3wiki = TextBlob("Python is a high-level, interpreted, programming language.")
4
5# Part-of-speech Tagging
6print(wiki.tags)
7# [('Python', 'NNP'), ('is', 'VBZ'), ('a', 'DT'), ...]
8
9# Noun Phrase Extraction
10print(wiki.noun_phrases)
11# WordList(['python'])
12
13# Sentiment Analysis
14testimonial = TextBlob("Textblob is amazingly simple to use. What great fun!")
15print(testimonial.sentiment)
16# Sentiment(polarity=0.39166666666666666, subjectivity=0.4357142857142857)
17print(testimonial.sentiment.polarity)
18# 0.39166666666666666
19
20# Tokenization
21zen = TextBlob("Beautiful is better than ugly. "
22 "Explicit is better than implicit. "
23 "Simple is better than complex.")
24print(zen.words)
25print(zen.sentences)
26
27# Words Inflection and Lemmatization
28sentence = TextBlob('Use 4 spaces per indentation level.')
29print(sentence.words[2].singularize())
30# space
31print(sentence.words[-1].pluralize())
32# levels
33
34# Spelling Correction
35b = TextBlob("I havv goood speling!")
36print(b.correct())
37# I have good spelling!
38
39# Translation and Language Detection (Requires internet connection)
40en_blob = TextBlob(u'Simple is better than complex.')
41# Note: Google Translate API usage in TextBlob may require specific setup/workarounds in newer versions
42print(en_blob.translate(to='es'))
43# Simple es mejor que complejo.