{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyMjUJE1hCPosygYRij3Dr+a", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Problem Statement: Sentiment Analysis\n", "\n", "Develop a Python program that analyzes the sentiment of a given text input. Sentiment analysis involves determining whether a piece of text expresses positive, negative, or neutral sentiments. The program should utilize the TextBlob library, which provides a simple API for sentiment analysis.\n", "\n", "## Requirements:\n", "\n", "1. The program should prompt the user to input a text for sentiment analysis.\n", "2. It should analyze the sentiment of the input text using TextBlob.\n", "3. The sentiment analysis should include determining the polarity and subjectivity of the text.\n", "4. The program should categorize the sentiment as positive, negative, or neutral based on the polarity.\n", "5. It should display the sentiment category (positive, negative, or neutral), the polarity value (ranging from -1 to 1), and the subjectivity value (ranging from 0 to 1).\n", "\n", "\n", "```\n", "Enter text to analyze sentiment: I love this movie, it's fantastic!\n", "Sentiment: Positive\n", "Polarity: 0.8\n", "Subjectivity: 0.9\n", "```\n", "\n", "> **Note**: Ensure error handling is implemented to handle unexpected inputs or errors during sentiment analysis." ], "metadata": { "id": "mEjFkntDjnWu" } }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-qpc20bSjTRV", "outputId": "1ca4fb88-2bbd-4437-fb8d-7d98f8372b9c" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Enter text to analyze sentiment: Happy\n", "Sentiment: Positive\n", "Polarity: 0.8\n", "Subjectivity: 1.0\n" ] } ], "source": [ "from textblob import TextBlob\n", "\n", "def analyze_sentiment(text):\n", " analysis = TextBlob(text)\n", "\n", " # Get polarity and subjectivity\n", " polarity = analysis.sentiment.polarity\n", " subjectivity = analysis.sentiment.subjectivity\n", "\n", " # Determine sentiment\n", " if polarity > 0:\n", " sentiment = \"Positive\"\n", " elif polarity < 0:\n", " sentiment = \"Negative\"\n", " else:\n", " sentiment = \"Neutral\"\n", "\n", " return sentiment, polarity, subjectivity\n", "\n", "text = input(\"Enter text to analyze sentiment: \")\n", "sentiment, polarity, subjectivity = analyze_sentiment(text)\n", "print(f\"Sentiment: {sentiment}\")\n", "print(f\"Polarity: {polarity}\")\n", "print(f\"Subjectivity: {subjectivity}\")\n" ] } ] }