List of usage examples for edu.stanford.nlp.trees Tree value
@Override
public String value()
From source file:de.tudarmstadt.ukp.dkpro.core.corenlp.internal.CoreNlp2DKPro.java
License:Open Source License
private static org.apache.uima.jcas.tcas.Annotation convertConstituentTreeNode(JCas aJCas, TreebankLanguagePack aTreebankLanguagePack, Tree aNode, org.apache.uima.jcas.tcas.Annotation aParentFS, boolean internStrings, MappingProvider constituentMappingProvider, List<CoreLabel> tokens) { // Get node label String nodeLabelValue = aNode.value(); // Extract syntactic function from node label String syntacticFunction = null; AbstractTreebankLanguagePack tlp = (AbstractTreebankLanguagePack) aTreebankLanguagePack; int gfIdx = nodeLabelValue.indexOf(tlp.getGfCharacter()); if (gfIdx > 0) { syntacticFunction = nodeLabelValue.substring(gfIdx + 1); nodeLabelValue = nodeLabelValue.substring(0, gfIdx); }/* w w w . j a v a 2s . c o m*/ // Check if node is a constituent node on sentence or phrase-level if (aNode.isPhrasal()) { Type constType = constituentMappingProvider.getTagType(nodeLabelValue); IntPair span = aNode.getSpan(); int begin = tokens.get(span.getSource()).get(CharacterOffsetBeginAnnotation.class); int end = tokens.get(span.getTarget()).get(CharacterOffsetEndAnnotation.class); Constituent constituent = (Constituent) aJCas.getCas().createAnnotation(constType, begin, end); constituent.setConstituentType(internStrings ? nodeLabelValue.intern() : nodeLabelValue); constituent.setSyntacticFunction( internStrings && syntacticFunction != null ? syntacticFunction.intern() : syntacticFunction); constituent.setParent(aParentFS); // Do we have any children? List<org.apache.uima.jcas.tcas.Annotation> childAnnotations = new ArrayList<>(); for (Tree child : aNode.getChildrenAsList()) { org.apache.uima.jcas.tcas.Annotation childAnnotation = convertConstituentTreeNode(aJCas, aTreebankLanguagePack, child, constituent, internStrings, constituentMappingProvider, tokens); if (childAnnotation != null) { childAnnotations.add(childAnnotation); } } // Now that we know how many children we have, link annotation of // current node with its children constituent.setChildren(FSCollectionFactory.createFSArray(aJCas, childAnnotations)); constituent.addToIndexes(); return constituent; } // Create parent link on token else if (aNode.isPreTerminal()) { // link token to its parent constituent List<Tree> children = aNode.getChildrenAsList(); assert children.size() == 1; Tree terminal = children.get(0); CoreLabel label = (CoreLabel) terminal.label(); Token token = label.get(TokenKey.class); token.setParent(aParentFS); return token; } else { throw new IllegalArgumentException("Node must be either phrasal nor pre-terminal"); } }
From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.util.StanfordAnnotator.java
License:Open Source License
/** * Creates linked constituent annotations + POS annotations * //from w w w .java 2 s . c o m * @param aTreebankLanguagePack * the language pack. * @param aNode * the source tree * @param aParentFS * the parent annotation * @param aCreatePos * sets whether to create or not to create POS tags * @return the child-structure (needed for recursive call only) */ private Annotation createConstituentAnnotationFromTree(TreebankLanguagePack aTreebankLanguagePack, Tree aNode, Annotation aParentFS, boolean aCreatePos) { String nodeLabelValue = aNode.value(); String syntacticFunction = null; AbstractTreebankLanguagePack tlp = (AbstractTreebankLanguagePack) aTreebankLanguagePack; int gfIdx = nodeLabelValue.indexOf(tlp.getGfCharacter()); if (gfIdx > 0) { syntacticFunction = nodeLabelValue.substring(gfIdx + 1); nodeLabelValue = nodeLabelValue.substring(0, gfIdx); } // calculate span for the current subtree IntPair span = tokenTree.getSpan(aNode); // Check if the node has been marked by a TSurgeon operation. // If so, add a tag-annotation on the constituent if (nodeLabelValue.contains(TAG_SEPARATOR) && !nodeLabelValue.equals(TAG_SEPARATOR)) { int separatorIndex = nodeLabelValue.indexOf(TAG_SEPARATOR); String tag = nodeLabelValue.substring(0, separatorIndex); nodeLabelValue = nodeLabelValue.substring(separatorIndex + 1, nodeLabelValue.length()); createTagAnnotation(span.getSource(), span.getTarget(), tag); } // Check if node is a constituent node on sentence or phrase-level if (aNode.isPhrasal()) { // add annotation to annotation tree Constituent constituent = createConstituentAnnotation(span.getSource(), span.getTarget(), nodeLabelValue, syntacticFunction); // link to parent if (aParentFS != null) { constituent.setParent(aParentFS); } // Do we have any children? List<Annotation> childAnnotations = new ArrayList<Annotation>(); for (Tree child : aNode.getChildrenAsList()) { Annotation childAnnotation = createConstituentAnnotationFromTree(aTreebankLanguagePack, child, constituent, aCreatePos); if (childAnnotation != null) { childAnnotations.add(childAnnotation); } } // Now that we know how many children we have, link annotation of // current node with its children FSArray children = new FSArray(jCas, childAnnotations.size()); int curChildNum = 0; for (FeatureStructure child : childAnnotations) { children.set(curChildNum, child); curChildNum++; } constituent.setChildren(children); // write annotation for current node to index jCas.addFsToIndexes(constituent); return constituent; } // If the node is a word-level constituent node (== POS): // create parent link on token and (if not turned off) create POS tag else if (aNode.isPreTerminal()) { // create POS-annotation (annotation over the token) POS pos = createPOSAnnotation(span.getSource(), span.getTarget(), nodeLabelValue); // in any case: get the token that is covered by the POS // TODO how about multi word prepositions etc. (e.g. "such as") List<Token> coveredTokens = JCasUtil.selectCovered(jCas, Token.class, pos); // the POS should only cover one token assert coveredTokens.size() == 1; Token token = coveredTokens.get(0); // only add POS to index if we want POS-tagging if (aCreatePos) { jCas.addFsToIndexes(pos); token.setPos(pos); } // link token to its parent constituent if (aParentFS != null) { token.setParent(aParentFS); } return token; } else { throw new IllegalArgumentException("Node must be either phrasal nor pre-terminal"); } }
From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.features.ProductionRulesFeature.java
License:Apache License
public static void extractProductionRulesRecursively(Tree tree, FrequencyDistribution<String> rules) { if (tree.getChildrenAsList().size() > 1) { String rule = tree.value() + "->"; for (Tree t : tree.getChildrenAsList()) { rule = rule + t.value() + ","; }// w ww. j ava 2s. c o m //System.out.println(rule); rules.addSample(rule, 1); } for (Tree t : tree.getChildrenAsList()) { extractProductionRulesRecursively(t, rules); } }
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 . j av a 2s . c o m*/ } //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; }//w w w.j a v a2s . 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 www. j a v a2 s. 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()); sb.append(" "); } else {//from w ww . j a va2 s. co m treeToStringHelper(child, sb); } } }
From source file:edu.albany.cubism.util.StanfordChineseParser.java
public void printTree(Tree t) { tp.printTree(t);/*from w w w .j av a2s . c o m*/ 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:edu.cmu.ark.QuestionTransducer.java
License:Open Source License
/** * Returns the singular present tense form of a tensed verb. * This only affects the output when generating from sentences where * first and second person pronouns are the subject. * * E.g.,/*from w w w.j av a 2 s . co m*/ * Affects: * I walk -> Who walks? (rather than, Who walk?) * * Does not affect: * He walks -> Who walks? * */ private String getSingularFormSubtree(Tree tensedVerbSubtree) { String res = ""; String lemma = AnalysisUtilities.getInstance().getLemma(tensedVerbSubtree.getChild(0).label().toString(), tensedVerbSubtree.label().toString()); String pos = tensedVerbSubtree.value(); if (pos.equals("VBD")) { res = tensedVerbSubtree.toString(); } else { res = "(VBZ " + AnalysisUtilities.getInstance().getSurfaceForm(lemma, "VBZ") + ")"; } return res; }
From source file:edu.cmu.deiis.annotator.StanfordCoreNLPAnnotator.java
License:Open Source License
private void addTreebankNodeToIndexes(TreebankNode node, JCas jCas, Tree tree, List<CoreLabel> tokenAnns) { // figure out begin and end character offsets CoreMap label = (CoreMap) tree.label(); CoreMap beginToken = tokenAnns.get(label.get(BeginIndexAnnotation.class)); CoreMap endToken = tokenAnns.get(label.get(EndIndexAnnotation.class) - 1); int nodeBegin = beginToken.get(CharacterOffsetBeginAnnotation.class); int nodeEnd = endToken.get(CharacterOffsetEndAnnotation.class); // set span, node type, children (mutual recursion), and add it to the JCas node.setBegin(nodeBegin);/* ww w.j a v a 2 s . c o m*/ node.setEnd(nodeEnd); node.setNodeType(tree.value()); node.setChildren(this.addTreebankNodeChildrenToIndexes(node, jCas, tokenAnns, tree)); node.setLeaf(node.getChildren().size() == 0); node.addToIndexes(); }