jmansfield89/Tweet_NLP_Sentiment_Analysis
2
1# LIBRARY IMPORTS2from streamlit.web import cli as stcli3import pandas as pd4import sys5from streamlit import cli as stcli6import plotly.express as px7 8 9# GLOBAL VARIABLES10# Create variable for Tweet being analyzed in this app11tweet_url = 'https://twitter.com/Meta/status/1453795115701440524'12 13 14# FUNCTIONS15def data_import():16 """17 Imports data from "df_redacted.csv" as a dataframe.18 """19 df_redacted = pd.read_csv('df_redacted.csv')20 return df_redacted21 22 23def data_manipulation(df_redacted):24 """25 Manipulates the data imported from the CSV file to prepare for bar chart.26 """27 28 # Create new dataframe, reset the index, and rename columns29 sentiment_counts = pd.DataFrame(df_redacted['sentiment_score'].value_counts(dropna=False))30 sentiment_counts = sentiment_counts.reset_index()31 sentiment_counts.columns = ['Sentiment', 'Count']32 33 # Find sentiment category with the highest count34 sentiment = sentiment_counts.loc[sentiment_counts['Count'].idxmax(), 'Sentiment']35 36 return sentiment_counts, sentiment37 38 39def display_header(sentiment):40 """41 Displays the header section of the app.42 """43 st.header('This app runs a sentiment analysis of the replies to a Facebook Tweet '44 'announcing their rebranding to Meta.')45 st.header('RESULT: {}'.format(sentiment))46 47 48def display_chart(sentiment_counts):49 """50 Displays the chosen chart for the data.51 """52 # Display count for each sentiment category53 fig = px.bar(sentiment_counts,54 x='Sentiment',55 y='Count',56 title='Tweet Replies Count by Sentiment Category')57 fig.update_layout(title_x=0.5)58 st.plotly_chart(fig, use_container_width=True)59 60 61def display_footer(tweet_url, sentiment):62 """63 Displays the footer section of the app.64 """65 st.markdown('**Objective:** Understand public reaction of a Tweet by analyzing the sentiment of each reply.')66 st.markdown('**Analysis:** This app runs sentiment analysis on 10,948 replies to a Facebook Tweet announcing '67 'their rebranding to Meta on 10/28/2021. Link to Tweet: {}'.format(tweet_url))68 st.markdown('**Results:** Most frequent sentiment category for the replies to this Tweet: **{}**'.format(sentiment))69 st.markdown('**Notes:** ')70 st.markdown('- The VADER model was used to analyze the sentiment of each reply: '71 'https://github.com/cjhutto/vaderSentiment')72 st.markdown('- Due to Twitter developer policies, I am not able to share the data set of downloaded Tweet replies '73 'so my DATA EXTRACTION and DATA CLEANSING steps are not shown at this time but will be added soon!')74 st.markdown('**Plans for Version 2.0:**')75 st.markdown('- Formulate method for cleaning Tweet replies, such as removing those that are from bots or are spam.')76 st.markdown('- Analyze the sentiment of replies using the BERTweet model, which would be more appropriate for this '77 'project since it was trained on a corpus of Tweets: https://github.com/VinAIResearch/BERTweet')78 79 80def main():81 """82 Main function for the app which calls all other functions to display the app.83 """84 # DATA IMPORT85 df_redacted = data_import()86 87 # DATA MANIPULATION88 sentiment_counts, sentiment = data_manipulation(df_redacted)89 90 # DISPLAY DATA91 display_header(sentiment)92 display_chart(sentiment_counts)93 display_footer(tweet_url, sentiment)94 95 96if __name__ == '__main__':97 if st._is_running_with_streamlit:98 main()99 else:100 sys.argv = ['streamlit', 'run', sys.argv[0]]101 sys.exit(stcli.main())102 