-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer_utilities.py
More file actions
227 lines (174 loc) · 5.94 KB
/
Copy pathtokenizer_utilities.py
File metadata and controls
227 lines (174 loc) · 5.94 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import numpy as np
import operator
import math
import re
##Tokenization Utilities. This should be kept in sync with the C# tokenizer
class Tokenizer:
def __init__(self, vocab_len, sentence_len, right_pad = True, right_trun =True):
self.vocab = {}
self.vocab_len = vocab_len
self.max_sentence_len = sentence_len
self.right_pad = right_pad
self.right_trun = right_trun
def load_dict(word_dict):
self.vocab = word_dict
def load_dict_file(self, vocab_file_path):
with open(vocab_file_path, "r") as ins:
index = 0
for line in ins:
self.vocab[line.rstrip()] = index
index += 1
def remove_invalid_chars(self, str):
blacklist_chars = '!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\t\n\r–’≥®²‘“”°'
str_list = []
for char in str:
if char not in blacklist_chars:
str_list.append(char)
else: ##BUG BUG : new change
str_list.append(' ')
return ''.join(str_list)
def tokenize(self, str):
if self.isNaN(str):
str =''
lowered_str = str.lower()
cleaned_str = self.remove_invalid_chars(lowered_str)
splitted = cleaned_str.split(' ') ## Still need to remove white spaces
tokens = []
start = 0
token_counter = 0
if self.right_trun == False:
start = max(len(splitted) - self.max_sentence_len, 0)
for index in range(start, len(splitted)):
current_token = splitted[index]
if current_token == '': #BUGBUG: new change
continue
if current_token in self.vocab:
if self.vocab[current_token] < self.vocab_len:
tokens.append(self.vocab[current_token])
token_counter += 1
if token_counter >= self.max_sentence_len:
break
zeros_list = [0] * (max(self.max_sentence_len - token_counter, 0))
if self.right_pad:
tokens = tokens + zeros_list
else:
tokens = zeros_list + tokens
return tokens
def tokenize_texts(self, chunks, column):
for chunk in chunks:
tokenized_text = []
for text in chunk[column]:
tokenized = self.tokenize(text)
tokenized_text.append(tokenized)
yield tokenized_text
def gen_dict(self, chunks, column):
word_dict = {}
for chunk in chunks:
for text in chunk[column]:
lowered_str = text.lower()
cleaned_str = self.remove_invalid_chars(lowered_str)
splitted = cleaned_str.split(' ')
for token in splitted:
if token not in word_dict:
word_dict[token] = 1
else:
word_dict[token] += 1
sorted_words = sorted(word_dict.items(), key=operator.itemgetter(1), reverse=True)
sorted_dict = dict()
for index in range(len(sorted_words)):
sorted_dict[sorted_words[index][0]] = index
self.vocab = sorted_dict
return sorted_dict
def isNaN(self,num):
return num != num
class Table_Tokenizer:
def __init__(self, vocab_len, sentence_len, right_pad = True, right_trun =True):
self.vocab = {}
self.vocab_len = vocab_len
self.max_sentence_len = sentence_len
self.right_pad = right_pad
self.right_trun = right_trun
self.struct_labels = {'<td>':' TDATSTART ','</td>':' TDATEND ', '<tr>':' TROWSTART ', '</tr>':' TROWEND ','<th>':' THEADERSTART ', '</th>':' THEADEREND ','<h1>':' THEAD1START ', '</h1>':' THEAD1END ',
'<h2>':' THEAD2START ', '</h2>':' THEAD2END ', '<h3>':' THEAD3START ', '</h3>':' THEAD3END ', '<h4>':' THEAD4START ', '</h4>':' THEAD4END ','<p>':' PARAGSTART ', '</p>':' PARAGEND ',
'#n#':'','#r#':'','#tab#':'','<table':'TABLESTART', '</table':'TABLEEND'}
def load_dict(word_dict):
self.vocab = word_dict
def load_dict_file(self, vocab_file_path):
with open(vocab_file_path, "r") as ins:
index = 0
for line in ins:
self.vocab[line.rstrip()] = index
index += 1
def replace_table_struct_labels(self, text):
for key in self.struct_labels:
text = text.replace(key,self.struct_labels[key])
return text
def remove_invalid_chars(self, str):
blacklist_chars = '!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\t\n\r–’≥®²‘“”°'
str_list = []
for char in str:
if char not in blacklist_chars:
str_list.append(char)
else: ##BUG BUG : new change
str_list.append(' ')
return ''.join(str_list)
def tokenize(self, str):
if self.isNaN(str):
str =''
lowered_str = str.lower()
lowered_str = re.sub(r'cellpadding="[0-9]*"', '',lowered_str)
lowered_str = re.sub(r'border="[0-9]*"', '',lowered_str)
cleaned_str = self.replace_table_struct_labels(lowered_str)
cleaned_str = self.remove_invalid_chars(cleaned_str)
splitted = cleaned_str.split(' ') ## Still need to remove white spaces
tokens = []
start = 0
token_counter = 0
if self.right_trun == False:
start = max(len(splitted) - self.max_sentence_len, 0)
for index in range(start, len(splitted)):
current_token = splitted[index]
if current_token == '': #BUGBUG: new change
continue
if current_token in self.vocab:
if self.vocab[current_token] < self.vocab_len:
tokens.append(self.vocab[current_token])
token_counter += 1
if token_counter >= self.max_sentence_len:
break
zeros_list = [0] * (max(self.max_sentence_len - token_counter, 0))
if self.right_pad:
tokens = tokens + zeros_list
else:
tokens = zeros_list + tokens
return tokens
def tokenize_texts(self, chunks, column):
for chunk in chunks:
tokenized_text = []
for text in chunk[column]:
tokenized = self.tokenize(text)
tokenized_text.append(tokenized)
yield tokenized_text
def gen_dict(self, chunks, column):
word_dict = {}
for chunk in chunks:
for text in chunk[column]:
lowered_str = text.lower()
cleaned_str = self.remove_invalid_chars(lowered_str)
splitted = cleaned_str.split(' ')
for token in splitted:
if token not in word_dict:
word_dict[token] = 1
else:
word_dict[token] += 1
sorted_words = sorted(word_dict.items(), key=operator.itemgetter(1), reverse=True)
sorted_dict = dict()
for index in range(len(sorted_words)):
sorted_dict[sorted_words[index][0]] = index
self.vocab = sorted_dict
return sorted_dict
def isNaN(self,num):
return num != num