-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectorize_data.py
233 lines (196 loc) · 6.45 KB
/
vectorize_data.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
import click
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
def get_wiki_vocab(file):
"""
Build a vocabulary from Wikipedia articles.
Parameters
----------
file : str
File path to Wikipedia article text
Returns
-------
dict
Dictionary of words and their frequencies
"""
vocab = {}
with open(file, 'r', encoding='utf-8') as f:
line = f.readline()
while line:
if line[0:3]!='XXX':
line=line.strip('\n').split()
for token in line:
vocab[token.lower()] = vocab.get(token.lower(), 0) + 1
line=f.readline()
return vocab
def get_notes_vocab(file):
"""
Build a vocabulary from MIMIC notes text.
Parameters
----------
file : str
File path to combined_dataset from 1_preprocess_mimic.py
Returns
-------
dict
Dictionary of words and their frequencies
"""
vocab = {}
with open(file, 'r', encoding='utf-8') as f:
line = f.readline()
while line:
line = line.strip('\n').split()
if line[0] == 'codes:':
line = f.readline()
line = line.strip('\n').split()
if line[0] == 'notes:':
line = f.readline()
while line != 'end!\n':
line = line.strip('\n').split()
for token in line:
vocab[token.lower()] = vocab.get(token.lower(), 0) + 1
line = f.readline()
line = f.readline()
return vocab
def docs_to_strings(documents):
"""
Convert a list of lists of tokens to a list of strings.
Parameters
----------
documents : list
List of lists of tokens
Returns
-------
list
List of strings
"""
data = []
for doc in documents:
text = ''
for token in doc:
text += token + ' '
data.append(text)
return data
def get_wiki_documents(file, vocab):
"""
Get a list of lists of tokens from Wikipedia articles.
Parameters
----------
file : str
File path to Wikipedia article texts
vocab : dict
Dictionary of words and their frequencies
Returns
-------
list
List of lists of tokens
"""
documents = []
with open(file, 'r', encoding='utf-8') as f:
line = f.readline()
while line:
if line[0:4] == 'XXXd':
doc = []
line = f.readline()
while line[0:4] != 'XXXe':
line = line.strip('\n').split()
for token in line:
if token.lower() in vocab:
doc.append(token.lower())
line = f.readline()
documents.append(doc)
line = f.readline()
return documents
def get_notes(file, vocab):
"""
Get a list of lists of tokens from MIMIC notes.
Parameters
----------
file : str
File path to combined_dataset from 1_preprocess_mimic.py
vocab : dict
Dictionary of words and their frequencies
Returns
-------
list
List of lists of tokens
"""
documents = []
with open(file, 'r', encoding='utf-8') as f:
line = f.readline()
while line:
line = line.strip('\n').split()
if line[0] == 'codes:':
line = f.readline()
line = line.strip('\n').split()
if line[0] == 'notes:':
doc = []
line = f.readline()
while line != 'end!\n':
line = line.strip('\n').split()
for token in line:
if token.lower() in vocab:
doc.append(token)
line = f.readline()
documents.append(doc)
line = f.readline()
return documents
def vectorize(data, output_file, vectorizer):
"""
Apply a vectorizer to a list of strings.
Parameters
----------
data : list
List of strings
output_file : str
File path to save vectorized data
vectorizer : sklearn.feature_extraction.text.CountVectorizer or sklearn.feature_extraction.text.TfidfVectorizer
Vectorizer to apply
"""
embed = vectorizer.fit_transform(data)
embed = embed.A
embed = np.array(embed, dtype=float)
np.save(output_file, embed)
def process_data(file_wiki,
file_mimic,
output_wiki,
output_mimic,
vectorizer_type):
wiki_vocab = get_wiki_vocab(file_wiki)
note_vocab = get_notes_vocab(file_mimic)
vocab = set(note_vocab.keys()).intersection(set(wiki_vocab.keys()))
wiki_docs = get_wiki_documents(file_wiki, vocab)
notes = get_notes(file_mimic, vocab)
vocab_map = {}
for note in notes:
for token in note:
if token.lower() not in vocab_map.keys():
vocab_map[token.lower()] = len(vocab_map)
wiki_docs = docs_to_strings(wiki_docs)
notes = docs_to_strings(notes)
if vectorizer_type == 'binary':
vectorizer = CountVectorizer(min_df=1, vocabulary=vocab_map, binary=True)
elif vectorizer_type == 'count':
vectorizer = TfidfVectorizer(min_df=1, vocabulary=vocab_map, use_idf=False)
elif vectorizer_type == 'tfidf':
vectorizer = TfidfVectorizer(min_df=1, vocabulary=vocab_map)
vectorize(wiki_docs, output_wiki, vectorizer)
vectorize(notes, output_mimic, vectorizer)
@click.command()
@click.option('--file_wiki', default='data/wikipedia_knowledge')
@click.option('--file_mimic', default='data/combined_dataset')
@click.option('--output_wiki', default='data/wikivec')
@click.option('--output_mimic', default='data/notevec')
@click.option('--vectorizer_type', default='binary', type=click.Choice(['binary', 'count', 'tfidf']))
def process_data_(file_wiki,
file_mimic,
output_wiki,
output_mimic,
vectorizer_type):
process_data(file_wiki,
file_mimic,
output_wiki,
output_mimic,
vectorizer_type)
if __name__ == "__main__":
process_data_()