Search in sources :

Example 6 with MutableInstance

use of edu.cmu.minorthird.classify.MutableInstance in project lucida by claritylab.

the class Rule method main.

/**
     * Tests Rule creation, compilation and matching.
     * 
     * @param args
     */
public static void main(String[] args) {
    String test = "<RULE atype=\"TEST_TYPE\">" + "<RULE_ELEMENT feature_name=\"TEST_FEATURE1\">" + "<FEATURE_VALUE>value1</FEATURE_VALUE>" + "<FEATURE_VALUE>value2</FEATURE_VALUE>" + "<FEATURE_VALUE>value3</FEATURE_VALUE>" + "</RULE_ELEMENT>" + "<RULE_ELEMENT feature_name=\"FOCUS_TYPE\">" + "<FEATURE_VALUE>value3=</FEATURE_VALUE>" + "<FEATURE_VALUE>value4=new</FEATURE_VALUE>" + "</RULE_ELEMENT>" + "</RULE>";
    Document ruleDocument;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        ruleDocument = db.parse(new InputSource(new StringReader(test)));
        Rule r = new Rule(ruleDocument.getDocumentElement());
        System.out.println("Test input: " + test);
        System.out.println(r.toString());
        MutableInstance testInstance = new MutableInstance(test);
        testInstance.addBinary(new Feature("TEST_FEATURE1.value1"));
        testInstance.addBinary(new Feature("FOCUS_TYPE.value4"));
        System.out.println("Test instance: " + testInstance);
        System.out.println("matches test rule?: " + r.matches(testInstance));
        testInstance = new MutableInstance(test);
        testInstance.addBinary(new Feature("TEST_FEATURE1.value1"));
        testInstance.addBinary(new Feature("FOCUS_TYPE.value3"));
        System.out.println("Test instance: " + testInstance);
        System.out.println("matches test rule?: " + r.matches(testInstance));
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse XML string", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) MutableInstance(edu.cmu.minorthird.classify.MutableInstance) Document(org.w3c.dom.Document) Feature(edu.cmu.minorthird.classify.Feature)

Example 7 with MutableInstance

use of edu.cmu.minorthird.classify.MutableInstance in project lucida by claritylab.

the class EnglishFeatureExtractor method createInstance.

public Instance createInstance(String question) {
    String[] tokens = question.split("\\s+");
    List<String> words = new ArrayList<String>();
    for (String token : tokens) words.add(token);
    try {
        String parse = StanfordParser.parse(question);
        return createInstance(question, parse);
    } catch (Exception e) {
        log.error("Failed to parse question, using only word-level features.", e);
        List<Term> terms = new ArrayList<Term>();
        for (String word : words) terms.add(new Term(0, 0, word));
        MutableInstance instance = new MutableInstance(question);
        addWordLevelFeatures(instance, terms, null);
        return instance;
    }
}
Also used : ArrayList(java.util.ArrayList) MutableInstance(edu.cmu.minorthird.classify.MutableInstance) ArrayList(java.util.ArrayList) List(java.util.List) Term(edu.cmu.lti.javelin.qa.Term)

Aggregations

MutableInstance (edu.cmu.minorthird.classify.MutableInstance)7 Term (edu.cmu.lti.javelin.qa.Term)3 Feature (edu.cmu.minorthird.classify.Feature)2 Tree (edu.cmu.lti.chineseNLP.util.Tree)1 BasicDataset (edu.cmu.minorthird.classify.BasicDataset)1 Dataset (edu.cmu.minorthird.classify.Dataset)1 Example (edu.cmu.minorthird.classify.Example)1 Instance (edu.cmu.minorthird.classify.Instance)1 CrossValidatedDataset (edu.cmu.minorthird.classify.experiments.CrossValidatedDataset)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1