use of smile.data.SparseDataset in project smile by haifengl.
the class SparseDatasetParser method parse.
/**
* Parse a sparse dataset from an input stream.
* @param name the name of dataset.
* @param stream the input stream of data.
* @throws java.io.IOException
*/
public SparseDataset parse(String name, InputStream stream) throws IOException, ParseException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
try {
// process header
int nrow = 1;
String line = reader.readLine();
for (; nrow <= 3 && line != null; nrow++) {
String[] tokens = line.trim().split(" ");
if (tokens.length >= 3) {
break;
}
line = reader.readLine();
}
if (line == null) {
throw new IOException("Empty data source.");
}
SparseDataset sparse = new SparseDataset(name);
do {
String[] tokens = line.trim().split(" ");
if (tokens.length != 3) {
throw new ParseException("Invalid number of tokens.", nrow);
}
int d = Integer.parseInt(tokens[0]) - arrayIndexOrigin;
int w = Integer.parseInt(tokens[1]) - arrayIndexOrigin;
double c = Double.parseDouble(tokens[2]);
sparse.set(d, w, c);
line = reader.readLine();
nrow++;
} while (line != null);
return sparse;
} finally {
reader.close();
}
}
Aggregations