public AttributeDataset parse ( InputStream stream ) throws IOException , ParseException { try ( Reader r = new BufferedReader ( new InputStreamReader ( stream ) ) ) { StreamTokenizer tokenizer = new StreamTokenizer ( r ) ; initTokenizer ( tokenizer ) ; List < Attribute > attributes = new ArrayList <> ( ) ; String relationName = readHeader ( tokenizer , attributes ) ; if ( attributes . isEmpty ( ) ) { throw new IOException ( "no header information available" ) ; } Attribute response = null ; Attribute [ ] attr = new Attribute [ attributes . size ( ) ] ; attributes . toArray ( attr ) ; for ( int i = 0 ; i < attributes . size ( ) ; i ++ ) { if ( responseIndex == i ) { response = attributes . remove ( i ) ; break ; } } AttributeDataset data = new AttributeDataset ( relationName , attributes . toArray ( new Attribute [ attributes . size ( ) ] ) , response ) ; while ( true ) { // Check if end of file reached. getFirstToken ( tokenizer ) ; if ( tokenizer . ttype == StreamTokenizer . TT_EOF ) { break ; } // Parse instance if ( tokenizer . ttype == ' ' ) { readSparseInstance ( tokenizer , data , attr ) ; } else { readInstance ( tokenizer , data , attr ) ; } } for ( Attribute attribute : attributes ) { if ( attribute instanceof NominalAttribute ) { NominalAttribute a = ( NominalAttribute ) attribute ; a . setOpen ( false ) ; } if ( attribute instanceof StringAttribute ) { StringAttribute a = ( StringAttribute ) attribute ; a . setOpen ( false ) ; } } return data ; } }
Parse a dataset from given stream .