List of usage examples for edu.stanford.nlp.semgraph SemanticGraph getFirstRoot
public IndexedWord getFirstRoot()
From source file:edu.illinois.cs.cogcomp.pipeline.handlers.StanfordDepHandler.java
License:Open Source License
@Override public void addView(TextAnnotation textAnnotation) throws AnnotatorException { // If the sentence is longer than STFRD_MAX_SENTENCE_LENGTH there is no point in trying to // parse// w w w. j a va 2 s .co m StanfordParseHandler.checkLength(textAnnotation, throwExceptionOnSentenceLengthCheck, maxParseSentenceLength); TreeView treeView = new TreeView(ViewNames.DEPENDENCY_STANFORD, "StanfordDepHandler", textAnnotation, 1d); // The (tokenized) sentence offset in case we have more than one sentences in the record List<CoreMap> sentences = StanfordParseHandler.buildStanfordSentences(textAnnotation); Annotation document = new Annotation(sentences); posAnnotator.annotate(document); parseAnnotator.annotate(document); sentences = document.get(CoreAnnotations.SentencesAnnotation.class); if (sentences.get(0).get(TreeCoreAnnotations.TreeAnnotation.class).nodeString().equals("X")) { // This is most like because we ran out of time throw new AnnotatorException("Unable to parse TextAnnotation " + textAnnotation.getId() + ". " + "This is most likely due to a timeout."); } for (int sentenceId = 0; sentenceId < sentences.size(); sentenceId++) { boolean runtimeExceptionWasThrown = false; CoreMap sentence = sentences.get(sentenceId); if (maxParseSentenceLength > 0 && sentence.size() > maxParseSentenceLength) { logger.warn(HandlerUtils.getSentenceLengthError(textAnnotation.getId(), sentence.toString(), maxParseSentenceLength)); } else { SemanticGraph depGraph = sentence .get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class); IndexedWord root = null; try { root = depGraph.getFirstRoot(); } catch (RuntimeException e) { String msg = "ERROR in getting root of dep graph for sentence. Sentence is:\n" + sentence.toString() + "'\nDependency graph is:\n" + depGraph.toCompactString() + "\nText is:\n" + textAnnotation.getText(); logger.error(msg); System.err.println(msg); e.printStackTrace(); if (throwExceptionOnSentenceLengthCheck) throw e; else runtimeExceptionWasThrown = true; } if (!runtimeExceptionWasThrown) { int tokenStart = getNodePosition(textAnnotation, root, sentenceId); Pair<String, Integer> nodePair = new Pair<>(root.originalText(), tokenStart); Tree<Pair<String, Integer>> tree = new Tree<>(nodePair); populateChildren(depGraph, root, tree, textAnnotation, sentenceId); treeView.setDependencyTree(sentenceId, tree); } } } textAnnotation.addView(getViewName(), treeView); }
From source file:edu.nus.comp.nlp.stanford.UtilParser.java
License:Open Source License
public static DefaultMutableTreeNode toDMTree(IndexedWord root, SemanticGraph dependencies) { if (root == null) { root = dependencies.getFirstRoot(); }//from ww w . j a v a 2s .c o m DefaultMutableTreeNode node = new DefaultMutableTreeNode(); String nodeContent = root.value(); for (SemanticGraphEdge edge : dependencies.edgeIterable()) { if (edge.getDependent().equals(root)) { nodeContent = "<-" + edge.getRelation() + "- " + nodeContent; break; } } node.setUserObject(nodeContent); for (IndexedWord c : dependencies.getChildList(root)) { DefaultMutableTreeNode n = toDMTree(c, dependencies); node.add(n); } return node; }
From source file:nlp.prototype.NewJFrame.java
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked DefaultTreeModel model2 = (DefaultTreeModel) jTree2.getModel(); DefaultMutableTreeNode rootNode2 = new DefaultMutableTreeNode("top"); model2.setRoot(rootNode2);/*from w w w.j av a 2 s .c o m*/ /*TextCorpus textCorpus = processor.parseCorpus(jTextArea1.getText()); for (SentenceToken token : textCorpus.getSentences()) { DefaultMutableTreeNode sentenceTokenNode = new DefaultMutableTreeNode(); sentenceTokenNode.setUserObject(token); rootNode2.add(sentenceTokenNode); addNodes(token, sentenceTokenNode); } DefaultTokenSerializer serializer = new DefaultTokenSerializer(); Document xmlDocument = serializer.serialize(textCorpus); jTextArea4.setText(serializer.transform(xmlDocument)); jTextArea7.setText(serializer.transform(xmlDocument, this.jTextArea6.getText()));*/ Annotation document = new Annotation(jTextArea1.getText()); pipeline.annotate(document); List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class); Map<Integer, CorefChain> corefMap = document.get(CorefChainAnnotation.class); List<CoreLabel> tokens = document.get(CoreAnnotations.TokensAnnotation.class); DefaultListModel listModel = new DefaultListModel(); for (Class key : document.keySet()) { Object value = document.get(key); if (value != null && value.toString() != null && !value.toString().isEmpty()) { listModel.addElement(key.toString() + " - [" + value.toString() + "]"); } } DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel(); DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("top"); model.setRoot(rootNode); List<POSToken> tokenList = new ArrayList<>(); jList1.setModel(listModel); for (CoreMap sentence : sentences) { Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); SemanticGraph graph = sentence.get(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class); String root = graph.getFirstRoot().originalText(); MultiValuedMap<String, GrammarToken> map = new HashSetValuedHashMap<>(); for (SemanticGraphEdge edge : graph.edgeIterable()) { GrammarToken grammarToken = new GrammarToken(edge); map.put(grammarToken.getTarget(), grammarToken); } DefaultMutableTreeNode node = new DefaultMutableTreeNode(); POSToken token = new POSToken((CoreLabel) tree.label()); token.setGrammar(graph.toString()); node.setUserObject(token); rootNode.add(node); addNodes(tree, false, node, node, map, root, corefMap, tokens); tokenList.add(token); } setAdjacentNodes(tokenList); }
From source file:nlp.service.implementation.DefaultGrammarService.java
public DefaultGrammarService(SemanticGraph graph) { targetMap = new HashSetValuedHashMap<>(); rootIndex = graph.getFirstRoot().index(); for (SemanticGraphEdge edge : graph.edgeIterable()) { GrammaticalDependency dependency; try {/*w w w. j a va 2 s. co m*/ String relation = edge.getRelation().toString(); if (relation.contains(":")) { relation = relation.substring(relation.indexOf(':') + 1, relation.length()); } if (relation.equals("case")) { dependency = GrammaticalDependency.casemarker; } else { dependency = GrammaticalDependency.valueOf(relation); } } catch (IllegalArgumentException e) { dependency = GrammaticalDependency.unknown; } GrammaticalRelation<Integer> relation = new GrammaticalRelation<>(dependency, edge.getTarget().index(), edge.getSource().index()); targetMap.put(relation.getTarget(), relation); } }