Created
October 26, 2020 15:49
-
-
Save LowriWilliams/914311ad07bc32bf0fb44e06621b1981 to your computer and use it in GitHub Desktop.
aspect_sa/sentiment_analysis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 38, | |
| "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>reviews</th>\n", | |
| " <th>remove_lower_punct</th>\n", | |
| " <th>sentiment</th>\n", | |
| " <th>sentiment score</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>They look good and stick good! I just don't li...</td>\n", | |
| " <td>they look good and stick good i just dont like...</td>\n", | |
| " <td>Negative</td>\n", | |
| " <td>-0.1078</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>These stickers work like the review says they ...</td>\n", | |
| " <td>these stickers work like the review says they ...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.9136</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>These are awesome and make my phone look so st...</td>\n", | |
| " <td>these are awesome and make my phone look so st...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.8481</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>Item arrived in great time and was in perfect ...</td>\n", | |
| " <td>item arrived in great time and was in perfect ...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.9584</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>awesome! stays on, and looks great. can be use...</td>\n", | |
| " <td>awesome stays on and looks great can be used o...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.8957</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>5</th>\n", | |
| " <td>These make using the home button easy. My daug...</td>\n", | |
| " <td>these make using the home button easy my daugh...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.8126</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>6</th>\n", | |
| " <td>Came just as described.. It doesn't come unstu...</td>\n", | |
| " <td>came just as described it doesnt come unstuck ...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.7964</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>7</th>\n", | |
| " <td>it worked for the first week then it only char...</td>\n", | |
| " <td>it worked for the first week then it only char...</td>\n", | |
| " <td>Negative</td>\n", | |
| " <td>-0.4215</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>8</th>\n", | |
| " <td>Good case, solid build. Protects phone all aro...</td>\n", | |
| " <td>good case solid build protects phone all aroun...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.8555</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>9</th>\n", | |
| " <td>This is a fantastic case. Very stylish and pro...</td>\n", | |
| " <td>this is a fantastic case very stylish and prot...</td>\n", | |
| " <td>Positive</td>\n", | |
| " <td>0.9572</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " reviews \\\n", | |
| "0 They look good and stick good! I just don't li... \n", | |
| "1 These stickers work like the review says they ... \n", | |
| "2 These are awesome and make my phone look so st... \n", | |
| "3 Item arrived in great time and was in perfect ... \n", | |
| "4 awesome! stays on, and looks great. can be use... \n", | |
| "5 These make using the home button easy. My daug... \n", | |
| "6 Came just as described.. It doesn't come unstu... \n", | |
| "7 it worked for the first week then it only char... \n", | |
| "8 Good case, solid build. Protects phone all aro... \n", | |
| "9 This is a fantastic case. Very stylish and pro... \n", | |
| "\n", | |
| " remove_lower_punct sentiment \\\n", | |
| "0 they look good and stick good i just dont like... Negative \n", | |
| "1 these stickers work like the review says they ... Positive \n", | |
| "2 these are awesome and make my phone look so st... Positive \n", | |
| "3 item arrived in great time and was in perfect ... Positive \n", | |
| "4 awesome stays on and looks great can be used o... Positive \n", | |
| "5 these make using the home button easy my daugh... Positive \n", | |
| "6 came just as described it doesnt come unstuck ... Positive \n", | |
| "7 it worked for the first week then it only char... Negative \n", | |
| "8 good case solid build protects phone all aroun... Positive \n", | |
| "9 this is a fantastic case very stylish and prot... Positive \n", | |
| "\n", | |
| " sentiment score \n", | |
| "0 -0.1078 \n", | |
| "1 0.9136 \n", | |
| "2 0.8481 \n", | |
| "3 0.9584 \n", | |
| "4 0.8957 \n", | |
| "5 0.8126 \n", | |
| "6 0.7964 \n", | |
| "7 -0.4215 \n", | |
| "8 0.8555 \n", | |
| "9 0.9572 " | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "# apply sentiment analysis\n", | |
| "analyser = SentimentIntensityAnalyzer()\n", | |
| "\n", | |
| "sentiment_score_list = []\n", | |
| "sentiment_label_list = []\n", | |
| "\n", | |
| "for i in df['remove_lower_punct'].values.tolist():\n", | |
| " sentiment_score = analyser.polarity_scores(i)\n", | |
| "\n", | |
| " if sentiment_score['compound'] >= 0.05:\n", | |
| " sentiment_score_list.append(sentiment_score['compound'])\n", | |
| " sentiment_label_list.append('Positive')\n", | |
| " elif sentiment_score['compound'] > -0.05 and sentiment_score['compound'] < 0.05:\n", | |
| " sentiment_score_list.append(sentiment_score['compound'])\n", | |
| " sentiment_label_list.append('Neutral')\n", | |
| " elif sentiment_score['compound'] <= -0.05:\n", | |
| " sentiment_score_list.append(sentiment_score['compound'])\n", | |
| " sentiment_label_list.append('Negative')\n", | |
| " \n", | |
| "df['sentiment'] = sentiment_label_list\n", | |
| "df['sentiment score'] = sentiment_score_list\n", | |
| "\n", | |
| "display(df.head(10))" | |
| ] | |
| } | |
| ], | |
| "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.7.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment