-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_modules.py
560 lines (469 loc) · 20.8 KB
/
file_modules.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__synopsis__ : Tools for file load and save on various formats.
__description__ :
__project__ : my_modules
__author__ : 'Samujjwal Ghosh'
__version__ :
__date__ : June 2018
__copyright__ : "Copyright (c) 2018"
__license__ : "Python"; (Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html)
__classes__ :
__variables__ :
__methods__ :
TODO : 1.
"""
import os,sys,json,platform,pickle
import unicodedata
from collections import OrderedDict
import my_modules as mm
from scipy import sparse
date_time_tag = mm.get_date_time_tag()
def get_dataset_path():
"""
:return:
"""
if platform.system() == 'Windows':
dataset_path = 'D:\Datasets\Extreme Classification'
sys.path.append('D:\GDrive\Dropbox\IITH\\0 Research')
elif platform.system() == 'Linux':
dataset_path = '/home/cs16resch01001/datasets/Extreme Classification'
sys.path.append('/home/cs16resch01001/codes')
else: # OS X returns name 'Darwin'
dataset_path = '/Users/monojitdey/Downloads'
print(platform.system(),"os detected.")
return dataset_path
dataset_path = get_dataset_path()
def specials_table(specials="""< > * ? " / \ : |""",replace=' '):
"""
:param specials:
:param replace:
:return:
usage: file_name = file_name.translate(trans_table)
"""
trans_dict = {chars:replace for chars in specials}
trans_table = str.maketrans(trans_dict)
return trans_table
def read_unlabeled_json(file_name):
print("Method: read_unlabeled_json(file_name)")
unlabeled_tweets_dict = OrderedDict()
with open(file_name + ".json", encoding="utf-8") as f:
for line in f:
line = json.loads(line)
try:
tweet_text = line["retweeted_status"]["text"]
except KeyError:
tweet_text = line["text"]
tweet_text = unicodedata.normalize('NFKD', tweet_text).encode(
'ascii', 'ignore').decode("utf-8")
unlabeled_tweets_dict[line["id"]] = line
# TODO: read ids with "id" key. No need to change structure at all.
# TODO: Only add parsed_text, classes and features
unlabeled_tweets_dict["parsed_text"] = mm.parse_tweet(tweet_text)
unlabeled_tweets_dict[line["id"]]['classes'] = []
f.close()
return unlabeled_tweets_dict
def read_unlabeled_json_nochange(file_name):
print("Method: read_unlabeled_json(file_name)")
unlabeled_tweets_dict = OrderedDict()
with open(file_name + ".json", encoding="utf-8") as f:
for line in f:
line = json.loads(line)
try:
tweet_text = line["retweeted_status"]["text"]
except KeyError:
tweet_text = line["text"]
tweet_text = unicodedata.normalize('NFKD', tweet_text).encode(
'ascii', 'ignore').decode("utf-8")
unlabeled_tweets_dict["parsed_text"] = mm.parse_tweet(tweet_text)
unlabeled_tweets_dict[line["id"]]['classes'] = []
f.close()
return unlabeled_tweets_dict
def read_labeled(labeled_file,n_classes=7,k_similar=15,k_unique_words=25):
# print("Reading labeled Data from file: ",labeled_file)
if os.path.exists(labeled_file+"features_train.json") and os.path.exists(labeled_file+"features_validation.json")and os.path.exists(labeled_file+"features_test.json"):
print("Reading labeled Data from file: ",labeled_file+"features_validation"+".json")
train=mm.read_json(labeled_file+"features_train")
validation=mm.read_json(labeled_file+"features_validation")
test=mm.read_json(labeled_file+"features_test")
elif os.path.exists(labeled_file + "parsed_train" + ".json") and\
os.path.exists(labeled_file + "parsed_validation" + ".json") and\
os.path.exists(labeled_file + "parsed_test" + ".json"):
print("Reading labeled Data from file: ",labeled_file+"parsed_validation"+".json")
train = load_json(labeled_file + "parsed_train")
validation = load_json(labeled_file + "parsed_validation")
test = load_json(labeled_file + "parsed_test")
save_json(train, labeled_file + "parsed_train")
save_json(validation, labeled_file + "parsed_validation")
save_json(test, labeled_file + "parsed_test")
mm.derived_features(train,validation,test,n_classes,k_similar)
total_corpus,class_corpuses = mm.create_corpus(train,n_classes)
unique_words = mm.unique_words_class(class_corpuses,k_unique_words)
mm.manual_features(train,unique_words,n_classes)
mm.manual_features(validation,unique_words,n_classes)
mm.manual_features(test,unique_words,n_classes)
mm.save_json(train,labeled_file+"features_train")
mm.save_json(validation, labeled_file + "features_validation")
mm.save_json(test,labeled_file+"features_test")
elif os.path.exists(labeled_file + "train" + ".json") and\
os.path.exists(labeled_file + "validation" + ".json") and\
os.path.exists(labeled_file + "test" + ".json"):
print("Reading labeled Data from file: ",labeled_file+"validation"+".json")
train = load_json(labeled_file + "train")
validation = load_json(labeled_file + "validation")
test = load_json(labeled_file + "test")
train = mm.parse_tweets(train)
validation = mm.parse_tweets(validation)
test = mm.parse_tweets(test)
save_json(train, labeled_file + "parsed_train")
save_json(validation, labeled_file + "parsed_validation")
save_json(test, labeled_file + "parsed_test")
mm.derived_features(train,validation,test,n_classes,k_similar)
total_corpus,class_corpuses = mm.create_corpus(train,n_classes)
unique_words = mm.unique_words_class(class_corpuses,k_unique_words)
mm.manual_features(train,unique_words,n_classes)
mm.manual_features(validation,unique_words,n_classes)
mm.manual_features(test,unique_words,n_classes)
mm.save_json(train,labeled_file+"features_train")
mm.save_json(validation, labeled_file + "features_validation")
mm.save_json(test,labeled_file+"features_test")
elif os.path.exists(labeled_file + ".json"):
print("Reading labeled Data from file: ",labeled_file + ".json")
labeled_dict = load_json(labeled_file)
train, validation, test = train_test_read_split(labeled_dict)
save_json(train, labeled_file + "train")
save_json(validation, labeled_file + "validation")
save_json(test, labeled_file + "test")
train = mm.parse_tweets(train)
validation = mm.parse_tweets(validation)
test = mm.parse_tweets(test)
save_json(train, labeled_file + "parsed_train")
save_json(validation, labeled_file + "parsed_validation")
save_json(test, labeled_file + "parsed_test")
mm.derived_features(train,validation,test,n_classes,k_similar)
total_corpus,class_corpuses = mm.create_corpus(train,n_classes)
unique_words = mm.unique_words_class(class_corpuses,k_unique_words)
mm.manual_features(train,unique_words,n_classes)
mm.manual_features(validation,unique_words,n_classes)
mm.manual_features(test,unique_words,n_classes)
mm.save_json(train,labeled_file+"features_train")
mm.save_json(validation, labeled_file + "features_validation")
mm.save_json(test,labeled_file+"features_test")
else:
smerp_labeled = mm.read_smerp_labeled()
save_json(smerp_labeled, labeled_file)
train, validation, test = train_test_read_split(labeled_file)
print("Number of labeled tweets: ", len(smerp_labeled))
save_json(train, labeled_file + "train")
save_json(validation, labeled_file + "validation")
save_json(test, labeled_file + "test")
train = mm.parse_tweets(train)
validation = mm.parse_tweets(validation)
test = mm.parse_tweets(test)
save_json(train, labeled_file + "parsed_train")
save_json(validation, labeled_file + "parsed_validation")
save_json(test, labeled_file + "parsed_test")
mm.derived_features(train,validation,test,n_classes,k_similar)
total_corpus,class_corpuses = mm.create_corpus(train,n_classes)
unique_words = mm.unique_words_class(class_corpuses,k_unique_words)
mm.manual_features(train,unique_words,n_classes)
mm.manual_features(validation,unique_words,n_classes)
mm.manual_features(test,unique_words,n_classes)
mm.save_json(train,labeled_file+"features_train")
mm.save_json(validation, labeled_file + "features_validation")
mm.save_json(test,labeled_file+"features_test")
return train, validation, test
def read_smerp_labeled():
file_names = [0,1,2,3]
lab = OrderedDict()
for file in file_names:
# print("Reading file: ","smerp"+str(file)+".json")
single = read_json_array("smerp"+str(file))
for i, val in single.items():
if i in lab:
lab[i]["classes"].append(file)
else:
lab[i]=val
lab[i]["classes"]=[]
lab[i]["classes"].append(file)
# print("Finished file: ","smerp"+str(file)+".json")
# lab = merge_dicts(lab,single)
return lab
def read_json_array(json_array_file):
print("Method: read_json_array(json_array_file)")
json_array = OrderedDict()
data = open(json_array_file + '.json')
f = json.load(data)
for line in f:
line = json.loads(line)
try:
tweet_text = line["retweeted_status"]["text"]
except KeyError:
tweet_text = line["text"]
tweet_text = unicodedata.normalize('NFKD', tweet_text).encode('ascii',
'ignore').decode("utf-8")
json_array[line["id_str"]] = line
# TODO: read ids with "id" key. No need to change structure at all.
# TODO: Only add parsed_text, classes and features
json_array[line["id_str"]]['parsed_tweet'] = mm.parse_tweet(tweet_text)
json_array[line["id_str"]]['classes'] = []
return json_array
def read_json_array_nochange(json_array_file,label=False):
print("Method: read_json_array(json_array_file)")
json_array = OrderedDict()
data = open(json_array_file + '.json')
data = json.load(data)
for line in data:
# line = json.loads(line)
# print(line)
try:
tweet_text = line["retweeted_status"]["text"]
except KeyError:
tweet_text = line["text"]
tweet_text = unicodedata.normalize('NFKD', tweet_text).encode('ascii',
'ignore').decode("utf-8")
json_array[line["id_str"]] = line
json_array[line["id_str"]]['parsed_tweet'] = mm.parse_tweet(tweet_text)
if label:
json_array[line["id_str"]]['classes'] = [label]
else:
json_array[line["id_str"]]['classes'] = []
return json_array
def write_file(data,filename,file_path='',overwrite=False,mode='w',date_time_tag=''):
"""
Writes to file as string
:param data:
:param filename:
:param file_path:
:param overwrite:
:param mode:
:param date_time_tag:
:return:
"""
if not overwrite and os.path.exists(os.path.join(file_path,date_time_tag+filename+".txt")):
print("File already exists and Overwrite == False.")
return True
with open(os.path.join(file_path,date_time_tag+filename+".txt"),mode,encoding="utf-8") as text_file:
print("Saving text file: ", os.path.join(file_path,date_time_tag+filename+".txt"))
text_file.write(str(data))
text_file.write("\n")
text_file.write("\n")
text_file.close()
return True
def load_npz(filename,file_path=''):
"""
Loads numpy objects from npz files.
:param filename:
:param file_path:
:return:
"""
print("Reading NPZ file: ",os.path.join(file_path,filename + ".npz"))
if os.path.exists(os.path.join(file_path,filename + ".npz")):
npz = sparse.load_npz(os.path.join(file_path,filename + ".npz"))
return npz
else:
print("Warning: Could not open file: ",os.path.join(file_path,filename + ".npz"))
return False
def save_npz(data,filename,file_path='',overwrite=True):
"""
Saves numpy objects to file.
:param data:
:param filename:
:param file_path:
:param overwrite:
:return:
"""
print("Saving NPZ file: ",os.path.join(file_path,filename + ".npz"))
if not overwrite and os.path.exists(os.path.join(file_path,filename + ".npz")):
print("File already exists and Overwrite == False.")
return True
try:
sparse.save_npz(os.path.join(file_path,filename + ".npz"),data)
return True
except Exception as e:
print("Could not write to npz file:",os.path.join(file_path,filename + ".npz"))
print("Failure reason:",e)
return False
def read_file(file_name, mode='r', tag=False):
# print("Reading file: ",file_name)
if tag:
with open(date_time_tag + file_name, mode,
encoding="utf-8") as in_file:
data = str(in_file.read())
in_file.close()
else:
with open(file_name, mode, encoding="utf-8") as in_file:
data = str(in_file.read())
in_file.close()
return data
def read_files_folder(folder, mode='r',type='json'):
"""Reads all [type] files in a folder"""
files_dict = OrderedDict()
import glob
files = glob.glob(folder + '/*.'+type)
for file_name in files:
files_dict[file_name] = read_file(os.path.join(folder, file_name),
mode=mode)
return files_dict
def save_json(data,filename,file_path='',overwrite=False,indent=2,date_time_tag=''):
"""
:param data:
:param filename:
:param file_path:
:param overwrite:
:param indent:
:param date_time_tag:
:return:
"""
import json
print("Saving JSON file: ", os.path.join(file_path,date_time_tag+filename+".json"))
if not overwrite and os.path.exists(os.path.join(file_path,date_time_tag+filename+".json")):
print("File already exists and Overwrite == False.")
return True
try:
with open(os.path.join(file_path,date_time_tag+filename+".json"),'w') as json_file:
try:
json_file.write(json.dumps(data, indent=indent))
except Exception as e:
print("Writing json as string:",os.path.join(file_path,date_time_tag+filename+".json"))
json_file.write(json.dumps(str(data), indent=indent))
return True
json_file.close()
return True
except Exception as e:
print("Could not write to json file:",os.path.join(file_path,filename))
print("Failure reason:",e)
print("Writing file as plain text:",filename+".txt")
write_file(data,filename,date_time_tag=date_time_tag)
return False
def load_json(filename,file_path='',date_time_tag=''):
"""
Loads json file as python OrderedDict
:param filename:
:param file_path:
:param date_time_tag:
:return: OrderedDict
"""
# print("Reading JSON file: ",os.path.join(file_path,date_time_tag+filename+".json"))
if os.path.exists(os.path.join(file_path,date_time_tag+filename+".json")):
with open(os.path.join(file_path,date_time_tag+filename+".json"), encoding="utf-8") as file:
json_dict = OrderedDict(json.load(file))
file.close()
return json_dict
else:
print("Warning: Could not open file:",os.path.join(file_path,date_time_tag+filename+".json"))
return False
def train_test_read_split(data, test_size=0.3, validation_size=0.3):
"""Splits json file into Train, Validation and Test"""
print("Method: train_test_read_split(dict,test_size=0.3,validation_size=0.3",
"validation=True)")
train,test=mm.split_data(data,test_size)
print("train size:",len(train))
print("test size:",len(test))
train,validation=mm.split_data(train,validation_size)
print("validation size:",len(validation))
return train,validation,test
def save_pickle(data,pkl_file_name,pkl_file_path,overwrite=False,tag=False):
"""
saves python object as pickle file
:param data:
:param pkl_file_name:
:param pkl_file_path:
:param overwrite:
:return:
"""
# print("Method: save_pickle(data, pkl, tag=False)")
print("Writing to pickle file: ",os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
if not overwrite and os.path.exists(os.path.join(pkl_file_path,pkl_file_name + ".pkl")):
print("File already exists and Overwrite == False.")
return True
try:
if tag:
if os.path.exists(date_time_tag + os.path.join(pkl_file_path,pkl_file_name + ".pkl")):
print("Overwriting on pickle file: ", date_time_tag + os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
with open(date_time_tag + os.path.join(pkl_file_path,pkl_file_name + ".pkl"), 'wb') as pkl_file:
pickle.dump(data,pkl_file)
pkl_file.close()
return True
else:
if os.path.exists(os.path.join(pkl_file_path,pkl_file_name + ".pkl")):
print("Overwriting on pickle file: ", os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
with open(os.path.join(pkl_file_path,pkl_file_name + ".pkl"), 'wb') as pkl_file:
pickle.dump(data,pkl_file)
pkl_file.close()
return True
except Exception as e:
print("Could not write to pickle file: ", os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
print("Failure reason: ", e)
return False
def load_pickle(pkl_file_name,pkl_file_path):
"""
Loads pickle file from files.
:param pkl_file_name:
:param pkl_file_path:
:return:
"""
print("Method: load_pickle(pkl_file)")
try:
if os.path.exists(os.path.join(pkl_file_path,pkl_file_name + ".pkl")):
print("Reading pickle file: ",os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
with open(os.path.join(pkl_file_path,pkl_file_name + ".pkl"),'rb') as pkl_file:
loaded = pickle.load(pkl_file)
return loaded
except Exception as e:
print("Could not write to pickle file:", os.path.join(pkl_file_path,pkl_file_name + ".pkl"))
print("Failure reason:", e)
return False
def read_results(result_file_name):
if result_file_name.endswith('.json'):
name,ext = os.path.splitext(result_file_name)
dataset,param,value = name.split()
def read_nips_papers():
dataset_name="nips-papers"
db_name ="nips-papers_db.sqlite"
nips_db = mm.connect_sqllite(dataset_name,db_name)
mm.get_db_details(nips_db)
# read_sqllite(nips_db,table_name,cols="*",fetch_one=False)
def read_Semantic_Scholar():
dataset_name="Semantic_Scholar"
dataset_file="papers-2017-02-21_80.json"
sem_scho = OrderedDict()
with open(os.path.join(mm.get_dataset_path(),dataset_name,dataset_file),'r',
encoding="utf-8") as ss_file:
for line in ss_file:
# line = line.decode('unicode_escape').encode('ascii','ignore') # to remove unicode characters
line = json.loads(line)
sem_scho[line['id']] = line['paperAbstract']
# print(sem_scho)
print(len(sem_scho))
mm.save_json(sem_scho,'sem_scho')
# write_file(sem_scho,'sem_scho', mode='w', tag=False)
# save_pickle(sem_scho,'sem_scho', tag=False)
def read_xlsx(file,sheets=""):
"""Reads xlsx file as pandas dataframe for each sheet"""
import pandas as pd
data = pd.ExcelFile(file, sheet_name=sheets)
xlsx_obj = OrderedDict()
for sheet in data.sheet_names:
xlsx_obj[sheet] = data.parse(sheet)
print(data.sheet_names)
return data, xlsx_obj
from scipy.io import arff
import pandas as pd
def load_arff(filename):
print(filename)
data = arff.loadarff(filename)
print(type(data))
df = pd.DataFrame(data[0])
print(df.head())
return data,df
def main():
read_nips_papers()
return
dict1 = {1:{}, 2:{}}
dict1 = OrderedDict(dict1)
dict1 = mm.tag_dict(dict1, 'h')
print(dict1)
pass
if __name__ == "__main__": main()