Search in sources :

Example 26 with TreeView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.

the class ParseHelper method getHeadWordPosition.

/**
     * Get the head word of a constituent using the {@link HeadFinderBase} that is passed as an
     * argument. To use this function, first, a head finder should be created. For example:
     * <p>
     * 
     * <pre>
     *   TextAnnotation ta = ... // some text annotation
     *   Constituent c = ... // some constituent
     * 
     *   CollinsHeadFinder headFinder = new CollinsHeadFinder();
     * 
     *   int headId = ParseHelper.getHeadWordPosition(c, headFinder);
     * 
     *   // now we can do other things with the headId.
     *   String headWord = WordHelpers.getWord(ta, headId);
     * </pre>
     *
     * @param parseViewName The name of the view which contains the parse trees
     * @param c The constituent whose head we wish to find
     * @param headFinder The head finder
     * @return The index of the head word. The word itself can be obtained later by calling
     *         {@link WordHelpers#getWord(TextAnnotation, int)}.
     * @throws EdisonException
     */
public static int getHeadWordPosition(Constituent c, HeadFinderBase headFinder, String parseViewName) throws Exception {
    TextAnnotation ta = c.getTextAnnotation();
    TreeView parse = (TreeView) ta.getView(parseViewName);
    Constituent parsePhrase = parse.getParsePhrase(c);
    return headFinder.getHeadWordPosition(parsePhrase);
}
Also used : TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)

Example 27 with TreeView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView 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)

Example 28 with TreeView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.

the class CommaTest method setUp.

@Override
public void setUp() throws Exception {
    String[] tokens = "Says Gayle Key , a mathematics teacher , `` Hello world . ''".split("\\s+");
    TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(Collections.singletonList(tokens));
    TokenLabelView tlv = new TokenLabelView(ViewNames.POS, "Test", ta, 1.0);
    tlv.addTokenLabel(0, "VBZ", 1d);
    tlv.addTokenLabel(1, "NNP", 1d);
    tlv.addTokenLabel(2, "NNP", 1d);
    tlv.addTokenLabel(3, ",", 1d);
    tlv.addTokenLabel(4, "DT", 1d);
    tlv.addTokenLabel(5, "NN", 1d);
    tlv.addTokenLabel(6, "NN", 1d);
    tlv.addTokenLabel(7, ",", 1d);
    tlv.addTokenLabel(8, "``", 1d);
    tlv.addTokenLabel(9, "UH", 1d);
    tlv.addTokenLabel(10, "NN", 1d);
    tlv.addTokenLabel(11, ".", 1d);
    tlv.addTokenLabel(12, "''", 1d);
    TreeView parse = new TreeView(ViewNames.PARSE_STANFORD, "Test", ta, 1.0);
    String treeString = "(ROOT" + "  (SINV" + "    (VP (VBZ Says))" + "    (NP (NNP Gayle) (NNP Key))" + "    (, ,)" + "    (S" + "      (NP (DT a) (NNS mathematics))" + "      (VP (VBZ teacher) (, ,) (`` ``)" + "        (NP" + "          (INTJ (UH Hello))" + "          (NP (NN world)))))" + "    (. .) ('' '')))";
    parse.setParseTree(0, TreeParserFactory.getStringTreeParser().parse(treeString));
    SpanLabelView ner = new SpanLabelView(ViewNames.NER_CONLL, "Test", ta, 1.0);
    ner.addSpanLabel(1, 3, "PER", 1.0);
    SpanLabelView shallowParse = new SpanLabelView(ViewNames.SHALLOW_PARSE, "Test", ta, 1.0);
    shallowParse.addSpanLabel(0, 3, "NP", 1.0);
    shallowParse.addSpanLabel(4, 7, "NP", 1.0);
    shallowParse.addSpanLabel(9, 11, "NP", 1.0);
    // TODO dependency parse
    // TODO SRL view
    ta.addView(tlv.getViewName(), tlv);
    ta.addView(parse.getViewName(), parse);
    ta.addView(ner.getViewName(), ner);
    ta.addView(shallowParse.getViewName(), shallowParse);
    List<String> firstCommasRefinedLabels = Collections.singletonList("Substitute");
    List<String> secondCommasRefinedLabels = Arrays.asList("Substitute", "Quotation");
    CommaSRLSentence sentence = new CommaSRLSentence(ta, null, Arrays.asList(firstCommasRefinedLabels, secondCommasRefinedLabels));
    List<Comma> sentenceCommas = sentence.getCommas();
    commas = sentenceCommas.toArray(new Comma[sentenceCommas.size()]);
}
Also used : Comma(edu.illinois.cs.cogcomp.comma.datastructures.Comma) TokenLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TokenLabelView) CommaSRLSentence(edu.illinois.cs.cogcomp.comma.datastructures.CommaSRLSentence) TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) SpanLabelView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.SpanLabelView)

Example 29 with TreeView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.

the class TestCollinsHeadFinder method test2.

@Test
public final void test2() throws Exception {
    String sentence = "Mr. Gorbachev 's publicized tongue - lashing of the press on Oct. 13";
    String treeString = "(S (NP (NNP Mr.)	(NNP Gorbachev)	(POS 's))    (VP (VBN publicized)      " + "  (NP (NP (NN tongue)		(HYPH -)		(VBG lashing))            (PP (IN of)    " + "            (NP (DT the)                    (NN press))))        (PP (IN on)  " + "          (NP (NNP Oct.)                (CD 13)))))";
    Tree<String> tree = TreeParserFactory.getStringTreeParser().parse(treeString);
    TextAnnotation ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(Collections.singletonList(sentence.split("\\s+")));
    TreeView view = new TreeView("", "", ta, 1.0);
    view.setParseTree(0, tree);
    CollinsHeadFinder headFinder = new CollinsHeadFinder();
    Constituent headWord = headFinder.getHeadWord(view.getRootConstituent(0));
    assertEquals(headWord.getStartSpan(), 3);
    Constituent c = new Constituent("", "", ta, 7, 10);
    headWord = headFinder.getHeadWord(view.getParsePhrase(c));
    assertEquals(headWord.getStartSpan(), 7);
}
Also used : TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) TextAnnotation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) Test(org.junit.Test)

Example 30 with TreeView

use of edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView in project cogcomp-nlp by CogComp.

the class CollinsHeadFinderTest method setUp.

@Before
public void setUp() {
    String[] sentence = "Central Asia , North east Europe and Africa are continents .".split(" ");
    String treeString = "(S1 (S (NP (NP (NNP Central)             " + "  (NNP Asia))           (, ,)        " + "   (NP (NNP North)               (NNP east)  " + "             (NNP Europe))           (CC and)           " + "(NP (NNP Africa)))       (VP (AUX are)       " + "    (NP (NNS continents)))       (. .)))";
    Tree<String> tree = TreeParserFactory.getStringTreeParser().parse(treeString);
    ta = BasicTextAnnotationBuilder.createTextAnnotationFromTokens(Collections.singletonList(sentence));
    TreeView view = new TreeView(ViewNames.PARSE_GOLD, "", ta, 1.0);
    view.setParseTree(0, tree);
    ta.addView(ViewNames.PARSE_GOLD, view);
}
Also used : TreeView(edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView) Before(org.junit.Before)

Aggregations

TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)30 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)25 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)19 EdisonException (edu.illinois.cs.cogcomp.edison.utilities.EdisonException)13 Feature (edu.illinois.cs.cogcomp.edison.features.Feature)12 DiscreteFeature (edu.illinois.cs.cogcomp.edison.features.DiscreteFeature)11 LinkedHashSet (java.util.LinkedHashSet)11 Relation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation)9 Test (org.junit.Test)7 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)6 ArrayList (java.util.ArrayList)4 Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)3 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)3 List (java.util.List)3 RealFeature (edu.illinois.cs.cogcomp.edison.features.RealFeature)2 Annotation (edu.stanford.nlp.pipeline.Annotation)2 TreeCoreAnnotations (edu.stanford.nlp.trees.TreeCoreAnnotations)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 HashSet (java.util.HashSet)2 Comma (edu.illinois.cs.cogcomp.comma.datastructures.Comma)1