|
import pandas as pd |
|
import urllib |
|
|
|
|
|
|
|
def clean_text(text): |
|
text = text.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ') |
|
|
|
new_text = [] |
|
for t in text.split(): |
|
|
|
t = '@user' if t.startswith('@') and len(t) > 1 and t.replace( |
|
'@', '').lower() not in verified_users else t |
|
t = '{URL}' if t.startswith('http') else t |
|
new_text.append(t) |
|
|
|
return ' '.join(new_text) |
|
|
|
|
|
train = pd.read_csv('./emotion/2018-E-c-En-train.txt', sep='\t') |
|
validation = pd.read_csv('./emotion/2018-E-c-En-dev.txt', sep='\t') |
|
test = pd.read_csv('./emotion/2018-E-c-En-test-gold.txt', sep='\t') |
|
|
|
sem_emotions = train.columns.difference(['ID', 'Tweet', 'split', 'dataset']) |
|
|
|
|
|
with open('../data/tweet_emotion/map.txt', 'w') as f: |
|
for idx, em in enumerate(sem_emotions): |
|
f.write(f'{em},{idx}\n') |
|
|
|
|
|
cols_to_keep = ['text', 'gold_label_list'] |
|
|
|
verified_users = urllib.request.urlopen( |
|
'https://raw.githubusercontent.com/cardiffnlp/timelms/main/data/verified_users.v091122.txt').readlines() |
|
verified_users = [x.decode().strip('\n').lower() for x in verified_users] |
|
|
|
|
|
train['gold_label_list'] = train[sem_emotions].values.tolist() |
|
train['text'] = train['Tweet'] |
|
train['text'] = train['text'].apply(clean_text) |
|
train[cols_to_keep].to_json('../data/tweet_emotion/train.jsonl', lines=True, orient='records') |
|
|
|
validation['gold_label_list'] = validation[sem_emotions].values.tolist() |
|
validation['text'] = validation['Tweet'] |
|
validation['text'] = validation['text'].apply(clean_text) |
|
validation[cols_to_keep].to_json('../data/tweet_emotion/validation.jsonl', lines=True, orient='records') |
|
|
|
test['gold_label_list'] = test[sem_emotions].values.tolist() |
|
test['text'] = test['Tweet'] |
|
test['text'] = test['text'].apply(clean_text) |
|
test[cols_to_keep].to_json('../data/tweet_emotion/test.jsonl', lines=True, orient='records') |
|
|