List of usage examples for edu.stanford.nlp.trees Tree lastChild
public Tree lastChild()
From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.TreeWithTokens.java
License:Open Source License
private Tree getRightmostLeaf(Tree t) { if (t.isLeaf()) { return t; } else {/* w ww . j a va2s. co m*/ return getRightmostLeaf(t.lastChild()); } }
From source file:elkfed.coref.discourse_entities.DiscourseEntity.java
License:Open Source License
private Tree massimoHeadFindHack(Tree npNode) { LanguagePlugin lang_plugin = ConfigProperties.getInstance().getLanguagePlugin(); /*//from w w w. j a v a 2s. c o m * NOTE (yv): * We should really have a decent configurable head finder. * The "generic" head finder below probably works, but ... * this is ugly enough for English, but making it work for * English *and* Italian (and possibly other languages) * is only something for very enthusiastic people with * slight masochistic tendencies. */ //CollinsHeadFinder hf = new CollinsHeadFinder(); //ModCollinsHeadFinder hf = new ModCollinsHeadFinder(); /* -- trivial -- */ if (npNode.numChildren() == 0) return npNode; if (npNode.numChildren() == 1) { if (npNode.firstChild().numChildren() == 0) return npNode; return massimoHeadFindHack(npNode.firstChild()); } /* -- coordination -- */ if (npNode.numChildren() > 2) { for (Tree n : npNode.children()) { if (lang_plugin.labelCat(n.nodeString()) == NodeCategory.CC) return null; } } /* -- last child is a noun (common/proper) --*/ /* NB: will it work for italian though? */ NodeCategory firstpos = lang_plugin.labelCat(npNode.firstChild().nodeString()); NodeCategory nextpos = lang_plugin.labelCat(npNode.getChild(1).nodeString()); NodeCategory lastpos = lang_plugin.labelCat(npNode.lastChild().nodeString()); if (lastpos == NodeCategory.CN) return npNode.lastChild(); if (lastpos == NodeCategory.PN) return npNode.lastChild(); /* -- (NP (NP (DT the) (NN man)) (PP (in from) (NP (NNP UNCLE)))) -- */ if (firstpos == NodeCategory.NP && nextpos != NodeCategory.CN) return massimoHeadFindHack(npNode.firstChild()); /* -- misc -- */ Tree found_head = null; int state = 0; for (Tree n : npNode.children()) { NodeCategory ncat = lang_plugin.labelCat(n.nodeString()); if (ncat == NodeCategory.CN || ncat == NodeCategory.PN || ncat == NodeCategory.PRO) { state = 4; found_head = n; } else if (ncat == NodeCategory.NP && state < 3) { state = 3; found_head = n; } else if (ncat == NodeCategory.ADJ && state < 3) { state = 2; found_head = n; } } if (found_head != null) { if (state == 3) { return massimoHeadFindHack(found_head); } return found_head; } // if (ConfigProperties.getInstance().getDbgPrint()) System.err.println("Couldn't find a head for NP:" + npNode.pennString()); return null; }
From source file:org.lambda3.text.simplification.discourse.utils.parseTree.ParseTreeExtractionUtils.java
License:Open Source License
public static Tree getLastLeaf(Tree tree) { if (tree.isLeaf()) { return tree; } else {/*from w w w. j ava 2 s. co m*/ return getLastLeaf(tree.lastChild()); } }
From source file:reck.parser.lexparser.RECKLexicalizedParser.java
License:Open Source License
public RECKCTTreeNodeImpl convertToRECKTree(Tree root, int startSentence, String content) { RECKCTTreeNodeImpl newRoot = null;// w w w.j av a 2 s. c om Charseq pos = null; List nodeList = root.getLeaves(); HashSet parentSet = new HashSet(); int docIndex = startSentence; String st = null; // compute leaves' positions for (int i = 0; i < nodeList.size(); i++) { Tree oldNode = (Tree) nodeList.get(i); st = oldNode.toString(); int start = content.indexOf(st, docIndex); if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) { if (st.indexOf("&") != -1) { String tmp = st.replaceAll("&", "&"); start = content.indexOf(tmp, docIndex); if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) { tmp = st.replaceAll("&", "&"); start = content.indexOf(tmp, docIndex); } } if (start != -1 && start - docIndex <= maxDistanceBetweenLeaves) { docIndex = start + st.length() + 4; } else { st = reConvert(st); start = content.indexOf(st, docIndex); if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) { if (st.equals("-LRB-") || st.equals("-LCB-")) { int i1 = content.indexOf("(", docIndex); int i2 = content.indexOf("[", docIndex); int i3 = content.indexOf("{", docIndex); if (i1 == -1) i1 = content.length(); if (i2 == -1) i2 = content.length(); if (i3 == -1) i3 = content.length(); if ((i1 == i2) && (i1 == i3)) System.out.println("Come here !"); else if (i1 < i2) { if (i3 < i1) { // st = "{"; start = i3; } else { // st = "("; start = i1; } } else { if (i3 < i2) { // st = "{"; start = i3; } else { // st = "["; start = i2; } } docIndex = start + 1; } else if (st.equals("-RRB-") || st.equals("-RCB-")) { int i1 = content.indexOf(")", docIndex); int i2 = content.indexOf("]", docIndex); int i3 = content.indexOf("}", docIndex); if (i1 == -1) i1 = content.length(); if (i2 == -1) i2 = content.length(); if (i3 == -1) i3 = content.length(); if ((i1 == i2) && (i1 == i3)) System.out.println("Come here !"); else if (i1 < i2) { if (i3 < i1) { // st = "}"; start = i3; } else { // st = ")"; start = i1; } } else { if (i3 < i2) { // st = "}"; start = i3; } else { // st = "]"; start = i2; } } docIndex = start + 1; } else { for (int k = 0; k < newStrings.length; k++) { st = st.replace(newStrings[k], oldStrings[k]); } String oldSubSt1 = new String(new char[] { (char) 39, (char) 39 }); String oldSubSt2 = new String(new char[] { (char) 96, (char) 96 }); String newSubSt = new String(new char[] { (char) 34 }); if (st.indexOf(oldSubSt1) != -1 && content.substring(docIndex).indexOf(newSubSt) != -1) st = st.replace(oldSubSt1, newSubSt); else if (st.indexOf(oldSubSt2) != -1 && content.substring(docIndex).indexOf(newSubSt) != -1) st = st.replace(oldSubSt2, newSubSt); int i39 = content.indexOf(39, docIndex); int i96 = content.indexOf(96, docIndex); if ((st.indexOf(39) != -1) && (i96 != -1 && i96 - docIndex <= maxDistanceBetweenLeaves)) st = st.replace((char) 39, (char) 96); else if ((st.indexOf(96) != -1) && (i39 != -1 && i39 - docIndex <= maxDistanceBetweenLeaves)) st = st.replace((char) 96, (char) 39); start = content.indexOf(st, docIndex); if (start == -1 || start - docIndex > maxDistanceBetweenLeaves) System.out.println("Come here !"); else docIndex = start + st.length(); } } else docIndex = start + st.length(); } } else docIndex = start + st.length(); // Test if next node is a sentence splitter, means "." if (st.endsWith(".") && i < nodeList.size() - 1) { Tree nextNode = (Tree) nodeList.get(i + 1); String nextLabel = nextNode.label().value(); int nextStart = content.indexOf(nextLabel, docIndex); if (nextLabel.equals(".") && (nextStart == -1 || nextStart - docIndex > maxDistanceBetweenLeaves)) { docIndex--; oldNode.setLabel(new StringLabel(st.substring(0, st.length() - 1))); } } pos = new Charseq(start, docIndex); RECKCTTreeNodeImpl newNode = new RECKCTTreeNodeImpl(new StringLabel(st), (List) oldNode.getChildrenAsList(), pos); Tree parent = oldNode.parent(root); parent.setChild(parent.indexOf(oldNode), newNode); parentSet.add(parent); } nodeList.clear(); nodeList.addAll(parentSet); // compute upper nodes' positions while (!nodeList.isEmpty()) { parentSet = new HashSet(); for (int i = 0; i < nodeList.size(); i++) { Tree oldNode = (Tree) nodeList.get(i); Iterator nodeIter = oldNode.getChildrenAsList().iterator(); Tree node = (Tree) nodeIter.next(); while (node instanceof RECKCTTreeNodeImpl && nodeIter.hasNext()) { node = (Tree) nodeIter.next(); } if (node instanceof RECKCTTreeNodeImpl) { Long start = ((RECKCTTreeNodeImpl) oldNode.firstChild()).getPosition().getStart(); Long end = ((RECKCTTreeNodeImpl) oldNode.lastChild()).getPosition().getEnd(); pos = new Charseq(start, end); RECKCTTreeNodeImpl newNode = new RECKCTTreeNodeImpl(oldNode.label(), (List) oldNode.getChildrenAsList(), pos); Tree parent = oldNode.parent(root); parent.setChild(parent.indexOf(oldNode), newNode); parentSet.add(parent); // if oldNode is in parentSet, remove it if (parentSet.contains(oldNode)) { parentSet.remove(oldNode); } } else { parentSet.add(oldNode); } } nodeList.clear(); if (parentSet.size() == 1 && parentSet.contains(root)) { Long start = ((RECKCTTreeNodeImpl) root.firstChild()).getPosition().getStart(); Long end = ((RECKCTTreeNodeImpl) root.lastChild()).getPosition().getEnd(); pos = new Charseq(start, end); newRoot = new RECKCTTreeNodeImpl(root.label(), (List) root.getChildrenAsList(), pos); } else { nodeList.addAll(parentSet); } } return newRoot; }