Create lesson1_task3.py

pull/34/head
Stephen Howell (MSFT) 3 years ago committed by GitHub
parent b2606290e4
commit 48e4790d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,23 @@
from textblob import TextBlob
# The book file is supplied, but you can get it (and many other books) yourself from Project Gutenberg
with open('pride.txt', encoding="utf8") as f:
file_contents = f.read()
book_pride = TextBlob(file_contents)
positive_sentiment_sentences =[]
negative_sentiment_sentences =[]
for sentence in book_pride.sentences:
if sentence.sentiment.polarity == 1:
positive_sentiment_sentences.append(sentence)
if sentence.sentiment.polarity == -1:
negative_sentiment_sentences.append(sentence)
print("The " + str(len(positive_sentiment_sentences)) + " most positive sentences:")
for sentence in positive_sentiment_sentences:
print("+ " + str(sentence.replace("\n", "").replace(" ", " ")))
print("The " + str(len(negative_sentiment_sentences)) + " most negative sentences:")
for sentence in negative_sentiment_sentences:
print("- " + str(sentence.replace("\n", "").replace(" ", " ")))
Loading…
Cancel
Save