Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
333 changes: 331 additions & 2 deletions Project/SentimentAnalyzer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,342 @@
"metadata": {},
"outputs": [],
"source": [
"# You may start here"
"pip install pandas "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd #import pandas"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"data.csv\" , low_memory=False) #read the dataset in a dataframe to utilize the data in python."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install TextBlob"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from textblob import TextBlob #import TextBlob to procces the text."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"#this function will procces the reviews to get the sentiment from it.\n",
"def polarity(review):\n",
" score = TextBlob(review)\n",
" return score.sentiment.polarity"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"df[\"sentiment_score\"] = df[\"reviews.text\"].astype(str).apply(sentiment)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"def classifyer(score): #classify each review based on the sentiment score as good/bad/Neutral.\n",
" if score > 0.2:\n",
" return 'Good'\n",
" elif score < -0.2:\n",
" return 'Bad'\n",
" elif score >= -0.2 and score <= 0.2:\n",
" return 'Neutral'\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"df[\"sentimentAnalysis\"] = df[\"sentiment_score\"].apply(classifyer) #call and apply the classifyer function on a new column named sentimentAnalysis."
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>sentiment_score</th>\n",
" <th>sentimentAnalysis</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>34635</th>\n",
" <td>0.059896</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34636</th>\n",
" <td>0.275238</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34637</th>\n",
" <td>0.228125</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34638</th>\n",
" <td>0.292133</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34639</th>\n",
" <td>0.035156</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34640</th>\n",
" <td>0.136364</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34641</th>\n",
" <td>0.135185</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34642</th>\n",
" <td>0.225000</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34643</th>\n",
" <td>0.643750</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34644</th>\n",
" <td>0.250000</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34645</th>\n",
" <td>0.503788</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34646</th>\n",
" <td>0.125000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34647</th>\n",
" <td>0.533333</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34648</th>\n",
" <td>0.550000</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34649</th>\n",
" <td>0.050000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34650</th>\n",
" <td>0.125000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34651</th>\n",
" <td>0.000000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34652</th>\n",
" <td>0.056250</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34653</th>\n",
" <td>0.178571</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34654</th>\n",
" <td>-0.068750</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34655</th>\n",
" <td>0.183333</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34656</th>\n",
" <td>0.000000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34657</th>\n",
" <td>0.183333</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34658</th>\n",
" <td>-0.100000</td>\n",
" <td>Neutral</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34659</th>\n",
" <td>0.412245</td>\n",
" <td>Good</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" sentiment_score sentimentAnalysis\n",
"34635 0.059896 Neutral\n",
"34636 0.275238 Good\n",
"34637 0.228125 Good\n",
"34638 0.292133 Good\n",
"34639 0.035156 Neutral\n",
"34640 0.136364 Neutral\n",
"34641 0.135185 Neutral\n",
"34642 0.225000 Good\n",
"34643 0.643750 Good\n",
"34644 0.250000 Good\n",
"34645 0.503788 Good\n",
"34646 0.125000 Neutral\n",
"34647 0.533333 Good\n",
"34648 0.550000 Good\n",
"34649 0.050000 Neutral\n",
"34650 0.125000 Neutral\n",
"34651 0.000000 Neutral\n",
"34652 0.056250 Neutral\n",
"34653 0.178571 Neutral\n",
"34654 -0.068750 Neutral\n",
"34655 0.183333 Neutral\n",
"34656 0.000000 Neutral\n",
"34657 0.183333 Neutral\n",
"34658 -0.100000 Neutral\n",
"34659 0.412245 Good"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[[\"sentiment_score\",\"sentimentAnalysis\"]].tail(25)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Answer: the result is based on the classifyer function which will display good if sentiment score over 0.2, bad if score under 0.2 and neutral in between.'"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#A description of the project.?\n",
"\"\"\"Answer:Getting sentiment analysis of products reviews from a dataset and making sense of the data based on polarity score of each one to classfy them as good,bad or neutral. \"\"\"\n",
"#A description of the dataset used.?\n",
"\"\"\"Answer:Dataset of products and thier reviews.\"\"\"\n",
"#A description of the methods used to analyze the data.?\n",
"\"\"\"Answer:A polarity function that process the reviews to get the sentiment from it.\n",
" then use the Classifyer function to classify each review based on the sentiment score as good/bad/Neutral.\"\"\"\n",
"#A description of the results.?\n",
"\"\"\"Answer: the result is based on the classifyer function which will display good if sentiment score over 0.2, bad if score under 0.2 and neutral in between.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
Expand Down