Twitter Sentiment Analysis with Spatial Visualisation and Analysis

Methodology

Second approach using: MongoDb, VADER, matplotlib and DASH

Data Collection

if i in listofcountries:
if (len(i) == 2):
country = pycountry.countries.get(alpha_2=i.upper())
i = puncremover(country.name.lower())
elif(len(i) == 3):
country = pycountry.countries.get(alpha_3=i.upper())
i = puncremover(country.name.lower())
collection = db[i]
# print(i, status.text)
try:
collection.insert_one(status._json)
except Exception as e:
print(e)
pass
punc = '''!-[]{};:'"\,<>./?@#$%^&*_~'''def puncremover(test_str):
for ele in test_str:
if ele in punc:
test_str = test_str.replace(ele, "")
return test_str

Sentiment Analysis and Visualisation

fig = px.choropleth(df, geojson=countries, locations='country code', color='score',
color_continuous_scale="sunset",
range_color=(0, 1),
scope="world",
labels={'score':'sentiment score'},
projection="orthographic",
hover_data=["number of tweets","country name"]
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
topics = Untitled.topicsapp = dash.Dash()app.layout = html.Div([
html.H2("Sentiment Analysis"),
html.Div(
[
dcc.Dropdown(
id="Topics",
options=[{
'label': i,
'value': i
} for i in topics],
value='All Topics'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Graph(id='funnel-graph'),
])
@app.callback(
dash.dependencies.Output('funnel-graph', 'figure'),
[dash.dependencies.Input('Topics', 'value')])
def update_graph(Topics):
if Topics in topics:
df_plot = Untitled.databases[Topics]
else:
df_plot = Untitled.databases['coronavirus_covid']
pv = pd.pivot_table(
df_plot,
index=['country'],
)
trace1 = go.Bar(x=pv.index, y=pv['positive'], name='Positive')
trace2 = go.Bar(x=pv.index, y=pv['neutral'], name='Neutral')
trace3 = go.Bar(x=pv.index, y=pv['negative'], name='Negative')
return {
'data': [trace1, trace2, trace3],
'layout':
go.Layout(
title='Sentiments for {}'.format(Topics),
barmode='stack')
}
if __name__ == '__main__':
app.run_server(debug=True)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store