List of usage examples for edu.stanford.nlp.trees Tree setChild
public Tree setChild(int i, Tree t)
From source file:Anaphora_Resolution.ParseAllXMLDocuments.java
public static Tree HobbsResolve(Tree pronoun, ArrayList<Tree> forest) { Tree wholetree = forest.get(forest.size() - 1); // The last one is the one I am going to start from ArrayList<Tree> candidates = new ArrayList<Tree>(); List<Tree> path = wholetree.pathNodeToNode(wholetree, pronoun); System.out.println(path);/* w w w .ja v a2s . c om*/ // Step 1 Tree ancestor = pronoun.parent(wholetree); // This one locates the NP the pronoun is in, therefore we need one more "parenting" ! // Step 2 ancestor = ancestor.parent(wholetree); //System.out.println("LABEL: "+pronoun.label().value() + "\n\tVALUE: "+pronoun.firstChild()); while (!ancestor.label().value().equals("NP") && !ancestor.label().value().equals("S")) ancestor = ancestor.parent(wholetree); Tree X = ancestor; path = X.pathNodeToNode(wholetree, pronoun); System.out.println(path); // Step 3 for (Tree relative : X.children()) { for (Tree candidate : relative) { if (candidate.contains(pronoun)) break; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild()); if ((candidate.parent(wholetree) != X) && (candidate.parent(wholetree).label().value().equals("NP") || candidate.parent(wholetree).label().value().equals("S"))) if (candidate.label().value().equals("NP")) // "Propose as the antecedent any NP node that is encountered which has an NP or S node between it and X" candidates.add(candidate); } } // Step 9 is a GOTO step 4, hence I will envelope steps 4 to 8 inside a while statement. while (true) { // It is NOT an infinite loop. // Step 4 if (X.parent(wholetree) == wholetree) { for (int q = 1; q < MAXPREVSENTENCES; ++q) {// I am looking for the prev sentence (hence we start with 1) if (forest.size() - 1 < q) break; // If I don't have it, break Tree prevTree = forest.get(forest.size() - 1 - q); // go to previous tree // Now we look for each S subtree, in order of recency (hence right-to-left, hence opposite order of that of .children() ). ArrayList<Tree> backlist = new ArrayList<Tree>(); for (Tree child : prevTree.children()) { for (Tree subtree : child) { if (subtree.label().value().equals("S")) { backlist.add(child); break; } } } for (int i = backlist.size() - 1; i >= 0; --i) { Tree Treetovisit = backlist.get(i); for (Tree relative : Treetovisit.children()) { for (Tree candidate : relative) { if (candidate.contains(pronoun)) continue; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild()); if (candidate.label().value().equals("NP")) { // "Propose as the antecedent any NP node that you find" if (!candidates.contains(candidate)) candidates.add(candidate); } } } } } break; // It will always come here eventually } // Step 5 ancestor = X.parent(wholetree); //System.out.println("LABEL: "+pronoun.label().value() + "\n\tVALUE: "+pronoun.firstChild()); while (!ancestor.label().value().equals("NP") && !ancestor.label().value().equals("S")) ancestor = ancestor.parent(wholetree); X = ancestor; // Step 6 if (X.label().value().equals("NP")) { // If X is an NP for (Tree child : X.children()) { // Find the nominal nodes that X directly dominates if (child.label().value().equals("NN") || child.label().value().equals("NNS") || child.label().value().equals("NNP") || child.label().value().equals("NNPS")) if (!child.contains(pronoun)) candidates.add(X); // If one of them is not in the path between X and the pronoun, add X to the antecedents } } // Step SETTE for (Tree relative : X.children()) { for (Tree candidate : relative) { if (candidate.contains(pronoun)) continue; // I am looking to all the nodes to the LEFT (i.e. coming before) the path leading to X. contain <-> in the path //System.out.println("LABEL: "+relative.label().value() + "\n\tVALUE: "+relative.firstChild()); if (candidate.label().value().equals("NP")) { // "Propose as the antecedent any NP node that you find" boolean contains = false; for (Tree oldercandidate : candidates) { if (oldercandidate.contains(candidate)) { contains = true; break; } } if (!contains) candidates.add(candidate); } } } // Step 8 if (X.label().value().equals("S")) { boolean right = false; // Now we want all branches to the RIGHT of the path pronoun -> X. for (Tree relative : X.children()) { if (relative.contains(pronoun)) { right = true; continue; } if (!right) continue; for (Tree child : relative) { // Go in but do not go below any NP or S node. Go below the rest if (child.label().value().equals("NP")) { candidates.add(child); break; // not sure if this means avoid going below NP but continuing with the rest of non-NP children. Should be since its DFS. } if (child.label().value().equals("S")) break; // Same } } } } // Step 9 is a GOTO, so we use a while. System.out.println(pronoun + ": CHAIN IS " + candidates.toString()); ArrayList<Integer> scores = new ArrayList<Integer>(); for (int j = 0; j < candidates.size(); ++j) { Tree candidate = candidates.get(j); Tree parent = null; int parent_index = 0; for (Tree tree : forest) { if (tree.contains(candidate)) { parent = tree; break; } ++parent_index; } scores.add(0); if (parent_index == 0) scores.set(j, scores.get(j) + 100); // If in the last sentence, +100 points scores.set(j, scores.get(j) + syntacticScore(candidate, parent)); if (existentialEmphasis(candidate)) // Example: "There was a dog standing outside" scores.set(j, scores.get(j) + 70); if (!adverbialEmphasis(candidate, parent)) scores.set(j, scores.get(j) + 50); if (headNounEmphasis(candidate, parent)) scores.set(j, scores.get(j) + 80); int sz = forest.size() - 1; // System.out.println("pronoun in sentence " + sz + "(sz). Candidate in sentence "+parent_index+" (parent_index)"); int dividend = 1; for (int u = 0; u < sz - parent_index; ++u) dividend *= 2; //System.out.println("\t"+dividend); scores.set(j, scores.get(j) / dividend); System.out.println(candidate + " -> " + scores.get(j)); } int max = -1; int max_index = -1; for (int i = 0; i < scores.size(); ++i) { if (scores.get(i) > max) { max_index = i; max = scores.get(i); } } Tree final_candidate = candidates.get(max_index); System.out.println("My decision for " + pronoun + " is: " + final_candidate); // Decide what candidate, with both gender resolution and Lappin and Leass ranking. Tree pronounparent = pronoun.parent(wholetree).parent(wholetree); // 1 parent gives me the NP of the pronoun int pos = 0; for (Tree sibling : pronounparent.children()) { System.out.println("Sibling " + pos + ": " + sibling); if (sibling.contains(pronoun)) break; ++pos; } System.out.println("Before setchild: " + pronounparent); @SuppressWarnings("unused") Tree returnval = pronounparent.setChild(pos, final_candidate); System.out.println("After setchild: " + pronounparent); return wholetree; // wholetree is already modified, since it contains pronounparent }
From source file:edu.cmu.ark.nlp.sent.SentenceSimplifier.java
License:Open Source License
private void removeConjoinedSiblingsHelper(Tree copy, int childindex) { //if(GlobalProperties.getDebug()) System.err.println("removeConjoinedSiblingsHelper: "+copy.toString()); Tree child = copy.getNodeNumber(childindex); Tree parent = child.parent(copy);// ww w . ja v a 2 s . c o m Tree gparent = parent.parent(copy); int parentIdx = gparent.objectIndexOf(parent); //By an annoying PTB convention, some verb phrase conjunctions //can conjoin two verb preterminals under a VP, //rather than only allowing VP nodes to be conjoined. //e.g., John walked and played. //So, we add an extra VP node in between if necessary if (child.label().toString().startsWith("VB")) { gparent.removeChild(parentIdx); Tree newTree = factory.newTreeNode("VP", new ArrayList<Tree>()); newTree.addChild(child); gparent.addChild(parentIdx, newTree); } else { gparent.setChild(parentIdx, child); } }
From source file:edu.cmu.ark.SentenceSimplifier.java
License:Open Source License
private void removeConjoinedSiblingsHelper(Tree copy, int childindex) { if (GlobalProperties.getDebug()) System.err.println("removeConjoinedSiblingsHelper: " + copy.toString()); Tree child = copy.getNodeNumber(childindex); Tree parent = child.parent(copy);/* w ww.j ava2s .c o m*/ Tree gparent = parent.parent(copy); int parentIdx = gparent.indexOf(parent); //By an annoying PTB convention, some verb phrase conjunctions //can conjoin two verb preterminals under a VP, //rather than only allowing VP nodes to be conjoined. //e.g., John walked and played. //So, we add an extra VP node in between if necessary if (child.label().toString().startsWith("VB")) { gparent.removeChild(parentIdx); Tree newTree = factory.newTreeNode("VP", new ArrayList<Tree>()); newTree.addChild(child); gparent.addChild(parentIdx, newTree); } else { gparent.setChild(parentIdx, child); } }
From source file:reck.parser.lexparser.RECKLexicalizedParser.java
License:Open Source License
public RECKCTTreeNodeImpl convertToRECKTree(Tree root, int startSentence, String content) { RECKCTTreeNodeImpl newRoot = null;/* www . j av a 2s. c o m*/ 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; }