parent
05dcd88fa2
commit
29d04d94f8
@ -1,23 +0,0 @@
|
||||
from textblob import TextBlob
|
||||
|
||||
# You should download the book text, clean it, and import it here
|
||||
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(" ", " ")))
|
@ -0,0 +1,87 @@
|
||||
{
|
||||
"metadata": {
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": 3
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2,
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from textblob import TextBlob\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# You should download the book text, clean it, and import it here\n",
|
||||
"with open(\"pride.txt\", encoding=\"utf8\") as f:\n",
|
||||
" file_contents = f.read()\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"book_pride = TextBlob(file_contents)\n",
|
||||
"positive_sentiment_sentences = []\n",
|
||||
"negative_sentiment_sentences = []"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for sentence in book_pride.sentences:\n",
|
||||
" if sentence.sentiment.polarity == 1:\n",
|
||||
" positive_sentiment_sentences.append(sentence)\n",
|
||||
" if sentence.sentiment.polarity == -1:\n",
|
||||
" negative_sentiment_sentences.append(sentence)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"The \" + str(len(positive_sentiment_sentences)) + \" most positive sentences:\")\n",
|
||||
"for sentence in positive_sentiment_sentences:\n",
|
||||
" print(\"+ \" + str(sentence.replace(\"\\n\", \"\").replace(\" \", \" \")))\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(\"The \" + str(len(negative_sentiment_sentences)) + \" most negative sentences:\")\n",
|
||||
"for sentence in negative_sentiment_sentences:\n",
|
||||
" print(\"- \" + str(sentence.replace(\"\\n\", \"\").replace(\" \", \" \")))"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in new issue