Search in sources :

Example 1 with CollinsHeadDependencyParser

use of edu.illinois.cs.cogcomp.nlp.utilities.CollinsHeadDependencyParser in project cogcomp-nlp by CogComp.

the class HeadFinderDependencyViewGenerator method getDependencyTree.

public static TreeView getDependencyTree(TextAnnotation input, String parseViewName, String dependencyViewName) {
    CollinsHeadDependencyParser depParser = new CollinsHeadDependencyParser(false);
    TreeView parseTreeView = (TreeView) input.getView(parseViewName);
    TreeView depTreeView = new TreeView(dependencyViewName, viewGenerator, input, 1d);
    int size = 0;
    for (int i = 0; i < input.getNumberOfSentences(); i++) {
        if (parseTreeView.getTree(i) != null) {
            Constituent parseTreeRoot = parseTreeView.getRootConstituent(i);
            Tree<Pair<String, Integer>> labeledDependencyTree = depParser.getLabeledDependencyTree(parseTreeRoot);
            try {
                depTreeView.setDependencyTree(i, labeledDependencyTree);
            } catch (IllegalStateException e) {
                System.err.println(parseTreeView);
                System.err.println("Unlabeled dependency tree (for debugging): ");
                System.err.println(depParser.getDependencyTree(parseTreeRoot));
                throw e;
            }
            size += input.getSentence(i).size();
            int nConstituents = depTreeView.getNumberOfConstituents();
            if (nConstituents != size) {
                logger.error("{} nodes in dependency tree, " + "{} tokens in text so far", nConstituents, size);
                Set<Integer> set = new LinkedHashSet<>();
                for (int tokenId = 0; tokenId < size; tokenId++) {
                    set.add(tokenId);
                }
                for (Constituent c : depTreeView.getConstituents()) {
                    set.remove(c.getStartSpan());
                }
                StringBuilder sb = new StringBuilder();
                for (int tokenId : set) {
                    sb.append(input.getToken(tokenId)).append(" ");
                }
                logger.error("Dependency tree does not cover tokens: {}", sb.toString());
            }
        }
    }
    return depTreeView;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CollinsHeadDependencyParser(edu.illinois.cs.cogcomp.nlp.utilities.CollinsHeadDependencyParser) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 2 with CollinsHeadDependencyParser

use of edu.illinois.cs.cogcomp.nlp.utilities.CollinsHeadDependencyParser in project cogcomp-nlp by CogComp.

the class TestParseViewGenerator method testCharniakParseViewGenerator.

@Test
public final void testCharniakParseViewGenerator() {
    String sentence = "This is a test .";
    String treeString = "(S1 (S (NP (DT This))   (VP (AUX is)       (NP (DT a)           (NN test)))(. .)))";
    Tree<String> tree = TreeParserFactory.getStringTreeParser().parse(treeString);
    TextAnnotation ta = TextAnnotationUtilities.createFromTokenizedString(sentence);
    TreeView parseView = new TreeView(ViewNames.PARSE_CHARNIAK, "My_PARSER", ta, 1d);
    parseView.setParseTree(0, tree);
    ta.addView(ViewNames.PARSE_CHARNIAK, parseView);
    TreeView view = (TreeView) ta.getView(ViewNames.PARSE_CHARNIAK);
    logger.info(ParseHelper.getParseTree(ViewNames.PARSE_CHARNIAK, ta, 0).toString());
    assertEquals(tree, view.getTree(0));
    CollinsHeadDependencyParser depParser = new CollinsHeadDependencyParser(true);
    Tree<Pair<String, Integer>> depTree = depParser.getLabeledDependencyTree(view.getRootConstituent(0));
    TreeView depView = new TreeView(ViewNames.DEPENDENCY, "HeadRuleDependencyTree", ta, 1.0);
    logger.info(depTree.toString());
    depView.setDependencyTree(0, depTree);
    ta.addView(ViewNames.DEPENDENCY, depView);
    logger.info(depView.toString());
    logger.info(depView.getTree(0).toString());
}
Also used : CollinsHeadDependencyParser(edu.illinois.cs.cogcomp.nlp.utilities.CollinsHeadDependencyParser) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair) Test(org.junit.Test)

Aggregations

Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)2 CollinsHeadDependencyParser (edu.illinois.cs.cogcomp.nlp.utilities.CollinsHeadDependencyParser)2 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)1 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)1 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1