-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifyFrankenstein.py
272 lines (242 loc) · 11.8 KB
/
classifyFrankenstein.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import sys
import pickle
import re
import numpy as np
from nltk.tag import pos_tag_sents
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
# from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
# from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import Pipeline
SAMPLE_SIZES = [25, 50, 100, 200, 400]
TESTING_METHOD = "groups"
if TESTING_METHOD == "shifts":
SHIFT_PROPORTION = 0.1
NUMBER_OF_SHIFTS = int(1 / SHIFT_PROPORTION)
elif TESTING_METHOD == "groups":
NUMBER_OF_GROUPS = 10
FEATURE_TYPE = "TAGS"
def loadFrankenstein():
franken_path = "/Users/tim/GitHub/frankenstein/clean_texts/author_unknown/"
f = open(franken_path + "MWS-PBS-frankenstein-clean.txt", encoding='utf-8')
text = f.read()
f.close()
return text
def getFunctionWords():
"""Gets list of function words for feature vector."""
try:
f = open("/Users/tim/GitHub/frankenstein/function_words.pck", "rb")
except:
print("Can't find function word list.")
sys.exit()
f_words = pickle.load(f)
f.close()
return f_words
def getTokens():
tkn_regex = re.compile(r'[A-Za-z0-9æëâêô]+')
count_vect = CountVectorizer(analyzer='word', token_pattern=tkn_regex)
analyze = count_vect.build_analyzer()
franken_tokens = analyze(raw_text)
return franken_tokens
def loadTrainingSamples():
"""Loads samples and labels from pickle and returns a list of samples, a list of sample labels and a list of label names."""
samples_path = "/Users/tim/GitHub/frankenstein/sampled_texts/check/"
try:
g = open("{}samples_{}.pck".format(samples_path, SAMPLE_SIZE), "rb")
except:
print("Can't find sample file.")
sys.exit()
sample_files = pickle.load(g)
g.close()
smpls, lbls, nms = sample_files
return smpls, lbls, nms
def getShiftedSamples(shift_number):
"""Makes and shifts samples."""
token_count = len(tokens)
sampling_max = token_count - (token_count % max(SAMPLE_SIZES))
absolute_shift = int(shift_number * SHIFT_PROPORTION * SAMPLE_SIZE)
if absolute_shift == 0:
sample_start_index = 0
else:
skipped_tokens = tokens[:absolute_shift]
sample_start_index = re.search(r'[^\wæëâêô]*' + r'[^\wæëâêô]+'.join(skipped_tokens), raw_text, re.I).end() + 1
skipped_text = raw_text[:sample_start_index]
list_of_samples = []
for i in range(0, sampling_max - SAMPLE_SIZE + 1, SAMPLE_SIZE):
# if i % (10 * SAMPLE_SIZE) == 0:
# print("Working on sample {}".format(int(i / SAMPLE_SIZE)))
lower_boundary = i + absolute_shift
upper_boundary = lower_boundary + SAMPLE_SIZE
if not upper_boundary > sampling_max:
token_sublist = tokens[lower_boundary:upper_boundary]
sample_end_index = re.search(r'[^\wæëâêô]*' + r'[^\wæëâêô]+'.join(token_sublist), raw_text, re.I).end() + 1
text_sample = raw_text[sample_start_index:sample_end_index]
list_of_samples.append(text_sample)
sample_start_index = sample_end_index
else:
continue # Remove statement to patch skipped start text to final sample (this results in equal amount of samples compared to list of unshifted samples)
token_sublist = tokens[lower_boundary:sampling_max]
sample_end_index = re.search(r'[^\wæëâêô]*' + r'[^\wæëâêô]+'.join(token_sublist), raw_text, re.I).end() + 1
text_fragment = raw_text[sample_start_index:sample_end_index]
text_sample = text_fragment + skipped_text
list_of_samples.append(text_sample)
return list_of_samples
def getGroupedSamples(group_num):
"""Makes and groups samples."""
token_count = len(tokens)
sampling_max = token_count - (token_count % max(SAMPLE_SIZES))
total_num_samples = sampling_max / SAMPLE_SIZE
print("Total number of samples:", int(total_num_samples))
basic_group_size = int(total_num_samples / NUMBER_OF_GROUPS)
num_leftover_samples = total_num_samples % NUMBER_OF_GROUPS
size_list = []
for i in range(NUMBER_OF_GROUPS):
size = basic_group_size
if num_leftover_samples != 0:
size += 1
num_leftover_samples -= 1
size_list.append(size)
token_boundaries = []
lower_boundary = 0
for j in size_list:
upper_boundary = lower_boundary + j * SAMPLE_SIZE
token_boundaries.append([lower_boundary, upper_boundary])
lower_boundary = upper_boundary
start_boundary, stop_boundary = token_boundaries[group_num - 1]
group_size = size_list[group_num - 1]
sample_list = []
for k in range(group_size):
end_boundary = start_boundary + SAMPLE_SIZE
token_sublist = tokens[start_boundary:end_boundary]
string_indices = re.search(r'[^\wæëâêô]*' + r'[^\wæëâêô]+'.join(token_sublist), raw_text, re.I).span()
text_sample = raw_text[string_indices[0]:string_indices[1]]
sample_list.append(text_sample)
if end_boundary == stop_boundary and k != group_size - 1:
print("Problem with group sampler!")
start_boundary = end_boundary
return sample_list
def loadSamples(identifier):
"""Checks whether samples have already been made and either loads them or calls a function to make them."""
franken_smpls_path = "/Users/tim/GitHub/frankenstein/sampled_texts/check/franken/"
if TESTING_METHOD == "shifts":
try:
f = open("{}samples_{}-s{}.pck".format(franken_smpls_path, SAMPLE_SIZE, identifier), "rb")
franken_samples = pickle.load(f)
except:
franken_samples = getShiftedSamples(identifier)
f = open("{}samples_{}-s{}.pck".format(franken_smpls_path, SAMPLE_SIZE, identifier), "wb")
pickle.dump(franken_samples, f)
f.close()
elif TESTING_METHOD == "groups":
try:
f = open("{}samples_{}-g{}.pck".format(franken_smpls_path, SAMPLE_SIZE, identifier), "rb")
franken_samples = pickle.load(f)
except:
franken_samples = getGroupedSamples(identifier)
f = open("{}samples_{}-g{}.pck".format(franken_smpls_path, SAMPLE_SIZE, identifier), "wb")
pickle.dump(franken_samples, f)
f.close()
return franken_samples
def tagSamples(s_list):
"""Takes a list of samples and returns a list of tagged samples, where each tagged sample is a continues string of tags seperated by whitespaces. This format is easy to parse for the Scikit Learn vectorizer."""
new_list = []
for s in s_list:
sentences = re.split(r'([;.!?][ "])', s)
sentences2 = []
for sentence_index in range(0, len(sentences) - 1, 2):
sentences2.append(sentences[sentence_index] + sentences[sentence_index + 1])
sentences2.append(sentences[-1])
tokenized_sentences = [word_tokenize(sentence) for sentence in sentences2]
tagged_sentences = pos_tag_sents(tokenized_sentences)
tag_list = [tpl[1] for sentence in tagged_sentences for tpl in sentence]
tag_string = " ".join(tag_list)
new_list.append(tag_string)
return new_list
def classifyFrankenstein(test_smpls):
"""Classifies Frankenstein samples using samples with known authors."""
train_lbls = np.array(training_labels)
if FEATURE_TYPE == "TOKENS":
train_lbls = np.array(training_labels)
token_regex = re.compile(r'[A-Za-z0-9æëâêô]+')
classifier = Pipeline([('vect', CountVectorizer(analyzer='word', token_pattern=token_regex, vocabulary=function_words)), ('clf', SVC(kernel="linear"))])
classifier = classifier.fit(training_samples, train_lbls)
predicted_classes = classifier.predict(test_smpls)
elif FEATURE_TYPE == "TAGS":
train_tags = tagSamples(training_samples)
test_tags = tagSamples(test_smpls)
tag_regex = re.compile(r'[^ ]+')
classifier = Pipeline([('vect', CountVectorizer(analyzer='word', token_pattern=tag_regex, lowercase=False, ngram_range=(2, 2))), ('clf', SVC(kernel="linear"))])
classifier = classifier.fit(train_tags, train_lbls)
predicted_classes = classifier.predict(test_tags)
return predicted_classes
def writeOutput(predicted, identifier):
if TESTING_METHOD == "shifts":
if SAMPLE_SIZE == SAMPLE_SIZES[0] and identifier == 0:
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_shifts_" + FEATURE_TYPE + ".csv", "w")
f.write("sample_size,shift_percentage")
for author in label_names:
f.write(",%_" + author)
f.write("\n")
f.close()
shift_percentage = SHIFT_PROPORTION * 100 * identifier
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_shifts_" + FEATURE_TYPE + ".csv", "a")
f.write("{},{}".format(SAMPLE_SIZE, shift_percentage))
for name in label_names:
name_index = label_names.index(name)
absolute_count = (predicted == name_index).sum()
percentage = (absolute_count / len(predicted)) * 100
f.write("," + str(percentage))
f.write("\n")
f.close()
elif TESTING_METHOD == "groups":
if SAMPLE_SIZE == SAMPLE_SIZES[0] and identifier == 1:
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_groups_" + FEATURE_TYPE + ".csv", "w")
f.write("sample_size,group")
for author in label_names:
f.write(",%_" + author)
f.write("\n")
f.close()
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_groups_" + FEATURE_TYPE + ".csv", "a")
f.write("{},{}".format(SAMPLE_SIZE, identifier))
for name in label_names:
name_index = label_names.index(name)
absolute_count = (predicted == name_index).sum()
percentage = (absolute_count / len(predicted)) * 100
f.write("," + str(percentage))
f.write("\n")
f.close()
def writePBSSamples(pred_classes, group):
"""Writes a list that provides indices to the samples that were classified as being written by Percy Bysshe Shelley."""
if SAMPLE_SIZE == SAMPLE_SIZES[0] and group == 1:
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_" + FEATURE_TYPE + "_PBS_samples.csv", "w")
f.write("sample_size,group,sample_index\n")
f.close()
PBS_index = label_names.index("LAM")
if PBS_index in pred_classes:
f = open("/Users/tim/GitHub/frankenstein/results/check4/franken_results_" + FEATURE_TYPE + "_PBS_samples.csv", "a")
sample_indices = [i for i in range(len(pred_classes)) if pred_classes[i] == PBS_index]
for index in sample_indices:
f.write("{},{},{}\n".format(SAMPLE_SIZE, group, index))
f.close()
def loopThroughSampleStrategy():
"""Main loop for the handling of Frankenstein samples."""
if TESTING_METHOD == "shifts":
for shift in range(NUMBER_OF_SHIFTS):
test_samples = loadSamples(shift)
print("Shift {}: {} samples".format(shift, len(test_samples)))
classifier_output = classifyFrankenstein(test_samples)
writeOutput(classifier_output, shift)
elif TESTING_METHOD == "groups":
for group in range(1, NUMBER_OF_GROUPS + 1):
test_samples = loadSamples(group)
print("Group {}: {} samples".format(group, len(test_samples)))
classifier_output = classifyFrankenstein(test_samples)
writeOutput(classifier_output, group)
writePBSSamples(classifier_output, group)
raw_text = loadFrankenstein()
function_words = getFunctionWords()
tokens = getTokens()
for SAMPLE_SIZE in SAMPLE_SIZES:
training_samples, training_labels, label_names = loadTrainingSamples()
loopThroughSampleStrategy()