# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import tensorflow as tf
import tensorflow_hub as hub

import matplotlib.pyplot as plt
import pandas as pd
import re
import numpy as np
# -

data = pd.read_csv('tweets.csv',encoding='latin-1',names=['polarity','id','date','query','username','text'])

data = data.sample(frac = 1,random_state=25) 

data.head()


def tweet_cleaner(text):
    pat1 = r'@[A-Za-z0-9]+'
    pat2 = r'https?://[A-Za-z0-9./]+'
    combined_pat = r'|'.join((pat1, pat2))
    stripped = re.sub(combined_pat, '', text)
    try:
        clean = stripped.decode("utf-8-sig").replace(u"\ufffd", "?")
    except:
        clean = stripped
    letters_only = re.sub("[^a-zA-Z]", " ", clean)
    lower_case = letters_only.lower()
    # During the letters_only process two lines above, it has created unnecessay white spaces,
    # I will tokenize and join together to remove unneccessary white spaces
    return lower_case.strip()


v_cleaner = np.vectorize(tweet_cleaner)
texts = v_cleaner(data['text'].values)
texts

labels = data['polarity'].values == 4
labels

BATCH_SIZE = 128
AUTOTUNE = tf.data.AUTOTUNE
examples = len(data.index)
train_val = int(np.ceil(0.6*examples))
val_test = int(np.ceil(0.8*examples))

train_ds = tf.data.Dataset.from_tensor_slices((texts[:train_val],labels[:train_val])).batch(BATCH_SIZE).prefetch(AUTOTUNE)
val_ds = tf.data.Dataset.from_tensor_slices((texts[train_val:val_test],labels[train_val:val_test])).batch(BATCH_SIZE).prefetch(AUTOTUNE)
test_ds = tf.data.Dataset.from_tensor_slices((texts[val_test:],labels[val_test:])).batch(BATCH_SIZE).prefetch(AUTOTUNE)

for text_batch, label_batch in train_ds.take(1):
  for i in range(5):
    print(f'Review: {text_batch.numpy()[i]}')
    label = label_batch.numpy()[i]
    print(label)

hub_layer = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/nnlm-en-dim128-with-normalization/1", output_shape=[128],
                           input_shape=[], dtype=tf.string)



model = tf.keras.Sequential([
    hub_layer,
#     tf.keras.layers.Dense(64, activation='relu'),
#     tf.keras.layers.Dense(128,activation='relu'),
#     tf.keras.layers.Dense(1, activation='sigmoid')
])

num_epochs = 50
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
                                 optimizer=tf.keras.optimizers.Adam(),
                                 metrics=['accuracy'])
model.summary()
# history = model.fit(train_ds, epochs=num_epochs, 
#                       validation_data=val_ds)





dataset_name = 'sentiment140'
saved_model_path = './{}_hub'.format(dataset_name.replace('/', '_'))
model.save(saved_model_path, include_optimizer=False)



StarcoderdataJupyterScriptsDedupFiltered