Skip to content

Commit

Permalink
Refactoring TypeInference
Browse files Browse the repository at this point in the history
  • Loading branch information
datumbox committed Apr 17, 2015
1 parent 195c25d commit 6acf286
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/datumbox/common/dataobjects/Dataset.java
Expand Up @@ -104,7 +104,7 @@ public static Dataset parseCSVFile(Reader reader, Map<String, TypeInference.Data
String column = entry.getKey(); String column = entry.getKey();
TypeInference.DataType dataType = entry.getValue(); TypeInference.DataType dataType = entry.getValue();


Object value = TypeInference.parse(row.get(column), dataType); //parse the string value according to the DataType Object value = TypeInference.DataType.parse(row.get(column), dataType); //parse the string value according to the DataType
if (yColumnName.equals(column)) { if (yColumnName.equals(column)) {
y = value; y = value;
} }
Expand Down
86 changes: 44 additions & 42 deletions src/main/java/com/datumbox/common/dataobjects/TypeInference.java
Expand Up @@ -32,6 +32,49 @@ public enum DataType {
ORDINAL(Short.class), //ordinal variable => the value is short ORDINAL(Short.class), //ordinal variable => the value is short
NUMERICAL(Number.class), //numberical variable => the value is any numeric except of short NUMERICAL(Number.class), //numberical variable => the value is any numeric except of short
CATEGORICAL(Object.class); //categorical variable => the value is anything else CATEGORICAL(Object.class); //categorical variable => the value is anything else

/**
* Takes a String and tries to translate it to the provided DataType.
* The result value is always casted to Object.
*
* @param s
* @param dataType
* @return
*/
protected static Object parse(String s, DataType dataType) {
if(s==null || s.isEmpty() || s.toLowerCase().equals("null")) {
return null;
}

if(dataType == DataType.BOOLEAN) {
switch (s.toLowerCase()) {
case "1":
case "true":
case "yes":
return Boolean.TRUE;
case "0":
case "false":
case "no":
return Boolean.FALSE;
default:
return null;
}
}
else if (dataType == DataType.ORDINAL) {
return Short.valueOf(s);
}
else if (dataType == DataType.NUMERICAL) {
return Double.valueOf(s);
}
else if (dataType == DataType.CATEGORICAL) {
return s;
}
else {
//can happen if null DataType is provided
throw new RuntimeException("Unknown Datatype");
}
}



private final Class klass; private final Class klass;


Expand All @@ -45,53 +88,12 @@ private DataType(Class klass) {
* @param v * @param v
* @return * @return
*/ */
public boolean isInstance(Object v) { private boolean isInstance(Object v) {
return klass.isInstance(v); return klass.isInstance(v);
} }


} }


/**
* Takes a String and tries to translate it to the provided DataType.
* The result value is always casted to Object.
*
* @param s
* @param dataType
* @return
*/
public static Object parse(String s, DataType dataType) {
if(s==null || s.isEmpty() || s.toLowerCase().equals("null")) {
return null;
}

if(dataType == DataType.BOOLEAN) {
switch (s.toLowerCase()) {
case "1":
case "true":
case "yes":
return Boolean.TRUE;
case "0":
case "false":
case "no":
return Boolean.FALSE;
default:
return null;
}
}
else if (dataType == DataType.ORDINAL) {
return Short.valueOf(s);
}
else if (dataType == DataType.NUMERICAL) {
return Double.valueOf(s);
}
else if (dataType == DataType.CATEGORICAL) {
return s;
}
else {
//can happen if null DataType is provided
throw new RuntimeException("Unknown Datatype");
}
}


/** /**
* Detects the DataType of a particular value. * Detects the DataType of a particular value.
Expand Down

0 comments on commit 6acf286

Please sign in to comment.