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; }
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 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; }
private static GrammaticalStructure semanticGraphToGrammaticalStructure(SemanticGraph sg) { /* sg.typedDependency() generates an ArrayList */ List<TypedDependency> deps = (List<TypedDependency>) sg.typedDependencies(); IndexedWord root = deps.get(0).gov(); TreeGraphNode rootNode = new TreeGraphNode(root); GrammaticalStructure gs = new UniversalEnglishGrammaticalStructure(deps, rootNode); return gs; }
/** * 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; }
for (TypedDependency td : dependencies) { String reln = td.reln().toString(); String gov = td.gov().value(); String govTag = td.gov().tag(); int govIdx = td.gov().index(); String dep = td.dep().value(); String depTag = td.dep().tag(); int copyGov = td.gov().copyCount(); if (copyGov > 0) { govCopy = " copy=\"" + copyGov + '\"';
/** * 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() + ")"; }
token.get(CoreAnnotations.CoarseTagAnnotation.class) : pos; String lemma = token.lemma() != null ? token.lemma() : "_"; Integer gov = indexedDeps.containsKey(idx) ? indexedDeps.get(idx).gov().index() : 0; String reln = indexedDeps.containsKey(idx) ? indexedDeps.get(idx).reln().toString() : "erased"; String out = String.format("%d\t%s\t%s\t%s\t%s\t_\t%d\t%s\t_\t_\n", idx, word, lemma, cPos, pos, gov, reln);
/** * 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); }
IndexedWord head = rcmod.gov(); if (depNodes == null) { depNodes = Generics.newArrayList(); if (connected.gov().equals(rcmod.dep()) && (connected.reln() == XCLAUSAL_COMPLEMENT || connected.reln() == CONJUNCT)) { depNodes.add(connected.dep()); if (!prep.gov().equals(dep) || prep.reln() != PREPOSITIONAL_MODIFIER) { continue; if (other.gov().equals(prep.dep()) && (other.reln() == PREPOSITIONAL_COMPLEMENT || other.reln() == PREPOSITIONAL_OBJECT)) { found = true; break;
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); }