List of usage examples for edu.stanford.nlp.trees Tree isLeaf
public boolean isLeaf()
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++;/*w ww . j a v a 2s .co 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
/** Given a tree, uppercases all its non-terminal label (so that the * CollinsHeadFinder can be applied to it) and add a positional index to * the terminal nodes./*from w ww . j a v a 2s. c o m*/ * * @param tree the tree whose labels are to be uppercased */ private void normalizeTree(Tree tree) { 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++; } else { nodeLabel.setValue(nodeLabel.value().toUpperCase()); } } }
From source file:englishparser.EnglishParser.java
private static void display(Tree t) { t.pennPrint();/*w ww. ja v a 2s .co m*/ for (Tree child : t.children()) { if (!child.isLeaf()) { display(child); } } }
From source file:ims.cs.qsample.features.components.SentenceConstituentFeatures.java
License:Open Source License
/** * Recursion step for tree featues/*from www . j a va 2s . c om*/ * @param sentence * @param t complete tree * @param level current level * @param governingLabels list of governing labels * @param parent information about direct parent * @param isLeftmost is the node the leftmost one in the constituent specified by ancestorWhereLeftmost * @param ancestorWhereLeftmost */ private static void addTreeFeatures(Sentence sentence, Tree t, int level, List<NodeFeatures> governingLabels, NodeFeatures parent, boolean isLeftmost, NodeFeatures ancestorWhereLeftmost) { if (t.isLeaf()) { /* terminal nodes */ // get the current token represented by this subtree Token pToken = sentence.treeLookup.get(t); // check if token is null. this can happen if the token was unaligned previously (e.g., because of // a parser error) if (pToken == null) { if (StaticConfig.verbose) System.err.println(sentence.sentenceId + " Dropping tree without associated token: " + t + " "); return; } FeatureSet fs = pToken.boundaryFeatureSet; // leftmost feature (see Pareti paper for description) if (StaticConfig.constituentLeftmost && isLeftmost) fs.add(LEFTMOST_FEATURE); // level in tree if (StaticConfig.constituentLevel) { fs.add(LEVEL_FEATURE + level); addLevelBinHeuristic(pToken, LEVEL_FEATURE, level); } // leftmost feature label if (StaticConfig.constituentAncestorL) { fs.add(AL_FEATURE + "LBL:" + ancestorWhereLeftmost.label); fs.add(AL_FEATURE + "LVL:" + ancestorWhereLeftmost.level); addLevelBinHeuristic(pToken, AL_FEATURE + "LVL", ancestorWhereLeftmost.level); } // parent in constituent tree if (StaticConfig.constituentParent) { fs.add(PARENT_FEATURE + "LBL:" + parent.label); } // labels of all ancestors if (StaticConfig.constituentGoverning) { /* "Ancestor" features in the paper */ for (NodeFeatures nf : governingLabels) { // label with and without depth fs.add(GOV_FEATURE + nf.label + "@" + nf.level); /* ambiguous in paper */ fs.add(GOV_FEATURE + nf.label); fs.add(GOV_FEATURE + nf.label + "@-" + (level - nf.level)); /* ambiguous in paper */ addLevelBinHeuristic(pToken, GOV_FEATURE + nf.label + "@", nf.level); addLevelBinHeuristic(pToken, GOV_FEATURE + nf.label + "@-", (level - nf.level)); } } } else { // non-terminal node List<Tree> childList = t.getChildrenAsList(); String label = t.label().toString(); // copy governing node features for next recursion step List<NodeFeatures> governingLabelsUpdate = new LinkedList<NodeFeatures>(governingLabels); governingLabelsUpdate.add(new NodeFeatures(label, level)); // set leftmost ancestor if (ancestorWhereLeftmost == null) { ancestorWhereLeftmost = new NodeFeatures(label, level); } // check for pre-terminals -- otherwise, set the leftmost flag for the first constituent if (childList.size() > 1) { isLeftmost = true; } // call function for all children for (Tree child : childList) { addTreeFeatures(sentence, child, level + 1, governingLabelsUpdate, new NodeFeatures(label, level), isLeftmost, ancestorWhereLeftmost); isLeftmost = false; ancestorWhereLeftmost = null; } } }
From source file:knu.univ.lingvo.coref.Mention.java
License:Open Source License
private boolean isListLike() { // See if this mention looks to be a conjunction of things // Check for "or" and "and" and "," int commas = 0; // boolean firstLabelLike = false; // if (originalSpan.size() > 1) { // String w = originalSpan.get(1).word(); // firstLabelLike = (w.equals(":") || w.equals("-")); // }// w w w .j a v a 2s . co m String mentionSpanString = spanToString(); String subTreeSpanString = StringUtils.joinWords(mentionSubTree.yieldWords(), " "); if (subTreeSpanString.equals(mentionSpanString)) { // subtree represents this mention well.... List<Tree> children = mentionSubTree.getChildrenAsList(); for (Tree t : children) { String label = t.value(); String ner = null; if (t.isLeaf()) { ner = ((CoreLabel) t.getLeaves().get(0).label()).ner(); } if ("CC".equals(label)) { // Check NER type if (ner == null || "O".equals(ner)) { return true; } } else if (label.equals(",")) { if (ner == null || "O".equals(ner)) { commas++; } } } } if (commas <= 2) { // look at the string for and/or boolean first = true; for (CoreLabel t : originalSpan) { String tag = t.tag(); String ner = t.ner(); String w = t.word(); if (tag.equals("TO") || tag.equals("IN") || tag.startsWith("VB")) { // prepositions and verbs are too hard for us return false; } if (!first) { if (w.equalsIgnoreCase("and") || w.equalsIgnoreCase("or")) { // Check NER type if (ner == null || "O".equals(ner)) { return true; } } } first = false; } } return (commas > 2); }
From source file:nlp.service.implementation.DefaultLanguageProcessor.java
@Override public SentenceToken assembleToken(SemanticGraph semanticGraph, CoreMap sentence) { Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); CoreLabel coreLabel = (CoreLabel) tree.label(); SentenceToken token = new SentenceToken(coreLabel.get(BeginIndexAnnotation.class), coreLabel.get(EndIndexAnnotation.class)); List<WordToken> wordTokens = new ArrayList<>(); GrammarService grammarService = new DefaultGrammarService(semanticGraph); if (!tree.isLeaf()) { addChildNodes(grammarService, wordTokens, token, tree.children()); }/* ww w .ja va 2 s.c om*/ populateGrammarRelations(grammarService, token, wordTokens); return token; }
From source file:nlp.service.implementation.DefaultLanguageProcessor.java
private void addChildNodes(GrammarService grammarService, List<WordToken> wordTokens, Token token, Tree[] children) {/*w w w . jav a 2 s.c om*/ for (Tree child : children) { Tree childToAdd = child; Tree grandChild = getLeaf(child); if (grandChild != null) { childToAdd = grandChild; } if (childToAdd.isLeaf()) { WordToken leafToken = createWordToken(grammarService, token, (CoreLabel) childToAdd.label()); token.addToken(leafToken); wordTokens.add(leafToken); } else { Token childToken = createPhraseToken(token, (CoreLabel) childToAdd.label()); if (childToken != null) { token.addToken(childToken); addChildNodes(grammarService, wordTokens, childToken, childToAdd.children()); } } } }
From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java
License:Apache License
public StringBuilder toStringBuilderExtenderByAnotherLinkedTree1(StringBuilder sb, Tree t, Tree treeToInsert, String[] corefWords) {/*from w w w .ja v a2s .co m*/ if (t.isLeaf()) { if (t.label() != null) { sb.append(t.label().value()); } return sb; } else { sb.append('('); if (t.label() != null) { if (t.value() != null) { sb.append(t.label().value()); } } boolean bInsertNow = false; Tree[] kids = t.children(); if (kids != null) { for (Tree kid : kids) { if (corefWords != null) { String word = corefWords[corefWords.length - 1]; String phraseStr = kid.toString(); phraseStr = phraseStr.replace(")", ""); if (phraseStr.endsWith(word)) { bInsertNow = true; } } } if (bInsertNow) { for (Tree kid : kids) { sb.append(' '); toStringBuilderExtenderByAnotherLinkedTree1(sb, kid, null, null); } sb.append(' '); toStringBuilderExtenderByAnotherLinkedTree1(sb, treeToInsert, null, null); } else { for (Tree kid : kids) { sb.append(' '); toStringBuilderExtenderByAnotherLinkedTree1(sb, kid, treeToInsert, corefWords); } } } return sb.append(')'); } }
From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java
License:Apache License
public List<Tree> getASubtreeWithRootAsNodeForWord1(Tree tree, Tree currentSubTree, String[] corefWords) { if (currentSubTree.isLeaf()) { return null; }/* www .ja v a 2 s .co m*/ List<Tree> result = null; Tree[] kids = currentSubTree.children(); if (kids != null) { boolean bFound = false; String word = corefWords[corefWords.length - 1]; for (Tree kid : kids) { if (bFound) { result.add(kid); } else { String phraseStr = kid.toString(); phraseStr = phraseStr.replace(")", ""); if (phraseStr.endsWith(word)) { // found bFound = true; result = new ArrayList<Tree>(); } } } if (bFound) { return result; } // if not a selected node, proceed with iteration for (Tree kid : kids) { List<Tree> ts = getASubtreeWithRootAsNodeForWord1(tree, kid, corefWords); if (ts != null) return ts; } } return null; }
From source file:opennlp.tools.parse_thicket.kernel_interface.TreeExtenderByAnotherLinkedTree.java
License:Apache License
public Tree[] getASubtreeWithRootAsNodeForWord(Tree tree, Tree currentSubTree, String[] corefWords) { if (currentSubTree.isLeaf()) { return null; }// ww w . j a v a 2 s . co m boolean bInsertNow = false; /* * List<ParseTreeNode> bigTreeNodes = * parsePhrase(currentSubTree.label().value()); for(ParseTreeNode * smallNode: bigTreeNodes ){ if * (bigTreeNodes.get(0).getWord().equals("") ) continue; String word = * bigTreeNodes.get(0).getWord(); for(String cWord: corefWords){ * * if (word.equalsIgnoreCase(cWord)) bInsertNow=true; } } */ String nodePhraseStr = currentSubTree.toString(); System.out.println(nodePhraseStr); for (String w : corefWords) nodePhraseStr = nodePhraseStr.replace(w, ""); // all words are covered if (nodePhraseStr.toUpperCase().equals(nodePhraseStr)) bInsertNow = true; // if(bInsertNow) // return currentSubTree; Tree[] kids = currentSubTree.children(); if (kids != null) { /* * for (Tree kid : kids) { List<ParseTreeNode> bigTreeNodes = * parsePhrase(kid.label().value()); if (bigTreeNodes!=null && * bigTreeNodes.size()>0 && bigTreeNodes.get(0)!=null && * bigTreeNodes.get(0).getWord().equalsIgnoreCase(corefWords[0])){ * bInsertNow=true; return kids; } * * } */ for (Tree kid : kids) { Tree[] t = getASubtreeWithRootAsNodeForWord(tree, kid, corefWords); if (t != null) return t; } } return null; }