-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVD_getMatrixCompletion.py
371 lines (282 loc) · 11.1 KB
/
SVD_getMatrixCompletion.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
#http://radimrehurek.com/2014/03/data-streaming-in-python-generators-iterators-iterables/
import json
import gensim
import fio
import numpy, scipy.sparse
from scipy.sparse.linalg import svds as sparsesvd
import re
import porter
import pickle
import softImputeWrapper
import ILP_baseline as ILP
import os
phraseext = ".key" #a list
studentext = ".keys.source" #json
countext = ".dict" #a dictionary
lpext = ".lp"
lpsolext = ".sol"
ngramext = ".ngram.json"
corpusdictexe = ".corpus.dict"
cscexe = ".mat.txt"
mcexe = ".mc.txt"
ngramTag = "___"
def ProcessLine(line,ngrams=[1]):
#tokens = list(gensim.utils.tokenize(line, lower=True, errors='ignore'))
tokens = line.lower().split()
new_tokens = []
for n in ngrams:
ngram = ILP.getNgramTokenized(tokens, n, NoStopWords=True, Stemmed=True, ngramTag=ngramTag)
new_tokens = new_tokens + ngram
return " ".join(new_tokens)
def iter_folder(folder, extension, ngrams=[1]):
for subdir, dirs, files in os.walk(folder):
for file in sorted(files):
if not file.endswith(extension): continue
print file
document = open(file).readlines()
for line in document:
line = ProcessLine(line, ngrams)
#print line
# break document into utf8 tokens
yield gensim.utils.tokenize(line, lower=True, errors='ignore')
def iter_documents(outdir, types, sheets = range(0,25), np='syntax', ngrams=[1]):
"""
Generator: iterate over all relevant documents, yielding one
document (=list of utf8 tokens) at a time.
"""
print "types:", types
# find all .txt documents, no matter how deep under top_directory
for i, sheet in enumerate(sheets):
week = i + 1
dir = outdir + str(week) + '/'
for question in types:
prefix = dir + question + "." + np
filename = prefix + phraseext
if not fio.IsExist(filename): continue
document = open(prefix + phraseext).readlines()
for line in document:
line = ProcessLine(line,ngrams)
#print line
# break document into utf8 tokens
yield gensim.utils.tokenize(line, lower=True, errors='ignore')
def readbook(path, ngrams=[1]):
document = open(path).readlines()
for line in document:
line = re.sub( '\s+', ' ', line).strip()
if len(line) == 0: continue
line = ProcessLine(line, ngrams)
# break document into utf8 tokens
yield gensim.utils.tokenize(line, lower=True, errors='ignore')
class TxtSubdirsCorpus(object):
"""
Iterable: on each iteration, return bag-of-words vectors,
one vector for each document.
Process one document at a time using generators, never
load the entire corpus into RAM.
"""
def __init__(self, top_dir, types=['POI', 'MP', 'LP'], sheets = range(0,25), np='syntax', ngrams=[1]):
self.types = types
self.top_dir = top_dir
self.np = np
self.ngrams = ngrams
self.sheets = sheets
# create dictionary = mapping for documents => sparse vectors
self.dictionary = gensim.corpora.Dictionary(iter_documents(top_dir, types, sheets, np, ngrams))
def __iter__(self):
"""
Again, __iter__ is a generator => TxtSubdirsCorpus is a streamed iterable.
"""
for tokens in iter_documents(self.top_dir, self.types, self.sheets, self.np, self.ngrams):
# transform tokens (strings) into a sparse vector, one at a time
yield self.dictionary.doc2bow(tokens)
class TacCorpus(object):
def __init__(self, top_dir, ngrams=[1]):
self.top_dir = top_dir
self.dictionary = gensim.corpora.Dictionary(iter_documents(top_dir, ngrams))
def __iter__(self):
"""
Again, __iter__ is a generator => TxtSubdirsCorpus is a streamed iterable.
"""
for tokens in iter_folder(self.top_dir, self.ngrams):
# transform tokens (strings) into a sparse vector, one at a time
yield self.dictionary.doc2bow(tokens)
class BookCorpus(object):
"""
Iterable: on each iteration, return bag-of-words vectors,
one vector for each document.
Process one document at a time using generators, never
load the entire corpus into RAM.
"""
def __init__(self, path, ngrams=[1]):
self.path = path
self.ngrams = ngrams
# create dictionary = mapping for documents => sparse vectors
self.dictionary = gensim.corpora.Dictionary(readbook(path, ngrams))
def __iter__(self):
"""
Again, __iter__ is a generator => TxtSubdirsCorpus is a streamed iterable.
"""
for tokens in readbook(self.path, self.ngrams):
# transform tokens (strings) into a sparse vector, one at a time
yield self.dictionary.doc2bow(tokens)
def SaveCSC2(csc, filename):
s = csc.shape
m = s[0]
n = s[1]
body = []
for i in range(m):
row = []
for j in range(n):
row.append(csc[i, j])
body.append(row)
fio.WriteMatrix(filename, body, header=None)
def SaveCSC(csc, filename):
A = csc.toarray()
s = csc.shape
m = s[0]
n = s[1]
data = []
for i in range(m):
row = []
for j in range(n):
x = A[i][j]
if x != 0:
row.append([j, A[i][j]])
data.append(row)
with open(filename, 'w') as fin:
json.dump(data, fin, indent = 2)
def SaveSparseMatrix(A, filename):
m = len(A)
n = len(A[0])
data = []
for i in range(m):
row = []
for j in range(n):
x = A[i][j]
if x != 0:
row.append([j, A[i][j]])
data.append(row)
with open(filename, 'w') as fin:
json.dump(data, fin, indent = 2)
def SaveNewA(A, dict, path, ngrams, prefixname="", sheets = range(0,25), np='sentence', types=['POI', 'MP', 'LP']):
TotoalLine = 0
for i in sheets:
week = i + 1
dir = path + str(week) + '/'
for type in types:
prefix = dir + type + "." + np
print prefix
if not fio.IsExist(prefix + phraseext):
print prefix + phraseext
continue
document = open(prefix + phraseext).readlines()
LineRange = range(TotoalLine, TotoalLine + len(document))
TotoalLine = TotoalLine + len(document)
Bigrams = []
for line in document:
line = ProcessLine(line, ngrams)
tokens = list(gensim.utils.tokenize(line, lower=True, errors='ignore'))
Bigrams = Bigrams + tokens
PartA = {}
for bigram in set(Bigrams):
if bigram not in dict:
print "error", bigram
id = dict[bigram]
row = A[id]
PartA[bigram] = [row[x] for x in LineRange]
svdAname = dir + type + '.' +prefixname + '.softA'
print svdAname
with open(svdAname, 'w') as fout:
json.dump(PartA, fout, indent=2)
def ToBinary(csc):
A = csc.toarray()
s = csc.shape
m = s[0]
n = s[1]
m = len(A)
n = len(A[0])
for i in range(m):
row = []
for j in range(n):
if A[i][j] >= 1:
A[i][j] = 1
return A
def CheckBinary(A):
m = len(A)
n = len(A[0])
for i in range(m):
row = []
for j in range(n):
if A[i][j] != 0 and A[i][j] != 1: return False
return True
def getSVD(prefix, np, corpusname, ngrams, rank_max, softImpute_lambda, binary_matrix, output, types = ['POI', 'MP', 'LP']):
#types = ['POI', 'MP', 'LP']
path = prefix
sheets = range(0,26)
dictname = output + "_".join(types) + '_' + corpusname + corpusdictexe
# # that's it! the streamed corpus of sparse vectors is ready
# if corpusname=='book':
# corpus = BookCorpus(np, ngrams)
# elif corpusname == 'tac':
# corpus = TacCorpus(prefix, ngrams)
# dictname = path + '_' + corpusname + corpusdictexe
# else:
# corpus = TxtSubdirsCorpus(prefix, types, sheets, np, ngrams)
#
# fio.SaveDict2Json(corpus.dictionary.token2id, dictname)
#
# # or run truncated Singular Value Decomposition (SVD) on the streamed corpus
# #from gensim.models.lsimodel import stochastic_svd as svd
# #u, s = svd(corpus, rank=300, num_terms=len(corpus.dictionary), chunksize=5000)
#
# #https://pypi.python.org/pypi/sparsesvd/
# scipy_csc_matrix = gensim.matutils.corpus2csc(corpus)
# print scipy_csc_matrix.shape
#
# print "binary_matrix: ", binary_matrix
#
# A = ToBinary(scipy_csc_matrix)
#
# rank = rank_max
# print rank
#
# name = 'X'
# newA = softImputeWrapper.SoftImpute(A.T, rank=rank, Lambda=softImpute_lambda, name=name, folder=output)
prefix = str("500_2.0")
newA = softImputeWrapper.LoadMC(Lambda=prefix, name='newX', folder=output)
if newA != None:
print newA.shape
prefix = '2.0'
token2id = fio.LoadDictJson(dictname)
SaveNewA(newA, token2id, path, ngrams, prefix, np=np, types=types)
def TestProcessLine():
line = "how to determine the answers to part iii , in the activity ."
print ProcessLine(line, [1, 2]).split()
tokens = line.lower().split()
ngrams = []
for n in [1,2]:
grams = ILP.getNgramTokenized(tokens, n, NoStopWords=True, Stemmed=True)
ngrams = ngrams + grams
print ngrams
def getMC_IE256():
ILP_dir = "../../data/IE256/MC/"
outdir = "../../data/matrix/exp8/"
#TestProcessLine()
from config import ConfigFile
config = ConfigFile(config_file_name='config_IE256.txt')
for np in ['sentence']:
getSVD(ILP_dir, np, corpusname='corpus', ngrams=config.get_ngrams(), rank_max = config.get_rank_max(), softImpute_lambda = config.get_softImpute_lambda(), binary_matrix = config.get_binary_matrix(), output=outdir, types=['q1','q2'])
print "done"
if __name__ == '__main__':
getMC_IE256()
exit(-1)
excelfile = "../../data/2011Spring_norm.xls"
sennadatadir = "../../data/senna/"
ILP_dir = "../../data/IE256/MC/"
outdir = ILP_dir
#TestProcessLine()
from config import ConfigFile
config = ConfigFile(config_file_name='tac_config.txt')
for np in ['sentence']:
getSVD(ILP_dir, np, corpusname='corpus', ngrams=config.get_ngrams(), rank_max = config.get_rank_max(), softImpute_lambda = config.get_softImpute_lambda(), binary_matrix = config.get_binary_matrix(), output=outdir)
print "done"