-
Notifications
You must be signed in to change notification settings - Fork 17
/
vectorize_data.py
executable file
·134 lines (108 loc) · 3.54 KB
/
vectorize_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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import argparse
import numpy as np
import os
import pickle
import html2text
from autoscrape.vectorization import Vectorizer
class Data:
def __init__(self, X=None, y=None):
self.X = X
self.y = y
def dump(self, filepath):
with open(filepath, "wb") as f:
pickle.dump(self, f)
def load(self, filepath):
with open(filepath, "rb") as f:
tmp_d = pickle.load(f)
self.X = tmp_d.X
self.y = tmp_d.y
def parse_args():
desc = "Convenience script for vectorizing webpage training data."
parser = argparse.ArgumentParser(
description=desc,
)
parser.add_argument(
"--html_embeddings", type=str, required=True,
help="Location of HTML character embeddings file."
)
parser.add_argument(
"--word_embeddings", type=str, required=True,
help="Location of word embeddings file."
)
parser.add_argument(
"--output_file", type=str, default="data.pickle",
help="Output file for data matrices."
)
parser.add_argument(
'--loglevel', type=str, default="INFO",
choices=["DEBUG", "INFO", "WARN", "ERROR"],
help="Loglevel (default: INFO)"
)
# parser.add_argument(
# '--driver', type=str, default="Firefox",
# choices=["Firefox", "Chrome", "remote"],
# help="Which browser driver to use",
# )
parser.add_argument(
"dir", type=str,
help=("""
Location of directory containing training HTML data. This directory needs to have the following subdirectories, which correspond to classes: data_pages, error_pages, links_to_documents, links_to_search, search_pages
"""
)
)
args = parser.parse_args()
return args
def load_file(filename):
with open(filename, "r") as f:
return f.read()
if __name__ == "__main__":
args = parse_args()
cls_data = {}
total_records = 0
for root, dirs, files in os.walk(args.dir):
cls = root.split("/")[-1]
if not files or not cls:
continue
cls_data[cls] = []
for file in files:
filepath = os.path.join(root, file)
cls_data[cls].append(filepath)
records = len(cls_data[cls])
print("Class=%s Records=%s" % (cls, records))
total_records += records
print("Total records: %s" % total_records)
print("Loading vectorizer")
vectorizer = Vectorizer(
html_embeddings_file=args.html_embeddings,
word_embeddings_file=args.word_embeddings,
loglevel=args.loglevel,
)
dim = vectorizer.html.dim + vectorizer.word.dim
print("Vector dimension: %s" % dim)
X = np.zeros(shape=(total_records, dim))
y = np.zeros(shape=(total_records, 1))
html2text.config.BODY_WIDTH = 0
base_dir = os.path.abspath(os.curdir)
keys = list(cls_data.keys())
I = 0
for ix in range(len(keys)):
cls = keys[ix]
for file in cls_data[cls]:
print("I=%s" % I, end="\r")
abs_path = os.path.join(base_dir, file)
print("File=%s, Absolute Path=%s" % (file, abs_path))
html = load_file(abs_path)
parser = html2text.HTML2Text()
parser.feed(html)
text = parser.close()
x = vectorizer.vectorize(html, text)
X[I, :] = x
y[I, :] = [keys.index(cls)]
print("x=%s" % x)
I += 1
print("X: %s" % X)
print("y: %s" % y)
data = Data(X=X, y=y)
data.dump(args.output_file)