use of edu.cmu.minorthird.classify.Instance in project lucida by claritylab.
the class FeatureExtractor method printFeaturesFromQuestions.
/**
* 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 questionSetFileName the name of the file containing the dataset to load
* @param features a List of the features to print
*/
public void printFeaturesFromQuestions(String questionSetFileName, List<String> features) {
String questions = IOUtil.readFile(questionSetFileName);
for (String question : questions.split("[\\n\\r\\f]")) {
Instance instance = createInstance(question);
StringBuilder sb = new StringBuilder();
if (features.size() > 0) {
for (Iterator it = instance.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() + " " + question);
} else
System.out.println(instance + " " + question);
}
}
use of edu.cmu.minorthird.classify.Instance in project lucida by claritylab.
the class ScoreNormalizationFilter method createExample.
/**
* Creates a training/evaluation example from a judged answer candidate.
*
* @param features selected features
* @param result judged answer candidate
* @param results all answers to the question
* @param qid question ID
* @return training/evaluation example
*/
private static Example createExample(String[] features, Result result, Result[] results, String qid) {
// create instance with selected features
Instance instance = createInstance(features, result, results, qid);
// create example from the instance and its class label
String label = result.isCorrect() ? ExampleSchema.POS_CLASS_NAME : ExampleSchema.NEG_CLASS_NAME;
Example example = new Example(instance, new ClassLabel(label));
return example;
}
use of edu.cmu.minorthird.classify.Instance in project lucida by claritylab.
the class ScoreNormalizationFilter method apply.
/**
* Normalizes the scores of the factoid answers, using the features
* specified in <code>SELECTED_FEATURES</code> and the classifier specified
* in <code>classifier</code>.
*
* @param results array of <code>Result</code> objects
* @return array of <code>Result</code> objects with normalized scores
*/
public Result[] apply(Result[] results) {
// classifier not loaded
if (classifier == null)
return results;
for (Result result : results) {
// only factoid answers with 1 extraction technique
if (result.getScore() <= 0 || result.getScore() == Float.POSITIVE_INFINITY || result.getExtractionTechniques() == null || result.getExtractionTechniques().length != 1)
continue;
// create instance with selected features
Instance instance = createInstance(SELECTED_FEATURES, result, results);
// classify instance
ClassLabel label = classifier.classification(instance);
// get weight of positive class as result score
double weight = label.posProbability();
result.setNormScore((float) weight);
}
// preserve original order of results
// results = preserveOrderResorting(results);
// results = preserveOrderAveraging(results);
results = preserveOrderTop(results);
return results;
}
use of edu.cmu.minorthird.classify.Instance in project lucida by claritylab.
the class QuestionClassifier method getAnswerTypes.
/**
* Classifies the question represented by the given List of Terms and parse tree
* as having a particular answer type and possibly subtype.
*
* @param terms the Terms that make up the question to classify
* @param parseTreeStr the syntactic parse tree of the question, in String format
*
* @return the candidate answer type / subtypes.
* @throws Exception
*/
public List<AnswerType> getAnswerTypes(List<Term> terms, String parseTreeStr) throws Exception {
if (!isInitialized())
throw new Exception("getAnswerTypes called while not initialized");
String question = "";
for (Term term : terms) question += term.getText() + " ";
// create the instance
Instance instance = new MutableInstance(question);
if (extractor != null)
instance = extractor.createInstance(terms, parseTreeStr);
return classify(instance);
}
use of edu.cmu.minorthird.classify.Instance 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;
}
Aggregations