-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentiment_analysis.py
73 lines (66 loc) · 2.75 KB
/
sentiment_analysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
import pandas as pd
from textblob import TextBlob
from newsapi import NewsApiClient
from dotenv import load_dotenv
import os
from datetime import datetime, timedelta
load_dotenv()
def get_news(ticker, max_articles=20):
newsapi = NewsApiClient(api_key=os.getenv("NEWSAPI_KEY"))
try:
news = newsapi.get_everything(q=ticker, language='en', sort_by='relevancy', page=1)
if news['status'] == 'ok':
return news['articles'][:max_articles]
else:
st.error(f"Error fetching news: {news.get('message', 'Unknown error')}")
return []
except Exception as e:
st.error(f"Failed to fetch news: {e}")
return []
def analyze_sentiment(text):
analysis = TextBlob(text)
polarity = analysis.sentiment.polarity
if polarity > 0:
return "Positive", polarity
elif polarity < 0:
return "Negative", polarity
else:
return "Neutral", polarity
def sentiment_analysis(articles):
sentiment_results = []
for article in articles:
description = article.get('description')
if description:
sentiment, polarity = analyze_sentiment(description)
sentiment_results.append((sentiment, polarity, article))
return sentiment_results
def show_sentiment_analysis_page():
st.title("Sentiment Analysis")
st.sidebar.header("User Input")
ticker = st.sidebar.text_input("Enter the ticker:", "DOW")
if ticker:
articles = get_news(ticker, max_articles=20)
if articles:
st.subheader(f"Recent News for {ticker}")
sentiment_results = sentiment_analysis(articles)
if sentiment_results:
for sentiment, polarity, article in sentiment_results:
title = article.get('title', 'No title available')
description = article.get('description', 'No description available')
url = article.get('url', '#')
st.write(f"### {title}")
st.write(description)
st.write(f"Sentiment: **{sentiment}** (Polarity: {polarity:.2f})")
st.write(f"[Read more]({url})")
st.write('---')
avg_polarity = sum(p[1] for p in sentiment_results) / len(sentiment_results)
st.write(f"Overall Sentiment for {ticker} is {'Positive' if avg_polarity > 0 else 'Negative' if avg_polarity < 0 else 'Neutral'} (Average Polarity: {avg_polarity:.2f})")
else:
st.write("No valid descriptions available for sentiment analysis.")
else:
st.error("No news found for the ticker")
else:
st.warning("Please enter a ticker")
if __name__ == "__main__":
show_sentiment_analysis_page()