|
import pandas as pd |
|
import numpy as np |
|
import re |
|
import snscrape.modules.twitter as sntwitter |
|
from transformers import pipeline |
|
import plotly.express as px |
|
|
|
|
|
def get_tweets(username, length=10, option = None): |
|
|
|
query = "@traveloka -filter:links filter:replies" |
|
if option == "Advanced": |
|
query = username |
|
tweets = [] |
|
|
|
|
|
for i,tweet in enumerate(sntwitter.TwitterSearchScraper(query).get_items()): |
|
if i>=length: |
|
break |
|
tweets.append([tweet.content]) |
|
|
|
|
|
tweets_df = pd.DataFrame(tweets, columns=["content"]) |
|
tweets_df['content'] = tweets_df['content'].str.replace('@[^\s]+','') |
|
tweets_df['content'] = tweets_df['content'].str.replace('#[^\s]+','') |
|
tweets_df['content'] = tweets_df['content'].str.replace('http\S+','') |
|
tweets_df['content'] = tweets_df['content'].str.replace('pic.twitter.com\S+','') |
|
tweets_df['content'] = tweets_df['content'].str.replace('RT','') |
|
tweets_df['content'] = tweets_df['content'].str.replace('amp','') |
|
|
|
tweets_df['content'] = tweets_df['content'].str.replace('[^\w\s#@/:%.,_-]', '', flags=re.UNICODE) |
|
|
|
|
|
tweets_df['content'] = tweets_df['content'].str.strip() |
|
|
|
|
|
tweets_df['content'] = tweets_df['content'].str.replace('\s+', ' ') |
|
|
|
|
|
tweets_df = tweets_df[tweets_df['content'] != ''] |
|
return tweets_df |
|
|
|
|
|
def get_sentiment(df): |
|
|
|
classifier = pipeline("sentiment-analysis",model = "indobert") |
|
df['sentiment'] = df['content'].apply(lambda x: classifier(x)[0]['label']) |
|
|
|
cols = df.columns.tolist() |
|
cols = cols[-1:] + cols[:-1] |
|
df = df[cols] |
|
|
|
return df |
|
|
|
def get_bar_chart(df): |
|
df= df.groupby(['sentiment']).count().reset_index() |
|
|
|
|
|
fig = px.bar(df, x="sentiment", y="content", color="sentiment",text = "content", color_discrete_map={"positif": "#00cc96", "negatif": "#ef553b","netral": "#636efa"}) |
|
|
|
fig.update_layout(showlegend=False) |
|
|
|
fig.update_layout(margin=dict(t=0, b=100, l=0, r=0)) |
|
|
|
|
|
fig.update_traces(textposition='outside') |
|
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide') |
|
|
|
|
|
fig.update_yaxes(title_text='Jumlah Komentar') |
|
|
|
return fig |
|
|