create a streamlit interface for predicting sentiment analysis on tweets which are positive or negative, data may be on CSV or scraped online on Xtwitter. the code is in my computer on link : "C:\Users\FIRELINKS\Downloads\Pulling_Tweets.ipynb". here is the code import tweepy import pandas as pd
To upload files, please first save the app
import streamlit as st
import pandas as pd
from textblob import TextBlob
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
import re
# Download required NLTK data
try:
nltk.data.find('tokenizers/punkt')
nltk.data.find('corpora/stopwords')
nltk.data.find('corpora/wordnet')
except LookupError:
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
def clean_text(text):
# Convert to lowercase
text = text.lower()
# Remove URLs
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# Remove user @ references and '#' from tweet
text = re.sub(r'\@\w+|\#\w+', '', text)
# Remove punctuations and numbers
text = re.sub(r'[^\w\s]', '', text)
# Remove numbers
text = re.sub(r'\d+', '', text)
return text
def preprocess_text(text):
# Clean the text
text = clean_text(text)
# Tokenization
tokens = word_tokenize(text)
# Remove stopwords
stop_words = set(stopwords.words('english'))
tokens = [token for token in tokens if token not in stop_words]
# Lemmatization
lemmatizer = WordNetLemmatizer()
tokens = [lemmatizer.lemmatize(token) for token in tokens]
return ' '.join(tokens)
def analyze_sentiment(text):
analysis = TextBlob(text)
if analysis.sentiment.polarity > 0:
return 'Positive'
elif analysis.sentiment.polarity < 0:
return 'Negative'
else:
return 'Neutral'
def get_sentiment_score(text):
analysis = TextBlob(text)
return analysis.sentiment.polarity
st.title('Tweet Sentiment Analysis')
# File upload option
st.header('Option 1: Upload CSV file')
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
# Text input option
st.header('Option 2: Enter Tweet Text')
tweet_text = st.text_area("Enter tweet text here:", height=100)
if uploaded_file is not None:
# Read CSV file
df = pd.read_csv(uploaded_file)
# Check if 'text' or 'tweet' column exists
text_column = None
possible_columns = ['text', 'tweet', 'content', 'Tweet']
for col in possible_columns:
if col in df.columns:
text_column = col
break
if text_column:
st.write("Processing tweets from CSV file...")
# Create progress bar
progress_bar = st.progress(0)
# Process each tweet
df['processed_text'] = df[text_column].astype(str).apply(preprocess_text)
df['sentiment'] = df['processed_text'].apply(analyze_sentiment)
df['sentiment_score'] = df['processed_text'].apply(get_sentiment_score)
# Show results
st.subheader('Analysis Results')
# Display sentiment distribution
sentiment_counts = df['sentiment'].value_counts()
st.bar_chart(sentiment_counts)
# Display the dataframe with results
st.write(df[[text_column, 'sentiment', 'sentiment_score']])
# Download results
csv = df.to_csv(index=False)
st.download_button(
label="Download results as CSV",
data=csv,
file_name="sentiment_analysis_results.csv",
mime="text/csv"
)
else:
st.error("Could not find a text column in the CSV file. Please ensure your CSV has a column named 'text', 'tweet', or 'content'.")
elif tweet_text:
st.subheader('Analysis Results')
# Process the input text
processed_text = preprocess_text(tweet_text)
sentiment = analyze_sentiment(processed_text)
sentiment_score = get_sentiment_score(processed_text)
# Display results
col1, col2 = st.columns(2)
with col1:
st.metric("Sentiment", sentiment)
with col2:
st.metric("Sentiment Score", round(sentiment_score, 3))
# Show processed text
st.subheader('Processed Text')
st.write(processed_text)
# Add information about the analysis
st.sidebar.title('About')
st.sidebar.info('''
This app performs sentiment analysis on tweets using TextBlob.
- Sentiment scores range from -1 (most negative) to 1 (most positive)
- Text preprocessing includes:
- Removing URLs, mentions, and hashtags
- Converting to lowercase
- Removing punctuation and numbers
- Removing stopwords
- Lemmatization
''')
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?