-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatahandler.py
315 lines (271 loc) · 8.57 KB
/
datahandler.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
import csv
import sys
import re
import random
import utils
class Datahandler:
"""
Datahandler
======
Container of datasets to be passed on to a featurizer. Can convert .csv
files into a dataset
Parameters
------
max_n : int, optional, default False
Maximum number of data instances *per dataset* user wants to work with.
Attributes
-----
dataset : dict
Dictionary where key is the name of a dataset, and the value its rows.
Will not be filled if data is streamed.
rows : list of lists
list of documents, document is a list of csv-fields
Examples
-----
Interactive:
>>> reader = Datareader(max_n=1000)
>>> reader.set('blogs.csv')
>>> docs = reader.rows
>>> reader.set_rows(docs)
"""
def __init__(self):
self.headers = "label tweet_id author_id date time authorname text tagged".split()
self.dataset = {}
self.rows = []
def set_rows(self, rows):
"""
Data reader
=====
Function to read in rows directly
Parameters
-----
rows : list of lists (rows and columns)
"""
self.rows = rows
self.rows_2_dataset()
def set(self, filename):
"""
Csv reader
=====
Function to read in a csv file and store it as a dict of lists
Parameters
-----
filename : str
The name of the csv file
Returns
-----
dataset : dict of lists
each column with an identifier
"""
csv.field_size_limit(sys.maxsize)
rows = []
try:
with open(filename, 'r', encoding = 'utf-8') as csvfile:
csv_reader = csv.reader(csvfile)
for line in csv_reader:
rows.append(line)
except:
csvfile = open(filename, 'r', encoding = 'utf-8')
csv_reader = csv.reader(line.replace('\0','') for line in csvfile.readlines())
for line in csv_reader:
rows.append(line)
self.rows = rows[1:]
self.rows_2_dataset()
def write_csv(self, outfile):
"""
CSV writer
=====
Function to write rows to a file in csv format
"""
self.dataset_2_rows()
utils.write_csv([self.headers] + self.rows, outfile)
def dataset_2_rows(self):
"""
Dataset converter
=====
Converts a dataset into rows
Needed to write a dataset to a file in csv format
Sets
-----
self.rows : list of lists (rows and columns respectively)
"""
self.encode_tagged()
self.rows = list(zip(*[self.dataset[field] for field in self.headers]))
self.decode_tagged()
def rows_2_dataset(self):
"""
Row converter
=====
Converts rows into a dataset
"""
self.dataset = {k: [] for k in self.headers}
for row in self.rows:
for category, val in zip(self.headers, row):
self.dataset[category].append(val)
self.decode_tagged()
def decode_tagged(self):
"""
Frog decoder
=====
Function to decode a frog string into a list of lists per document
"""
if self.dataset['tagged'][0] != '-':
new_tagged = []
for doc in self.dataset['tagged']:
new_tagged.append([token.split("\t") for token in doc.split("\n")])
self.dataset['tagged'] = new_tagged
def encode_tagged(self):
"""
Frog encoder
=====
Function to encode a frog list into a string
"""
tagstrings = []
for doc in self.dataset['tagged']:
tagstrings.append("\n".join(["\t".join(token) for token in doc]))
self.dataset['tagged'] = tagstrings
def split_dataset(self, shuffle = False):
"""
Dataset splitter
=====
Function to split the dataset in two sets of 90% and 10 %
Parameters
-----
shuffle : Bool
choose to shuffle the dataset before splitting (in order to mix
the labels)
Returns
------
train, test : list, list
"""
if shuffle:
random.shuffle(self.rows)
train_split = int(len(self.rows) * 0.9)
return(self.rows[:train_split], self.rows[train_split:])
def return_sequences(self, tag):
"""
Tag selecter
=====
Function to extract the sequence of a specific tag per
document
Presumes a column with frogged data
Parameters
-----
tag : str
the tag of which to return the sequences
options: 'token', 'lemma', 'postag', 'sentence'
Returns
-----
sequences : list of lists
the sequence of a tag per document
"""
tagdict = {'token' : 0, 'lemma' : 1, 'postag' : 2, 'sentence' : 3}
tagindex = tagdict[tag]
sequences = []
for instance in self.dataset['tagged']:
sequences.append([token[tagindex] for token in instance])
return sequences
def filter_instances(self, blacklist):
"""
Instance filter
=====
Function to filter instances from the dataset if they contain a string
from the blacklist
Parameters
-----
blacklist : list of strings
Any instance that contains a word from the blacklist is filtered
"""
tokenized_docs = self.return_sequences('token')
filtered_docs = [] #list of indices
for i, doc in enumerate(tokenized_docs):
black = False
for token in doc:
for string in blacklist:
if re.match(string, token, re.IGNORECASE):
black = True
if not black:
filtered_docs.append(i)
self.dataset_2_rows()
self.rows = [self.rows[i] for i in filtered_docs]
self.rows_2_dataset()
def normalize(self, regex, dummy):
"""
Normalizer
=====
Function to normalize tokens and lemmas that match a regex to a dummy
Parameters
-----
regex : re.compile object
the regular expression to match
dummy : string
the dummy to replace a matching token with
"""
new_tagged = []
for doc in self.dataset['tagged']:
new_doc = []
for token in doc:
if regex.match(token[0]):
token[0] = dummy
token[1] = dummy
new_doc.append(token)
new_tagged.append(new_doc)
self.dataset['tagged'] = new_tagged
def normalize_urls(self):
"""
URL normalizer
=====
Function to normalize URLs to a dummy
"""
find_url = re.compile(r"^(http://|www|[^\.]+)\.([^\.]+\.)*[^\.]{2,}")
dummy = "_URL_"
self.normalize(find_url, dummy)
def normalize_usernames(self):
"""
Username normalizer
=====
Function to normalize usernames to a dummy
presumes 'twitter-format' (@username)
"""
find_username = re.compile("^@\w+")
dummy = "_USER_"
self.normalize(find_username, dummy)
def filter_punctuation(self):
"""
Punctuation remover
=====
Function to remove punctuation from frogged data
"""
new_tagged = []
for doc in self.dataset['tagged']:
new_doc = []
for token in doc:
try:
if not token[2] == "LET()":
new_doc.append(token)
except:
continue
new_tagged.append(new_doc)
self.dataset['tagged'] = new_tagged
def set_label(self, label):
"""
Label editor
=====
Function to set a universal label for each instance
Parameters
-----
label : string
"""
self.dataset['label'] = [label] * len(self.dataset['label'])
self.dataset_2_rows()
def to_lower(self):
new_tagged = []
for doc in self.dataset['tagged']:
new_doc = []
for token in doc:
new_doc.append([token[0].lower(), token[1].lower(), token[2], token[3]])
new_tagged.append(new_doc)
self.dataset['tagged'] = new_tagged
def sample(self, samplesize):
self.rows = random.sample(self.rows, samplesize)
self.rows_2_dataset()