Skip to content

Instantly share code, notes, and snippets.

@TiGaI
Created May 28, 2020 02:37
Show Gist options
  • Save TiGaI/94ff89c6d8bbdffe4f67b9666c657fa0 to your computer and use it in GitHub Desktop.
Save TiGaI/94ff89c6d8bbdffe4f67b9666c657fa0 to your computer and use it in GitHub Desktop.
import plotly
import plotly.graph_objs as go
import pandas as pd
import numpy as np
import json
import nltk
#for NLP purpose
from nltk.corpus import stopwords
nltk.download('stopwords')
def create_plot(client):
query = """
SELECT *
FROM tweetScraper.tweet
"""
df = client.query(query).to_dataframe()
stop = stopwords.words('english')
#convert to lower
df['newTweet'] = df['tweet'].apply(lambda x: ' '.join([word for word in x.lower().split() if word not in (stop)]))
#Remove Punctuations
df['newTweetRemove'] = df['newTweet'].str.replace('[^\w\s]','')
#Count Words
countFreq = df.newTweetRemove.str.split(expand=True).stack().value_counts()[:100]
bar = [go.Bar(
x=countFreq.index,
y=countFreq.values,
marker_color='lightsalmon',
text=countFreq.values,
textposition='auto'
)
]
graph = go.Figure(bar)
graph.update_layout(
title='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.
)
# Create a trace
table = go.Figure([go.Table(
header=dict(
values=df.columns[1:6],
font=dict(size=12),
align="left"
),
cells=dict(
values=[df[k].tolist() for k in df.columns[1:6]],
align = "left")
)
])
table.update_layout(
autosize=True,
)
graphJSON = json.dumps(graph, cls=plotly.utils.PlotlyJSONEncoder)
tableJSON = json.dumps(table, cls=plotly.utils.PlotlyJSONEncoder)
return graphJSON, tableJSON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment