List of usage examples for edu.stanford.nlp.semgraph SemanticGraph getRoots
public Collection<IndexedWord> getRoots()
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 a v a2s . c o 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; }