Search in sources :

Example 1 with DateAttribute

use of smile.data.DateAttribute in project smile by haifengl.

the class ArffParser method parseAttribute.

/**
     * Parses the attribute declaration.
     *
     * @return an attributes in this relation
     * @throws IOException 	if the information is not read
     * 				successfully
     */
private Attribute parseAttribute(StreamTokenizer tokenizer) throws IOException, ParseException {
    Attribute attribute = null;
    // Get attribute name.
    getNextToken(tokenizer);
    String attributeName = tokenizer.sval;
    getNextToken(tokenizer);
    // Check if attribute is nominal.
    if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
        // Attribute is real, integer, or string.
        if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_REAL) || tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_INTEGER) || tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_NUMERIC)) {
            attribute = new NumericAttribute(attributeName);
            readTillEOL(tokenizer);
        } else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_STRING)) {
            attribute = new StringAttribute(attributeName);
            readTillEOL(tokenizer);
        } else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_DATE)) {
            String format = null;
            if (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
                if ((tokenizer.ttype != StreamTokenizer.TT_WORD) && (tokenizer.ttype != '\'') && (tokenizer.ttype != '\"')) {
                    throw new ParseException("not a valid date format", tokenizer.lineno());
                }
                format = tokenizer.sval;
                readTillEOL(tokenizer);
            } else {
                tokenizer.pushBack();
            }
            attribute = new DateAttribute(attributeName, null, format);
            readTillEOL(tokenizer);
        } else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_RELATIONAL)) {
            readTillEOL(tokenizer);
        } else if (tokenizer.sval.equalsIgnoreCase(ARFF_END_SUBRELATION)) {
            getNextToken(tokenizer);
        } else {
            throw new ParseException("Invalid attribute type or invalid enumeration", tokenizer.lineno());
        }
    } else {
        // Attribute is nominal.
        List<String> attributeValues = new ArrayList<>();
        tokenizer.pushBack();
        // Get values for nominal attribute.
        if (tokenizer.nextToken() != '{') {
            throw new ParseException("{ expected at beginning of enumeration", tokenizer.lineno());
        }
        while (tokenizer.nextToken() != '}') {
            if (tokenizer.ttype == StreamTokenizer.TT_EOL) {
                throw new ParseException("} expected at end of enumeration", tokenizer.lineno());
            } else {
                attributeValues.add(tokenizer.sval.trim());
            }
        }
        String[] values = new String[attributeValues.size()];
        for (int i = 0; i < values.length; i++) {
            values[i] = attributeValues.get(i);
        }
        attribute = new NominalAttribute(attributeName, values);
    }
    getLastToken(tokenizer, false);
    getFirstToken(tokenizer);
    if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
        throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
    }
    return attribute;
}
Also used : NominalAttribute(smile.data.NominalAttribute) Attribute(smile.data.Attribute) NominalAttribute(smile.data.NominalAttribute) NumericAttribute(smile.data.NumericAttribute) DateAttribute(smile.data.DateAttribute) StringAttribute(smile.data.StringAttribute) StringAttribute(smile.data.StringAttribute) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) NumericAttribute(smile.data.NumericAttribute) DateAttribute(smile.data.DateAttribute)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Attribute (smile.data.Attribute)1 DateAttribute (smile.data.DateAttribute)1 NominalAttribute (smile.data.NominalAttribute)1 NumericAttribute (smile.data.NumericAttribute)1 StringAttribute (smile.data.StringAttribute)1