This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
forked from asayeed/lt2212-v20-a3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha3_features.py
125 lines (90 loc) · 4.04 KB
/
a3_features.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
import os
import sys
import argparse
import numpy as np
import pandas as pd
# Whatever other imports you need
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from nltk.stem import WordNetLemmatizer
from nltk.tokenize import word_tokenize
from glob import glob
from collections import Counter
from sklearn.decomposition import TruncatedSVD
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert directories into table.")
parser.add_argument("inputdir", type=str, help="The root of the author directories.")
parser.add_argument("outputfile", type=str, help="The name of the output file containing the table of instances.")
parser.add_argument("dims", type=int, help="The output feature dimensions.")
parser.add_argument("--test", "-T", dest="testsize", type=int, default="20", help="The percentage (integer) of instances to label as test.")
args = parser.parse_args()
print("Reading {}...".format(args.inputdir))
# Do what you need to read the documents here.
def word_counts_n(txt):
to_df = []
for count in Counter(txt).most_common():
if count[0].isalpha():
to_df.append(count)
return to_df
def build_table(samples):
allfiles = glob("{}/*/*.*".format(args.inputdir), recursive=True)
column = {}
filenames = []
classnames = []
wordcounts = {}
for f in allfiles:
classname = f.split("/")[1]
filename = f.split("/")[1] + "_" + f.split("/")[2]
filenames.append(filename)
classnames.append(classname)
with open(f, "r") as doc:
word_n = word_counts_n(doc.read().split(" "))
if len(word_n) == 0:
continue
for word in word_n:
if filename not in wordcounts:
wordcounts[filename] = {}
wordcounts[filename][word[0]] = word[1]
if word[0] not in column:
column[word[0]] = []
for f in allfiles:
classname = f.split("/")[1]
filename = f.split("/")[1] + "_" + f.split("/")[2]
with open(f,"r") as doc:
word_n = word_counts_n(doc.read().split(" "))
for c in column:
if filename in wordcounts and c in wordcounts[filename]:
column[c].append(wordcounts[filename][c])
else:
column[c].append(0)
l = []
for f in allfiles:
with open(f,"r") as doc:
l.append(len(doc.read().split(" ")))
for name in column:
if name is not "class" and name is not "filename":
t = column[name]
res_c = [float(ti)/li for ti,li in zip(t,l)]
column[name] = res_c
df = pd.DataFrame(column)
svd = TruncatedSVD(n_components=args.dims)
to_file = svd.fit_transform(df.to_numpy())
X = shuffle(to_file, classnames)
X_train = to_file[:int(len(to_file)*0.8)]
X_test = to_file[int(len(to_file)*0.8):]
y_train = classnames[:int(len(classnames)*0.8)]
y_test = classnames[int(len(classnames)*0.8):]
train = pd.DataFrame(X_train)
train["filename"] = y_train
train["target"] = ["train"] * len(X_train)
test = pd.DataFrame(X_test)
test["filename"] = y_test
test["target"] = ["test"] * len(X_test)
return train.append(test)
print("Constructing table with {} feature dimensions and {}% test instances...".format(args.dims, args.testsize))
combined = build_table("enron_sample")
print("Writing to {}...".format(args.outputfile))
combined.to_csv(path_or_buf=args.outputfile, mode="w", index=False)
print("Done!")