Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
paulNrad authored Aug 15, 2018
1 parent 567ae01 commit b00ddee
Showing 1 changed file with 205 additions and 0 deletions.
205 changes: 205 additions & 0 deletions Day-3/NLP-9 Sentiment Analysis.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import nltk\n",
"import csv\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"negative = []\n",
"with open('words_negative.csv', newline='') as file:\n",
" reader = csv.reader(file)\n",
" for row in reader:\n",
" negative.append(row)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"positive = []\n",
"with open(\"words_positive.csv\", newline='') as file:\n",
" reader = csv.reader(file)\n",
" for row in reader:\n",
" positive.append(row)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def sentiment(text):\n",
" temp = []\n",
" text_sent = nltk.sent_tokenize(text)\n",
" for sentence in text_sent:\n",
" n_count = 0\n",
" p_count = 0\n",
" sent_words = nltk.word_tokenize(sentence)\n",
" for word in sent_words:\n",
" print (word)\n",
" for item in positive:\n",
" if(word == item[0]):\n",
" print(\"---> \"+item[0])\n",
" p_count +=1\n",
" for item in negative:\n",
" if(word == item[0]):\n",
" print(\"---> \"+item[0])\n",
" n_count +=1\n",
"\n",
" if(p_count > 0 and n_count == 0): #any number of only positives (+) [case 1]\n",
" #print \"+ : \" + sentence\n",
" temp.append(1)\n",
" elif(n_count%2 > 0): #odd number of negatives (-) [case2]\n",
" #print \"- : \" + sentence\n",
" temp.append(-1)\n",
" elif(n_count%2 ==0 and n_count > 0): #even number of negatives (+) [case3]\n",
" #print \"+ : \" + sentence\n",
" temp.append(1)\n",
" else:\n",
" #print \"? : \" + sentence\n",
" temp.append(0)\n",
" return temp"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentiment(\"It was terribly bad.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentiment(\"Actualluty, it was not bad at all.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sentiment(\"This is a sentance about nothing.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mylist = sentiment(\"I saw this movie the other night. I can say I was not disappointed! The actiing and story line was amazing and kept me on the edge of my seat the entire time. While I did not care for the music, it did not take away from the overall experience. I would highly recommend this movie to anyone who enjoys thirllers.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mylist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"comments = []\n",
"with open(\"reviews.csv\", newline='') as file:\n",
" reader = csv.reader(file)\n",
" for row in reader:\n",
" comments.append(row)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"comments[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rate = sentiment(str(comments[0]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.average(rate)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for review in comments:\n",
" print(\"\\n\")\n",
" print(np.average(sentiment(str(review))))\n",
" print(review)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit b00ddee

Please sign in to comment.