-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtag.py
240 lines (208 loc) · 8.17 KB
/
hashtag.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import json
import string
import nltk
import pandas as pd
import plotly.graph_objects as go
import plotly
import re
df_general = pd.read_csv(
'https://raw.githubusercontent.com/FabioPalliparambil98/covid-dataset/main/combined_generaltweets.csv')
df_restriction = pd.read_csv(
'https://raw.githubusercontent.com/FabioPalliparambil98/covid-dataset/main/combined_restrictiontweets.csv')
df_vaccination = pd.read_csv(
'https://raw.githubusercontent.com/FabioPalliparambil98/covid-dataset/main/combined_vaccinationtweets.csv')
# df_general = pd.read_csv(
# 'https://raw.githubusercontent.com/FabioPalliparambil98/COVID-19_General_Tweets/main/combined_generaltweets.csv')
# df_restriction = pd.read_csv(
# 'https://raw.githubusercontent.com/FabioPalliparambil98/COVID-19_Restriction_Tweets/main/combined_restrictiontweets.csv')
# df_vaccination = pd.read_csv(
# 'https://raw.githubusercontent.com/FabioPalliparambil98/COVID-19_Vaccination_Tweets/main/combined_vaccinationtweets.csv')
"""
Function for collecting all the Hashtag
"""
def collectHashtag(x):
hashtags = []
for i in x:
ht = re.findall(r"#(\w+)", i)
hashtags.append(ht)
return hashtags
"""
Function for collecting hashed into multiple dataframe
"""
def clean_hastag(text):
text = "".join([char for char in text if char not in string.punctuation])
text = text.lower()
return text
def hastag_dataframe(df):
HT_general = collectHashtag(df['text'])
HT_general = sum(HT_general, [])
general_tag = nltk.FreqDist(HT_general)
hashtag_general = pd.DataFrame({'Hashtag': list(general_tag.keys()), 'Count': list(general_tag.values())})
hashtag_general['Hashtag'] = hashtag_general['Hashtag'].apply(lambda x: clean_hastag(x))
hashtag_general['Hashtag'] = hashtag_general['Hashtag'].drop_duplicates()
hashtag_general = hashtag_general.dropna()
hashtag_general = hashtag_general.nlargest(columns='Count', n=15)
return hashtag_general
df_general_hash_tag = hastag_dataframe(df_general)
df_restriction_hash_tag = hastag_dataframe(df_restriction)
df_vaccination_hash_tag = hastag_dataframe(df_vaccination)
def create_plot(df_general_hashtag, df_restriction_hashtag, df_vaccination_hashtag):
scatter_general = [go.Bar(
x=df_general_hashtag['Hashtag'],
y=df_general_hashtag['Count'],
marker_color='lightsalmon',
text=df_general_hashtag['Count'],
textposition='auto'
)
]
graph_general = go.Figure(scatter_general)
graph_general.update_layout(
title='General Frequency Tweet Count',
xaxis_tickfont_size=14,
yaxis=dict(
title='Word Frequency Count',
titlefont_size=16,
tickfont_size=14,
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
xaxis_tickangle=-45,
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
scatter_restriction = [go.Bar(
x=df_restriction_hashtag['Hashtag'],
y=df_restriction_hashtag['Count'],
marker_color='lightsalmon',
text=df_restriction_hashtag['Count'],
textposition='auto'
)
]
graph_restriction = go.Figure(scatter_restriction)
graph_restriction.update_layout(
title='restriction Frequency Tweet Count',
xaxis_tickfont_size=14,
yaxis=dict(
title='Word Frequency Count',
titlefont_size=16,
tickfont_size=14,
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
xaxis_tickangle=-45,
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
scatter_vaccination = [go.Bar(
x=df_vaccination_hashtag['Hashtag'],
y=df_vaccination_hashtag['Count'],
marker_color='lightsalmon',
text=df_vaccination_hashtag['Count'],
textposition='auto'
)
]
graph_vaccination = go.Figure(scatter_vaccination)
graph_vaccination.update_layout(
title='General Frequency Tweet Count',
xaxis_tickfont_size=14,
yaxis=dict(
title='Word Frequency Count',
titlefont_size=16,
tickfont_size=14,
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
xaxis_tickangle=-45,
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1 # gap between bars of the same location coordinate.
)
""" Scatter """
# Create a trace
scatter_line_genreal = [go.Scatter(
x=df_general_hashtag['Hashtag'],
y=df_general_hashtag['Count'],
mode='lines+markers',
name='lines+markers',
marker_color='rgba(255, 182, 193, .9)'
# mode='markers'
)]
""" Scatter """
# Create a trace
scatter_line_restriction = [go.Scatter(
x=df_restriction_hashtag['Hashtag'],
y=df_restriction_hashtag['Count'],
mode='lines+markers',
name='lines+markers',
marker_color='rgba(255, 182, 193, .9)'
# mode='markers'
)]
""" Scatter """
# Create a trace
scatter_line_vaccination = [go.Scatter(
x=df_vaccination_hashtag['Hashtag'],
y=df_vaccination_hashtag['Count'],
mode='lines+markers',
name='lines+markers',
marker_color='rgba(255, 182, 193, .9)'
# mode='markers'
)]
""" all toghether"""
all_scatter_line = go.Figure()
all_scatter_line.add_trace(
go.Bar(
x=df_general_hashtag['Hashtag'],
y=df_general_hashtag['Count'],
name='COVID-19 General Hashtag'
))
all_scatter_line.add_trace(
go.Bar(
x=df_vaccination_hashtag['Hashtag'],
y=df_vaccination_hashtag['Count'],
name='COVID-19 Restriction Hashtag'
))
all_scatter_line.add_trace(
go.Bar(
x=df_restriction_hashtag['Hashtag'],
y=df_restriction_hashtag['Count'],
name='COVID-19 Vaccination Hashtag'
))
Scatters = go.Figure(data=go.Scatter(
x=df_general_hashtag['Hashtag'],
y=df_general_hashtag['Count'],
mode='markers',
marker=dict(size=[100, 95, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30],
color=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
))
# labels = ['Oxygen', 'Hydrogen', 'Carbon_Dioxide', 'Nitrogen']
# values = [4500, 2500, 1053, 500]
colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen']
Pie = go.Figure(data=[go.Pie(labels=df_general_hashtag['Hashtag'],
values=df_general_hashtag['Count'],
textinfo='label+percent',
insidetextorientation='radial',
hole=.2)])
general_JSON = json.dumps(scatter_general, cls=plotly.utils.PlotlyJSONEncoder)
restriction_JSON = json.dumps(scatter_restriction, cls=plotly.utils.PlotlyJSONEncoder)
vaccination_JSON = json.dumps(scatter_vaccination, cls=plotly.utils.PlotlyJSONEncoder)
line_genreal_JSON = json.dumps(scatter_line_genreal, cls=plotly.utils.PlotlyJSONEncoder)
line_restriction_JSON = json.dumps(scatter_line_restriction, cls=plotly.utils.PlotlyJSONEncoder)
line_vaccination_JSON = json.dumps(scatter_line_vaccination, cls=plotly.utils.PlotlyJSONEncoder)
all_scatter_lineJSON = json.dumps(all_scatter_line, cls=plotly.utils.PlotlyJSONEncoder)
Scatters_JSON = json.dumps(Scatters, cls=plotly.utils.PlotlyJSONEncoder)
Pie_json = json.dumps(Pie, cls=plotly.utils.PlotlyJSONEncoder)
return general_JSON, restriction_JSON, vaccination_JSON, line_genreal_JSON, line_restriction_JSON, line_vaccination_JSON, all_scatter_lineJSON, Scatters_JSON, Pie_json