@Test public void testParseFeatureVectorFromString() { String strNegFV = "-1;1:1.0;2:2.0;3:3.0"; String strPosFV = "1;1:1.0;2:2.0;3:3.0"; String malformedFV = "1.0;1:1.0;2:2.0;3;3.0"; FeatureVector fv = new FeatureVector(3); FeatureVectorParser.parseFeatureVectorFromString(fv, strNegFV); if (fv.size() != 3) fail("Failed to correctly parse String to FeatureVector"); if(fv.getClassLabel() != ClassLabel.NEGATIVE) fail("Failed to correctly parse negative FeatureVector"); for (int i = 0; i < fv.size(); i++) if (fv.get(i) != i + 1.0) fail("Failed to correctly parse String to FeatureVector"); FeatureVectorParser.parseFeatureVectorFromString(fv, strPosFV); if (fv.size() != 3) fail("Failed to correctly parse String to FeatureVector"); if(fv.getClassLabel() != ClassLabel.POSITIVE) fail("Failed to correctly parse negative FeatureVector"); for (int i = 0; i < fv.size(); i++) if (fv.get(i) != i + 1.0) fail("Failed to correctly parse String to FeatureVector"); try { FeatureVectorParser.parseFeatureVectorFromString(fv, malformedFV); fail("Failed to catch exception on malformed input"); } catch(Exception e) {} }
public static void parseFeatureVectorFromString (FeatureVector fv, String strFeatureVector) throws StringIndexOutOfBoundsException { int classLabel = Integer.parseInt(strFeatureVector.substring(0, strFeatureVector.indexOf(';'))); String [] toks = strFeatureVector.substring( strFeatureVector.indexOf(';')+1).split(";"); if (fv.size() < toks.length) throw new IllegalArgumentException("String contains more than " + String.valueOf(toks.length) + " features"); fv.setClassLabel(ClassLabel.getClassLabel(classLabel)); for (String feature : toks) { String featureNum = feature.substring(0, feature.indexOf(":")); String featureVal = feature.substring(feature.indexOf(":")+1); fv.set(Integer.parseInt(featureNum)-1, Double.parseDouble(featureVal)); } }
FeatureVectorParser { public static void parseFeatureVectorFromString (FeatureVector fv, String strFeatureVector) throws StringIndexOutOfBoundsException { int classLabel = Integer.parseInt(strFeatureVector.substring(0, strFeatureVector.indexOf(';'))); String [] toks = strFeatureVector.substring( strFeatureVector.indexOf(';')+1).split(";"); if (fv.size() < toks.length) throw new IllegalArgumentException("String contains more than " + String.valueOf(toks.length) + " features"); fv.setClassLabel(ClassLabel.getClassLabel(classLabel)); for (String feature : toks) { String featureNum = feature.substring(0, feature.indexOf(":")); String featureVal = feature.substring(feature.indexOf(":")+1); fv.set(Integer.parseInt(featureNum)-1, Double.parseDouble(featureVal)); } } }
FeatureVectorParser { public static void parseFeatureVectorFromString (FeatureVector fv, String strFeatureVector) throws StringIndexOutOfBoundsException { int classLabel = Integer.parseInt(strFeatureVector.substring(0, strFeatureVector.indexOf(';'))); String [] toks = strFeatureVector.substring( strFeatureVector.indexOf(';')+1).split(";"); if (fv.size() < toks.length) throw new IllegalArgumentException("String contains more than " + String.valueOf(toks.length) + " features"); fv.setClassLabel(ClassLabel.getClassLabel(classLabel)); for (String feature : toks) { String featureNum = feature.substring(0, feature.indexOf(":")); String featureVal = feature.substring(feature.indexOf(":")+1); fv.set(Integer.parseInt(featureNum)-1, Double.parseDouble(featureVal)); } }  }
FeatureVectorParser { public static void parseFeatureVectorFromString (FeatureVector fv, String strFeatureVector) throws StringIndexOutOfBoundsException { int classLabel = Integer.parseInt(strFeatureVector.substring(0, strFeatureVector.indexOf(';'))); String [] toks = strFeatureVector.substring( strFeatureVector.indexOf(';')+1).split(";"); if (fv.size() < toks.length) throw new IllegalArgumentException("String contains more than " + String.valueOf(toks.length) + " features"); fv.setClassLabel(ClassLabel.getClassLabel(classLabel)); for (String feature : toks) { String featureNum = feature.substring(0, feature.indexOf(":")); String featureVal = feature.substring(feature.indexOf(":")+1); fv.set(Integer.parseInt(featureNum)-1, Double.parseDouble(featureVal)); } }  static ArrayList<String> readFile(String fileName); static void parseFeatureVectorFromString(FeatureVector fv,
			String strFeatureVector); static int strFeatureVectorLength(String strFeatureVector); }
FeatureVectorParser { public static void parseFeatureVectorFromString (FeatureVector fv, String strFeatureVector) throws StringIndexOutOfBoundsException { int classLabel = Integer.parseInt(strFeatureVector.substring(0, strFeatureVector.indexOf(';'))); String [] toks = strFeatureVector.substring( strFeatureVector.indexOf(';')+1).split(";"); if (fv.size() < toks.length) throw new IllegalArgumentException("String contains more than " + String.valueOf(toks.length) + " features"); fv.setClassLabel(ClassLabel.getClassLabel(classLabel)); for (String feature : toks) { String featureNum = feature.substring(0, feature.indexOf(":")); String featureVal = feature.substring(feature.indexOf(":")+1); fv.set(Integer.parseInt(featureNum)-1, Double.parseDouble(featureVal)); } }  static ArrayList<String> readFile(String fileName); static void parseFeatureVectorFromString(FeatureVector fv,
			String strFeatureVector); static int strFeatureVectorLength(String strFeatureVector);  }