{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNs+mL3FuFwJixqlz34eHXX", "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": "code", "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns" ], "metadata": { "id": "-I0iwu0lGx37" }, "execution_count": 23, "outputs": [] }, { "cell_type": "markdown", "source": [ "# transformer" ], "metadata": { "id": "tYWE2ItALNIr" } }, { "cell_type": "code", "source": [ "from transformers import pipeline\n", "\n", "def sentiment_analysis1(text):\n", " # Load the sentiment analysis pipeline with the BERT model\n", " classifier = pipeline(\"sentiment-analysis\", model=\"nlptown/bert-base-multilingual-uncased-sentiment\")\n", "\n", " # Perform sentiment analysis\n", " result = classifier(text)\n", " return result\n", "\n", "# Example usage:\n", "financial_news = \"The stock market experienced a significant downturn today amid concerns over inflation.\"\n", "result = sentiment_analysis1(financial_news)\n", "print(\"Sentiment Analysis Result:\", result)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JNfVZxzuGx-y", "outputId": "f73c6293-4a01-4abb-bcb4-8c370eaf40aa" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentiment Analysis Result: [{'label': '2 stars', 'score': 0.47418859601020813}]\n" ] } ] }, { "cell_type": "markdown", "source": [ "Label Interpretation: The label '2 stars' suggests that the sentiment expressed in the text you analyzed is generally negative. In sentiment rating systems where labels are presented as stars (often ranging from 1 star to 5 stars), a rating of 2 stars typically denotes dissatisfaction or a negative view. It implies that the text likely contains criticisms or less favorable opinions.\n", "\n", "Score Interpretation: The confidence score of approximately 0.474 (or 47.4%) associated with this label indicates a moderate level of confidence in the assessment. This isn't a very high confidence level, which might suggest that the sentiment expressed in the text was not overwhelmingly clear or that the text contained mixed sentiments, making it harder for the model to assign a more definitive sentiment rating with higher confidence.\n", "\n", "Considerations:" ], "metadata": { "id": "fzvz6mGYQC8G" } }, { "cell_type": "code", "source": [ "data = {\n", " 'headline': [\n", " \"Apple's revenue exceeds expectations with a strong quarterly report\",\n", " \"Major banks face scrutiny as financial regulations tighten\",\n", " \"Tesla's new factory investment raises concerns over debt levels\"\n", " ]\n", "}\n", "\n", "df = pd.DataFrame(data)\n", "df" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 143 }, "id": "7d30p39CGyBq", "outputId": "20668295-f231-4bc7-fb81-3bcb4947bef1" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ " headline\n", "0 Apple's revenue exceeds expectations with a st...\n", "1 Major banks face scrutiny as financial regulat...\n", "2 Tesla's new factory investment raises concerns..." ], "text/html": [ "\n", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
headline
0Apple's revenue exceeds expectations with a st...
1Major banks face scrutiny as financial regulat...
2Tesla's new factory investment raises concerns...
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", " \n", " \n", " \n", "
\n", "\n", "
\n", "
\n" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "dataframe", "variable_name": "df", "summary": "{\n \"name\": \"df\",\n \"rows\": 3,\n \"fields\": [\n {\n \"column\": \"headline\",\n \"properties\": {\n \"dtype\": \"string\",\n \"num_unique_values\": 3,\n \"samples\": [\n \"Apple's revenue exceeds expectations with a strong quarterly report\",\n \"Major banks face scrutiny as financial regulations tighten\",\n \"Tesla's new factory investment raises concerns over debt levels\"\n ],\n \"semantic_type\": \"\",\n \"description\": \"\"\n }\n }\n ]\n}" } }, "metadata": {}, "execution_count": 24 } ] }, { "cell_type": "code", "source": [ "df['sentiment'] = sentiment_analysis1(df['headline'].tolist())\n", "print(df)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Pl9PBlJoGx_l", "outputId": "e1e05c92-ae31-4d6f-8686-4d95a0c506ba" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " headline \\\n", "0 Apple's revenue exceeds expectations with a st... \n", "1 Major banks face scrutiny as financial regulat... \n", "2 Tesla's new factory investment raises concerns... \n", "\n", " sentiment \n", "0 {'label': '5 stars', 'score': 0.5569013357162476} \n", "1 {'label': '4 stars', 'score': 0.27764415740966... \n", "2 {'label': '5 stars', 'score': 0.4056451618671417} \n" ] } ] }, { "cell_type": "markdown", "source": [ "# FinBERT" ], "metadata": { "id": "x6E3Hlr_LRN8" } }, { "cell_type": "markdown", "source": [ "We will load a FINBERT model from Hugging Face’s model repository. Please note that, as of my last training data, specific models like ‘yiyanghkust/finbert-tone’ can be used for demonstration:" ], "metadata": { "id": "GSEVwDPsLvRr" } }, { "cell_type": "code", "source": [ "from transformers import pipeline\n", "\n", "# Initialize the sentiment analysis model\n", "model_name = \"yiyanghkust/finbert-tone\"\n", "finbert = pipeline(\"sentiment-analysis\", model=model_name)\n", "\n", "# Function to analyze sentiment\n", "def analyze_sentiment(text):\n", " results = finbert(text)\n", " return results\n", "\n", "# Example usage:\n", "financial_news = \"The company's revenue has exceeded expectations, but concerns about regulatory challenges remain.\"\n", "sentiment_result = analyze_sentiment(financial_news)\n", "\n", "# Print the results\n", "print(\"Sentiment Analysis Result:\", sentiment_result)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XGjjiU4tLfQy", "outputId": "3a69364b-963a-4a83-ecaf-0e9e4e059ce6" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentiment Analysis Result: [{'label': 'Positive', 'score': 0.9999998807907104}]\n" ] } ] }, { "cell_type": "code", "source": [ "df['sentiment'] = analyze_sentiment(df['headline'].tolist())\n", "print(df)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Y3dash_BLfOH", "outputId": "2c33e82f-9fad-4e2b-c9a4-76da2a1f18e8" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " headline \\\n", "0 Apple's revenue exceeds expectations with a st... \n", "1 Major banks face scrutiny as financial regulat... \n", "2 Tesla's new factory investment raises concerns... \n", "\n", " sentiment \n", "0 {'label': 'Positive', 'score': 0.9999991655349... \n", "1 {'label': 'Negative', 'score': 0.9769049882888... \n", "2 {'label': 'Negative', 'score': 0.9999909400939... \n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "ds_W5NMTLfLp" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "d-Rt5L9FLfI3" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "qM07r4i3Ee_b" }, "outputs": [], "source": [ "pos=\"In two years at Texas State Dr. Yi was perhaps the most exceptional teacher \\\n", "I have had. His passion for what he teaches as well as his students well-being is \\\n", "evident. He went far beyond what is necessary to provide us with material via his canvas pages. \\\n", "I would highly highly recommend other students in Finance take his course(s) they would be better for it. \\\n", "Thank you\"" ] }, { "cell_type": "code", "source": [ "neg =\"Was hard to understand what the professor was saying. \\\n", "Materials on the test sometimes would reflect only a small fraction of the homework and review,\\\n", "the rest seemed to be vague in where he got the information from. \\\n", "It was also hard to stay engaged during class, I fell asleep every time I attended class. \\\n", "The lectures were practically useless to me\"" ], "metadata": { "id": "8nQlUH9sEfx0" }, "execution_count": 2, "outputs": [] }, { "cell_type": "markdown", "source": [ "### Using TextBlob" ], "metadata": { "id": "51fNhMb-D8jB" } }, { "cell_type": "code", "source": [ "#!pip install textblob\n", "\n", "from textblob import TextBlob\n", "print(TextBlob(pos).sentiment)\n", "print(TextBlob(neg).sentiment)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "97OY_XFPEfvL", "outputId": "8bf277ed-683e-4f84-f9dc-2b4eb46e1c0e" }, "execution_count": 5, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentiment(polarity=0.25645833333333334, subjectivity=0.645625)\n", "Sentiment(polarity=-0.3055555555555556, subjectivity=0.5305555555555556)\n" ] } ] }, { "cell_type": "code", "source": [ "!pip install vaderSentiment" ], "metadata": { "id": "O9tiQxKYFYiX", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "409986c4-eed9-4360-b492-2529e679afe5", "collapsed": true }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting vaderSentiment\n", " Downloading vaderSentiment-3.3.2-py2.py3-none-any.whl (125 kB)\n", "\u001b[K |████████████████████████████████| 125 kB 5.4 MB/s \n", "\u001b[?25hRequirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from vaderSentiment) (2.23.0)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (1.24.3)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (3.0.4)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (2022.9.24)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (2.10)\n", "Installing collected packages: vaderSentiment\n", "Successfully installed vaderSentiment-3.3.2\n" ] } ] }, { "cell_type": "code", "source": [ "from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer\n", "vs_pos = SentimentIntensityAnalyzer().polarity_scores(pos)\n", "vs_neg = SentimentIntensityAnalyzer().polarity_scores(neg)\n", "\n", "print(\"{:-<65} {}\".format(pos, str(vs_pos)))\n", "print('--------------')\n", "print(\"{:-<65} {}\".format(neg, str(vs_neg)))\n", "print(vs_pos)\n", "print(vs_neg)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "UbSrqC7kEf0a", "outputId": "3ddf4541-cd95-4e21-a2c8-396a0cb10b11" }, "execution_count": 19, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "In two years at Texas State Dr. Yi was perhaps the most exceptional teacher I have had. His passion for what he teaches as well as his students well-being is evident. He went far beyond what is necessary to provide us with material via his canvas pages. I would highly highly recommend other students in Finance take his course(s) they would be better for it. Thank you {'neg': 0.0, 'neu': 0.82, 'pos': 0.18, 'compound': 0.9113}\n", "--------------\n", "Was hard to understand what the professor was saying. Materials on the test sometimes would reflect only a small fraction of the homework and review,the rest seemed to be vague in where he got the information from. It was also hard to stay engaged during class, I fell asleep every time I attended class. The lectures were practically useless to me {'neg': 0.107, 'neu': 0.852, 'pos': 0.041, 'compound': -0.3182}\n", "{'neg': 0.0, 'neu': 0.82, 'pos': 0.18, 'compound': 0.9113}\n", "{'neg': 0.107, 'neu': 0.852, 'pos': 0.041, 'compound': -0.3182}\n" ] } ] }, { "cell_type": "code", "source": [ "!pip install flair" ], "metadata": { "id": "Efybhot0GmHK", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "fd517338-9741-4649-a787-c4e63fa2591d", "collapsed": true }, "execution_count": 22, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting flair\n", " Downloading flair-0.13.1-py3-none-any.whl (388 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m388.3/388.3 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting boto3>=1.20.27 (from flair)\n", " Downloading boto3-1.34.103-py3-none-any.whl (139 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m139.3/139.3 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting bpemb>=0.3.2 (from flair)\n", " Downloading bpemb-0.3.5-py3-none-any.whl (19 kB)\n", "Collecting conllu>=4.0 (from flair)\n", " Downloading conllu-4.5.3-py2.py3-none-any.whl (16 kB)\n", "Collecting deprecated>=1.2.13 (from flair)\n", " Downloading Deprecated-1.2.14-py2.py3-none-any.whl (9.6 kB)\n", "Collecting ftfy>=6.1.0 (from flair)\n", " Downloading ftfy-6.2.0-py3-none-any.whl (54 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m54.4/54.4 kB\u001b[0m \u001b[31m6.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: gdown>=4.4.0 in /usr/local/lib/python3.10/dist-packages (from flair) (5.1.0)\n", "Requirement already satisfied: gensim>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.3.2)\n", "Requirement already satisfied: huggingface-hub>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from flair) (0.20.3)\n", "Collecting janome>=0.4.2 (from flair)\n", " Downloading Janome-0.5.0-py2.py3-none-any.whl (19.7 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m19.7/19.7 MB\u001b[0m \u001b[31m40.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting langdetect>=1.0.9 (from flair)\n", " Downloading langdetect-1.0.9.tar.gz (981 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m981.5/981.5 kB\u001b[0m \u001b[31m60.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: lxml>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.9.4)\n", "Requirement already satisfied: matplotlib>=2.2.3 in /usr/local/lib/python3.10/dist-packages (from flair) (3.7.1)\n", "Requirement already satisfied: more-itertools>=8.13.0 in /usr/local/lib/python3.10/dist-packages (from flair) (10.1.0)\n", "Collecting mpld3>=0.3 (from flair)\n", " Downloading mpld3-0.5.10-py3-none-any.whl (202 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m202.6/202.6 kB\u001b[0m \u001b[31m24.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting pptree>=3.1 (from flair)\n", " Downloading pptree-3.1.tar.gz (3.0 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from flair) (2.8.2)\n", "Collecting pytorch-revgrad>=0.2.0 (from flair)\n", " Downloading pytorch_revgrad-0.2.0-py3-none-any.whl (4.6 kB)\n", "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from flair) (2023.12.25)\n", "Requirement already satisfied: scikit-learn>=1.0.2 in /usr/local/lib/python3.10/dist-packages (from flair) (1.2.2)\n", "Collecting segtok>=1.5.11 (from flair)\n", " Downloading segtok-1.5.11-py3-none-any.whl (24 kB)\n", "Collecting sqlitedict>=2.0.0 (from flair)\n", " Downloading sqlitedict-2.1.0.tar.gz (21 kB)\n", " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: tabulate>=0.8.10 in /usr/local/lib/python3.10/dist-packages (from flair) (0.9.0)\n", "Requirement already satisfied: torch!=1.8,>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from flair) (2.2.1+cu121)\n", "Requirement already satisfied: tqdm>=4.63.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.66.4)\n", "Collecting transformer-smaller-training-vocab>=0.2.3 (from flair)\n", " Downloading transformer_smaller_training_vocab-0.4.0-py3-none-any.whl (14 kB)\n", "Requirement already satisfied: transformers[sentencepiece]<5.0.0,>=4.18.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.40.2)\n", "Collecting urllib3<2.0.0,>=1.0.0 (from flair)\n", " Downloading urllib3-1.26.18-py2.py3-none-any.whl (143 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m143.8/143.8 kB\u001b[0m \u001b[31m15.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting wikipedia-api>=0.5.7 (from flair)\n", " Downloading Wikipedia_API-0.6.0-py3-none-any.whl (14 kB)\n", "Collecting semver<4.0.0,>=3.0.0 (from flair)\n", " Downloading semver-3.0.2-py3-none-any.whl (17 kB)\n", "Collecting botocore<1.35.0,>=1.34.103 (from boto3>=1.20.27->flair)\n", " Downloading botocore-1.34.103-py3-none-any.whl (12.2 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.2/12.2 MB\u001b[0m \u001b[31m49.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting jmespath<2.0.0,>=0.7.1 (from boto3>=1.20.27->flair)\n", " Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n", "Collecting s3transfer<0.11.0,>=0.10.0 (from boto3>=1.20.27->flair)\n", " Downloading s3transfer-0.10.1-py3-none-any.whl (82 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m82.2/82.2 kB\u001b[0m \u001b[31m10.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (1.25.2)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (2.31.0)\n", "Requirement already satisfied: sentencepiece in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (0.1.99)\n", "Requirement already satisfied: wrapt<2,>=1.10 in /usr/local/lib/python3.10/dist-packages (from deprecated>=1.2.13->flair) (1.14.1)\n", "Requirement already satisfied: wcwidth<0.3.0,>=0.2.12 in /usr/local/lib/python3.10/dist-packages (from ftfy>=6.1.0->flair) (0.2.13)\n", "Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (4.12.3)\n", "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (3.14.0)\n", "Requirement already satisfied: scipy>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.2.0->flair) (1.11.4)\n", "Requirement already satisfied: smart-open>=1.8.1 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.2.0->flair) (6.4.0)\n", "Requirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (2023.6.0)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (6.0.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (4.11.0)\n", "Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (24.0)\n", "Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from langdetect>=1.0.9->flair) (1.16.0)\n", "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.2.1)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (0.12.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (4.51.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.4.5)\n", "Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (9.4.0)\n", "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (3.1.2)\n", "Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from mpld3>=0.3->flair) (3.1.4)\n", "Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0.2->flair) (1.4.2)\n", "Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn>=1.0.2->flair) (3.5.0)\n", "Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (1.12)\n", "Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (3.3)\n", "Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB)\n", "Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB)\n", "Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB)\n", "Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB)\n", "Collecting nvidia-cublas-cu12==12.1.3.1 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB)\n", "Collecting nvidia-cufft-cu12==11.0.2.54 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB)\n", "Collecting nvidia-curand-cu12==10.3.2.106 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB)\n", "Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB)\n", "Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB)\n", "Collecting nvidia-nccl-cu12==2.19.3 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_nccl_cu12-2.19.3-py3-none-manylinux1_x86_64.whl (166.0 MB)\n", "Collecting nvidia-nvtx-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB)\n", "Requirement already satisfied: triton==2.2.0 in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (2.2.0)\n", "Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch!=1.8,>=1.5.0->flair)\n", " Using cached nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB)\n", "Requirement already satisfied: tokenizers<0.20,>=0.19 in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (0.19.1)\n", "Requirement already satisfied: safetensors>=0.4.1 in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (0.4.3)\n", "Requirement already satisfied: protobuf in /usr/local/lib/python3.10/dist-packages (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (3.20.3)\n", "Collecting accelerate>=0.21.0 (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair)\n", " Downloading accelerate-0.30.1-py3-none-any.whl (302 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m302.6/302.6 kB\u001b[0m \u001b[31m30.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->gdown>=4.4.0->flair) (2.5)\n", "Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->mpld3>=0.3->flair) (2.1.5)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (3.3.2)\n", "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (3.7)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (2024.2.2)\n", "Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (1.7.1)\n", "Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->torch!=1.8,>=1.5.0->flair) (1.3.0)\n", "Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from accelerate>=0.21.0->transformers[sentencepiece]<5.0.0,>=4.18.0->flair) (5.9.5)\n", "Building wheels for collected packages: langdetect, pptree, sqlitedict\n", " Building wheel for langdetect (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for langdetect: filename=langdetect-1.0.9-py3-none-any.whl size=993227 sha256=00545d1b58fc43b721aa119f90655c079d6498ca4a8173aec5239a20ee0844f3\n", " Stored in directory: /root/.cache/pip/wheels/95/03/7d/59ea870c70ce4e5a370638b5462a7711ab78fba2f655d05106\n", " Building wheel for pptree (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for pptree: filename=pptree-3.1-py3-none-any.whl size=4609 sha256=4153de6ec39294a300d40d0251cd2d2a80abc0017b7a69f7dafd4e5ad8f654bf\n", " Stored in directory: /root/.cache/pip/wheels/9f/b6/0e/6f26eb9e6eb53ff2107a7888d72b5a6a597593956113037828\n", " Building wheel for sqlitedict (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for sqlitedict: filename=sqlitedict-2.1.0-py3-none-any.whl size=16862 sha256=78c5d299b136b70a5becdd792153256480ae5c9ddc7172c7f397459160fa4087\n", " Stored in directory: /root/.cache/pip/wheels/79/d6/e7/304e0e6cb2221022c26d8161f7c23cd4f259a9e41e8bbcfabd\n", "Successfully built langdetect pptree sqlitedict\n", "Installing collected packages: sqlitedict, pptree, janome, urllib3, semver, segtok, nvidia-nvtx-cu12, nvidia-nvjitlink-cu12, nvidia-nccl-cu12, nvidia-curand-cu12, nvidia-cufft-cu12, nvidia-cuda-runtime-cu12, nvidia-cuda-nvrtc-cu12, nvidia-cuda-cupti-cu12, nvidia-cublas-cu12, langdetect, jmespath, ftfy, deprecated, conllu, nvidia-cusparse-cu12, nvidia-cudnn-cu12, botocore, wikipedia-api, s3transfer, nvidia-cusolver-cu12, mpld3, bpemb, boto3, pytorch-revgrad, accelerate, transformer-smaller-training-vocab, flair\n", " Attempting uninstall: urllib3\n", " Found existing installation: urllib3 2.0.7\n", " Uninstalling urllib3-2.0.7:\n", " Successfully uninstalled urllib3-2.0.7\n", "Successfully installed accelerate-0.30.1 boto3-1.34.103 botocore-1.34.103 bpemb-0.3.5 conllu-4.5.3 deprecated-1.2.14 flair-0.13.1 ftfy-6.2.0 janome-0.5.0 jmespath-1.0.1 langdetect-1.0.9 mpld3-0.5.10 nvidia-cublas-cu12-12.1.3.1 nvidia-cuda-cupti-cu12-12.1.105 nvidia-cuda-nvrtc-cu12-12.1.105 nvidia-cuda-runtime-cu12-12.1.105 nvidia-cudnn-cu12-8.9.2.26 nvidia-cufft-cu12-11.0.2.54 nvidia-curand-cu12-10.3.2.106 nvidia-cusolver-cu12-11.4.5.107 nvidia-cusparse-cu12-12.1.0.106 nvidia-nccl-cu12-2.19.3 nvidia-nvjitlink-cu12-12.4.127 nvidia-nvtx-cu12-12.1.105 pptree-3.1 pytorch-revgrad-0.2.0 s3transfer-0.10.1 segtok-1.5.11 semver-3.0.2 sqlitedict-2.1.0 transformer-smaller-training-vocab-0.4.0 urllib3-1.26.18 wikipedia-api-0.6.0\n" ] } ] }, { "cell_type": "code", "source": [ "from flair.models import TextClassifier\n", "from flair.data import Sentence\n", "\n", "classifier = TextClassifier.load('en-sentiment')\n", "\n", "sentence = Sentence(pos)\n", "classifier.predict(sentence)\n", "\n", "# print sentence with predicted labels\n", "print('Sentence above is: ', sentence.labels)\n", "\n", "sentence = Sentence(neg)\n", "classifier.predict(sentence)\n", "\n", "# print sentence with predicted labels\n", "print('Sentence above is: ', sentence.labels)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2m4IvOT1GmDv", "outputId": "47354256-0230-4abc-8120-cde59649a592" }, "execution_count": 26, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentence above is: ['Sentence[74]: \"In two years at Texas State Dr. Yi was perhaps the most exceptional teacher I have had. His passion for what he teaches as well as his students well-being is evident. He went far beyond what is necessary to provide us with material via his canvas pages. I would highly highly recommend other students in Finance take his course(s) they would be better for it. Thank you\"'/'POSITIVE' (0.9975)]\n", "Sentence above is: ['Sentence[65]: \"Was hard to understand what the professor was saying. Materials on the test sometimes would reflect only a small fraction of the homework and review,the rest seemed to be vague in where he got the information from. It was also hard to stay engaged during class, I fell asleep every time I attended class. The lectures were practically useless to me\"'/'NEGATIVE' (1.0)]\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "pl9zcaFfGl9i" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from transformers import pipeline\n", "\n", "def sentiment_analysis1(text):\n", " # Load the sentiment analysis pipeline with the BERT model\n", " classifier = pipeline(\"sentiment-analysis\", model=\"nlptown/bert-base-multilingual-uncased-sentiment\")\n", "\n", " # Perform sentiment analysis\n", " result = classifier(text)\n", " return result\n", "\n", "# Example usage:\n", "\n", "result = sentiment_analysis1(pos)\n", "print(\"Sentiment Analysis Result:\", result)\n", "\n", "result = sentiment_analysis1(neg)\n", "print(\"Sentiment Analysis Result:\", result)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "c6f97d46-842d-4227-ac28-10d8711b3aef", "id": "Ph84JZmbne8Z" }, "execution_count": 28, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentiment Analysis Result: [{'label': '5 stars', 'score': 0.7873240113258362}]\n", "Sentiment Analysis Result: [{'label': '2 stars', 'score': 0.6215679049491882}]\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "hZTQQF6VoWi4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from transformers import pipeline\n", "\n", "# Initialize the sentiment analysis model\n", "model_name = \"yiyanghkust/finbert-tone\"\n", "finbert = pipeline(\"sentiment-analysis\", model=model_name)\n", "\n", "# Function to analyze sentiment\n", "def analyze_sentiment(text):\n", " results = finbert(text)\n", " return results\n", "\n", "# Example usage:\n", "sentiment_result = analyze_sentiment(pos)\n", "\n", "# Print the results\n", "print(\"Sentiment Analysis Result:\", sentiment_result)\n", "\n", "\n", "# Example usage:\n", "sentiment_result = analyze_sentiment(neg)\n", "\n", "# Print the results\n", "print(\"Sentiment Analysis Result:\", sentiment_result)\n" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "e481f30a-05c6-407a-f44d-f8fe9043ffda", "id": "nZ0WxBCioW86" }, "execution_count": 30, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Sentiment Analysis Result: [{'label': 'Positive', 'score': 0.9995104074478149}]\n", "Sentiment Analysis Result: [{'label': 'Neutral', 'score': 0.999244213104248}]\n" ] } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "iWL3DVFGoWgV" }, "execution_count": null, "outputs": [] } ] }