forked from dongfang91/Triplet-Search-ConNorm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_files.py
119 lines (89 loc) · 2.59 KB
/
read_files.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
# encoding: utf-8
import csv
import json
import os
import pickle
import shutil
import sys
def create_folder(filename):
if "\\" in filename:
a = '\\'.join(filename.split('\\')[:-1])
else:
a = '/'.join(filename.split('/')[:-1])
if not os.path.exists(a):
os.makedirs(a)
def save_in_json(filename, array):
create_folder(filename)
with open(filename + '.txt', 'w') as outfile:
json.dump(array, outfile)
print("Save into files: ", filename)
outfile.close()
def read_from_json(filename):
with open(filename + '.txt', 'r') as outfile:
data = json.load(outfile)
outfile.close()
return data
def save_in_pickle(file, array):
create_folder(file)
with open(file, 'wb') as handle:
pickle.dump(array, handle, protocol=4)
def read_from_pickle(file):
with open(file, 'rb') as handle:
if sys.version_info[0] == 2:
data = pickle.load(handle)
else:
data = pickle.load(handle, encoding='latin1')
return data
def readfrom_txt(path):
data = open(path).read()
return data
def textfile2list(path):
data = readfrom_txt(path)
txt_list = list()
for line in data.splitlines():
txt_list.append(line)
return txt_list
def read_from_tsv_umls(filename):
data = readfrom_txt(filename)
dataset = []
for line in data.splitlines():
row = line.split("\t")
dataset.append(row)
return dataset
def read_from_tsv(filename):
with open(filename, 'r') as mycsvfile:
files = csv.reader(mycsvfile, delimiter='\t')
dataset = list()
for row in files:
dataset.append(row)
return dataset
def read_from_csv(filename):
with open(filename, 'r') as mycsvfile:
files = csv.reader(mycsvfile, delimiter=',')
dataset = list()
for row in files:
dataset.append(row)
return dataset
def save_in_tsv(filename, items):
create_folder(filename)
with open(filename, 'w', encoding='utf8', newline='') as tsv_file:
tsv_writer = csv.writer(tsv_file, delimiter='\t', lineterminator='\n')
for item in items:
tsv_writer.writerow(item)
def save_in_txt(filename, items):
create_folder(filename)
with open(filename, 'w') as f:
for item in items:
f.write("%s\n" % item)
def add_dict(term, key, value):
if key not in term:
term[key] = [value]
else:
term[key].append(value)
return term
def add_dict_count(term, key, counts):
if key not in term:
term[key] = 1
else:
term[key] += 1
return term