List of usage examples for edu.stanford.nlp.semgraph SemanticGraph edgeListSorted
public List<SemanticGraphEdge> edgeListSorted()
From source file:context.core.tokenizer.SemanticAnnotation.java
License:Open Source License
/** * * @param text/*w w w. j a v a 2s. c o m*/ * @param docId * @return */ public static Map<String, CustomEdge> tokenize(String text, String docId) { Map<String, CustomEdge> customEdges = new LinkedHashMap<>(); Annotation document = new Annotation(text); pipeline.annotate(document); List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class); int sentIndex = 0; for (CoreMap sentence : sentences) { // traversing the words in the current sentence // a CoreLabel is a CoreMap with additional token-specific methods int index = 0; SemanticGraph dependencies = sentence .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class); // System.out.println(dependencies); for (SemanticGraphEdge edge : dependencies.edgeListSorted()) { CustomEdge cedge = new CustomEdge(); cedge.setDocId(docId); cedge.setSentenceIndex(sentIndex); cedge.setIndex(index); cedge.setWord1(removePOS(edge.getSource() + "")); cedge.setWord2(removePOS(edge.getTarget() + "")); cedge.setType(edge.getRelation() + ""); // System.out.println(edge + " >d: " + edge.getDependent() + " >g: " + edge.getGovernor() + " > " + edge.getRelation() + "> " + edge.getSource() + " > " + edge.getTarget() + " >w: " + edge.getWeight()); customEdges.put(cedge.getWord1() + "/" + cedge.getWord2() + "/" + cedge.getDocId() + "/" + cedge.getSentenceIndex(), cedge); index++; } // Collection<TypedDependency> deps = dependencies.typedDependencies(); // for (TypedDependency typedDep : deps) { // GrammaticalRelation reln = typedDep.reln(); // String type = reln.toString(); // System.out.println("type=" + type + " >> " + typedDep); // } // Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); // sentIndex++; } return customEdges; }
From source file:context.core.tokenizer.SPOExtractor.java
License:Open Source License
static List<SPOStructure> extractSPOs(CoreMap sentence, String docId, int sentIndex) { // traversing the words in the current sentence // a CoreLabel is a CoreMap with additional token-specific methods int index = 0; Map<String, CustomEdge> customEdges = new LinkedHashMap<>(); SemanticGraph dependencies = sentence .get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class); for (SemanticGraphEdge edge : dependencies.edgeListSorted()) { CustomEdge cedge = new CustomEdge(); cedge.setDocId(docId);/*ww w.j av a 2 s .c o m*/ cedge.setSentenceIndex(sentIndex); cedge.setIndex(index); cedge.setWord1(edge.getSource().originalText()); cedge.setWord2(edge.getTarget().originalText()); cedge.setType(edge.getRelation() + ""); customEdges.put(cedge.getWord1() + "/" + cedge.getWord2() + "/" + cedge.getDocId() + "/" + cedge.getSentenceIndex(), cedge); } Collection<String> verbs = extractVerbs(customEdges.values()); List<SPOStructure> spos_list = new ArrayList<>(); for (String v : verbs) { SPOStructure spo = new SPOStructure(); for (CustomEdge cedge : customEdges.values()) { if (cedge.getType().equals("nsubj") && cedge.getWord1().equals(v)) { CustomToken subject = new CustomToken(); String expandedSubject = expandNoun(cedge.getWord2(), customEdges.values()); subject.setWord(expandedSubject); spo.addSubject(subject); } else if (cedge.getType().equals("dobj") && cedge.getWord1().equals(v)) { CustomToken object = new CustomToken(); String expandedObject = expandNoun(cedge.getWord2(), customEdges.values()); object.setWord(expandedObject); spo.addObject(object); } } if (spo.getObjects().isEmpty()) { for (CustomEdge cedge : customEdges.values()) { if (cedge.getType().contains("prep") && cedge.getWord1().equals(v)) { CustomToken object = new CustomToken(); String expandedObject = expandNoun(cedge.getWord2(), customEdges.values()); object.setWord(expandedObject); spo.addObject(object); break; } } } if (spo.getObjects().size() > 0 && spo.getSubjects().size() > 0) { CustomToken predicate = new CustomToken(); predicate.setWord(v); spo.setPredicate(predicate); spos_list.add(spo); } } return spos_list; }
From source file:de.tudarmstadt.ukp.dkpro.core.corenlp.internal.CoreNlp2DKPro.java
License:Open Source License
public static void convertDependencies(JCas aJCas, Annotation document, MappingProvider mappingProvider, boolean internStrings) { for (CoreMap s : document.get(SentencesAnnotation.class)) { SemanticGraph graph = s.get(CollapsedDependenciesAnnotation.class); //SemanticGraph graph = s.get(EnhancedDependenciesAnnotation.class); // If there are no dependencies for this sentence, skip it. Might well mean we // skip all sentences because normally either there are dependencies for all or for // none./*from ww w . j a va2s. c o m*/ if (graph == null) { continue; } for (IndexedWord root : graph.getRoots()) { Dependency dep = new ROOT(aJCas); dep.setDependencyType("root"); dep.setDependent(root.get(TokenKey.class)); dep.setGovernor(root.get(TokenKey.class)); dep.setBegin(dep.getDependent().getBegin()); dep.setEnd(dep.getDependent().getEnd()); dep.setFlavor(DependencyFlavor.BASIC); dep.addToIndexes(); } for (SemanticGraphEdge edge : graph.edgeListSorted()) { Token dependent = edge.getDependent().get(TokenKey.class); Token governor = edge.getGovernor().get(TokenKey.class); // For the type mapping, we use getShortName() instead, because the <specific> // actually doesn't change the relation type String labelUsedForMapping = edge.getRelation().getShortName(); // The nndepparser may produce labels in which the shortName contains a colon. // These represent language-specific labels of the UD, cf: // http://universaldependencies.github.io/docs/ext-dep-index.html labelUsedForMapping = StringUtils.substringBefore(labelUsedForMapping, ":"); // Need to use toString() here to get "<shortname>_<specific>" String actualLabel = edge.getRelation().toString(); Type depRel = mappingProvider.getTagType(labelUsedForMapping); Dependency dep = (Dependency) aJCas.getCas().createFS(depRel); dep.setDependencyType(internStrings ? actualLabel.intern() : actualLabel); dep.setDependent(dependent); dep.setGovernor(governor); dep.setBegin(dep.getDependent().getBegin()); dep.setEnd(dep.getDependent().getEnd()); dep.setFlavor(edge.isExtra() ? DependencyFlavor.ENHANCED : DependencyFlavor.BASIC); dep.addToIndexes(); } } }
From source file:edu.jhu.hlt.concrete.stanford.PreNERCoreMapWrapper.java
License:Open Source License
private List<Dependency> makeDependencies(SemanticGraph graph) { List<Dependency> depList = new ArrayList<Dependency>(); for (IndexedWord root : graph.getRoots()) { // this mimics CoreNLP's handling String rel = GrammaticalRelation.ROOT.getLongName().replaceAll("\\s+", ""); int dep = root.index() - 1; Dependency depend = DependencyFactory.create(dep, rel); depList.add(depend);/*from w ww . ja va2 s. co m*/ } for (SemanticGraphEdge edge : graph.edgeListSorted()) { String rel = edge.getRelation().toString().replaceAll("\\s+", ""); int gov = edge.getSource().index() - 1; int dep = edge.getTarget().index() - 1; Dependency depend = DependencyFactory.create(dep, rel, gov); depList.add(depend); } return depList; }
From source file:it.uniroma2.sag.kelp.input.parser.impl.StanfordParserWrapper.java
License:Apache License
@Override public DependencyGraph parse(String sentenceString) { Annotation document = new Annotation(sentenceString); pipeline.annotate(document);/*w ww . j av a2 s . c o m*/ List<CoreMap> sentences = document.get(SentencesAnnotation.class); CoreMap sentence = sentences.get(0); DependencyGraph graph = new DependencyGraph(); graph.setSentence(sentenceString); graph.setParserName("StanfordParser"); graph.setParserVersion("3.6.0"); graph.setNodes(new ArrayList<DGNode>()); int nId = 1; for (CoreLabel token : sentence.get(TokensAnnotation.class)) { DGNode node = new DGNode(); Map<String, Object> nodeProps = new HashMap<String, Object>(); nodeProps.put("surface", token.originalText()); nodeProps.put("lemma", token.lemma()); nodeProps.put("pos", token.tag()); nodeProps.put("start", token.beginPosition()); nodeProps.put("end", token.endPosition()); nodeProps.put("id", nId); nId++; graph.getNodes().add(node); node.setProperties(nodeProps); } SemanticGraph dependencies = null; switch (dependencyType) { case BASIC: dependencies = sentence.get(BasicDependenciesAnnotation.class); break; case COLLAPSED: dependencies = sentence.get(CollapsedDependenciesAnnotation.class); break; case COLLAPSED_CCPROCESSED: dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); break; default: dependencies = sentence.get(BasicDependenciesAnnotation.class); break; } dependencies.edgeListSorted(); List<DGRelation> relations = new ArrayList<DGRelation>(); for (IndexedWord node : dependencies.vertexListSorted()) { DGRelation relation = new DGRelation(); relation.setProperties(new HashMap<String, Object>()); DGNode child = graph.getDGNodeById(node.index()); relation.setTarget(child); Collection<IndexedWord> parentsTmp = dependencies.getParents(node); ArrayList<IndexedWord> parents = new ArrayList<IndexedWord>(); for (IndexedWord par : parentsTmp) { SemanticGraphEdge edge = dependencies.getEdge(par, node); DGNode parent = graph.getDGNodeById(edge.getGovernor().index()); if (parent.getProperties().get("id") != child.getProperties().get("id")) parents.add(par); } if (parents.isEmpty()) { relation.getProperties().put("type", "root"); relation.getProperties().put("fromId", new Integer(0)); relation.setSource(null); graph.setRoot(relation); } else { Iterator<IndexedWord> it = parents.iterator(); while (it.hasNext()) { IndexedWord par = it.next(); SemanticGraphEdge edge = dependencies.getEdge(par, node); DGNode parent = graph.getDGNodeById(edge.getGovernor().index()); relation.setSource(parent); relation.getProperties().put("fromId", parent.getProperties().get("id")); relation.getProperties().put("type", edge.getRelation().toString()); } } relations.add(relation); } graph.setRelations(relations); return graph; }
From source file:opendial.bn.values.RelationalVal.java
License:Open Source License
public void addGraph(SemanticGraph newGraph) { int oldGraphSize = graph.size(); for (IndexedWord iw : newGraph.vertexListSorted()) { IndexedWord copy = new IndexedWord(iw); copy.setIndex(graph.size());/*ww w .ja va 2 s .co m*/ graph.addVertex(copy); } for (SemanticGraphEdge edge : newGraph.edgeListSorted()) { int dep = edge.getDependent().index() + oldGraphSize; int gov = edge.getGovernor().index() + oldGraphSize; GrammaticalRelation rel = edge.getRelation(); addEdge(gov, dep, rel.getLongName()); } cachedHashCode = 0; }
From source file:shef.mt.tools.ParsingProcessor.java
@Override public void processNextSentence(Sentence s) { //Create resource objects: ArrayList<String> POSData = new ArrayList<>(); HashMap<Integer, Integer> depData = new HashMap<>(); //Get sentences' tokens: String[] sentenceTokens = s.getTokens(); //Create content object: Annotation document = new Annotation(s.getText()); //Annotate content object: pipeline.annotate(document);/*from w w w . j a va 2s. c o m*/ //Initialize index shift: int shift = 0; //Get sentence fragments: List<CoreMap> sentences = document.get(SentencesAnnotation.class); //Initialize token buffer and index: String buffer = ""; int index = 0; for (CoreMap sentence : sentences) { //Get tokens from sentence fragment: List<CoreLabel> tokens = sentence.get(TokensAnnotation.class); //Add tokens to resulting POS tag list: for (CoreLabel token : tokens) { String[] fragments = new String[] { token.originalText() }; if (token.originalText().contains(" ")) { fragments = token.originalText().split(" "); } String pos = token.get(PartOfSpeechAnnotation.class); for (String fragment : fragments) { buffer += fragment; if (buffer.trim().equals(sentenceTokens[index])) { POSData.add(pos); index += 1; buffer = ""; } } } //Check for dependency parsing requirement: if (this.requiresDepCounts) { //Get dependency relations: SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class); List<SemanticGraphEdge> deps = dependencies.edgeListSorted(); //For each edge, add 1 to occurrences of source and target indexes: for (SemanticGraphEdge sge : deps) { int sourceIndex = shift + sge.getSource().index() - 1; int targetIndex = shift + sge.getTarget().index() - 1; if (depData.get(sourceIndex) == null) { depData.put(sourceIndex, 1); } else { depData.put(sourceIndex, depData.get(sourceIndex) + 1); } if (depData.get(targetIndex) == null) { depData.put(targetIndex, 1); } else { depData.put(targetIndex, depData.get(targetIndex) + 1); } //Increase shift: shift += tokens.size(); } } } //Add resources to sentence: s.setValue("postags", POSData); s.setValue("depcounts", depData); }