-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathauthorship.py
377 lines (322 loc) · 13.7 KB
/
authorship.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
Authorship attribution classifier.
"""
import argparse
import art
import csv
import glob
import importlib
import math
import nltk
import os
import random
import shutil
import time
import yaml
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
VERBOSE = False
class Dataset():
def __init__(self):
self.training = []
self.testing = []
def contents(self):
"""Returns map of (testing, docgroup_index, doc_index) to document"""
results = {}
for i, docgroup in enumerate(self.training):
content = docgroup.contents()
for key in content:
results[(0, i, key)] = content[key]
for i, docgroup in enumerate(self.testing):
content = docgroup.contents()
for key in content:
results[(1, i, key)] = content[key]
return results
def add_vectors(self, vectors):
for (testing, docgroup_index, doc_index) in vectors:
vector = vectors[testing, docgroup_index, doc_index]
if testing:
self.testing[docgroup_index].add_vector(doc_index, vector)
else:
self.training[docgroup_index].add_vector(doc_index, vector)
class DocumentGroup():
def __init__(self, author, filenames, testing):
self.author = author
self.filenames = filenames
self.testing = testing
self.documents = []
for filename in self.filenames:
self.documents.append(Document(filename))
def contents(self):
return { i: self.documents[i] for i in range(len(self.documents)) }
def add_vector(self, doc_index, vector):
self.documents[doc_index].vector.extend(vector)
class Document():
def __init__(self, filename):
self.filename = filename
self.contents = { preprocessor: open(os.path.join(filename, preprocessor)).read()
for preprocessor in os.listdir(self.filename) }
self.vector = []
def get(self, preprocessor):
return self.contents[preprocessor]
class Runner():
def __init__(self, config):
self.config = config
self.src = config["configuration"]["src"]
self.iterations = config["configuration"]["repetitions"]
self.authors = config["authors"]
self.skip = config["configuration"]["skip"] if "skip" in config["configuration"] else []
self.clean = config["configuration"]["clean"] if "clean" in config["configuration"] else []
preprocessors = config["preprocessors"] if "preprocessors" in config else []
self.preprocessors = [Preprocessor(p) for p in preprocessors]
features = config["features"] if "features" in config else []
self.features = [Feature(f) for f in features]
self.segment_dir = os.path.join(self.src, "segments")
self.results_dir = os.path.join(self.src, "results")
self.results_file = os.path.join(self.results_dir, "summary.csv")
def log(self, message, override=False):
if override or VERBOSE:
print(message)
def run(self):
"""Runs all trials and gets results."""
self.preprocess()
for i in range(1, self.iterations + 1):
self.run_instance(i)
def mkdir(self, dirname):
try:
os.makedirs(dirname)
except FileExistsError:
pass
def should_skip(self, name):
return name in self.skip
def preprocess(self):
def clean():
for dirname in self.clean:
dirname = os.path.join(self.src, dirname)
self.log(f"Cleaning up directory {dirname}...")
shutil.rmtree(dirname, ignore_errors=True)
def segment(src, dst, base, size):
"""
Takes input files from `src`, concatenates them,
and then segments them into files of `size` sentences
each into the directory `dst`.
"""
def seg(l, n):
"""Divides list l into segments of size n."""
return [l[i:i+n] for i in range(0, len(l), n)]
infiles = glob.glob(src)
contents = " ".join(open(infile).read().replace("\n", " ")
for infile in infiles)
sentences = nltk.sent_tokenize(contents)
segments = seg(sentences, size)
digits = math.ceil(math.log10(len(segments)))
self.mkdir(dst)
for i, segment in enumerate(segments):
contents = " ".join(segment)
docname = f"{base}_{str(i).zfill(digits)}"
self.log(f"Generating segment {docname}...")
self.mkdir(os.path.join(dst, docname))
for preprocessor in self.preprocessors:
filename = os.path.join(dst, docname, f"{preprocessor.name}")
if os.path.exists(filename):
self.log(f" Skipping {preprocessor.name} for {docname}, already exists...")
continue
self.log(f" Using preprocessor {preprocessor.name} for {docname}...")
result = preprocessor.process(contents)
f = open(filename, "w")
f.write(result)
f.close()
def preserve(src, dst, base):
"""Preserves the input files to the output."""
infiles = glob.glob(src)
digits = math.ceil(math.log10(len(infiles)))
self.mkdir(dst)
for i, infile in enumerate(infiles):
contents = open(infile).read().replace("\n", " ")
docname = f"{base}_{str(i).zfill(digits)}"
self.log(f"Generating segment {docname}...")
self.mkdir(os.path.join(dst, docname))
for preprocessor in self.preprocessors:
self.log(f" Using preprocessor {preprocessor.name} for {docname}...")
result = preprocessor.process(contents)
f = open(os.path.join(dst, docname, f"{preprocessor.name}"), "w")
f.write(result)
f.close()
clean()
# Create results file initially
self.mkdir(self.results_dir)
f = open(self.results_file, "w")
f.write("iteration,accurate,inaccurate,unknown\n")
f.close()
if self.should_skip("preprocessing"):
self.log("Skipping preprocessing...")
return
self.log("Preprocessing...")
for segmentation in self.config["segmenters"]:
if "preserve" in segmentation and segmentation["preserve"]:
preserve(os.path.join(self.src, segmentation["src"]),
self.segment_dir,
self.authors[segmentation["author"]])
else:
segment(os.path.join(self.src, segmentation["src"]),
self.segment_dir,
self.authors[segmentation["author"]], segmentation["size"])
def run_instance(self, i):
"""Runs a single instance of the classifier."""
self.log(f"Running authorship classifier iteration {i}...")
self.current_iteration = i
self.separate()
self.prepare_documents()
self.train()
self.predict()
self.results()
def separate(self):
"""Create the separation of testing and training files."""
self.workdir = os.path.join(self.src, "separations", str(self.current_iteration))
if self.should_skip("separation"):
self.log("Skipping separation...")
return
# Create separations directory.
self.log("Running separations...")
self.mkdir(self.workdir)
# Creat training and testing directories.
training = os.path.join(self.workdir, "training")
testing = os.path.join(self.workdir, "testing")
self.mkdir(training)
self.mkdir(testing)
for separation in self.config["separations"]:
self.log(f"Running separation for {separation['author']}...")
base = self.authors[separation["author"]]
files = glob.glob(os.path.join(self.segment_dir, f"{base}_*"))
random.shuffle(files)
test_count = math.floor(len(files) * separation["holdout"])
testing_files = files[:test_count]
training_files = files[test_count:]
for filename in testing_files:
basename = os.path.basename(filename)
shutil.copytree(filename,
os.path.join(self.workdir, "testing", basename))
self.log(f" Placed {basename} into testing...")
for filename in training_files:
basename = os.path.basename(filename)
shutil.copytree(filename,
os.path.join(self.workdir, "training", basename))
self.log(f" Placed {basename} into training...")
def prepare_documents(self):
dataset = Dataset()
for author in self.authors:
base = self.authors[author]
training = glob.glob(os.path.join(self.workdir, "training", f"{base}*"))
if training:
group = DocumentGroup(author, training, True)
dataset.training.append(group)
testing = glob.glob(os.path.join(self.workdir, "testing", f"{base}*"))
if testing:
group = DocumentGroup(author, testing, False)
dataset.testing.append(group)
self.dataset = dataset
def train(self):
def training_labels():
data = []
labels = []
for docgroup in self.dataset.training:
for document in docgroup.documents:
data.append(document.vector)
labels.append(docgroup.author)
return data, labels
for feature in self.features:
self.log(f"Generating vectors for feature {feature.config}...")
feature.train(self.dataset)
data, labels = training_labels()
model = self.config["configuration"].get("model") or "NB"
if model == "NB":
clf = GaussianNB()
elif model == "SVM":
clf = SVC()
elif model == "Perceptron":
clf = sklearn.linear_model.Perceptron(tol=1e-3, random_state=0)
else:
raise Exception(f"Invalid model {model}.")
clf.fit(data, labels)
self.clf = clf
def predict(self):
for docgroup in self.dataset.testing:
for document in docgroup.documents:
prediction = self.clf.predict([document.vector])[0]
document.prediction = prediction
def results(self):
contents = f"\n===== RESULTS {self.current_iteration} =====\n"
accurate, inaccurate, unknown, total = 0, 0, 0, 0
for docgroup in self.dataset.testing:
is_unknown = docgroup.author.lower() in ["unknown", "anonymous"]
contents += "\n" + docgroup.author + "\n"
for document in docgroup.documents:
total += 1
if is_unknown:
unknown += 1
elif document.prediction == docgroup.author:
accurate += 1
else:
inaccurate += 1
contents += f" {document.filename}: {document.prediction}\n"
# Summary
accurate_percent = accurate * 100 / total
inaccurate_percent = inaccurate * 100 / total
unknown_percent = unknown * 100 / total
contents += "\nSUMMARY\n"
contents += "Accurate: {} ({:.2f}%)\n".format(accurate, accurate_percent)
contents += "Inaccurate: {} ({:.2f}%)\n".format(inaccurate, inaccurate_percent)
contents += "Unknown: {} ({:.2f}%)\n".format(unknown, unknown_percent)
print(contents)
# Log to file
digits = math.ceil(math.log10(self.iterations))
f = open(os.path.join(self.results_dir, f"results{str(self.current_iteration).zfill(digits)}.txt"), "w")
f.write(contents)
f.close()
f = open(os.path.join(self.results_file), "a")
writer = csv.writer(f, lineterminator="\n")
writer.writerow([self.current_iteration, round(accurate / total, 4), round(inaccurate / total, 4), round(unknown / total, 4)])
f.close()
class Preprocessor():
def __init__(self, name):
try:
self.name = name
self.module = importlib.import_module(f"preprocessors.{name}")
except ModuleNotFoundError:
raise Exception(f"Could not find preprocessor {name}.")
def process(self, contents):
return self.module.preprocess(contents)
class Feature():
def __init__(self, feature):
try:
self.name = feature["name"]
self.module = importlib.import_module(f"features.{self.name}")
self.config = feature
except ModuleNotFoundError:
raise Exception(f"Could not find feature {self.name}.")
def train(self, dataset):
contents = dataset.contents()
vectors = self.module.train(self.config, contents)
dataset.add_vectors(vectors)
def main():
print(art.text2art("Authorship"))
print(art.text2art("Attribution"))
# time.sleep(1)
config = parse_config()
runner = Runner(config)
runner.run()
def parse_config():
global VERBOSE
parser = argparse.ArgumentParser(
description="Run an authorship attribution classifier on anonymous documents."
)
parser.add_argument("config", type=str)
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
if args.verbose:
VERBOSE = True
contents = open(args.config).read()
data = yaml.load(contents)
return data
if __name__ == "__main__":
main()