forked from blei-lab/diln
-
Notifications
You must be signed in to change notification settings - Fork 0
/
importData.c
238 lines (220 loc) · 5.8 KB
/
importData.c
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
// (C) Copyright 2010, John Paisley, Chong Wang and David M. Blei
// This file is part of DILN-C.
// DILN-C is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your
// option) any later version.
// DILN-C is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include "importData.h"
#include "gsl_wrapper.h"
int countVecSize(char * filename)
// get the dimensionality of the vector written in filename
{
int M = 0;
int c;
FILE *file = fopen(filename,"r");
while ((c = getc(file)) != EOF)
{
if (c == '\n')
M++;
}
fclose(file);
return M;
}
int countMatRows(char * filename)
// get the number of rows in the matrix written in filename (csv file)
{
int M = 0;
int c;
FILE * file = fopen(filename,"r");
while ((c = getc(file)) != EOF)
{
if (c == '\n')
M++;
}
fclose(file);
return M;
}
int countMatCols(char * filename)
// get the number of columns in the matrix written in filename (csv file)
{
int N = 0;
int c;
FILE * file = fopen(filename,"r");
while ((c = getc(file)) != '\n')
{
if (c == ',')
N++;
}
N++;
fclose(file);
return N;
}
void readVector(char * filename, gsl_vector * y)
// read in a vector from filename
{
double val;
int i;
int M = y->size;
FILE * file = fopen(filename,"r");
for (i = 0; i < M; i++)
{
fscanf(file, "%lf", &val);
gsl_vector_set(y,i,val);
}
fclose(file);
}
void readMatrix(char * filename, gsl_matrix * X)
// read in a matrix from filename (csv file)
{
int i, c, m = 0, n = 0;
double val;
int M = X->size1;
int N = X->size2;
FILE * file = fopen(filename,"r");
for (i = 0; i < M*N; i++)
{
fscanf(file, "%lf", &val);
gsl_matrix_set(X,m,n,val);
n++;
if (n == N)
{
n = 0;
m++;
}
c = getc(file);
}
fclose(file);
}
void writeVector(char * filename, gsl_vector * y)
// write the vector y to filename
{
FILE * file = fopen(filename,"w");
int n;
int N = y->size;
for (n = 0; n < N; n++)
{
fprintf(file,"%lf",gsl_vector_get(y,n));
putc('\n',file);
}
fclose(file);
}
void writeMatrix(char * filename, gsl_matrix * X)
// write the matrix X to filename (as csv file)
{
FILE * file = fopen(filename,"w");
int m, n;
int M = X->size1;
int N = X->size2;
for (m = 0; m < M; m++)
{
for (n = 0; n < N; n++)
{
fprintf(file,"%lf",gsl_matrix_get(X,m,n));
putc(',',file);
}
putc('\n',file);
}
fclose(file);
}
void writeArray(char * filename, double y[], int N)
// write the array y to filename
{
FILE * file = fopen(filename,"w");
int n;
for (n = 0; n < N; n++)
{
fprintf(file,"%lf",y[n]);
putc('\n',file);
}
fclose(file);
}
int countDocs(char * filename)
// return the number of documents in filename
{
FILE * file = fopen(filename,"r");
int D = 0;
int c;
while ((c = getc(file)) != EOF)
{
if (c == '\n')
D++;
}
fclose(file);
return D;
}
void countText(char * filename, corpus * data)
// allocate space for the text data in filename
{
FILE * file = fopen(filename,"r");
int c;
int d = 0;
int numDocUnique = 0;
data->docs = malloc(sizeof(doc));
while ((c = getc(file)) != EOF)
{
if (c == ':')
numDocUnique++;
if (c == '\n')
{
data->docs = (doc*) realloc(data->docs, sizeof(doc)*(d+1));
data->docs[d].numUnique = numDocUnique;
numDocUnique = 0;
data->docs[d].wordCnt = gsl_vector_calloc(data->docs[d].numUnique);
data->docs[d].wordIdx = gsl_vector_calloc(data->docs[d].numUnique);
d++;
}
}
fclose(file);
}
void readText(char * filename, corpus * data)
// read in text data from filename
{
FILE * file = fopen(filename,"r");
int val;
int d, n, c;
int totNumWords = 0;
int docNumWords = 0;
for (d = 0; d < data->numDocs; d++)
{
n = 0;
fscanf(file, "%d", &val); // remove first term (not of interest)
while (n < data->docs[d].numUnique)
{
fscanf(file, "%d", &val);
vecSet(data->docs[d].wordIdx,n,val);
c = getc(file);
fscanf(file, "%d", &val);
vecSet(data->docs[d].wordCnt,n,val);
docNumWords = docNumWords + val;
totNumWords = totNumWords + val;
n++;
}
data->docs[d].numWords = docNumWords;
docNumWords = 0;
}
data->numWords = totNumWords;
fclose(file);
int maxval = 0;
for (d = 0; d < data->numDocs; d++)
{
for (n = 0; n < data->docs[d].numUnique; n++)
{
if (vecGet(data->docs[d].wordIdx,n) > maxval)
maxval = vecGet(data->docs[d].wordIdx,n);
}
}
maxval++;
data->vocabSize = maxval;
}