import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from transformers import pipeline
def sentiment_analysis1(text):
# Load the sentiment analysis pipeline with the BERT model
classifier = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
# Perform sentiment analysis
result = classifier(text)
return result
# Example usage:
financial_news = "The stock market experienced a significant downturn today amid concerns over inflation."
result = sentiment_analysis1(financial_news)
print("Sentiment Analysis Result:", result)
Sentiment Analysis Result: [{'label': '2 stars', 'score': 0.47418859601020813}]
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.
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.
Considerations:
data = {
'headline': [
"Apple's revenue exceeds expectations with a strong quarterly report",
"Major banks face scrutiny as financial regulations tighten",
"Tesla's new factory investment raises concerns over debt levels"
]
}
df = pd.DataFrame(data)
df
headline | |
---|---|
0 | Apple's revenue exceeds expectations with a st... |
1 | Major banks face scrutiny as financial regulat... |
2 | Tesla's new factory investment raises concerns... |
df['sentiment'] = sentiment_analysis1(df['headline'].tolist())
print(df)
headline \ 0 Apple's revenue exceeds expectations with a st... 1 Major banks face scrutiny as financial regulat... 2 Tesla's new factory investment raises concerns... sentiment 0 {'label': '5 stars', 'score': 0.5569013357162476} 1 {'label': '4 stars', 'score': 0.27764415740966... 2 {'label': '5 stars', 'score': 0.4056451618671417}
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:
from transformers import pipeline
# Initialize the sentiment analysis model
model_name = "yiyanghkust/finbert-tone"
finbert = pipeline("sentiment-analysis", model=model_name)
# Function to analyze sentiment
def analyze_sentiment(text):
results = finbert(text)
return results
# Example usage:
financial_news = "The company's revenue has exceeded expectations, but concerns about regulatory challenges remain."
sentiment_result = analyze_sentiment(financial_news)
# Print the results
print("Sentiment Analysis Result:", sentiment_result)
Sentiment Analysis Result: [{'label': 'Positive', 'score': 0.9999998807907104}]
df['sentiment'] = analyze_sentiment(df['headline'].tolist())
print(df)
headline \ 0 Apple's revenue exceeds expectations with a st... 1 Major banks face scrutiny as financial regulat... 2 Tesla's new factory investment raises concerns... sentiment 0 {'label': 'Positive', 'score': 0.9999991655349... 1 {'label': 'Negative', 'score': 0.9769049882888... 2 {'label': 'Negative', 'score': 0.9999909400939...
pos="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 ="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"
#!pip install textblob
from textblob import TextBlob
print(TextBlob(pos).sentiment)
print(TextBlob(neg).sentiment)
Sentiment(polarity=0.25645833333333334, subjectivity=0.645625) Sentiment(polarity=-0.3055555555555556, subjectivity=0.5305555555555556)
!pip install vaderSentiment
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/ Collecting vaderSentiment Downloading vaderSentiment-3.3.2-py2.py3-none-any.whl (125 kB) |████████████████████████████████| 125 kB 5.4 MB/s Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from vaderSentiment) (2.23.0) 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) Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (3.0.4) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (2022.9.24) Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->vaderSentiment) (2.10) Installing collected packages: vaderSentiment Successfully installed vaderSentiment-3.3.2
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
vs_pos = SentimentIntensityAnalyzer().polarity_scores(pos)
vs_neg = SentimentIntensityAnalyzer().polarity_scores(neg)
print("{:-<65} {}".format(pos, str(vs_pos)))
print('--------------')
print("{:-<65} {}".format(neg, str(vs_neg)))
print(vs_pos)
print(vs_neg)
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} -------------- 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} {'neg': 0.0, 'neu': 0.82, 'pos': 0.18, 'compound': 0.9113} {'neg': 0.107, 'neu': 0.852, 'pos': 0.041, 'compound': -0.3182}
!pip install flair
Collecting flair Downloading flair-0.13.1-py3-none-any.whl (388 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 388.3/388.3 kB 6.0 MB/s eta 0:00:00 Collecting boto3>=1.20.27 (from flair) Downloading boto3-1.34.103-py3-none-any.whl (139 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 139.3/139.3 kB 8.7 MB/s eta 0:00:00 Collecting bpemb>=0.3.2 (from flair) Downloading bpemb-0.3.5-py3-none-any.whl (19 kB) Collecting conllu>=4.0 (from flair) Downloading conllu-4.5.3-py2.py3-none-any.whl (16 kB) Collecting deprecated>=1.2.13 (from flair) Downloading Deprecated-1.2.14-py2.py3-none-any.whl (9.6 kB) Collecting ftfy>=6.1.0 (from flair) Downloading ftfy-6.2.0-py3-none-any.whl (54 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.4/54.4 kB 6.6 MB/s eta 0:00:00 Requirement already satisfied: gdown>=4.4.0 in /usr/local/lib/python3.10/dist-packages (from flair) (5.1.0) Requirement already satisfied: gensim>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.3.2) Requirement already satisfied: huggingface-hub>=0.10.0 in /usr/local/lib/python3.10/dist-packages (from flair) (0.20.3) Collecting janome>=0.4.2 (from flair) Downloading Janome-0.5.0-py2.py3-none-any.whl (19.7 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.7/19.7 MB 40.6 MB/s eta 0:00:00 Collecting langdetect>=1.0.9 (from flair) Downloading langdetect-1.0.9.tar.gz (981 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 981.5/981.5 kB 60.7 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done Requirement already satisfied: lxml>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.9.4) Requirement already satisfied: matplotlib>=2.2.3 in /usr/local/lib/python3.10/dist-packages (from flair) (3.7.1) Requirement already satisfied: more-itertools>=8.13.0 in /usr/local/lib/python3.10/dist-packages (from flair) (10.1.0) Collecting mpld3>=0.3 (from flair) Downloading mpld3-0.5.10-py3-none-any.whl (202 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 202.6/202.6 kB 24.6 MB/s eta 0:00:00 Collecting pptree>=3.1 (from flair) Downloading pptree-3.1.tar.gz (3.0 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.10/dist-packages (from flair) (2.8.2) Collecting pytorch-revgrad>=0.2.0 (from flair) Downloading pytorch_revgrad-0.2.0-py3-none-any.whl (4.6 kB) Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.10/dist-packages (from flair) (2023.12.25) Requirement already satisfied: scikit-learn>=1.0.2 in /usr/local/lib/python3.10/dist-packages (from flair) (1.2.2) Collecting segtok>=1.5.11 (from flair) Downloading segtok-1.5.11-py3-none-any.whl (24 kB) Collecting sqlitedict>=2.0.0 (from flair) Downloading sqlitedict-2.1.0.tar.gz (21 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: tabulate>=0.8.10 in /usr/local/lib/python3.10/dist-packages (from flair) (0.9.0) Requirement already satisfied: torch!=1.8,>=1.5.0 in /usr/local/lib/python3.10/dist-packages (from flair) (2.2.1+cu121) Requirement already satisfied: tqdm>=4.63.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.66.4) Collecting transformer-smaller-training-vocab>=0.2.3 (from flair) Downloading transformer_smaller_training_vocab-0.4.0-py3-none-any.whl (14 kB) Requirement already satisfied: transformers[sentencepiece]<5.0.0,>=4.18.0 in /usr/local/lib/python3.10/dist-packages (from flair) (4.40.2) Collecting urllib3<2.0.0,>=1.0.0 (from flair) Downloading urllib3-1.26.18-py2.py3-none-any.whl (143 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 143.8/143.8 kB 15.2 MB/s eta 0:00:00 Collecting wikipedia-api>=0.5.7 (from flair) Downloading Wikipedia_API-0.6.0-py3-none-any.whl (14 kB) Collecting semver<4.0.0,>=3.0.0 (from flair) Downloading semver-3.0.2-py3-none-any.whl (17 kB) Collecting botocore<1.35.0,>=1.34.103 (from boto3>=1.20.27->flair) Downloading botocore-1.34.103-py3-none-any.whl (12.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 12.2/12.2 MB 49.7 MB/s eta 0:00:00 Collecting jmespath<2.0.0,>=0.7.1 (from boto3>=1.20.27->flair) Downloading jmespath-1.0.1-py3-none-any.whl (20 kB) Collecting s3transfer<0.11.0,>=0.10.0 (from boto3>=1.20.27->flair) Downloading s3transfer-0.10.1-py3-none-any.whl (82 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 82.2/82.2 kB 10.6 MB/s eta 0:00:00 Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (1.25.2) Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (2.31.0) Requirement already satisfied: sentencepiece in /usr/local/lib/python3.10/dist-packages (from bpemb>=0.3.2->flair) (0.1.99) Requirement already satisfied: wrapt<2,>=1.10 in /usr/local/lib/python3.10/dist-packages (from deprecated>=1.2.13->flair) (1.14.1) 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) Requirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (4.12.3) Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from gdown>=4.4.0->flair) (3.14.0) Requirement already satisfied: scipy>=1.7.0 in /usr/local/lib/python3.10/dist-packages (from gensim>=4.2.0->flair) (1.11.4) 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) 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) Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (6.0.1) 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) Requirement already satisfied: packaging>=20.9 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.10.0->flair) (24.0) Requirement already satisfied: six in /usr/local/lib/python3.10/dist-packages (from langdetect>=1.0.9->flair) (1.16.0) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.2.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (4.51.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (1.4.5) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (9.4.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib>=2.2.3->flair) (3.1.2) Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from mpld3>=0.3->flair) (3.1.4) 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) 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) Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (1.12) Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch!=1.8,>=1.5.0->flair) (3.3) Collecting nvidia-cuda-nvrtc-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (23.7 MB) Collecting nvidia-cuda-runtime-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (823 kB) Collecting nvidia-cuda-cupti-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (14.1 MB) Collecting nvidia-cudnn-cu12==8.9.2.26 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cudnn_cu12-8.9.2.26-py3-none-manylinux1_x86_64.whl (731.7 MB) Collecting nvidia-cublas-cu12==12.1.3.1 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl (410.6 MB) Collecting nvidia-cufft-cu12==11.0.2.54 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl (121.6 MB) Collecting nvidia-curand-cu12==10.3.2.106 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl (56.5 MB) Collecting nvidia-cusolver-cu12==11.4.5.107 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl (124.2 MB) Collecting nvidia-cusparse-cu12==12.1.0.106 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl (196.0 MB) Collecting nvidia-nccl-cu12==2.19.3 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_nccl_cu12-2.19.3-py3-none-manylinux1_x86_64.whl (166.0 MB) Collecting nvidia-nvtx-cu12==12.1.105 (from torch!=1.8,>=1.5.0->flair) Using cached nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl (99 kB) 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) Collecting nvidia-nvjitlink-cu12 (from nvidia-cusolver-cu12==11.4.5.107->torch!=1.8,>=1.5.0->flair) Using cached nvidia_nvjitlink_cu12-12.4.127-py3-none-manylinux2014_x86_64.whl (21.1 MB) 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) 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) 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) Collecting accelerate>=0.21.0 (from transformers[sentencepiece]<5.0.0,>=4.18.0->flair) Downloading accelerate-0.30.1-py3-none-any.whl (302 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 302.6/302.6 kB 30.7 MB/s eta 0:00:00 Requirement already satisfied: soupsieve>1.2 in /usr/local/lib/python3.10/dist-packages (from beautifulsoup4->gdown>=4.4.0->flair) (2.5) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->mpld3>=0.3->flair) (2.1.5) 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) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->bpemb>=0.3.2->flair) (3.7) 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) 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) 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) 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) Building wheels for collected packages: langdetect, pptree, sqlitedict Building wheel for langdetect (setup.py) ... done Created wheel for langdetect: filename=langdetect-1.0.9-py3-none-any.whl size=993227 sha256=00545d1b58fc43b721aa119f90655c079d6498ca4a8173aec5239a20ee0844f3 Stored in directory: /root/.cache/pip/wheels/95/03/7d/59ea870c70ce4e5a370638b5462a7711ab78fba2f655d05106 Building wheel for pptree (setup.py) ... done Created wheel for pptree: filename=pptree-3.1-py3-none-any.whl size=4609 sha256=4153de6ec39294a300d40d0251cd2d2a80abc0017b7a69f7dafd4e5ad8f654bf Stored in directory: /root/.cache/pip/wheels/9f/b6/0e/6f26eb9e6eb53ff2107a7888d72b5a6a597593956113037828 Building wheel for sqlitedict (setup.py) ... done Created wheel for sqlitedict: filename=sqlitedict-2.1.0-py3-none-any.whl size=16862 sha256=78c5d299b136b70a5becdd792153256480ae5c9ddc7172c7f397459160fa4087 Stored in directory: /root/.cache/pip/wheels/79/d6/e7/304e0e6cb2221022c26d8161f7c23cd4f259a9e41e8bbcfabd Successfully built langdetect pptree sqlitedict 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 Attempting uninstall: urllib3 Found existing installation: urllib3 2.0.7 Uninstalling urllib3-2.0.7: Successfully uninstalled urllib3-2.0.7 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
from flair.models import TextClassifier
from flair.data import Sentence
classifier = TextClassifier.load('en-sentiment')
sentence = Sentence(pos)
classifier.predict(sentence)
# print sentence with predicted labels
print('Sentence above is: ', sentence.labels)
sentence = Sentence(neg)
classifier.predict(sentence)
# print sentence with predicted labels
print('Sentence above is: ', sentence.labels)
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)] 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)]
from transformers import pipeline
def sentiment_analysis1(text):
# Load the sentiment analysis pipeline with the BERT model
classifier = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
# Perform sentiment analysis
result = classifier(text)
return result
# Example usage:
result = sentiment_analysis1(pos)
print("Sentiment Analysis Result:", result)
result = sentiment_analysis1(neg)
print("Sentiment Analysis Result:", result)
Sentiment Analysis Result: [{'label': '5 stars', 'score': 0.7873240113258362}] Sentiment Analysis Result: [{'label': '2 stars', 'score': 0.6215679049491882}]
from transformers import pipeline
# Initialize the sentiment analysis model
model_name = "yiyanghkust/finbert-tone"
finbert = pipeline("sentiment-analysis", model=model_name)
# Function to analyze sentiment
def analyze_sentiment(text):
results = finbert(text)
return results
# Example usage:
sentiment_result = analyze_sentiment(pos)
# Print the results
print("Sentiment Analysis Result:", sentiment_result)
# Example usage:
sentiment_result = analyze_sentiment(neg)
# Print the results
print("Sentiment Analysis Result:", sentiment_result)
Sentiment Analysis Result: [{'label': 'Positive', 'score': 0.9995104074478149}] Sentiment Analysis Result: [{'label': 'Neutral', 'score': 0.999244213104248}]