List of usage examples for edu.stanford.nlp.trees Tree isLeaf
public boolean isLeaf()
From source file:DependencyParser.RunStanfordParser.java
public static void dfs(Tree node, Tree parent, Pattern patt) { if (node == null || node.isLeaf()) { return;/*from w w w . ja va 2s. com*/ } //if node is a NP - Get the terminal nodes to get the words in the NP if (node.value().equals("NP")) { // System.out.println(" Noun Phrase is "); /*List<Tree> leaves = node.getLeaves(); for(Tree leaf : leaves) { System.out.println(leaf.toString()); }*/ //Matcher match = patt.matcher(a.trim()); Matcher match = patt.matcher(node.toString().trim()); while (match.find()) { System.out.println("NP: " + match.group()); } } for (Tree child : node.children()) { dfs(child, node, patt); } }
From source file:edu.albany.cubism.sentiment.Sandbox.java
public boolean searchTreeForSubject(Tree tree, String relation, int index) { for (Tree child : tree.children()) { this.addMapping(index, child); if (child.isLeaf()) { /* If the leaf equals the search term then the relation has been found */ if (child.value().equals(relation)) { return true; }/*from w w w .java 2 s .c o m*/ } else { /* If the relation has been found */ if (searchTreeForSubject(child, relation, index + 1)) { /* if subject _recusion_level has not been recorded */ if (this.subject_recusion_level < 0) { this.subject_recusion_level = index; } return true; } } } return false; }
From source file:edu.albany.cubism.sentiment.Sandbox.java
public Tree searchTreeForObject(Tree tree, String relation) { Tree result;//from w w w .j ava2s. c o m for (Tree child : tree.children()) { if (child.isLeaf()) { /* If the leaf equals the search term then the relation has been found */ if (child.value().equals(relation)) { found = true; } } else { if (found) { /* If relation if found then this is the tree immedidate following the relation */ return child; } else { /* If the sub tree has been found then pass-it-up the recursion */ if ((result = searchTreeForObject(child, relation)) != null) { return result; } } } } return null; }
From source file:edu.albany.cubism.sentiment.Sandbox.java
private void treeToStringHelper(Tree tree, StringBuilder sb) { for (Tree child : tree.children()) { if (child.isLeaf()) { sb.append(child.value());//from w ww. j a va2s.co m sb.append(" "); } else { treeToStringHelper(child, sb); } } }
From source file:edu.cmu.cs.in.hoop.visualizers.HoopParseTreeViewer.java
License:Open Source License
/** * /* w w w .j a v a 2 s . co m*/ */ private void processInput(String aSentence) { debug("processInput (" + aSentence + ")"); Tree thisTree = theLexicalizedParser.apply(aSentence); debug("We have a tree!"); PrintWriter pw = new PrintWriter(System.out, true); TreePrint posPrinter = new TreePrint("wordsAndTags"); posPrinter.printTree(thisTree, pw); ArrayList ar = thisTree.taggedYield(); debug(ar.toString()); for (Tree subtree : thisTree) { if (thisTree.isLeaf() == true) { debug("Tree leaf: " + subtree.label().value()); } else { debug("Tree node: " + subtree.label().value()); } } treePanel.setTree(thisTree); }
From source file:edu.cmu.deiis.annotator.StanfordCoreNLPAnnotator.java
License:Open Source License
private FSArray addTreebankNodeChildrenToIndexes(TreebankNode parent, JCas jCas, List<CoreLabel> tokenAnns, Tree tree) {// ww w . j ava 2s . c o m Tree[] childTrees = tree.children(); // collect all children (except leaves, which are just the words - POS tags are pre-terminals in // a Stanford tree) List<TreebankNode> childNodes = new ArrayList<TreebankNode>(); for (Tree child : childTrees) { if (!child.isLeaf()) { // set node attributes and add children (mutual recursion) TreebankNode node = new TreebankNode(jCas); node.setParent(parent); this.addTreebankNodeToIndexes(node, jCas, child, tokenAnns); childNodes.add(node); } } // convert the child list into an FSArray FSArray childNodeArray = new FSArray(jCas, childNodes.size()); for (int i = 0; i < childNodes.size(); ++i) { childNodeArray.set(i, childNodes.get(i)); } return childNodeArray; }
From source file:edu.jhu.hlt.concrete.stanford.PreNERCoreMapWrapper.java
License:Open Source License
/** * * @param root//from ww w . j a v a2 s .co m * @param left * @param right * @param n * is the length of the sentence is tokens. * @param p * @param tokenizationUUID * @return The constituent ID * @throws AnalyticException */ private static int constructConstituent(Tree root, int left, int right, int n, Parse p, UUID tokenizationUUID, HeadFinder hf) throws AnalyticException { Constituent constituent = new Constituent(); constituent.setId(p.getConstituentListSize()); constituent.setTag(root.value()); constituent.setStart(left); constituent.setEnding(right); p.addToConstituentList(constituent); Tree headTree = null; if (!root.isLeaf()) { try { headTree = hf.determineHead(root); } catch (java.lang.IllegalArgumentException iae) { LOGGER.warn("Failed to find head, falling back on rightmost constituent."); headTree = root.children()[root.numChildren() - 1]; } } int i = 0, headTreeIdx = -1; int leftPtr = left; for (Tree child : root.getChildrenAsList()) { int width = child.getLeaves().size(); int childId = constructConstituent(child, leftPtr, leftPtr + width, n, p, tokenizationUUID, hf); constituent.addToChildList(childId); leftPtr += width; if (headTree != null && child == headTree) { assert (headTreeIdx < 0); headTreeIdx = i; } i++; } if (headTreeIdx >= 0) constituent.setHeadChildIndex(headTreeIdx); if (!constituent.isSetChildList()) constituent.setChildList(new ArrayList<Integer>()); return constituent.getId(); }
From source file:edu.ucla.cs.scai.qa.questionclassifier.SyntacticTreeNode.java
public SyntacticTreeNode(Tree t, ArrayList<CoreLabel> tokens, SyntacticTreeNode parent) throws Exception { this.parent = parent; value = t.value();// w w w .j av a2s.c o m if (t.isLeaf()) { CoreLabel c = tokens.remove(0); begin = c.beginPosition(); end = c.endPosition(); if (c == null) { throw new Exception("Mapping between TreeNode and CoreLabel not found"); } else { lemma = c.lemma(); ner = c.ner(); //System.out.println(value + " -> " + c.value()); if (!value.equals(c.value())) { throw new Exception("Different words have been matched!"); } } } else { boolean hasNPchildren = false; boolean hasWHNPchildren = false; boolean hasQPchildren = false; begin = Integer.MAX_VALUE; end = Integer.MIN_VALUE; for (Tree c : t.children()) { SyntacticTreeNode child = new SyntacticTreeNode(c, tokens, this); children.add(child); if (child.value.equals("NP")) { hasNPchildren = true; } else if (child.value.equals("QP")) { hasQPchildren = true; } else if (child.value.equals("WHNP")) { hasWHNPchildren = true; } begin = Math.min(begin, child.begin); end = Math.max(end, child.end); } if (value.equals("NP")) { if (hasNPchildren) { npCompound = true; } else if (hasQPchildren) { npQp = true; } else { npSimple = true; } } else if (value.equals("WHNP")) { //can a WHNP node have QP children? if (hasNPchildren || hasWHNPchildren) { whnpCompound = true; } else if (!hasQPchildren) { whnpSimple = true; } } } }
From source file:elkfed.coref.discourse_entities.DiscourseEntity.java
License:Open Source License
/** * Reads the premodifiers from the input Mention and creates Property objects as attributes for every premodifier * not part of an embedded NE. Embedded NEs are to be treated as relations to other Discourse Entities. * @param np The NP to be processed/*from www. j a v a 2s . c o m*/ * @return Set A Set of Property Objects; one for every premodifier (attribute) */ private Set<Property> computeAttributes(Mention np) { LanguagePlugin lang_plugin = ConfigProperties.getInstance().getLanguagePlugin(); Set<Property> result = new LinkedHashSet<Property>(); List<Tree> preModifiers = np.getPremodifiers(); // straight from Mention //DEBUG //System.out.println("Number of premodifiers of "+np.getMarkableString()+" :"+ // preModifiers.size()); char pos = '\0'; if ((preModifiers != null) && (preModifiers.size() > 0)) { for (int i = 0; i < preModifiers.size(); i++) { Tree mod = preModifiers.get(i); // Expected structure: // (NP (DT the) (JJ last) (NN supper)) if (mod.isLeaf()) { // this shouldn't happen' System.out.println("WARNING: UNEXPECTED LEAF " + mod.nodeString()); //result.add(new Property(Property.ATTRIBUTE, mod.nodeString())); //result.add(new Property(Property.ATTRIBUTE, getSense(mod.nodeString()))); } else { NodeCategory ncat = lang_plugin.labelCat(mod.nodeString()); if (mod.isPreTerminal()) { if (ncat == NodeCategory.CN || ncat == NodeCategory.ADJ) { if (ncat == NodeCategory.CN) { pos = 'N'; } if (ncat == NodeCategory.ADJ) { pos = 'A'; } //System.out.println("Pre terminal node "+ mod.nodeString()); Tree wordNode = mod.firstChild(); _logger.fine("Adding attribute " + wordNode.nodeString() + " to entity"); result.add(new Property(Property.ATTRIBUTE, wordNode.nodeString(), pos)); } } } } } return result; }
From source file:elkfed.coref.discourse_entities.DiscourseEntity.java
License:Open Source License
private Set<Property> computeInitialRelations(Mention np) { LanguagePlugin lang_plugin = ConfigProperties.getInstance().getLanguagePlugin(); Set<Property> result = new LinkedHashSet<Property>(); List<Tree> postModifiers = np.getPostmodifiers(); // straight from Mention char pos = '\0'; //DEBUG//from ww w .j av a 2 s .c o m //System.out.println("Number of postmodifiers of "+np.getMarkableString()+" :"+ // postModifiers.size()); if ((postModifiers != null) && (postModifiers.size() > 0)) { for (int i = 0; i < postModifiers.size(); i++) { Tree mod = postModifiers.get(i); // Expected structure: // (NP (NN software) (PP from (NP India)) if (mod.isLeaf()) { // this shouldn't happen' System.out.println("WARNING: UNEXPECTED LEAF " + mod.nodeString()); //result.add(new Property(Property.ATTRIBUTE, mod.nodeString())); //result.add(new Property(Property.ATTRIBUTE, getSense(mod.nodeString()))); } else { if (mod.isPreTerminal()) { // this shouldn't happen either, // but we'll add it to the properties NodeCategory ncat = lang_plugin.labelCat(mod.nodeString()); if (ncat == NodeCategory.CN || ncat == NodeCategory.ADJ) { if (ncat == NodeCategory.CN) { pos = 'N'; } if (ncat == NodeCategory.ADJ) { pos = 'A'; } } } else { //System.out.println("Type of postmodifier: " + mod.nodeString()); NodeCategory ncat = lang_plugin.labelCat(mod.nodeString()); if (ncat == NodeCategory.PP) { if (mod.numChildren() == 2) { // (PP (in from) (NP (nnp India))) Tree prepNode = mod.getChild(0); Tree npNode = mod.getChild(1); Tree npHead = massimoHeadFindHack(npNode); if (npHead != null && prepNode != null) { //DEBUG //System.out.println("Adding relation "+ // prepNode.firstChild().nodeString()+" "+ // npHead.firstChild().nodeString() ); /* -- no clue what it means, just fixed so that it doesn't crash (Olga) -- */ if (prepNode.numChildren() > 0) prepNode = prepNode.firstChild(); result.add( new Property(prepNode.nodeString(), npHead.firstChild().nodeString())); } } } } } } //end outer loop } //end if premodified return result; }