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);
}
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());
}
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()]);
}
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);
}
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);
}
Aggregations