-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaselines.py
More file actions
219 lines (168 loc) · 8.44 KB
/
baselines.py
File metadata and controls
219 lines (168 loc) · 8.44 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Implement four baselines for the task.
# Majority baseline: always assigns the majority class of the training data
# Random baseline: randomly assigns one of the classes. Make sure to set a random seed and average the accuracy over 100 runs.
# Length baseline: determines the class based on a length threshold
# Frequency baseline: determines the class based on a frequency threshold
import pandas as pd
from model.data_loader import DataLoader
from collections import Counter
from sklearn.metrics import accuracy_score
import random
import spacy
from wordfreq import zipf_frequency, word_frequency
nlp = spacy.load("en_core_web_sm")
# Each baseline returns predictions for the test data. The length and frequency baselines determine a threshold using the development data.
def majority_baseline(train_sentences_input, train_labels_input, test_input_input, test_labels_input):
train_labels_list = []
for element in train_labels_input:
train_labels_list.append(element.strip())
train_labels_list = " ".join(train_labels_list).split(" ")
n_occurences = train_labels_list.count("N")
c_occurences = train_labels_list.count("C")
if n_occurences > c_occurences:
majority_class = "N"
else:
majority_class = "C"
predictions = []
tokens_list = []
for instance in test_input_input:
tokens = instance.split(" ")
instance_predictions = [majority_class for t in tokens]
predictions.append(instance_predictions)
tokens_list.append(tokens)
final_predictions = [item for sublist in predictions for item in sublist]
final_tokens = [item for sublist in tokens_list for item in sublist]
test_labels_tmp = []
for element in test_labels_input:
test_labels_tmp.append(element.strip())
test_labels_list = " ".join(test_labels_tmp).split(" ")
final_df = pd.DataFrame({"tokens": final_tokens, "predictions": final_predictions})
accuracy = accuracy_score(test_labels_list, final_predictions)
return accuracy, final_df
def random_baseline(train_sentences, train_labels, test_input, test_labels):
subjects = ["N", "C"]
predictions = []
tokens_list = []
for instance in test_input:
tokens = instance.split(" ")
instance_predictions = [random.choice(subjects) for t in tokens]
predictions.append(instance_predictions)
tokens_list.append(tokens)
predictions = [item for sublist in predictions for item in sublist]
final_tokens = [item for sublist in tokens_list for item in sublist]
test_labels_list = []
for element in test_labels:
test_labels_list.append(element.strip())
test_labels_list = " ".join(test_labels_list).split(" ")
final_df = pd.DataFrame({"tokens": final_tokens, "predictions": predictions})
accuracy = accuracy_score(test_labels_list, predictions)
return accuracy, final_df
def length_baseline(train_sentences, train_labels, test_input_input, test_labels_input):
predictions = []
token_length_each = []
tokens_each = []
for instance in train_sentences:
doc = nlp(instance)
for token in doc:
if token.text not in ["\n"]:
tokens_each.append(token.text)
token_length_each.append(len(token.text))
# length > 7 "C", length < 7 "N"
predictions = []
tokens_list = []
for instance in test_input_input:
tokens = instance.split(" ")
for k in tokens:
if k not in ["\n"]:
length = len(k)
if length > 7:
label = "C"
else:
label = "N"
predicted_label = label
predictions.append(predicted_label)
tokens_list.append(k)
test_labels_list = []
for element in test_labels_input:
test_labels_list.append(element.strip())
test_labels_list = " ".join(test_labels_list).split(" ")
final_df = pd.DataFrame({"tokens": tokens_list, "predictions": predictions})
accuracy = accuracy_score(test_labels_list, predictions)
return accuracy, final_df
def frequency_baseline(train_sentences, train_labels, test_input_input, test_labels_input):
frequency_of_tokens = {}
freq_list = []
for instance in train_sentences:
doc = nlp(instance)
for token in doc:
if token.text not in ["\n"]:
frequency = zipf_frequency(token.text, 'en', wordlist='small')
frequency_of_tokens.update({token.text: frequency})
freq_list.append(frequency)
# > 6 N; <6 C
predictions = []
tokens_list = []
for instance in test_input_input:
tokens = instance.split(" ")
for k in tokens:
if k not in ["\n"]:
try:
token_freq = freq_list[k]
if token_freq > 6:
label = "N"
else:
label = "C"
except:
label = "N"
predicted_label = label
predictions.append(predicted_label)
tokens_list.append(k)
test_labels_list = []
for element in test_labels_input:
test_labels_list.append(element.strip())
test_labels_list = " ".join(test_labels_list).split(" ")
final_df = pd.DataFrame({"tokens": tokens_list, "predictions": predictions})
accuracy = accuracy_score(test_labels_list, predictions)
return accuracy, final_df
if __name__ == '__main__':
train_path = "data/preprocessed/train/"
dev_path = "data/preprocessed/val/"
test_path = "data/preprocessed/test/"
# Note: this loads all instances into memory. If you work with bigger files in the future, use an iterator instead.
with open(train_path + "sentences.txt") as sent_file:
train_sentences = sent_file.readlines()
with open(train_path + "labels.txt") as label_file:
train_labels = label_file.readlines()
with open(dev_path + "sentences.txt") as dev_file:
dev_sentences = dev_file.readlines()
with open(dev_path + "labels.txt") as dev_label_file:
dev_labels = dev_label_file.readlines()
with open(test_path + "sentences.txt") as testfile:
test_input = testfile.readlines()
with open(test_path + "labels.txt") as test_label_file:
test_labels = test_label_file.readlines()
majority_accuracy, majority_predictions = majority_baseline(train_sentences, train_labels, test_input, test_labels)
majority_accuracy_dev, majority_predictions_dev = majority_baseline(train_sentences, train_labels, dev_sentences,
dev_labels)
print("Test acuracy for majority test:", majority_accuracy)
print("Test acuracy for majority dev:", majority_accuracy_dev)
random_accuracy, random_predictions = random_baseline(train_sentences, train_labels, test_input, test_labels)
random_accuracy_dev, random_predictions_dev = random_baseline(train_sentences, train_labels, dev_sentences,
dev_labels)
print("Test acuracy for random test:", random_accuracy)
print("Test acuracy for random dev:", random_accuracy_dev)
length_accuracy, length_predictions = length_baseline(train_sentences, train_labels, test_input, test_labels)
length_accuracy_dev, length_predictions_dev = length_baseline(train_sentences, train_labels, dev_sentences,
dev_labels)
print("Test acuracy for length test:", length_accuracy)
print("Test acuracy for length dev:", length_accuracy_dev)
frequency_accuracy, frequency_predictions = frequency_baseline(train_sentences, train_labels, test_input,
test_labels)
frequency_accuracy_dev, frequency_predictions_dev = frequency_baseline(train_sentences, train_labels, dev_sentences,
dev_labels)
print("Test acuracy for frequency test:", frequency_accuracy)
print("Test acuracy for frequency dev:", frequency_accuracy_dev)
majority_predictions.to_csv("experiments/baselines/majority_test.csv", index=False)
random_predictions.to_csv("experiments/baselines/random_test.csv", index=False)
length_predictions.to_csv("experiments/baselines/length_test.csv", index=False)
frequency_predictions.to_csv("experiments/baselines/frequency_test.csv", index=False)