-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdata.py
135 lines (128 loc) · 5.02 KB
/
data.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import csv
import re
from utils import majority_voting
import numpy as np
import random
#from gensim.models import KeyedVectors
def get_data(action, dataset):
with open('Data/Wikipedia/' + dataset +'/' + dataset +'_annotated_comments.tsv', encoding='utf-8') as tsvfile:
i = 0
data_comments = []
data_rev_ids = []
reader = csv.reader(tsvfile, delimiter='\t')
'''for row in reader:
if i > 0:
if action is 'train':
if row[6] == 'train':
data_comments.append(row[1])
data_rev_ids.append(row[0])
elif action is 'dev':
if row[6] == 'dev':
data_comments.append(row[1])
data_rev_ids.append(row[0])
elif action is 'test':
if row[6] == 'test':
data_comments.append(row[1])
data_rev_ids.append(row[0])
else:
print("action not found!")
i += 1'''
for row in reader:
if i > 0:
if action is 'train':
if row[6] == 'train':
data_comments.append(row[1])
data_rev_ids.append(row[0])
elif action is 'test':
if row[6] == 'dev' or row[6] == 'test':
data_comments.append(row[1])
data_rev_ids.append(row[0])
else:
print("action not found!")
i += 1
return data_comments, data_rev_ids
'''def get_data_test_validation(action, dataset):
with open('Data/Wikipedia/' + dataset +'/' + dataset +'_annotated_comments.tsv', encoding='utf-8') as tsvfile:
i = 0
data_comments = []
data_rev_ids = []
validate_data_comments = []
validate_data_rev_ids = []
reader = csv.reader(tsvfile, delimiter='\t')
for row in reader:
if i > 0:
if action is 'train':
if row[6] == 'train':
data_comments.append(row[1])
data_rev_ids.append(row[0])
elif action is 'dev' or action is 'test':
if row[6] == 'dev':
r = random.randint(1,101)
if r <= 25:
validate_data_comments.append(row[1])
validate_data_rev_ids.append(row[0])
else:
data_comments.append(row[1])
data_rev_ids.append(row[0])
elif row[6] == 'test':
data_comments.append(row[1])
data_rev_ids.append(row[0])
else:
print("action not found!")
i += 1
#print (row[1])
return validate_data_comments, validate_data_rev_ids, data_comments, data_rev_ids'''
def get_train_validation_data(dataset):
with open('Data/Wikipedia/' + dataset +'/' + dataset +'_annotated_comments.tsv', encoding='utf-8') as tsvfile:
i = 0
train_data_comments = []
train_data_rev_ids = []
validate_data_comments = []
validate_data_rev_ids = []
reader = csv.reader(tsvfile, delimiter='\t')
for row in reader:
if i > 0:
if row[6] == 'train':
r = random.randint(1,101)
if r > 5: # for attack and aggression it is 10
train_data_comments.append(row[1])
train_data_rev_ids.append(row[0])
else:
validate_data_comments.append(row[1])
validate_data_rev_ids.append(row[0])
i += 1
return train_data_comments, train_data_rev_ids, validate_data_comments, validate_data_rev_ids
def get_annotations(dataset):
rev_id_map = {}
with open('Data/Wikipedia/' + dataset +'/' + dataset +'_annotations.tsv', encoding='utf-8') as tsvfile:
i = 0
reader = csv.reader(tsvfile, delimiter='\t')
for row in reader:
if i > 0:
if rev_id_map.get(row[0]) is None:
val = []
else:
val = rev_id_map.get(row[0])
if dataset == 'toxicity':
val.append(row[2])
elif dataset == 'attack':
val.append(row[6])
elif dataset == 'aggression':
val.append(row[2])
rev_id_map[row[0]] = val
i += 1
return rev_id_map
def generate_ylabels(rev_ids, rev_id_map):
count = 0
y_label = []
#print(len(rev_ids))
for ids in rev_ids:
#for key, value in rev_id_map.items():
value = rev_id_map[ids]
#print(value)
is_toxic = majority_voting(value)
if is_toxic == 1:
count += 1
y_label.append(is_toxic)
print(count)
return y_label