You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/6-NLP/3-Translation-Sentiment/solution/notebook.ipynb

175 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from textblob import TextBlob"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Length of file: 748132 characters\n"
]
}
],
"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",
"print(f\"Length of file: {len(file_contents)} characters\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"book_pride = TextBlob(file_contents)\n",
"positive_sentiment_sentences = []\n",
"negative_sentiment_sentences = []"
]
},
{
"cell_type": "code",
"execution_count": 8,
"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": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 26 most positive sentences:\n",
"+ “What an excellent father you have, girls,” said she, when the door wasshut.\n",
"+ He walked here, and he walked there, fancying himself so verygreat!\n",
"+ Mr. Darcy replied, with great intrepidity,--“Miss Elizabeth Bennet.”“Miss Elizabeth Bennet!” repeated Miss Bingley.\n",
"+ “Your examination of Mr. Darcy is over, I presume,” said Miss Bingley;“and pray what is the result?”“I am perfectly convinced by it that Mr. Darcy has no defect.\n",
"+ Family pride, and _filial_ pride, for he is very proud of what hisfather was, have done this.\n",
"+ _That_ would be the greatest misfortune of all!\n",
"+ How wonderfully these sort of things occur!\n",
"+ She owed her greatest relief to her friend Miss Lucas, whooften joined them, and good-naturedly engaged Mr. Collinss conversationto herself.\n",
"+ The improvement ofspending a night in London was added in time, and the plan became asperfect as plan could be.\n",
"+ She is avery great favourite with some ladies of my acquaintance, Mrs. Hurst andMiss Bingley.\n",
"+ It isdone, however, and it was done for the best.\n",
"+ I have the greatest dislike in theworld to that sort of thing.\n",
"+ Charlotte is anexcellent manager, I dare say.\n",
"+ “His father was an excellent man,” said Mrs. Gardiner.\n",
"+ “He is the best landlord, and the best master,” said she, “that everlived.\n",
"+ “He is perfectly well-behaved, polite, and unassuming,” said her uncle.\n",
"+ On reaching the house, they were shown through the hall into the saloon,whose northern aspect rendered it delightful for summer.\n",
"+ Our distress, my dear Lizzy, is very great.\n",
"+ And tell my dearLydia not to give any directions about her clothes till she has seen me,for she does not know which are the best warehouses.\n",
"+ “My dear, dear Lydia!” she cried: “this is delightful indeed!\n",
"+ Excellent parsonage-house!\n",
"+ But, however, he is very welcome to come toNetherfield, if he likes it.\n",
"+ Happy shall I be when his stay at Netherfield is over!”“I wish I could say anything to comfort you,” replied Elizabeth; “but itis wholly out of my power.\n",
"+ If I could but see you as happy!\n",
"+ He is perfectly amiable.\n",
"+ Your idea of theponies is delightful.\n"
]
}
],
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The 14 most negative sentences:\n",
"- Everybody is disgusted with his pride.\n",
"- what canhave induced him to behave so cruelly?”“A thorough, determined dislike of me--a dislike which I cannot butattribute in some measure to jealousy.\n",
"- To finda man agreeable whom one is determined to hate!\n",
"- “I should like to know how he behaves among strangers.”“You shall hear, then--but prepare for something very dreadful.\n",
"- The pause was to Elizabeths feelingsdreadful.\n",
"- “Wickham sovery bad!\n",
"- The separationbetween her and her family was rather noisy than pathetic.\n",
"- It would be dreadful!\n",
"- can she be ignorant of what youand Jane seem so well to understand?”“Oh, yes!--that, that is the worst of all.\n",
"- “She is so fond of Mrs. Forster,” said she, “it will be quite shockingto send her away!\n",
"- It was all over before I arrived; so my curiosity was not so dreadfully racked as _yours_ seems to have been.\n",
"- He called it, therefore, his duty to step forward, and endeavour to remedy an evil which had been brought on by himself.\n",
"- How you must have hated me after _that_ evening!”“Hate you!\n",
"- You were disgusted with the women who were always speaking,and looking, and thinking for _your_ approbation alone.\n"
]
}
],
"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(\" \", \" \")))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## This is the poem text assignment for sentiment analysis"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".Venv",
"language": "python",
"name": "python3"
},
"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.13.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}