Skip to content

Instantly share code, notes, and snippets.

@ShimanoN
Created February 7, 2023 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShimanoN/b1a03427f292c3064fc2eab870b442e7 to your computer and use it in GitHub Desktop.
Save ShimanoN/b1a03427f292c3064fc2eab870b442e7 to your computer and use it in GitHub Desktop.
Sentiment_Analysis.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"id": "d92cbccd-517f-46db-bae6-04df36f53f93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting textblob\n",
" Downloading textblob-0.17.1-py2.py3-none-any.whl (636 kB)\n",
" -------------------------------------- 636.8/636.8 kB 6.7 MB/s eta 0:00:00\n",
"Requirement already satisfied: nltk>=3.1 in d:\\anaconda\\lib\\site-packages (from textblob) (3.7)\n",
"Requirement already satisfied: regex>=2021.8.3 in d:\\anaconda\\lib\\site-packages (from nltk>=3.1->textblob) (2022.7.9)\n",
"Requirement already satisfied: joblib in d:\\anaconda\\lib\\site-packages (from nltk>=3.1->textblob) (1.1.0)\n",
"Requirement already satisfied: click in d:\\anaconda\\lib\\site-packages (from nltk>=3.1->textblob) (8.0.4)\n",
"Requirement already satisfied: tqdm in d:\\anaconda\\lib\\site-packages (from nltk>=3.1->textblob) (4.64.1)\n",
"Requirement already satisfied: colorama in d:\\anaconda\\lib\\site-packages (from click->nltk>=3.1->textblob) (0.4.5)\n",
"Installing collected packages: textblob\n",
"Successfully installed textblob-0.17.1\n"
]
}
],
"source": [
"!pip install textblob"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "9870b4ea-30d5-41f8-b04d-53d8fc64eac7",
"metadata": {},
"outputs": [],
"source": [
"# Sentiment Analysis from a User-Selected Text File\n",
"import tkinter as tk\n",
"from tkinter import messagebox\n",
"from tkinter import filedialog\n",
"from textblob import TextBlob\n",
"\n",
"def analyze_sentiment():\n",
" file_path = filedialog.askopenfilename(title=\"Select a text file\", filetypes=((\"Text Files\", \"*.txt\"), (\"All Files\", \"*.*\")))\n",
" with open(file_path, \"r\") as file:\n",
" text = file.read()\n",
" text_blob = TextBlob(text)\n",
" sentiment = text_blob.sentiment.polarity\n",
" if sentiment > 0:\n",
" sentiment_result = \"Positive\"\n",
" elif sentiment < 0:\n",
" sentiment_result = \"Negative\"\n",
" else:\n",
" sentiment_result = \"Neutral\"\n",
" sentiment_score = \"{:.2f}\".format(sentiment)\n",
" messagebox.showinfo(\"Sentiment Result\", \"Sentiment: \" + sentiment_result + \"\\nScore: \" + sentiment_score)\n",
"\n",
"root = tk.Tk()\n",
"root.geometry(\"300x200\")\n",
"root.title(\"Sentiment Analysis\")\n",
"\n",
"label = tk.Label(root, text=\"Select a text file to analyze\")\n",
"label.pack()\n",
"\n",
"button = tk.Button(root, text=\"Select File\", command=analyze_sentiment)\n",
"button.pack()\n",
"\n",
"root.mainloop()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bab91a8d-9b58-4fdb-a141-32ae05a1fda7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment