File size: 3,133 Bytes
0228682
4a525ac
 
0228682
 
4a525ac
0228682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a525ac
0228682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a525ac
0228682
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a525ac
0228682
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
from func import fetch_news, analyze_sentiment, extract_org_entities

import time

# ---------------- Page Setup ----------------
st.set_page_config(page_title="Stock News Sentiment Analysis", layout="centered")

st.markdown("""
    <style>
        .main { background-color: #f9fbfc; }
        .stTextInput>div>div>input, .stTextArea textarea {
            font-size: 16px;
            padding: 0.5rem;
        }
        .stButton>button {
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            padding: 0.5rem 1rem;
            border-radius: 8px;
        }
        .stButton>button:hover {
            background-color: #45a049;
        }
    </style>
""", unsafe_allow_html=True)

# ---------------- User Interface ----------------
st.title("\U0001F4CA Stock News Sentiment Analysis")
st.markdown("""
This tool analyzes the sentiment of news articles related to companies you mention in text.
\U0001F4A1 *Try input like:* `I want to check Apple, Tesla, and Microsoft.`
**Note:** If news fetching fails, it may be due to changes in the Finviz website or access restrictions.
""")

free_text = st.text_area("Enter text mentioning companies:", height=100)
tickers = extract_org_entities(free_text)

if tickers:
    cleaned_input = ", ".join(tickers)
    st.markdown(f"\U0001F50E **Identified Tickers:** `{cleaned_input}`")
else:
    tickers = []

# ---------------- Button Trigger ----------------
if st.button("Get News and Sentiment"):
    if not tickers:
        st.warning("Please mention at least one recognizable company.")
    else:
        progress_bar = st.progress(0)
        total_stocks = len(tickers)
        for idx, ticker in enumerate(tickers):
            st.subheader(f"Analyzing {ticker}...")
            news_list = fetch_news(ticker)

            if news_list:
                sentiments = []
                for news in news_list:
                    sentiment = analyze_sentiment(news['title'])
                    sentiments.append(sentiment)

                positive_count = sentiments.count("Positive")
                negative_count = sentiments.count("Negative")
                total = len(sentiments)
                positive_ratio = positive_count / total if total else 0
                negative_ratio = negative_count / total if total else 0

                if positive_ratio >= 0.4:
                    overall_sentiment = "Positive"
                elif negative_ratio >= 0.6:
                    overall_sentiment = "Negative"
                else:
                    overall_sentiment = "Neutral"

                st.write(f"**Top 3 News Articles for {ticker}**")
                for i, news in enumerate(news_list[:3], 1):
                    sentiment = sentiments[i-1]
                    st.markdown(f"{i}. [{news['title']}]({news['link']}) - **{sentiment}**")

                st.write(f"**Overall Sentiment for {ticker}: {overall_sentiment}**")
            else:
                st.write(f"No news available for {ticker}.")

            progress_bar.progress((idx + 1) / total_stocks)
            time.sleep(0.1)