-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSet.py
265 lines (247 loc) · 9.13 KB
/
DataSet.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
#!/usr/bin/env python
# encoding: utf-8
'''
@author: Jasperyang
@license: (C) Copyright 2013-2017, Jasperyang Corporation Limited.
@contact: [email protected]
@software: GibbsLDA
@file: DataSet.py
@time: 3/6/17 7:27 PM
@desc:
'''
import Constants
from Document import *
from Strtokenizer import *
class DataSet(object):
def __init__(self,*argv):
if len(argv) == 0:
self.docs = []
self._docs = [] # used only for inference
self._id2id = {} # also used only for inference
self.M = '' # number of documents
self.V = '' # number of words
elif len(argv) == 1:
self.docs = []
self._docs = [] # used only for inference
self._id2id = {} # also used only for inference
self.M = argv[0] # number of documents
self.V = '' # number of words
else:
print("invalid init")
def __del__(self):
return
def add_doc(self,doc,idx):
if 0 <= idx and idx < self.M :
if len(self.docs) > idx :
self.docs[idx] = doc
else :
self.docs.append(doc)
def _add_doc(self,_doc,idx):
if 0 <= idx and idx < self.M :
if len(self._docs) > idx:
self._docs[idx] = _doc
else :
self._docs.append(_doc)
# @tested
def write_wordmap(self,wordmapfile,pword2id):
fout = open(wordmapfile,'w',encoding = 'utf8')
if fout == None :
print("Cannot open file ", wordmapfile, " to write!\n")
return 1
fout.write(str(len(pword2id)) + "\n")
for key,value in pword2id.items():
fout.write(key + ' ' + str(value) +'\n')
fout.close()
return 0
# @tested
def read_wordmap1(self,wordmapfile,pword2id):
pword2id.clear() # 清空字典
fin = open(wordmapfile,encoding = 'utf8')
if fin == None :
print("Cannot open file ",wordmapfile," to read!\n")
return 1
line = fin.readline()
nwords = int(line)
for i in range(nwords):
line = fin.readline()
strtok = Strtokenizer(line,' \t\r\n')
if strtok.count_tokens() != 2 :
continue
pword2id[strtok.token(0)] = int(strtok.token(1))
fin.close()
return 0
# @tested
def read_wordmapdict(self,wordmapfile,pid2word,KN):
for i in range(KN):
pid2word.append({})
self.read_wordmap2(wordmapfile+'-'+str(i),pid2word[i])
def read_wordmap2(self,wordmapfile,pid2word):
pid2word.clear()
fin = open(wordmapfile,encoding = 'utf8')
if fin == None :
print("Cannot open file ", wordmapfile, " to read!\n")
return 1
line = fin.readline()
nword = int(line)
for i in range(nword) :
line = fin.readline()
strtok = Strtokenizer(line,' \t\r\n')
if strtok.count_tokens() != 2 :
continue
pid2word[strtok.token(1)] = strtok.token(0)
fin.close()
return 0
# @tested
# 读取训练数据 生成wordmap.txt 里面对应的是单词和单词id
# 每个doc里面存储的是里面所有的word对应的id
def read_trndata(self,dfile,wordmapfile,KN):
word2id = []
for i in range(KN):
word2id.append({})
fin = open(dfile, encoding='UTF-8')
if fin == None :
print("Cannot open file ", wordmapfile, " to read!\n")
return 1
line = fin.readline()
self.M = int(line)
if self.M <= 0 :
print("No document available!\n")
return 1
self.V = []
for i in range(self.M) :
line = fin.readline()
strtok = Strtokenizer(line,' \t\r\n') # 按照一行一行来划分(其实一行就是一篇文档)
length = int(strtok.count_tokens() / KN)
if length <= 0 :
print("Invalid (empty) document!\n")
self.M = self.V =0
return 1
pdoc = Document(length,KN)
for j in range(strtok.count_tokens()) :
index = j % KN
found = strtok.token(j) in word2id[index].keys()
if not found : # 没找到
pdoc.words[index].append(len(word2id[index])) # 这篇文档里有哪个编号的单词,加进去
word2id[index][strtok.token(j)] = len(word2id[index]) # 给每个单词标号
else :
pdoc.words[index].append(word2id[index][strtok.token(j)])
self.add_doc(pdoc,i)
fin.close()
for i in range(KN):
self.write_wordmap(wordmapfile+'-'+str(i),word2id[i])
for i in range(KN):
self.V.append(len(word2id[i]))
return 0
# @tested
# 读取预测数据
# id2_id : id是原文档中单词对应id , _id是新的文档中出现的原文档的词的编号(也是从0开始的,这个很难理解)
# _id2id : 以后可以用到
# 一个神奇的理解了很久的地方,就是新的文档中的出现的新词是不管的
def read_newdata(self,dfile,wordmapfile):
word2id = {}
id2_id = {}
self.read_wordmap1(wordmapfile,word2id) # 从wordmapfile中读入word2id
if len(word2id) <= 0 :
print("No word map available!\n")
return 1
fin = open(dfile)
if fin == None :
print("Cannot open file ",dfile," to read!\n")
return 1
line = fin.readline()
# get the number of new documents
self.M = int(line)
if self.M <= 0 :
print("No document available!\n")
return 1
self.V = 0
for i in range(self.M) :
line = fin.readline()
strtok = Strtokenizer(line,' \t\r\n')
length = strtok.count_tokens()
doc = []
_doc = []
for j in range(length) :
found = False
for key in word2id.keys():
if strtok.token(j) == key:
found = True
break
if found: # 找到 key
found2 = False
for value in id2_id.values() :
if value == word2id[strtok.token(j)] :
found2 = True
break
if not found2 : # 没找到 value
_id = len(id2_id)
id2_id[word2id[strtok.token(j)]] = _id
self._id2id[_id] = word2id[strtok.token(j)]
else : # 找到
_id = word2id[strtok.token(j)]
doc.append(word2id[strtok.token(j)])
_doc.append(_id)
else: # 没找到
tmp = ''
# 没决定要做什么
pdoc = Document(doc)
_pdoc = Document(_doc)
self.add_doc(pdoc,i)
self._add_doc(_pdoc,i)
fin.close()
self.V = len(id2_id)
return 0
# @tested
def read_newdata_withrawstrs(self,dfile,wordmapfile):
word2id = {}
id2_id = {}
self.read_wordmap1(wordmapfile,word2id) # 从wordmapfile中读入word2id
if len(word2id) <= 0 :
print("No word map available!\n")
return 1
fin = open(dfile)
if fin == None :
print("Cannot open file ",dfile," to read!\n")
return 1
line = fin.readline()
self.M = int(line)
if self.M <= 0 :
print("No document available!\n")
return 1
self.V = 0
for i in range(self.M) :
line = fin.readline()
strtok = Strtokenizer(line,' \t\r\n')
length = strtok.count_tokens()
doc = []
_doc = []
for j in range(length) :
found = False
for key in word2id.keys():
if strtok.token(j) == key:
found = True
break
if found: # 找到 key
found2 = False
for value in id2_id.values() :
if value == word2id[strtok.token(j)] :
found2 = True
break
if not found2 : # 没找到 value
_id = len(id2_id)
id2_id[word2id[strtok.token(j)]] = _id
self._id2id[_id] = word2id[strtok.token(j)]
else : # 找到
_id = word2id[strtok.token(j)]
doc.append(word2id[strtok.token(j)])
_doc.append(_id)
else: # 没找到
tmp = ''
# 没决定要做什么
pdoc = Document(doc,line)
_pdoc = Document(_doc,line)
self.add_doc(pdoc,i)
self._add_doc(_pdoc,i)
fin.close()
self.V = len(id2_id)
return 0