Search in sources :

Example 1 with DepStruct

use of edu.illinois.cs.cogcomp.depparse.core.DepStruct in project cogcomp-nlp by CogComp.

the class DepAnnotator method addView.

@Override
public void addView(TextAnnotation ta) throws AnnotatorException {
    for (String reqView : requiredViews) if (!ta.hasView(reqView))
        throw new AnnotatorException("TextAnnotation must have view: " + reqView);
    DepInst sent = new DepInst(ta);
    DepStruct deptree;
    try {
        deptree = (DepStruct) model.infSolver.getBestStructure(model.wv, sent);
    } catch (Exception e) {
        throw new AnnotatorException("Sentence cannot be parsed");
    }
    TreeView treeView = new TreeView(ViewNames.DEPENDENCY, ta);
    int rootPos = findRoot(deptree);
    // All the node positions are -1 to account for the extra <root> node added
    Pair<String, Integer> nodePair = new Pair<>(sent.forms[rootPos], rootPos - 1);
    Tree<Pair<String, Integer>> tree = new Tree<>(nodePair);
    populateChildren(tree, deptree, sent, rootPos);
    treeView.setDependencyTree(0, tree);
    ta.addView(ViewNames.DEPENDENCY, treeView);
}
Also used : AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) DepInst(edu.illinois.cs.cogcomp.depparse.core.DepInst) DepStruct(edu.illinois.cs.cogcomp.depparse.core.DepStruct) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) AnnotatorException(edu.illinois.cs.cogcomp.annotation.AnnotatorException) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 2 with DepStruct

use of edu.illinois.cs.cogcomp.depparse.core.DepStruct in project cogcomp-nlp by CogComp.

the class LabeledDepFeatureGenerator method getFeatureVector.

@Override
public IFeatureVector getFeatureVector(IInstance x, IStructure y) {
    DepInst sent = (DepInst) x;
    DepStruct tree = (DepStruct) y;
    return extractFeatures(sent, tree);
}
Also used : DepInst(edu.illinois.cs.cogcomp.depparse.core.DepInst) DepStruct(edu.illinois.cs.cogcomp.depparse.core.DepStruct)

Example 3 with DepStruct

use of edu.illinois.cs.cogcomp.depparse.core.DepStruct in project cogcomp-nlp by CogComp.

the class MainClass method test.

private static void test(String modelPath, String testDataPath, boolean updateMatrix) throws Exception {
    SLModel model = SLModel.loadModel(modelPath);
    ((LabeledChuLiuEdmondsDecoder) model.infSolver).loadDepRelDict();
    SLProblem sp = getStructuredData(testDataPath, (LabeledChuLiuEdmondsDecoder) model.infSolver);
    double acc_undirected = 0.0;
    double acc_directed_unlabeled = 0.0;
    double acc_labeled = 0.0;
    double total = 0.0;
    long totalTime = 0L;
    int totalLength = 0;
    for (int i = 0; i < sp.instanceList.size(); i++) {
        DepInst sent = (DepInst) sp.instanceList.get(i);
        totalLength += sent.size();
        DepStruct gold = (DepStruct) sp.goldStructureList.get(i);
        long startTime = System.currentTimeMillis();
        DepStruct prediction = (DepStruct) model.infSolver.getBestStructure(model.wv, sent);
        totalTime += (System.currentTimeMillis() - startTime);
        IntPair tmp_undirected = evaluate(sent, gold, prediction, false, false, false);
        IntPair tmp_directed_unlabeled = evaluate(sent, gold, prediction, true, false, false);
        IntPair tmp_labeled = evaluate(sent, gold, prediction, true, true, updateMatrix);
        acc_undirected += tmp_undirected.getFirst();
        acc_directed_unlabeled += tmp_directed_unlabeled.getFirst();
        acc_labeled += tmp_labeled.getFirst();
        total += tmp_directed_unlabeled.getSecond();
    }
    System.out.println("Parsing time taken for " + sp.size() + " sentences with average length " + totalLength / sp.size() + ": " + totalTime);
    System.out.println("Average parsing time " + totalTime / sp.size());
    System.out.println("undirected acc " + acc_undirected);
    System.out.println("directed unlabeled acc " + acc_directed_unlabeled);
    System.out.println("labeled acc " + acc_labeled);
    System.out.println("total " + total);
    System.out.println("%age correct undirected " + (acc_undirected * 1.0 / total));
    System.out.println("%age correct directed & unlabeled " + (acc_directed_unlabeled * 1.0 / total));
    System.out.println("%age correct labeled " + (acc_labeled * 1.0 / total));
    if (updateMatrix)
        printMatrix();
    System.out.println("Done with testing!");
}
Also used : LabeledChuLiuEdmondsDecoder(edu.illinois.cs.cogcomp.depparse.core.LabeledChuLiuEdmondsDecoder) DepInst(edu.illinois.cs.cogcomp.depparse.core.DepInst) DepStruct(edu.illinois.cs.cogcomp.depparse.core.DepStruct) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair)

Aggregations

DepInst (edu.illinois.cs.cogcomp.depparse.core.DepInst)3 DepStruct (edu.illinois.cs.cogcomp.depparse.core.DepStruct)3 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)1 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)1 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)1 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)1 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)1 LabeledChuLiuEdmondsDecoder (edu.illinois.cs.cogcomp.depparse.core.LabeledChuLiuEdmondsDecoder)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1