List of usage examples for edu.stanford.nlp.trees Tree isLeaf
public boolean isLeaf()
From source file:org.lambda3.text.simplification.discourse.utils.parseTree.ParseTreeExtractionUtils.java
License:Open Source License
public static List<Tree> getLeavesInBetween(Tree anchorTree, Tree leftNode, Tree rightNode, boolean includeLeft, boolean includeRight) { List<Tree> res = new ArrayList<>(); if (leftNode == null) { leftNode = getFirstLeaf(anchorTree); }//from ww w. java2s.c om if (rightNode == null) { rightNode = getLastLeaf(anchorTree); } int startLeafNumber = (includeLeft) ? getFirstLeaf(leftNode).nodeNumber(anchorTree) : getLastLeaf(leftNode).nodeNumber(anchorTree) + 1; int endLeafNumber = (includeRight) ? getLastLeaf(rightNode).nodeNumber(anchorTree) : getFirstLeaf(rightNode).nodeNumber(anchorTree) - 1; if ((startLeafNumber < 0) || (endLeafNumber < 0)) { return res; } for (int i = startLeafNumber; i <= endLeafNumber; ++i) { Tree node = anchorTree.getNodeNumber(i); if (node.isLeaf()) { res.addAll(node); } } return res; }
From source file:org.nlp2rdf.implementation.stanfordcore.StanfordCoreNLPWrapper.java
License:Open Source License
public void processTree(Tree currentNode, URIGenerator uriGenerator, String prefix, String text, OntModel model) {/*from w w w . j a v a2s.co m*/ // String tag = currentNode.label().value(); //log.info("Current Node :" + currentNode); //log.info("Label: " + currentNode.label() + ""); //log.info("Label Value: " + currentNode.label().value() + ""); //log.info("Preterminal: " + currentNode.isPreTerminal() + ""); //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + ""); if (currentNode.isLeaf()) { //the node is a leaf and belongs in the realm of pos tagging } else { Phrase p = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text, getSpan(currentNode), uriGenerator, model); List<Tree> children = currentNode.getChildrenAsList(); for (Tree child : children) { /* if (false && child.isPreTerminal()) { //skip preterminals log.debug("skipping preterminal: "+currentNode); log.debug("label: "+currentNode.label()); child = child.getChild(0); Word childTerminal = new Text2RDF().createStringAnnotationForClass(Word.class, prefix, text, getSpan(child), uriGenerator, model); p.addChild(childTerminal); */ Phrase childPhrase = new Text2RDF().createStringAnnotationForClass(Phrase.class, prefix, text, getSpan(child), uriGenerator, model); p.addChild(childPhrase); processTree(child, uriGenerator, prefix, text, model); log.info("Current Node :" + currentNode); log.info("Label: " + currentNode.label() + ""); log.info("Label Value: " + currentNode.label().value() + ""); log.info("Preterminal: " + currentNode.isPreTerminal() + ""); //log.info("Index: " + ((CoreLabel) currentNode.label()).get(CharacterOffsetBeginAnnotation.class) + ""); //adding syntax classes from olia and olia-top String tag = ((CoreLabel) currentNode.label()).get(CategoryAnnotation.class); Set<String> classes = penn_syntax.getClassURIsForTag(tag); for (String classUri : classes) { log.info("found: " + classUri + " for: " + tag); OntModel hierarchy = penn_syntax.getHierarchy(classUri); for (ExtendedIterator<OntClass> it = hierarchy.listClasses(); it.hasNext();) { OntClass oc = it.next(); p.addOntClass(model.createResource(oc.getURI())); } //Copy the hierarchy model.add(hierarchy); } } } }
From source file:pltag.parser.semantics.discriminative.ExtractFeatures.java
License:Open Source License
private boolean sameLabelAtLevel(Tree left, Tree right, int level) { if (left.isLeaf() || right.isLeaf()) return false; if (level == 0) return left.nodeString().equals(right.nodeString()); Tree[] leftChildren = left.children(); Tree[] rightChildren = right.children(); if (leftChildren.length != rightChildren.length) return false; for (int i = 0; i < leftChildren.length; i++) { if (!sameLabelAtLevel(leftChildren[i], rightChildren[i], level - 1)) return false; }//from w w w . j av a 2 s.co m return true; }
From source file:reactivetechnologies.sentigrade.engine.nlp.SentimentAnalyzer.java
License:Apache License
private static void print(Tree tree) { if (LOG.isDebugEnabled()) { Tree t = tree.deepCopy(); //setSentimentLabels(t); LOG.debug("leaf?" + t.isLeaf() + " phrrasal?" + t.isPhrasal() + " preterminal?" + t.isPreTerminal() + " class:" + RNNCoreAnnotations.getPredictedClass(t)); LOG.debug("" + t + ""); }/* w w w . ja va2 s .c o m*/ }
From source file:reactivetechnologies.sentigrade.engine.nlp.SentimentAnalyzer.java
License:Apache License
static void setSentimentLabels(Tree tree) { if (tree.isLeaf()) { return;//from w w w .j av a2 s.co m } for (Tree child : tree.children()) { setSentimentLabels(child); } outputPredictedClass(tree); // System.err.println("end '"+tree.label().value()+"'"); }
From source file:sg.edu.nus.comp.pdtb.parser.ArgExtComp.java
License:Open Source License
private String calcNodesSpan(List<String> nodes, Map<String, String> spanHashMap, String connSpan, List<String> otherArg) { Set<String> conn = new HashSet<>(); String[] c = connSpan.split(";"); for (String e : c) { conn.add(e);//from ww w . j a v a 2 s. co m } List<Span> spans = new ArrayList<>(); String[] d = otherArg != null ? otherArg.get(0).split(":") : null; int skipTreeNum = -1; int skipNodeNumber = -1; if (d != null) { skipTreeNum = Integer.parseInt(d[0]); skipNodeNumber = Integer.parseInt(d[1]); } // for debugging purposes @SuppressWarnings("unused") Tree skipNode = d != null ? trees.get(skipTreeNum).getNodeNumber(skipNodeNumber) : null; for (String txt : nodes) { String[] tmp = txt.split(":"); int treeNum = Integer.parseInt(tmp[0]); Tree root = trees.get(treeNum); Tree node = root.getNodeNumber(Integer.parseInt(tmp[1])); if (node == null) { continue; } Queue<Tree> children = new LinkedList<>(); children.add(node); while (children.size() > 0) { Tree child = children.poll(); // the same tree and the same node number as the other argument if (skipTreeNum == treeNum && child.nodeNumber(root) == skipNodeNumber) { continue; } else if (!child.isLeaf()) { children.addAll(child.getChildrenAsList()); } else { int nodeNum = child.nodeNumber(root); String span = spanHashMap.get(treeNum + ":" + nodeNum); if (span != null && !hasIntersection(span, conn)) { spans.add(new Span(span)); } } } } Collections.sort(spans); StringBuilder sb = new StringBuilder(); for (Span span : spans) { if (sb.length() > 0) { String end = sb.substring(sb.lastIndexOf(".") + 1); String start = Integer.toString(span.start); if (Integer.parseInt(start) - Integer.parseInt(end) > 2) { sb.append(";"); sb.append(span); } else { sb.delete(sb.lastIndexOf(".") + 1, sb.length()); try { end = Integer.toString(span.end); } catch (StringIndexOutOfBoundsException e) { log.error("Error: " + e.getMessage()); e.printStackTrace(); } sb.append(end); } } else { sb.append(span); } } String result = sb.toString(); String out = removePunctuation(result); return out; }
From source file:sg.edu.nus.comp.pdtb.parser.NonExplicitComp.java
License:Open Source License
public static void getProductionRules(Tree tree, HashMap<String, Integer> prodRules) { String[] tr = tree.value().replaceAll("=[0-9]+", "").split("-"); String value = tr.length > 0 ? tr[0] : tree.value(); if (tree.isLeaf() || tree.value().startsWith("-")) { value = tree.value();/*from ww w . j ava2s .c o m*/ } StringBuilder rule = new StringBuilder(value); rule.append(" ->"); for (Tree child : tree.children()) { if (!child.value().equalsIgnoreCase("-NONE-")) { String[] tmp = child.value().replaceAll("=[0-9]+", "").split("-"); String cVal = tmp.length > 0 ? tmp[0] : child.value(); if (child.isLeaf() || child.value().startsWith("-")) { cVal = child.value(); } rule.append(" "); rule.append(cVal); } } String key = rule.toString().replaceAll("\\s+", "_"); // has some child values if (!key.endsWith("->")) { Integer count = prodRules.get(key); if (count == null) { count = 0; } ++count; prodRules.put(key, count); } for (Tree child : tree.children()) { if (!child.value().equalsIgnoreCase("-NONE-")) { getProductionRules(child, prodRules); } } }
From source file:tml.utils.StanfordUtils.java
License:Apache License
/** * @param t the tree to which extract the content * @return the string with the content of the tree *///from w w w. ja v a 2 s . co m public static String nodeContent(Tree t, Tree pv) { if (t.isLeaf()) return t.value(); StringBuffer buff = new StringBuffer(); for (Tree tt : t.children()) { if (!t.value().equals("DT") && !t.value().equals("SYM") && !t.value().startsWith("PRP")) { buff.append(nodeContent(tt, t)); buff.append(" "); } } String clean = buff.toString().replace("\\s+", " ").trim(); return clean; }
From source file:tml.utils.StanfordUtils.java
License:Apache License
/** * @param t the tree to which extract the content * @return the string with the content of the tree *//*ww w.j a v a2 s .c o m*/ public static String nodeContent(Tree t) { if (t.isLeaf()) return t.value(); StringBuffer buff = new StringBuffer(); for (Tree tt : t.children()) { buff.append(nodeContent(tt)); buff.append(" "); } String clean = buff.toString().replace("\\s+", " ").trim(); return clean; }
From source file:tml.utils.StanfordUtils.java
License:Apache License
/** * Calculates the typed dependencies from a grammatical tree * @param tree the grammatical tree/*from w w w. j av a2 s . co m*/ */ public static List<String> calculateTypedDependencies(Tree tree) { double time = System.nanoTime(); List<String> output = new ArrayList<String>(); GrammaticalStructure gs = null; try { gs = getGrammaticalStructureFactory().newGrammaticalStructure(tree); } catch (Exception e) { logger.error(e); return null; } Collection<TypedDependency> tdl = gs.typedDependenciesCollapsed(); // Get the POS tag from each word Hashtable<String, String> posInfo = new Hashtable<String, String>(); for (Tree t : tree.getLeaves()) { Tree pt = null; for (Tree tt : tree.dominationPath(t)) { if (tt.isLeaf()) { posInfo.put(tt.nodeString(), pt.nodeString()); } pt = tt; } } for (Object obj : tdl.toArray()) { TypedDependency dep = (TypedDependency) obj; String wordGov = dep.gov().nodeString().split("-")[0]; String wordDep = dep.dep().nodeString().split("-")[0]; String posGov = posInfo.get(wordGov); String posDep = posInfo.get(wordDep); String dependencyString = dep.reln().toString() + "(" + dep.gov().pennString().trim() + "-" + posGov + ", " + dep.dep().pennString().trim() + "-" + posDep + ")"; output.add(dependencyString); } time = System.nanoTime() - time; logger.debug("Typed dependencies obtained in " + time * 10E-6 + " milliseconds"); return output; }