-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
72 lines (61 loc) · 2.01 KB
/
preprocessing.py
File metadata and controls
72 lines (61 loc) · 2.01 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
import codecs
import json
import logging
import numpy as np
import re
import string
import sys
import time
chars = string.ascii_lowercase + string.digits + string.punctuation
char_indices = dict((c, i + 1) for i, c in enumerate(chars))
logging.basicConfig(level=logging.INFO)
def time_count(fn):
def _wrapper(*args, **kwargs):
start = time.clock()
returns = fn(*args, **kwargs)
print("[time_count]: %s took %fs" % (fn.__name__, time.clock() - start))
return returns
return _wrapper
def binarize(x, sz=71):
return tf.to_float(tf.one_hot(x, sz, on_value=1, off_value=0, axis=-1))
def binarize_outshape(in_shape):
return in_shape[0], in_shape[1], 71
def striphtml(s):
p = re.compile(r'<.*?>')
return p.sub('', s)
def clean(s):
return re.sub(r'[^\x00-\x7f]', r'', s)
@time_count
def load_labels(fname, ntrain=1000, lowercase=True, return_jsonl=False):
labels = []
num_ignored = 0
num_labels = 0
jsonl_data = []
with codecs.open(fname, 'r', 'utf-8') as fin:
for json_line in fin:
d = json.loads(json_line.strip())
label = d.get('label', '')
if len(label) > 0:
labels.append(label.lower())
num_labels += 1
if num_labels % 100000 == 0:
print('.', end="", file=sys.stderr)
sys.stdout.flush()
if return_jsonl:
jsonl_data.append(d)
else:
num_ignored += 1
if num_labels == ntrain:
break
print('', file=sys.stderr)
logging.info('Loaded {0} labels, {1} ignored, {2} requested.'.format(
len(labels), num_ignored, ntrain))
if return_jsonl:
return labels, jsonl_data
return labels
def labels_to_matrix(labels, maxlen=16):
X = np.zeros((len(labels), maxlen), dtype=np.int32)
for i, label in enumerate(labels):
for j, char in enumerate(label[:maxlen]):
X[i, (maxlen - 1 - j)] = char_indices.get(char, 0)
return X