use of edu.cmu.minorthird.classify.Example in project lucida by claritylab.
the class FeatureExtractor method createExample.
/**
* Creates an edu.cmu.minorthird.classify.Example object from one line
* of a dataset file using {@link #createInstance(String, String)}.
*
* @param datasetLine the line from the dataset file from which to create
* the Example
* @return the Example created
* @throws Exception
*/
public Example[] createExample(String datasetLine) throws Exception {
Matcher m = datasetExamplePattern.matcher(datasetLine);
if (!m.matches())
throw new Exception("Malformed dataset line:\n" + datasetLine);
String[] aTypes = null;
aTypes = m.group(labelPosition).replaceAll(",$", "").replaceAll(",", ".").split("\\|");
String question = m.group(questionPosition);
String sentParse = null;
if (parsePosition > -1)
sentParse = m.group(parsePosition);
Instance instance = createInstance(question, sentParse);
Example[] result = new Example[aTypes.length];
//create example(s) and add it to list
for (int i = 0; i < aTypes.length; i++) {
String newATypeName = HierarchicalClassifier.getHierarchicalClassName(aTypes[i], classLevels, useClassLevels);
result[i] = new Example(instance, new ClassLabel(newATypeName));
}
return result;
}
use of edu.cmu.minorthird.classify.Example in project lucida by claritylab.
the class FeatureExtractor method printFeatures.
/**
* Prints the features generated for each example in an input file. If feature
* types are included as command-line arguments, only those types are printed.
* Otherwise, all features are printed.
*
* @param dataSetFileName the name of the file containing the dataset to load
* @param features a List of the features to print
*/
public void printFeatures(String dataSetFileName, List<String> features) {
Example[] examples = loadFile(dataSetFileName);
for (int i = 0; i < examples.length; i++) {
String src = (String) examples[i].getSource();
StringBuilder sb = new StringBuilder();
if (features.size() > 0) {
for (Iterator it = examples[i].binaryFeatureIterator(); it.hasNext(); ) {
Feature feat = (Feature) it.next();
String name = "";
for (String s : feat.getName()) name += "." + s;
name = name.replaceFirst(".", "");
if (features.contains(feat.getName()[0]))
sb.append(name + " ");
}
System.out.println(sb.toString() + " " + src);
} else
System.out.println(examples[i] + " " + src);
}
System.out.println("Loaded: " + getNumLoaded());
}
Aggregations