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(" ", " ")))