/** * This method gets rid of multiwords in conjunctions to avoid having them * creating disconnected constituents e.g., * "bread-1 as-2 well-3 as-4 cheese-5" will be turned into conj_and(bread, * cheese) and then dep(well-3, as-2) and dep(well-3, as-4) cannot be attached * to the graph, these dependencies are erased * * @param list List of words to get rid of multiword conjunctions from */ private static void eraseMultiConj(Collection<TypedDependency> list) { // find typed deps of form cc(gov, x) for (TypedDependency td1 : list) { if (td1.reln() == COORDINATION) { IndexedWord x = td1.dep(); // find typed deps of form dep(x,y) and kill them for (TypedDependency td2 : list) { if (td2.gov().equals(x) && (td2.reln() == DEPENDENT || td2.reln() == MULTI_WORD_EXPRESSION || td2.reln() == COORDINATION || td2.reln() == ADVERBIAL_MODIFIER || td2.reln() == NEGATION_MODIFIER || td2.reln() == AUX_MODIFIER)) { td2.setReln(KILL); } } } } filterKill(list); }
/** Returns a list of TypedDependency in the graph. * This method goes through all SemanticGraphEdge and converts them * to TypedDependency. * * @return A List of TypedDependency in the graph */ public Collection<TypedDependency> typedDependencies() { Collection<TypedDependency> dependencies = new ArrayList<>(); IndexedWord root = null; for (IndexedWord node : roots) { if (root == null) { root = new IndexedWord(node.docID(), node.sentIndex(), 0); root.setValue("ROOT"); } TypedDependency dependency = new TypedDependency(ROOT, root, node); dependencies.add(dependency); } for (SemanticGraphEdge e : this.edgeIterable()){ TypedDependency dependency = new TypedDependency(e.getRelation(), e.getGovernor(), e.getDependent()); if (e.isExtra()) { dependency.setExtra(); } dependencies.add(dependency); } return dependencies; }
/** * This is the constructor used by the parser. */ public SemanticGraph(Collection<TypedDependency> dependencies) { graph = new DirectedMultiGraph<>(outerMapFactory, innerMapFactory); roots = wordMapFactory.newSet(); for (TypedDependency d : dependencies) { IndexedWord gov = d.gov(); IndexedWord dep = d.dep(); GrammaticalRelation reln = d.reln(); if (reln != ROOT) { // the root relation only points to the root: the governor is a fake node that we don't want to add in the graph // It is unnecessary to call addVertex, since addEdge will // implicitly add vertices if needed //addVertex(gov); //addVertex(dep); addEdge(gov, dep, reln, Double.NEGATIVE_INFINITY, d.extra()); } else { //it's the root and we add it addVertex(dep); roots.add(dep); } } // there used to be an if clause that filtered out the case of empty // dependencies. However, I could not understand (or replicate) the error // it alluded to, and it led to empty dependency graphs for very short // fragments, // which meant they were ignored by the RTE system. Changed. (pado) // See also SemanticGraphFactory.makeGraphFromTree(). }
private static String toReadableString(Collection<TypedDependency> dependencies) { StringBuilder buf = new StringBuilder(); buf.append(String.format("%-20s%-20s%-20s%n", "dep", "reln", "gov")); buf.append(String.format("%-20s%-20s%-20s%n", "---", "----", "---")); for (TypedDependency td : dependencies) { buf.append(String.format("%-20s%-20s%-20s%n", td.dep(), td.reln(), td.gov())); } return buf.toString(); }
for (Iterator<TypedDependency> iter = list.iterator(); iter.hasNext();) { TypedDependency td = iter.next(); if (td.reln() == REFERENT) { refs.add(td); iter.remove(); IndexedWord dep = ref.dep();// take the relative word IndexedWord ant = ref.gov();// take the antecedent for (TypedDependency td : list) { if (td.dep().equals(dep) && td.reln() != REFERENT && !td.gov().equals(ant)) { if (DEBUG) { log.info("referent: changing " + td); td.setDep(ant); td.setExtra(); if (DEBUG) { log.info(" to " + td);
/** * Return a list of TypedDependencies which are not dependent on any node from the list. * * @param list The list of TypedDependencies to check * @return A list of TypedDependencies which are not dependent on any node from the list */ public static Collection<TypedDependency> getRoots(Collection<TypedDependency> list) { Collection<TypedDependency> roots = new ArrayList<>(); // need to see if more than one governor is not listed somewhere as a dependent // first take all the deps Collection<IndexedWord> deps = Generics.newHashSet(); for (TypedDependency typedDep : list) { deps.add(typedDep.dep()); } // go through the list and add typedDependency for which the gov is not a dep Collection<IndexedWord> govs = Generics.newHashSet(); for (TypedDependency typedDep : list) { IndexedWord gov = typedDep.gov(); if (!deps.contains(gov) && !govs.contains(gov)) { roots.add(typedDep); } govs.add(gov); } return roots; }
List<TypedDependency> extraDeps = new ArrayList<>(); for (TypedDependency td : dependencies) { if (td.extra()) { extraDeps.add(td); } else { buf.append(td.toString(labelFormat)).append('\n'); buf.append("======\n"); for (TypedDependency td : extraDeps) { buf.append(td.toString(labelFormat)).append('\n'); buf.append(td.toString(labelFormat)).append('\n');
private static void removeHeadsAssignedToPunc(Set<TypedDependency> depSet) { List<TypedDependency> deps = new ArrayList<>(depSet); for (TypedDependency dep : deps) { if (langIndependentPuncCheck(dep.dep().word())) { if (VERBOSE) { System.err.printf("Dropping Punctuation Dependency: %s\n", dep); } depSet.remove(dep); } } }
@Override public String toString() { return toString(CoreLabel.OutputFormat.VALUE_INDEX); }
public static Map<IndexedWord,List<TypedDependency>> govToDepMap(List<TypedDependency> deps) { Map<IndexedWord,List<TypedDependency>> govToDepMap = Generics.newHashMap(); for (TypedDependency dep : deps) { IndexedWord gov = dep.gov(); List<TypedDependency> depList = govToDepMap.get(gov); if (depList == null) { depList = new ArrayList<>(); govToDepMap.put(gov, depList); } depList.add(dep); } return govToDepMap; }
if (grel == null) { if (grelString.toLowerCase().equals("root")) { tdep = new TypedDependency(ROOT, dependencyRoot, tgWords.get(i)); } else { throw new RuntimeException("Unknown grammatical relation '" + parentId = -1; tdep = new TypedDependency(grel, (parentId == -1 ? dependencyRoot : tgWords.get(parentId)), tgWords.get(i));
private static boolean inConjDeps(TypedDependency td, List<Triple<TypedDependency, TypedDependency, Boolean>> conjs) { for (Triple<TypedDependency, TypedDependency, Boolean> trip : conjs) { if (td.equals(trip.first())) { return true; } } return false; }
List<TypedDependency> extraDeps = new ArrayList<>(); for (TypedDependency dep : deps) { if (dep.extra()) { extraDeps.add(dep); } else {
public static List<Collection<TypedDependency>> convertStringEquality(List<Collection<TypedDependency>> deps){ List<Collection<TypedDependency>> convertedDeps = new ArrayList<>(); for(Collection<TypedDependency> depSet : deps){ Collection<TypedDependency> converted = Generics.newHashSet(); for(TypedDependency dep : depSet){ converted.add(new TypedDependencyStringEquality(dep.reln(), dep.gov(), dep.dep())); } convertedDeps.add(converted); } return convertedDeps; }
for (Iterator<TypedDependency> iter = list.iterator(); iter.hasNext();) { TypedDependency td = iter.next(); if (td.reln() == REFERENT) { refs.add(td); iter.remove(); TreeGraphNode dep = ref.dep();// take the relative word TreeGraphNode ant = ref.gov();// take the antecedent for (TypedDependency td : list) { if (td.dep() == dep && td.reln() != RELATIVE && td.reln() != REFERENT && td.gov() != ant) { if (DEBUG) System.err.print("referent: changing " + td); td.setDep(ant); if (DEBUG) System.err.println(" to " + td);
List<TypedDependency> extraDeps = new ArrayList<>(); for (TypedDependency td : dependencies) { if (td.extra()) { extraDeps.add(td); } else { buf.append(td.toString(labelFormat)).append('\n'); buf.append("======\n"); for (TypedDependency td : extraDeps) { buf.append(td.toString(labelFormat)).append('\n'); buf.append(td.toString(labelFormat)).append('\n');
@Override public boolean test(TypedDependency d) { if (d == null) return false; IndexedWord l = d.dep(); if (l == null) return false; return npf.test(l.value()) && tf.test(l.tag()); }
@Override public boolean test(TypedDependency d) { return d != null && d.reln() != RELATIVE; }