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(); }
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 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()); }
private static Set<List<TypedDependency>> getGovMaxChains(Map<IndexedWord,List<TypedDependency>> govToDepMap, IndexedWord gov, int depth) { Set<List<TypedDependency>> depLists = Generics.newHashSet(); List<TypedDependency> children = govToDepMap.get(gov); if (depth > 0 && children != null) { for (TypedDependency child : children) { IndexedWord childNode = child.dep(); if (childNode == null) continue; Set<List<TypedDependency>> childDepLists = getGovMaxChains(govToDepMap, childNode, depth-1); if (childDepLists.size() != 0) { for (List<TypedDependency> childDepList : childDepLists) { List<TypedDependency> depList = new ArrayList<>(childDepList.size() + 1); depList.add(child); depList.addAll(childDepList); depLists.add(depList); } } else { depLists.add(Arrays.asList(child)); } } } return depLists; }
private static List<Set<TypedDependency>> toSets(Collection<TypedDependency> depCollection) { Set<TypedDependency> depSet = Generics.newHashSet(); Set<TypedDependency> unlabeledDepSet = Generics.newHashSet(); for (TypedDependency dep : depCollection) { unlabeledDepSet.add(new TypedDependencyStringEquality(null, dep.gov(), dep.dep())); depSet.add(new TypedDependencyStringEquality(dep.reln(), dep.gov(), dep.dep())); } List<Set<TypedDependency>> l = new ArrayList<>(2); l.add(depSet); l.add(unlabeledDepSet); return l; }
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; }
/** * 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; }
/** * Given a list of typedDependencies, returns true if the node "node" is the * governor of a conj relation with a dependent which is not a preposition * * @param node * A node in this GrammaticalStructure * @param list * A list of typedDependencies * @return true If node is the governor of a conj relation in the list with * the dep not being a preposition */ private static boolean isConjWithNoPrep(IndexedWord node, Collection<TypedDependency> list) { for (TypedDependency td : list) { if (td.gov().equals(node) && td.reln() == CONJUNCT) { // we have a conjunct // check the POS of the dependent String tdDepPOS = td.dep().tag(); if (!(tdDepPOS.equals("IN") || tdDepPOS.equals("TO"))) { return true; } } } return false; }
private List<Dependency<Label, Label, Object>> getSortedDeps(Tree tree, Predicate<Dependency<Label, Label, Object>> filter) { if (gsf != null) { GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); Collection<TypedDependency> deps = gs.typedDependencies(GrammaticalStructure.Extras.NONE); List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<>(); for (TypedDependency dep : deps) { sortedDeps.add(new NamedDependency(dep.gov(), dep.dep(), dep.reln().toString())); } Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator()); return sortedDeps; } else { Set<Dependency<Label, Label, Object>> depsSet = tree.mapDependencies(filter, hf, "root"); List<Dependency<Label, Label, Object>> sortedDeps = new ArrayList<>(depsSet); Collections.sort(sortedDeps, Dependencies.dependencyIndexComparator()); return sortedDeps; } }
private static String toStringIndex(TypedDependency td, Map<Integer, Integer> indexToPos) { IndexedWord gov = td.gov(); IndexedWord dep = td.dep(); return td.reln() + "(" + gov.value() + "-" + indexToPos.get(gov.index()) + gov.toPrimes() + ", " + dep.value() + "-" + indexToPos.get(dep.index()) + dep.toPrimes() + ")"; }
indexedDeps.put(dep.dep().index(), dep);
String preposition = pc.dep().value().toLowerCase(); if (preposition.equals("by")) {
depNodes.clear(); depNodes.add(rcmod.dep()); for (TypedDependency connected : list) { if (connected.gov().equals(rcmod.dep()) && (connected.reln() == XCLAUSAL_COMPLEMENT || connected.reln() == CONJUNCT)) { depNodes.add(connected.dep()); if (other.gov().equals(prep.dep()) && (other.reln() == PREPOSITIONAL_COMPLEMENT || other.reln() == PREPOSITIONAL_OBJECT)) { found = true; break; newDeps = Generics.newArrayList(); TypedDependency newDep = new TypedDependency(PREPOSITIONAL_OBJECT, prep.dep(), head); newDeps.add(newDep);
/** * Get GrammaticalRelation between gov and dep, and null if gov is not the * governor of dep. */ public GrammaticalRelation getGrammaticalRelation(IndexedWord gov, IndexedWord dep) { List<GrammaticalRelation> labels = Generics.newArrayList(); for (TypedDependency dependency : typedDependencies(Extras.MAXIMAL)) { if (dependency.gov().equals(gov) && dependency.dep().equals(dep)) { labels.add(dependency.reln()); } } return getGrammaticalRelationCommonAncestor(gov, gov, dep, dep, labels); }
public int compareTo(TypedDependency tdArg) { IndexedWord depArg = tdArg.dep(); IndexedWord depThis = this.dep(); int indexArg = depArg.index(); int indexThis = depThis.index(); if (indexThis > indexArg) { return 1; } else if (indexThis < indexArg) { return -1; } // dependent indices are equal, check governor int govIndexArg = tdArg.gov().index(); int govIndexThis = this.gov().index(); if (govIndexThis > govIndexArg) { return 1; } else if (govIndexThis < govIndexArg) { return -1; } // dependent and governor indices equal, the relation decides return this.reln().compareTo(tdArg.reln()); }
/** * 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(). }
/** * 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); }