# ---
# 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
# ---

# +
# Load data
import pandas as pd

col_names = ['sentiment','id','date','query_string','user','text']
data_path = 'training.1600000.processed.noemoticon.csv'

tweet_data = pd.read_csv(data_path, header=None, names=col_names, encoding="ISO-8859-1").sample(frac=1) # .sample(frac=1) shuffles the data
tweet_data = tweet_data[['sentiment', 'text']] # Disregard other columns
print(tweet_data.head())

# + colab={} colab_type="code" id="tUX3YexFy_kz"
# Preprocess function
import re
allowed_chars = ' AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789~`!@#$%^&*()-=_+[]{}|;:",./<>?'
punct = '!?,.@#'
maxlen = 280

def preprocess(text):
    return ''.join([' ' + char + ' ' if char in punct else char for char in [char for char in re.sub(r'http\S+', 'http', text, flags=re.MULTILINE) if char in allowed_chars]])[:maxlen]


# -

# Apply preprocessing
tweet_data['text'] = tweet_data['text'].apply(preprocess)

# Put __label__ in front of each sentiment
tweet_data['sentiment'] = '__label__' + tweet_data['sentiment'].astype(str)

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} colab_type="code" id="teQJsIJay_k1" outputId="5b3c28ae-05b4-4cee-e2f9-df50489c23ca"
# Save data
import os

# Create directory for saving data if it does not already exist
data_dir = './processed-data'
if not os.path.isdir(data_dir):
    os.mkdir(data_dir)

# Save a percentage of the data (you could also only load a fraction of the data instead)
amount = 0.125

tweet_data.iloc[0:int(len(tweet_data)*0.8*amount)].to_csv(data_dir + '/train.csv', sep='\t', index=False, header=False)
tweet_data.iloc[int(len(tweet_data)*0.8*amount):int(len(tweet_data)*0.9*amount)].to_csv(data_dir + '/test.csv', sep='\t', index=False, header=False)
tweet_data.iloc[int(len(tweet_data)*0.9*amount):int(len(tweet_data)*1.0*amount)].to_csv(data_dir + '/dev.csv', sep='\t', index=False, header=False)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="CRbruBmilCOt" outputId="5151d827-f90d-4ce6-f6f8-59b02be7f74f"
# Memory management
del tweet_data
import gc; gc.collect()

# + colab={"base_uri": "https://localhost:8080/", "height": 292} colab_type="code" id="BR5iIWRgy_k5" outputId="0e1616ab-b1c5-4007-d11d-5ff522fea812"
# Load the data into Corpus format
from flair.data_fetcher import NLPTaskDataFetcher
from pathlib import Path

corpus = NLPTaskDataFetcher.load_classification_corpus(Path(data_dir), test_file='test.csv', dev_file='dev.csv', train_file='train.csv')

# + colab={"base_uri": "https://localhost:8080/", "height": 69} colab_type="code" id="_6XPFKdqy_k7" outputId="68c8caa5-0b73-4e00-bb1d-53c7d8b575d8"
# Make label dictionary
label_dict = corpus.make_label_dictionary()

# + colab={"base_uri": "https://localhost:8080/", "height": 350} colab_type="code" id="f-nIq7k6y_k9" outputId="2ea897cc-175b-4830-894c-48227d50f46f"
# Load embeddings
from flair.embeddings import WordEmbeddings, FlairEmbeddings

word_embeddings = [WordEmbeddings('glove'),
#                    FlairEmbeddings('news-forward'),
#                    FlairEmbeddings('news-backward')
                  ]

# + colab={} colab_type="code" id="WiVHs1aRy_k_"
# Initialize embeddings
from flair.embeddings import DocumentRNNEmbeddings

document_embeddings = DocumentRNNEmbeddings(word_embeddings, hidden_size=512, reproject_words=True, reproject_words_dimension=256)

# + colab={} colab_type="code" id="y7x4mV67BPAh"
# Create model
from flair.models import TextClassifier

classifier = TextClassifier(document_embeddings, label_dictionary=label_dict)

# + colab={} colab_type="code" id="TRCGx_VdA-5F"
# Create model trainer
from flair.trainers import ModelTrainer

trainer = ModelTrainer(classifier, corpus)

# + colab={"base_uri": "https://localhost:8080/", "height": 868} colab_type="code" id="AHgC91YoA-2J" outputId="a613d884-a079-480c-97bc-4ddd500bf9b6"
# Train the model
trainer.train('model-saves',
              learning_rate=0.1,
              mini_batch_size=32,
              anneal_factor=0.5,
              patience=8,
              max_epochs=200)

# + colab={} colab_type="code" id="ozdqd8KiA-zZ"
# Load the model and make predictions
from flair.data import Sentence

classifier = TextClassifier.load('model-saves/final-model.pt')

pos_sentence = Sentence(preprocess('I love Python!'))
neg_sentence = Sentence(preprocess('Python is the worst!'))

classifier.predict(pos_sentence)
classifier.predict(neg_sentence)

print(pos_sentence.labels, neg_sentence.labels)

StarcoderdataJupyterScriptsDedupFiltered