text_html_output_2.png
from plotly.subplots import make_subplots

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.graph_objects as go

import translators as ts

df_new = pd.read_json('/kaggle/input/indonesiandata/emotion_id_tweets.json', lines=True)

ex_label = []
for i in range(df_new['annotation'].shape[0]):
    if df_new['annotation'][i]['labels'] == []:
        ex_label.append('no_emotion')
    else:
        ex_label.append(df_new.annotation[i]['labels'][0])

df = pd.DataFrame()
df['content'] = df_new['content']
df['emotion'] = ex_label

df['len'] = df['content'].astype(str).apply(len)

df = df.drop_duplicates(subset='content', keep='first')
df.shape

def missing_value_of_data(data):
    total = data.isnull().sum().sort_values(ascending=False)
    percentage = round(total / data.shape[0] * 100, 2)
    return pd.concat([total, percentage], axis=1, keys=['Total', 'Percentage'])
missing_value_of_data(df)

def count_values_in_column(data, feature):
    total = data.loc[:, feature].value_counts(dropna=False)
    percentage = round(data.loc[:, feature].value_counts(dropna=False, normalize=True) * 100, 2)
    return pd.concat([total, percentage], axis=1, keys=['Total', 'Percentage'])
count_values_in_column(df, 'emotion')

name_of_emotion = df.emotion.unique()
value_of_emotion = list(df.emotion.value_counts())
fig = go.Figure([go.Bar(x=list(name_of_emotion), y=value_of_emotion)])
fig.update_traces(textposition='outside')
fig.update_layout(uniformtext_minsize=8, uniformtext_mode='hide')

def ngrams_top(corpus, ngram_range, n=None):
    """
    List the top n words in a vocabulary according to occurrence in a text corpus.
    """
    vec = CountVectorizer(stop_words='english', ngram_range=ngram_range).fit(corpus)
    bag_of_words = vec.transform(corpus)
    sum_words = bag_of_words.sum(axis=0)
    words_freq = [(word, sum_words[0, idx]) for word, idx in vec.vocabulary_.items()]
    words_freq = sorted(words_freq, key=lambda x: x[1], reverse=True)
    total_list = words_freq[:n]
    df = pd.DataFrame(total_list, columns=['text', 'count'])
    return df

_1gram = ngrams_top(df['content'], (1, 1), n=10)

english_1gram = []
for i in range(10):
    english_1gram.append(ts.google(_1gram['text'][i], 'auto', 'en'))

fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x=_1gram['count'][::-1], y=_1gram['text'][::-1], name='Indonesian', marker_color='rgb(55, 83, 109)', orientation='h'), row=1, col=1)
fig.add_trace(go.Bar(x=_1gram['count'][::-1], y=english_1gram[::-1], name='English', marker_color='rgb(5, 3, 243)', orientation='h'), row=1, col=2)
fig.update_layout(height=600, width=800, title_text='1 grams for Indonesian/English')
fig.show()