File size: 2,467 Bytes
52e2e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
4e3e08b
52e2e2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8ff84a
 
 
 
 
 
 
 
 
 
 
 
 
52e2e2b
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pandas as pd
from glob import glob
import urllib


# format text
def clean_text(text):
    text = text.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ')

    new_text = []
    for t in text.split():
        # MAKE SURE to check lowercase
        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)


# test set
with open('./SemEval2017-task4-test.subtask-CE.english.txt') as f:
    test_lines = f.readlines()
test = [x.split('\t') for x in test_lines]
test = pd.DataFrame(test, columns=['id', 'topic', 'gold_label', 'text'])

# validation set
fnames = ['twitter-2016dev-CE.txt', 'twitter-2016devtest-CE.txt']

validation_lines = []
for input_f in fnames:
    with open(input_f) as f:
        lines = f.readlines()
        validation_lines.extend(lines)

validation = [x.split('\t') for x in validation_lines]
validation = pd.DataFrame(
    validation, columns=['id', 'topic', 'gold_label', 'text'])

# train set
fnames = ['./twitter-2016train-CE.txt', './twitter-2016test-CE.txt']

train_lines = []
for input_f in fnames:
    with open(input_f) as f:
        lines = f.readlines()
        train_lines.extend(lines)

train = [x.split('\t') for x in train_lines]
train = pd.DataFrame(
    train, columns=['id', 'topic', 'gold_label', 'text'])

# clean text
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['text'] = train['text'].apply(clean_text)
validation['text'] = validation['text'].apply(clean_text)
test['text'] = test['text'].apply(clean_text)

# map classes to 0-4
class_map = {
    -2:0,
    -1:1,
     0:2,
     1:3,
     2:4
}
train['gold_label'] = train['gold_label'].map(class_map)
validation['gold_label'] = validation['gold_label'].map(class_map)
test['gold_label'] = test['gold_label'].map(class_map)


# save splits
cols_to_keep = ['gold_label', 'topic', 'text']
train[cols_to_keep].to_json(
    '../data/tweet_sentiment/train.jsonl', lines=True, orient='records')
validation[cols_to_keep].to_json(
    '../data/tweet_sentiment/validation.jsonl', lines=True, orient='records')
test[cols_to_keep].to_json(
    '../data/tweet_sentiment/test.jsonl', lines=True, orient='records')