List of usage examples for edu.stanford.nlp.semgraph SemanticGraph getParent
public IndexedWord getParent(IndexedWord vertex)
From source file:count_dep.Count_dep.java
public LinkedList<Event> GetEvents(SemanticGraph dependencies, CoreMap sentence) { LinkedList<Event> res = new LinkedList<>(); LinkedList<IndexedWord> roots = new LinkedList<>(); List<CoreLabel> words = sentence.get(TokensAnnotation.class); List<GrammaticalRelation> senserel = new LinkedList<>(); senserel.add(GrammaticalRelation.valueOf("nsubj")); senserel.add(GrammaticalRelation.valueOf("dobj")); for (CoreLabel word : words) { if (word.tag().length() >= 2 && ("VB".equals(word.tag().substring(0, 2)) || "NN".equals(word.tag().substring(0, 2)))) { IndexedWord iword = new IndexedWord(word); roots.add(iword);//from w w w . j a va 2s . co m } } for (IndexedWord word : roots) { Event e = new Event(); e.trigger = word.word(); try { Set<IndexedWord> children = dependencies.getChildren(word); children.stream().forEach((iw) -> { e.arguments.add(new EventArgument(iw.word(), "")); }); if (dependencies.inDegree(word) > 0) { IndexedWord parent = dependencies.getParent(word); if (parent.tag().length() >= 2 && "VB".equals(parent.tag().substring(0, 2))) { Set<IndexedWord> children1 = dependencies.getChildrenWithRelns(parent, senserel); children1.remove(word); children1.stream().forEach((iw) -> { e.arguments.add(new EventArgument(iw.word(), "")); }); } else { e.arguments.add(new EventArgument(dependencies.getParent(word).word(), "")); } } } catch (java.lang.IllegalArgumentException error) { continue; } res.add(e); } return res; }
From source file:sleventextraction.SLEntity.java
public SLEntity(AceMention m, CoreMap senCM, SemanticGraph senSG) { this();/*from w w w . j a va2 s .c o m*/ 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;/* w w w. ja v a 2 s . c om*/ 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; }