Search in sources :

Example 6 with SparseDataset

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();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SparseDataset(smile.data.SparseDataset) IOException(java.io.IOException) ParseException(java.text.ParseException)

Aggregations

SparseDataset (smile.data.SparseDataset)6 Test (org.junit.Test)4 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ParseException (java.text.ParseException)1 Attribute (smile.data.Attribute)1 NominalAttribute (smile.data.NominalAttribute)1 LibsvmParser (smile.data.parser.LibsvmParser)1 AdjustedRandIndex (smile.validation.AdjustedRandIndex)1 RandIndex (smile.validation.RandIndex)1