-
Notifications
You must be signed in to change notification settings - Fork 21
/
Senti.py
142 lines (112 loc) · 4.72 KB
/
Senti.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import csv
import re
import pandas as pd
import nltk
nltk.download('vader_lexicon')
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import plotly.express as px
import plotly.graph_objects as go
from colorama import Fore, Style
from typing import Dict
import streamlit as st
def extract_video_id(youtube_link):
video_id_regex = r"^(?:https?:\/\/)?(?:www\.)?(?:youtube\.com\/watch\?v=|youtu.be\/)([a-zA-Z0-9_-]{11})"
match = re.search(video_id_regex, youtube_link)
if match:
video_id = match.group(1)
return video_id
else:
return None
def analyze_sentiment(csv_file):
# Initialize the sentiment analyzer
sid = SentimentIntensityAnalyzer()
# Read in the YouTube comments from the CSV file
comments = []
with open(csv_file, 'r', encoding='utf-8-sig') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
comments.append(row['Comment'])
# Count the number of neutral, positive, and negative comments
num_neutral = 0
num_positive = 0
num_negative = 0
for comment in comments:
sentiment_scores = sid.polarity_scores(comment)
if sentiment_scores['compound'] == 0.0:
num_neutral += 1
elif sentiment_scores['compound'] > 0.0:
num_positive += 1
else:
num_negative += 1
# Return the results as a dictionary
results = {'num_neutral': num_neutral, 'num_positive': num_positive, 'num_negative': num_negative}
return results
def bar_chart(csv_file: str) -> None:
# Call analyze_sentiment function to get the results
results: Dict[str, int] = analyze_sentiment(csv_file)
# Get the counts for each sentiment category
num_neutral = results['num_neutral']
num_positive = results['num_positive']
num_negative = results['num_negative']
# Create a Pandas DataFrame with the results
df = pd.DataFrame({
'Sentiment': ['Positive', 'Negative', 'Neutral'],
'Number of Comments': [num_positive, num_negative, num_neutral]
})
# Create the bar chart using Plotly Express
fig = px.bar(df, x='Sentiment', y='Number of Comments', color='Sentiment',
color_discrete_sequence=['#87CEFA', '#FFA07A', '#D3D3D3'],
title='Sentiment Analysis Results')
fig.update_layout(title_font=dict(size=20))
# Show the chart
st.plotly_chart(fig, use_container_width=True)
def plot_sentiment(csv_file: str) -> None:
# Call analyze_sentiment function to get the results
results: Dict[str, int] = analyze_sentiment(csv_file)
# Get the counts for each sentiment category
num_neutral = results['num_neutral']
num_positive = results['num_positive']
num_negative = results['num_negative']
# Plot the pie chart
labels = ['Neutral', 'Positive', 'Negative']
values = [num_neutral, num_positive, num_negative]
colors = ['yellow', 'green', 'red']
fig = go.Figure(data=[go.Pie(labels=labels, values=values, textinfo='label+percent',
marker=dict(colors=colors))])
fig.update_layout(title={'text': 'Sentiment Analysis Results', 'font': {'size': 20, 'family': 'Arial', 'color': 'grey'},
'x': 0.5, 'y': 0.9},
font=dict(size=14))
st.plotly_chart(fig)
def create_scatterplot(csv_file: str, x_column: str, y_column: str) -> None:
# Load data from CSV
data = pd.read_csv(csv_file)
# Create scatter plot using Plotly
fig = px.scatter(data, x=x_column, y=y_column, color='Category')
# Customize layout
fig.update_layout(
title='Scatter Plot',
xaxis_title=x_column,
yaxis_title=y_column,
font=dict(size=18)
)
# Display plot in Streamlit
st.plotly_chart(fig, use_container_width=True)
def print_sentiment(csv_file: str) -> None:
# Call analyze_sentiment function to get the results
results: Dict[str, int] = analyze_sentiment(csv_file)
# Get the counts for each sentiment category
num_neutral = results['num_neutral']
num_positive = results['num_positive']
num_negative = results['num_negative']
# Determine the overall sentiment
if num_positive > num_negative:
overall_sentiment = 'POSITIVE'
color = Fore.GREEN
elif num_negative > num_positive:
overall_sentiment = 'NEGATIVE'
color = Fore.RED
else:
overall_sentiment = 'NEUTRAL'
color = Fore.YELLOW
# Print the overall sentiment in color
print('\n'+ Style.BRIGHT+ color + overall_sentiment.upper().center(50, ' ') + Style.RESET_ALL)