List of usage examples for edu.stanford.nlp.trees Tree headTerminal
public Tree headTerminal(HeadFinder hf)
From source file:coreferenceresolver.util.Util.java
public static void assignNounPhrases(List<NounPhrase> nounPhrases, List<Review> reviews) { CollinsHeadFinder headFinder = new CollinsHeadFinder(); for (NounPhrase np : nounPhrases) { Review review = reviews.get(np.getReviewId()); Sentence sentence = review.getSentences().get(np.getSentenceId()); String npContent = ""; for (CRFToken token : np.getCRFTokens()) { npContent += token.getWord() + " "; }//from w ww. j a v a2 s. c om //Initiate a NP Tree Tree npNode = initNPTree(); for (CRFToken cRFToken : np.getCRFTokens()) { Tree cRFTokenTree = sentence.getTokens().get(cRFToken.getIdInSentence()).getTokenTree(); npNode.addChild(cRFTokenTree); } np.setNpNode(npNode); np.setHeadNode(npNode.headTerminal(headFinder)); int npOffsetBegin = sentence.getTokens().get(np.getCRFTokens().get(0).getIdInSentence()) .getOffsetBegin(); np.setOffsetBegin(npOffsetBegin); int npOffsetEnd = sentence.getTokens() .get(np.getCRFTokens().get(np.getCRFTokens().size() - 1).getIdInSentence()).getOffsetEnd(); np.setOffsetEnd(npOffsetEnd); review.addNounPhrase(np); sentence.addNounPhrase(np); sentence.setSentimentForNPs(); } }
From source file:edu.albany.cubism.util.StanfordChineseParser.java
public void printTree(Tree t) { tp.printTree(t);//w w w .j av a 2 s . com tp.printTree(t.headTerminal(new CollinsHeadFinder()));//SemanticHeadFinder())); //System.out.println("tree label: " + t.label()); List trees = t.subTreeList(); for (int i = 0; i < trees.size(); i++) { Tree sbt = (Tree) trees.get(i); /* * if (!sbt.isLeaf()) { trees.addAll(sbt.subTreeList()); } */ //System.out.println("sbt lable: " + sbt.label()); } //System.out.println("done"); List<Tree> leaves = t.getLeaves(); for (int i = 0; i < leaves.size(); i++) { Tree leaf = leaves.get(i); //if (leaf.parent() != null) { System.out.println(leaf.pennString() + " " + leaf.value()); //} } /* * Set dependencies = t.dependencies(); Iterator it = * dependencies.iterator(); while (it.hasNext()) { Dependency dependency * = (Dependency)it.next(); System.out.println(dependency.toString()); * System.out.println(dependency.name()); } */ }
From source file:elkfed.coref.mentions.Mention.java
License:Apache License
public String computePredicationType(Mention np) { String predType = null;/*from www .j a va2s . co m*/ Tree mentionTree = np.getHighestProjection(); Tree sentenceTree = np.getSentenceTree(); Tree parentNode = null; if (mentionTree == null && ConfigProperties.getInstance().getDbgPrint()) { System.out.println("No mentionTree for " + np.toString()); } if (mentionTree != null) parentNode = mentionTree.ancestor(1, sentenceTree); if (!(parentNode == null) && parentNode.children().length > 1 && parentNode.children()[1].label().toString().equals("VP") && parentNode.children()[1].children().length > 1) { String hword10 = parentNode.children()[1].children()[0].headTerminal(new ModCollinsHeadFinder()) .toString(); if (hword10.equals("is") || hword10.equals("are") || hword10.equals("was") || hword10.equals("were")) { Tree pchild11 = parentNode.children()[1].children()[1]; if (pchild11 != null) {// && if (pchild11.label().toString().equals("NP")) { String pchild11_headpos = pchild11.headPreTerminal(new ModCollinsHeadFinder()).label() .toString(); if (!pchild11_headpos.equals("JJS") && !pchild11_headpos.equals("NNP")) { predType = pchild11.headTerminal(new ModCollinsHeadFinder()).toString(); } } } } } return predType; }
From source file:elkfed.lang.EnglishLanguagePlugin.java
License:Apache License
public Tree[] calcParseExtra(Tree sentenceTree, int startWord, int endWord, Tree prsHead, HeadFinder StHeadFinder) {//from w w w . j a v a 2s. co m List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = null; if (endWord >= Leaves.size()) { // for marks that do not respect sentence boundaries endNode = Leaves.get(Leaves.size() - 1); } else { endNode = Leaves.get(endWord); } Tree prevNode = null; if (startWord > 0) prevNode = Leaves.get(startWord - 1); Tree nextNode = null; if (endWord < Leaves.size() - 1) nextNode = Leaves.get(endWord + 1); Tree[] result = new Tree[3]; //---------- calculate minimal np-like subtree, containing the head and included in the mention Tree HeadNode = prsHead; if (prsHead == null) { // todo: this should be fixed somehow though // todo (ctd): use getHeadIndex from NPHeadFinder, but need to reconstruct the markable // todo (ctd): mind marks spanning over sentene boundaries result[0] = null; result[1] = null; result[2] = null; return result; } Tree mincand = prsHead; Tree t = mincand; Tree minnp = null; Tree maxnp = null; while (t != null && (prevNode == null || !t.dominates(prevNode)) && (nextNode == null || !t.dominates(nextNode))) { if (t.value().equalsIgnoreCase("NP")) { mincand = t; t = null; } if (t != null) t = t.parent(sentenceTree); } result[0] = mincand; t = mincand; while (t != null && (t == mincand || !iscoordnp(t))) { if (t.value().equalsIgnoreCase("NP")) { if (t.headTerminal(StHeadFinder) == HeadNode) { maxnp = t; if (minnp == null) minnp = t; } else { t = null; } } if (t != null) t = t.parent(sentenceTree); } result[1] = minnp; result[2] = maxnp; return result; }
From source file:elkfed.lang.ItalianLanguagePlugin.java
License:Apache License
@Override public Tree[] calcParseExtra(Tree sentenceTree, int startWord, int endWord, Tree prsHead, HeadFinder StHeadFinder) {/*w w w . j a va 2 s. c o m*/ List<Tree> Leaves = sentenceTree.getLeaves(); Tree startNode = Leaves.get(startWord); Tree endNode = null; if (endWord >= Leaves.size()) { // for marks that do not respect sentence boundaries endNode = Leaves.get(Leaves.size() - 1); } else { endNode = Leaves.get(endWord); } Tree prevNode = null; if (startWord > 0) prevNode = Leaves.get(startWord - 1); Tree nextNode = null; if (endWord < Leaves.size() - 1) nextNode = Leaves.get(endWord + 1); Tree[] result = new Tree[3]; //---------- calculate minimal np-like subtree, containing the head and included in the mention Tree HeadNode = prsHead; if (prsHead == null) { // todo: this should be fixed somehow though // todo (ctd): use getHeadIndex from NPHeadFinder, but need to reconstruct the markable // todo (ctd): mind marks spanning over sentene boundaries result[0] = null; result[1] = null; result[2] = null; return result; } Tree mincand = prsHead; Tree t = mincand; Tree minnp = null; Tree maxnp = null; while (t != null && (prevNode == null || !t.dominates(prevNode)) && (nextNode == null || !t.dominates(nextNode))) { if (t.value().equalsIgnoreCase("NP")) { mincand = t; t = null; } if (t != null) t = t.parent(sentenceTree); } result[0] = mincand; t = mincand; while (t != null && (t == mincand || !iscoordnp(t))) { if (t.value().equalsIgnoreCase("NP")) { if (t.headTerminal(StHeadFinder) == HeadNode) { maxnp = t; if (minnp == null) minnp = t; } else { t = null; } } if (t != null) t = t.parent(sentenceTree); } result[1] = minnp; result[2] = maxnp; return result; }
From source file:elkfed.mmax.pipeline.P2Chunker.java
License:Apache License
private void addChunkMarkable(Tree nod, Tree pTree, int start, Boolean checkup) { // register new chunk markable, setting maxspan if needed List<Tree> lv = nod.getLeaves(); int npstart = Integer.valueOf(lv.get(0).label().value().split(INDEX_SEPARATOR)[1]); int npend = Integer.valueOf(lv.get(lv.size() - 1).label().value().split(INDEX_SEPARATOR)[1]); npstart += start;/*w ww.ja v a2 s. c o m*/ npend += start; final Map<String, String> cAttributes = new HashMap<String, String>(chunkAttributes); cAttributes.put(TAG_ATTRIBUTE, "np"); //store maxspan for embedded nps (either basic or explicitly marked for doing so) if (checkup || nod.value().equals("NP" + NPSTATUS_SEPARATOR + "2")) { Tree p = nod; Tree head = p.headTerminal(getHeadFinder()); Tree lastmax = null; while (p != null) { p = p.parent(pTree); if (p != null && p.value().startsWith("NP")) { if ((p.headTerminal(getHeadFinder()) == head) && (!iscoordnp(p))) lastmax = p; else p = null; } } if (lastmax != null) { List<Tree> lvm = lastmax.getLeaves(); int maxstart = Integer.valueOf(lvm.get(0).label().value().split(INDEX_SEPARATOR)[1]); int maxend = Integer.valueOf(lvm.get(lvm.size() - 1).label().value().split(INDEX_SEPARATOR)[1]); maxstart += start + 1; maxend += start + 1; cAttributes.put(MAXSPAN_ATTRIBUTE, "word_" + maxstart + "..word_" + maxend); } } chunkLevel.addMarkable(npstart, npend, cAttributes); }
From source file:elkfed.mmax.pipeline.P2Chunker.java
License:Apache License
private void normalizeTree(Tree tree) { // for leaves -- add positions // for nps -- add whether they are basic or not int leaveIndex = 0; for (Iterator<Tree> treeIt = tree.iterator(); treeIt.hasNext();) { Tree currentTree = treeIt.next(); Label nodeLabel = currentTree.label(); if (currentTree.isLeaf()) { nodeLabel.setValue(nodeLabel.value() + INDEX_SEPARATOR + leaveIndex); leaveIndex++;/*from ww w . j av a 2 s .c o m*/ } else { if (currentTree.value().toLowerCase().startsWith("np")) { Boolean found = false; //adjust this np for keeping (if not already discarded if (!currentTree.value().endsWith("0") && !currentTree.value().endsWith("2")) currentTree.label().setValue("NP" + NPSTATUS_SEPARATOR + "1"); //adjust upper np for discarding Tree p = currentTree; Tree head = p.headTerminal(getHeadFinder()); while (p != null && !found) { p = p.parent(tree); if (p != null && p.value().toLowerCase().startsWith("np") && p.headTerminal(getHeadFinder()) == head && (!iscoordnp(p))) { found = true; p.label().setValue("NP" + NPSTATUS_SEPARATOR + "0"); currentTree.label().setValue("NP" + NPSTATUS_SEPARATOR + "2"); } } } else { nodeLabel.setValue(nodeLabel.value().toUpperCase()); } } } }
From source file:elkfed.mmax.pipeline.SemTagger.java
License:Apache License
/** Finds the index of the head of a (non-basal) semantic role phrase */ private int findSemanticRoleHeadIndex(Markable semroleMarkable) { // 1. Get the syntactic tree semroleMarkable is contained into final int srStart = semroleMarkable.getLeftmostDiscoursePosition(); final int srEnd = semroleMarkable.getRightmostDiscoursePosition(); for (int i = 0; i < parseTrees.size(); i++) { final int sentStart = parseStart.get(i); final int sentEnd = parseEnd.get(i); if (srStart >= sentStart && srEnd <= sentEnd) { // GOTCHA! Tree tree = parseTrees.get(i); // 2. Find the lowest node containing the markable at its leaves final int srOnset = srStart - sentStart; final int srOffset = srEnd - sentStart; final List<Tree> leaves = tree.getLeaves(); final Tree startNode = leaves.get(srOnset); final Tree endNode = leaves.get(srOffset); Tree parentNode = startNode; while (parentNode != null && !parentNode.dominates(endNode)) { parentNode = parentNode.parent(tree); }// w w w. j a v a 2s .c o m Tree lowestProjection = null; if (parentNode == null) { lowestProjection = startNode; } else { lowestProjection = parentNode; } // 3. Find the head and return its index Tree headWord = lowestProjection.headTerminal(headFinder); return Integer.valueOf(headWord.label().value().split(INDEX_SEPARATOR)[1]) + sentStart; } } return -1; }
From source file:knu.univ.lingvo.coref.MentionExtractor.java
License:Open Source License
protected int getHeadIndex(Tree t) { // The trees passed in do not have the CoordinationTransformer // applied, but that just means the SemanticHeadFinder results are // slightly worse. Tree ht = t.headTerminal(headFinder); if (ht == null) return -1; // temporary: a key which is matched to nothing CoreLabel l = (CoreLabel) ht.label(); return l.get(CoreAnnotations.IndexAnnotation.class); }
From source file:knu.univ.lingvo.coref.MentionExtractor.java
License:Open Source License
/** * Post-processes the extracted mentions. Here we set the Mention fields required for coref and order mentions by tree-traversal order. * @param words List of words in each sentence, in textual order * @param trees List of trees, one per sentence * @param unorderedMentions List of unordered, unprocessed mentions * Each mention MUST have startIndex and endIndex set! * Optionally, if scoring is desired, mentions must have mentionID and originalRef set. * All the other Mention fields are set here. * @return List of mentions ordered according to the tree traversal * @throws Exception// w w w . j av a 2s .c o m */ public List<List<Mention>> arrange(Annotation anno, List<List<CoreLabel>> words, List<Tree> trees, List<List<Mention>> unorderedMentions, boolean doMergeLabels) throws Exception { List<List<Mention>> orderedMentionsBySentence = new ArrayList<List<Mention>>(); // // traverse all sentences and process each individual one // int mentionNumber = 0; for (int sent = 0, sz = words.size(); sent < sz; sent++) { List<CoreLabel> sentence = words.get(sent); Tree tree = trees.get(sent); List<Mention> mentions = unorderedMentions.get(sent); Map<String, List<Mention>> mentionsToTrees = Generics.newHashMap(); // merge the parse tree of the entire sentence with the sentence words if (doMergeLabels) mergeLabels(tree, sentence); // // set the surface information and the syntactic info in each mention // startIndex and endIndex MUST be set before! // for (Mention mention : mentions) { mention.sentenceNumber = sent; mention.mentionNumber = mentionNumber++; mention.contextParseTree = tree; mention.sentenceWords = sentence; mention.originalSpan = new ArrayList<CoreLabel>( mention.sentenceWords.subList(mention.startIndex, mention.endIndex)); if (!((CoreLabel) tree.label()).has(CoreAnnotations.BeginIndexAnnotation.class)) tree.indexSpans(0); if (mention.headWord == null) { Tree headTree = ((RuleBasedCorefMentionFinder) mentionFinder).findSyntacticHead(mention, tree, sentence); mention.headWord = (CoreLabel) headTree.label(); mention.headIndex = mention.headWord.get(CoreAnnotations.IndexAnnotation.class) - 1; } if (mention.mentionSubTree == null) { // mentionSubTree = highest NP that has the same head Tree headTree = tree.getLeaves().get(mention.headIndex); if (headTree == null) { throw new RuntimeException("Missing head tree for a mention!"); } Tree t = headTree; while ((t = t.parent(tree)) != null) { if (t.headTerminal(headFinder) == headTree && t.value().equals("NP")) { mention.mentionSubTree = t; } else if (mention.mentionSubTree != null) { break; } } if (mention.mentionSubTree == null) { mention.mentionSubTree = headTree; } } List<Mention> mentionsForTree = mentionsToTrees.get(treeToKey(mention.mentionSubTree)); if (mentionsForTree == null) { mentionsForTree = new ArrayList<Mention>(); mentionsToTrees.put(treeToKey(mention.mentionSubTree), mentionsForTree); } mentionsForTree.add(mention); // generates all fields required for coref, such as gender, number, etc. mention.process(dictionaries, semantics, this, singletonPredictor); } // // Order all mentions in tree-traversal order // List<Mention> orderedMentions = new ArrayList<Mention>(); orderedMentionsBySentence.add(orderedMentions); // extract all mentions in tree traversal order (alternative: tree.postOrderNodeList()) for (Tree t : tree.preOrderNodeList()) { List<Mention> lm = mentionsToTrees.get(treeToKey(t)); if (lm != null) { for (Mention m : lm) { orderedMentions.add(m); } } } // // find appositions, predicate nominatives, relative pronouns in this sentence // findSyntacticRelations(tree, orderedMentions); assert (mentions.size() == orderedMentions.size()); } return orderedMentionsBySentence; }