-
Notifications
You must be signed in to change notification settings - Fork 22
/
utils.py
347 lines (298 loc) · 11.1 KB
/
utils.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
import logging
from datetime import datetime
from os.path import dirname, abspath, join, exists
import requests
import os
import json
def get_logger(model_name):
BASE_DIR = dirname(abspath(__file__))
LOG_DIR = join(BASE_DIR, 'logs')
# LOG_DIR = '/output'
if not exists(LOG_DIR):
os.mkdir(LOG_DIR)
log_filename='{model_name}-{datetime}.log'.format(model_name=model_name, datetime=datetime.now())
log_filepath = join(LOG_DIR, log_filename)
logger = logging.getLogger('deep-text_classification-logger')
if not logger.handlers: # execute only if logger doesn't already exist
fileHandler = logging.FileHandler(log_filepath.format(datetime=datetime.now()))
streamHandler = logging.StreamHandler(os.sys.stdout)
formatter = logging.Formatter('[%(levelname)s] %(asctime)s > %(message)s', datefmt='%m-%d %H:%M:%S')
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
logger.setLevel(logging.INFO)
return logger
# adapted from https://stackoverflow.com/a/39225272
def download_file_from_google_drive(file_id, destination):
download_url = "https://docs.google.com/uc?export=download"
with requests.Session() as session:
response = session.get(download_url, params={'id': file_id}, stream=True)
token = get_confirm_token(response)
if token:
params = {'id': file_id, 'confirm': token}
response = session.get(download_url, params=params, stream=True)
save_response_content(response, destination)
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
def save_response_content(response, destination):
CHUNK_SIZE = 32768
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
# adapted from https://github.com/JonathanRaiman/pytreebank
def normalize_string(string):
"""
Standardize input strings by making
non-ascii spaces be ascii, and by converting
treebank-style brackets/parenthesis be characters
once more.
Arguments:
----------
string : str, characters to be standardized.
Returns:
--------
str : standardized
"""
return string.replace("\xa0", " ")\
.replace("\\", "")\
.replace("-LRB-", "(")\
.replace("-RRB-", ")")\
.replace("-LCB-", "{")\
.replace("-RCB-", "}")\
.replace("-LSB-", "[")\
.replace("-RSB-", "]")
def attribute_text_label(node, current_word):
"""
Tries to recover the label inside a string
of the form '(3 hello)' where 3 is the label,
and hello is the string. Label is not assigned
if the string does not follow the expected
format.
Arguments:
----------
node : LabeledTree, current node that should
possibly receive a label.
current_word : str, input string.
"""
node.text = normalize_string(current_word)
node.text = node.text.strip(" ")
node.udepth = 1
if len(node.text) > 0 and node.text[0].isdigit():
split_sent = node.text.split(" ", 1)
label = split_sent[0]
if len(split_sent) > 1:
text = split_sent[1]
node.text = text
if all(c.isdigit() for c in label):
node.label = int(label)
else:
text = label + " " + text
node.text = text
if len(node.text) == 0:
node.text = None
def create_tree_from_string(line):
"""
Parse and convert a string representation
of an example into a LabeledTree datastructure.
Arguments:
----------
line : str, string version of the tree.
Returns:
--------
LabeledTree : parsed tree.
"""
depth = 0
current_word = ""
root = None
current_node = root
for char in line:
if char == '(':
if current_node is not None and len(current_word) > 0:
attribute_text_label(current_node, current_word)
current_word = ""
depth += 1
if depth > 1:
# replace current head node by this node:
child = LabeledTree(depth=depth)
current_node.add_child(child)
current_node = child
root.add_general_child(child)
else:
root = LabeledTree(depth=depth)
root.add_general_child(root)
current_node = root
elif char == ')':
# assign current word:
if len(current_word) > 0:
attribute_text_label(current_node, current_word)
current_word = ""
# go up a level:
depth -= 1
if current_node.parent != None:
current_node.parent.udepth = max(current_node.udepth+1, current_node.parent.udepth)
current_node = current_node.parent
else:
# add to current read word
current_word += char
if depth != 0:
raise ParseError("Not an equal amount of closing and opening parentheses")
return root
class LabeledTree(object):
SCORE_MAPPING = [-12.5,-6.25,0.0,6.25,12.5]
def __init__(self,
depth=0,
text=None,
label=None,
children=None,
parent=None,
udepth=1):
self.label = label
self.children = children if children != None else []
self.general_children = []
self.text = text
self.parent = parent
self.depth = depth
self.udepth = udepth
def uproot(tree):
"""
Take a subranch of a tree and deep-copy the children
of this subbranch into a new LabeledTree
"""
uprooted = tree.copy()
uprooted.parent = None
for child in tree.all_children():
uprooted.add_general_child(child)
return uprooted
def shrink_tree(tree, final_depth):
if tree.udepth <= final_depth:
return tree
for branch in tree.general_children:
if branch.udepth == final_depth:
return branch.uproot()
def shrunk_trees(tree, final_depth):
if tree.udepth <= final_depth:
yield tree
for branch in tree.general_children:
if branch.udepth == final_depth:
yield branch.uproot()
def copy(self):
"""
Deep Copy of a LabeledTree
"""
return LabeledTree(
udepth = self.udepth,
depth = self.depth,
text = self.text,
label = self.label,
children = self.children.copy() if self.children != None else [],
parent = self.parent)
def add_child(self, child):
"""
Adds a branch to the current tree.
"""
self.children.append(child)
child.parent = self
self.udepth = max([child.udepth for child in self.children]) + 1
def add_general_child(self, child):
self.general_children.append(child)
def all_children(self):
if len(self.children) > 0:
for child in self.children:
for subchild in child.all_children():
yield subchild
yield self
else:
yield self
def lowercase(self):
"""
Lowercase all strings in this tree.
Works recursively and in-place.
"""
if len(self.children) > 0:
for child in self.children:
child.lowercase()
else:
self.text = self.text.lower()
def to_dict(self, index=0):
"""
Dict format for use in Javascript / Jason Chuang's display technology.
"""
index += 1
rep = {}
rep["index"] = index
rep["leaf"] = len(self.children) == 0
rep["depth"] = self.udepth
rep["scoreDistr"] = [0.0] * len(LabeledTree.SCORE_MAPPING)
# dirac distribution at correct label
if self.label is not None:
rep["scoreDistr"][self.label] = 1.0
mapping = LabeledTree.SCORE_MAPPING[:]
rep["rating"] = mapping[self.label] - min(mapping)
# if you are using this method for printing predictions
# from a model, the the dot product with the model's output
# distribution should be taken with this list:
rep["numChildren"] = len(self.children)
text = self.text if self.text != None else ""
seen_tokens = 0
witnessed_pixels = 0
for i, child in enumerate(self.children):
if i > 0:
text += " "
child_key = "child%d" % (i)
(rep[child_key], index) = child.to_dict(index)
text += rep[child_key]["text"]
seen_tokens += rep[child_key]["tokens"]
witnessed_pixels += rep[child_key]["pixels"]
rep["text"] = text
rep["tokens"] = 1 if (self.text != None and len(self.text) > 0) else seen_tokens
rep["pixels"] = witnessed_pixels + 3 if len(self.children) > 0 else text_size(self.text)
return (rep, index)
def to_json(self):
rep, _ = self.to_dict()
return json.dumps(rep)
def display(self):
from IPython.display import Javascript, display
display(Javascript("createTrees(["+self.to_json()+"])"))
display(Javascript("updateTrees()"))
def to_lines(self):
if len(self.children) > 0:
left_lines, right_lines = self.children[0].to_lines(), self.children[1].to_lines()
self_line = [left_lines[0] + " " + right_lines[0]]
return self_line + left_lines + right_lines
else:
return [self.text]
def to_labeled_lines(self):
if len(self.children) > 0:
left_lines, right_lines = self.children[0].to_labeled_lines(), self.children[1].to_labeled_lines()
self_line = [(self.label, left_lines[0][1] + " " + right_lines[0][1])]
return self_line + left_lines + right_lines
else:
return [(self.label, self.text)]
def __str__(self):
"""
String representation of a tree as visible in original corpus.
print(tree)
#=> '(2 (2 not) (3 good))'
Outputs
-------
str: the String representation of the tree.
"""
if len(self.children) > 0:
rep = "(%d " % self.label
for child in self.children:
rep += str(child)
return rep + ")"
else:
text = self.text\
.replace("(", "-LRB-")\
.replace(")", "-RRB-")\
.replace("{", "-LCB-")\
.replace("}", "-RCB-")\
.replace("[", "-LSB-")\
.replace("]", "-RSB-")
return ("(%d %s) " % (self.label, text))