List of usage examples for edu.stanford.nlp.semgraph SemanticGraph getEdge
public SemanticGraphEdge getEdge(IndexedWord gov, IndexedWord dep)
From source file:edu.illinois.cs.cogcomp.pipeline.handlers.StanfordDepHandler.java
License:Open Source License
private void populateChildren(SemanticGraph depGraph, IndexedWord root, Tree<Pair<String, Integer>> tree, TextAnnotation ta, int sentId) { if (depGraph.getChildren(root).size() == 0) return;/* ww w. j a va2 s . c om*/ for (IndexedWord child : depGraph.getChildren(root)) { int childPosition = getNodePosition(ta, child, sentId); Pair<String, Integer> nodePair = new Pair<>(child.originalText(), childPosition); Tree<Pair<String, Integer>> childTree = new Tree<>(nodePair); tree.addSubtree(childTree, new Pair<>(depGraph.getEdge(root, child).toString(), childPosition)); populateChildren(depGraph, child, childTree, ta, sentId); } }
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);//from ww w . j a va2s. com 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:sleventextraction.SLEntity.java
public SLEntity(AceMention m, CoreMap senCM, SemanticGraph senSG) { this();//from w ww. j av a2 s. c om isArg = m.isArg; argProb = m.argProb; role = m.role; if (m.getParent() instanceof AceJet.AceEntity) { this.entitytype = ((AceEntity) m.getParent()).type; this.entitysubtype = ((AceEntity) m.getParent()).subtype; } else if (m.getParent() instanceof AceJet.AceTimex) { this.entitytype = ""; this.entitysubtype = ""; } else if (m.getParent() instanceof AceJet.AceValue) { this.entitytype = ((AceValue) m.getParent()).type; this.entitysubtype = ((AceValue) m.getParent()).subtype; } else { this.entitytype = ""; this.entitysubtype = ""; } this.mentiontype = m.getType(); System.arraycopy(m.roleProb, 0, roleProb, 0, m.roleProb.length); ground = m.ground; span = senCM; SemanticGraph totaldep = span.get(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation.class); this.content = m.text.trim(); if (m.text.charAt(0) == '\"') { this.content = m.text.substring(1).trim(); } if ("s\nb".equals(this.content)) { this.content = "his brother"; } else if (" f".equals(this.content)) { this.content = "foreign"; } else if ("-l".equals(this.content)) { this.content = "US-led"; } else if ("s a".equals(this.content)) { if (span.toString().contains("Arafat's administration")) { this.content = "Arafat's administration"; } else if (span.toString().contains("bus attack")) { this.content = "bus attack"; } } else if ("33-month".equals(this.content)) { this.content = "33-month-old"; } else if ("U.S".equals(this.content)) { this.content = "U.S."; } else if ("four-day".equals(this.content)) { this.content = "four-day-old"; } else if ("U.N".equals(this.content)) { this.content = "U.N."; } else if ("33-year".equals(this.content)) { this.content = "33-year-old"; } Annotation document = ParseSentence(this.content); List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class); CoreMap cm = sentences.get(0); int pathlength = -1, imin = 1000; for (int i = 0; i < senCM.get(TokensAnnotation.class).size(); i++) { IndexedWord debug = new IndexedWord(senCM.get(TokensAnnotation.class).get(i)); boolean canmatch = true; for (int j = 0; j < cm.get(TokensAnnotation.class).size(); j++) { IndexedWord iw = new IndexedWord(senCM.get(TokensAnnotation.class).get(i + j)); IndexedWord shortiw = new IndexedWord(cm.get(TokensAnnotation.class).get(j)); if (!iw.word().equals(shortiw.word())) { if (SLEventExtraction.overlap(iw.word(), shortiw.word()) <= 0 || Double.isNaN(SLEventExtraction.overlap(iw.word(), shortiw.word()))) { canmatch = false; break; } } } if (canmatch) { for (int j = 0; j < cm.get(TokensAnnotation.class).size(); j++) { IndexedWord iw = new IndexedWord(senCM.get(TokensAnnotation.class).get(i + j)); this.ContentIws.add(iw); try { pathlength = totaldep.getPathToRoot(iw).size(); } catch (java.lang.IllegalArgumentException err) { pathlength = 100; } if (imin > pathlength) { imin = pathlength; this.head = iw; } } break; } } if (this.head == null) { return; } this.predicate = totaldep.getParent(this.head); if (this.predicate == null) { this.predicate = this.head; } else { IndexedWord curr = head; dep = totaldep.getEdge(predicate, curr).getRelation().getShortName(); if (totaldep.getEdge(predicate, curr).getRelation().getSpecific() != null) { dep += "_" + totaldep.getEdge(predicate, curr).getRelation().getSpecific(); } } }
From source file:sleventextraction.SLEventExtraction.java
public static List<SLEntity> BPGetNPs(SemanticGraph dependencies) { List<SLEntity> res = new LinkedList<>(); Set<IndexedWord> done_tokens = new HashSet<>(); Queue<IndexedWord> roots = new LinkedList(dependencies.getRoots()); Map<IndexedWord, Pair<String, IndexedWord>> modified_dep = new HashMap<>(); while (!roots.isEmpty()) { IndexedWord curr = roots.poll(); roots.addAll(dependencies.getChildren(curr)); if ((curr.tag().contains("NN") || curr.tag().contains("CD") || curr.tag().contains("PRP") || curr.tag().contains("JJ") || curr.tag().contains("RB") || curr.tag().contains("FW") || curr.tag().contains("DT") || dependencies.getParent(curr) != null && "csubj".equals( dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName()) || dependencies.getParent(curr) != null && "nsubj".equals( dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName()) || dependencies.getParent(curr) != null && "nsubjpass".equals( dependencies.getEdge(dependencies.getParent(curr), curr).getRelation().getShortName())) && !done_tokens.contains(curr)) { SLEntity e = new SLEntity(); e.head = curr;/*from ww w .j av a2s.co m*/ e.predicate = dependencies.getParent(curr); if (e.predicate == null) { // e is root e.predicate = e.head; e.content = GetNPstring(curr, dependencies, done_tokens); e.dep = "self"; } else { e.content = GetNPstring(curr, dependencies, done_tokens); try { e.dep = dependencies.getEdge(e.predicate, curr).getRelation().getShortName(); if (dependencies.getEdge(e.predicate, curr).getRelation().getSpecific() != null) { e.dep += "_" + dependencies.getEdge(e.predicate, curr).getRelation().getSpecific(); } if ("appos".equals(e.dep) || "conj_and".equals(e.dep)) { if (modified_dep.containsKey(e.predicate)) { e.dep = modified_dep.get(e.predicate).getKey(); e.predicate = modified_dep.get(e.predicate).getValue(); modified_dep.put(curr, new Pair<>(e.dep, e.predicate)); } else { e.dep = dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate) .getRelation().getShortName(); if (dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate) .getRelation().getSpecific() != null) { e.dep += "_" + dependencies.getEdge(dependencies.getParent(e.predicate), e.predicate) .getRelation().getSpecific(); } e.predicate = dependencies.getParent(e.predicate); modified_dep.put(curr, new Pair<>(e.dep, e.predicate)); } } else if (e.predicate.tag().contains("NN") || e.predicate.tag().contains("PRP") || e.predicate.tag().contains("CD") || e.predicate.tag().contains("RB")) { if (modified_dep.containsKey(e.predicate)) { e.predicate = modified_dep.get(e.predicate).getValue(); modified_dep.put(curr, new Pair<>(e.dep, e.predicate)); } else { if (dependencies.getParent(e.predicate) != null) { e.predicate = dependencies.getParent(e.predicate); modified_dep.put(curr, new Pair<>(e.dep, e.predicate)); } } } } catch (java.lang.NullPointerException err) { continue; } } if (modified_dep.containsKey(e.predicate)) { e.Grand_predicate = modified_dep.get(e.predicate).getValue(); } else { e.Grand_predicate = dependencies.getParent(e.predicate); if (e.Grand_predicate != null) { if (e.Grand_predicate.tag().contains("NN") || e.Grand_predicate.tag().contains("PRP") || e.Grand_predicate.tag().contains("CD") || e.Grand_predicate.tag().contains("RB")) { if (modified_dep.containsKey(e.Grand_predicate)) { e.Grand_predicate = modified_dep.get(e.Grand_predicate).getValue(); } else { if (dependencies.getParent(e.Grand_predicate) != null) { e.Grand_predicate = dependencies.getParent(e.Grand_predicate); } } } } } res.add(e); } } return res; }