From 3fb575deb8e3ad87f13b578364730d55e98ad974 Mon Sep 17 00:00:00 2001 From: "Stephen Howell (MSFT)" <38020233+stephen-howell@users.noreply.github.com> Date: Wed, 19 May 2021 20:47:11 +0100 Subject: [PATCH] Create lesson1_task3.py --- NLP/1-Introduction/solutions/lesson1_task3.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 NLP/1-Introduction/solutions/lesson1_task3.py diff --git a/NLP/1-Introduction/solutions/lesson1_task3.py b/NLP/1-Introduction/solutions/lesson1_task3.py new file mode 100644 index 000000000..6001cac7e --- /dev/null +++ b/NLP/1-Introduction/solutions/lesson1_task3.py @@ -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(" ", " ")))