|
22 | 22 | },
|
23 | 23 | {
|
24 | 24 | "cell_type": "code",
|
25 |
| - "execution_count": 2, |
| 25 | + "execution_count": 5, |
26 | 26 | "metadata": {},
|
27 |
| - "outputs": [], |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "Requirement already satisfied: textblob in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (0.18.0.post0)\n", |
| 33 | + "Requirement already satisfied: nltk>=3.8 in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (from textblob) (3.8.1)\n", |
| 34 | + "Requirement already satisfied: click in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (from nltk>=3.8->textblob) (8.0.4)\n", |
| 35 | + "Requirement already satisfied: joblib in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (from nltk>=3.8->textblob) (1.2.0)\n", |
| 36 | + "Requirement already satisfied: regex>=2021.8.3 in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (from nltk>=3.8->textblob) (2022.7.9)\n", |
| 37 | + "Requirement already satisfied: tqdm in c:\\users\\umesh\\anaconda3.x\\lib\\site-packages (from nltk>=3.8->textblob) (4.65.0)\n", |
| 38 | + "Requirement already satisfied: colorama in c:\\users\\umesh\\appdata\\roaming\\python\\python311\\site-packages (from click->nltk>=3.8->textblob) (0.4.6)\n" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "ename": "ModuleNotFoundError", |
| 43 | + "evalue": "No module named 'textblob'", |
| 44 | + "output_type": "error", |
| 45 | + "traceback": [ |
| 46 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 47 | + "\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", |
| 48 | + "Cell \u001b[1;32mIn[5], line 7\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrandom\u001b[39;00m \n\u001b[0;32m 6\u001b[0m get_ipython()\u001b[38;5;241m.\u001b[39msystem(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpip install textblob\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m----> 7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtextblob\u001b[39;00m\n", |
| 49 | + "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'textblob'" |
| 50 | + ] |
| 51 | + } |
| 52 | + ], |
28 | 53 | "source": [
|
29 | 54 | "\n",
|
30 | 55 | "import pandas as pd\n",
|
31 | 56 | "import numpy as np\n",
|
32 | 57 | "import itertools #to create efficent looping to fetch more data in a go\n",
|
33 | 58 | "import re \n",
|
34 |
| - "import random " |
| 59 | + "import random \n", |
| 60 | + "from textblob import TextBlob" |
35 | 61 | ]
|
36 | 62 | },
|
37 | 63 | {
|
|
778 | 804 | " g.close()"
|
779 | 805 | ]
|
780 | 806 | },
|
| 807 | + { |
| 808 | + "cell_type": "code", |
| 809 | + "execution_count": 6, |
| 810 | + "metadata": {}, |
| 811 | + "outputs": [ |
| 812 | + { |
| 813 | + "ename": "NameError", |
| 814 | + "evalue": "name 'TextBlob' is not defined", |
| 815 | + "output_type": "error", |
| 816 | + "traceback": [ |
| 817 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 818 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 819 | + "Cell \u001b[1;32mIn[6], line 22\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[38;5;66;03m# Example usage:\u001b[39;00m\n\u001b[0;32m 21\u001b[0m text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mI absolutely loved this movie! It was fantastic.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m---> 22\u001b[0m sentiment \u001b[38;5;241m=\u001b[39m \u001b[43manalyze_sentiment\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 23\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSentiment:\u001b[39m\u001b[38;5;124m\"\u001b[39m, sentiment)\n\u001b[0;32m 24\u001b[0m \u001b[38;5;66;03m# Assuming df is your DataFrame containing the reviews\u001b[39;00m\n", |
| 820 | + "Cell \u001b[1;32mIn[6], line 10\u001b[0m, in \u001b[0;36manalyze_sentiment\u001b[1;34m(text)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21manalyze_sentiment\u001b[39m(text):\n\u001b[0;32m 2\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[38;5;124;03m Analyzes the sentiment of the input text.\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[38;5;124;03m \u001b[39;00m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[38;5;124;03m - 'neutral' if sentiment polarity == 0\u001b[39;00m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[1;32m---> 10\u001b[0m blob \u001b[38;5;241m=\u001b[39m \u001b[43mTextBlob\u001b[49m(text)\n\u001b[0;32m 11\u001b[0m polarity \u001b[38;5;241m=\u001b[39m blob\u001b[38;5;241m.\u001b[39msentiment\u001b[38;5;241m.\u001b[39mpolarity\n\u001b[0;32m 13\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m polarity \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", |
| 821 | + "\u001b[1;31mNameError\u001b[0m: name 'TextBlob' is not defined" |
| 822 | + ] |
| 823 | + } |
| 824 | + ], |
| 825 | + "source": [ |
| 826 | + "def analyze_sentiment(text):\n", |
| 827 | + " \"\"\"\n", |
| 828 | + " Analyzes the sentiment of the input text.\n", |
| 829 | + " \n", |
| 830 | + " Returns:\n", |
| 831 | + " - 'positive' if sentiment polarity > 0\n", |
| 832 | + " - 'negative' if sentiment polarity < 0\n", |
| 833 | + " - 'neutral' if sentiment polarity == 0\n", |
| 834 | + " \"\"\"\n", |
| 835 | + " blob = TextBlob(text)\n", |
| 836 | + " polarity = blob.sentiment.polarity\n", |
| 837 | + " \n", |
| 838 | + " if polarity > 0:\n", |
| 839 | + " return 'positive'\n", |
| 840 | + " elif polarity < 0:\n", |
| 841 | + " return 'negative'\n", |
| 842 | + " else:\n", |
| 843 | + " return 'neutral'\n", |
| 844 | + "\n", |
| 845 | + "# Assuming df is your DataFrame containing the reviews\n", |
| 846 | + "df['sentiment'] = df['user_review'].apply(analyze_sentiment)\n" |
| 847 | + ] |
| 848 | + }, |
781 | 849 | {
|
782 | 850 | "cell_type": "markdown",
|
783 | 851 | "metadata": {},
|
|
818 | 886 | "name": "python",
|
819 | 887 | "nbconvert_exporter": "python",
|
820 | 888 | "pygments_lexer": "ipython3",
|
821 |
| - "version": "3.12.1" |
| 889 | + "version": "3.11.4" |
822 | 890 | },
|
823 | 891 | "orig_nbformat": 4
|
824 | 892 | },
|
|
0 commit comments