tree = gs.root(); // recover tree Collection<TypedDependency> bungRoots = null; System.out.println("------------- basic dependencies ---------------"); List<TypedDependency> gsb = gs.typedDependencies(GrammaticalStructure.Extras.NONE); System.out.println(StringUtils.join(gsb, "\n")); connected = GrammaticalStructure.isConnected(gsb); if ( ! connected && bungRoots == null) { bungRoots = GrammaticalStructure.getRoots(gsb); List<TypedDependency> gse = gs.typedDependencies(GrammaticalStructure.Extras.MAXIMAL); System.out.println(StringUtils.join(gse, "\n")); connected = GrammaticalStructure.isConnected(gse); if ( ! connected && bungRoots == null) { bungRoots = GrammaticalStructure.getRoots(gse); System.out.println(StringUtils.join(gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL), "\n")); System.out.println(StringUtils.join(gs.typedDependenciesCollapsedTree(), "\n")); List<TypedDependency> gscc = gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL); System.out.println(StringUtils.join(gscc, "\n")); connected = GrammaticalStructure.isConnected(gscc); if ( ! connected && bungRoots == null) { bungRoots = GrammaticalStructure.getRoots(gscc); printDependencies(gs, gs.typedDependencies(GrammaticalStructure.Extras.NONE), tree, conllx, false, opts.convertToUPOS); } else {
this.root = treeGraph; indexNodes(this.root); analyzeNode(root, root, relations, hf, puncFilter, tagFilter, basicGraph, completeGraph); attachStrandedNodes(root, root, false, puncFilter, tagFilter, basicGraph); typedDependencies = getDeps(puncTypedDepFilter, basicGraph); allTypedDependencies = Generics.newArrayList(typedDependencies); getExtraDeps(allTypedDependencies, puncTypedDepFilter, completeGraph);
/** * Get the typed dependencies after collapsing them. * The "collapsed" option corresponds to calling this method with argument * {@code true}. * * @param includeExtras If true, the list of typed dependencies * returned may include "extras", like controlling subjects * @return collapsed dependencies */ public List<TypedDependency> typedDependenciesCollapsed(Extras includeExtras) { List<TypedDependency> tdl = typedDependencies(includeExtras); collapseDependencies(tdl, false, includeExtras); return tdl; }
public List<TypedDependency> typedDependenciesEnhanced() { List<TypedDependency> tdl = typedDependencies(Extras.MAXIMAL); addEnhancements(tdl, UniversalEnglishGrammaticalStructure.ENHANCED_OPTIONS); return tdl; }
/** * Get the typed dependencies after mostly collapsing them, but keep a tree * structure. In order to do this, the code does: * <ol> * <li> no relative clause processing * <li> no xsubj relations * <li> no propagation of conjuncts * </ol> * This corresponds to the "tree" option. * * @return collapsed dependencies keeping a tree structure */ public Collection<TypedDependency> typedDependenciesCollapsedTree() { List<TypedDependency> tdl = typedDependencies(Extras.NONE); collapseDependenciesTree(tdl); return tdl; }
switch(mode) { case ENHANCED: deps = gs.typedDependenciesEnhanced(); break; case ENHANCED_PLUS_PLUS: deps = gs.typedDependenciesEnhancedPlusPlus(); break; case COLLAPSED_TREE: deps = gs.typedDependenciesCollapsedTree(); break; case COLLAPSED: deps = gs.typedDependenciesCollapsed(includeExtras); break; case CCPROCESSED: deps = gs.typedDependenciesCCprocessed(includeExtras); break; case BASIC: deps = gs.typedDependencies(includeExtras); break; default:
GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree); if (basicDependencies) { print(gs.typedDependencies(), "xml", includeTags, pw); print(gs.allTypedDependencies(), "xml", includeTags, pw); print(gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL), "xml", includeTags, pw); print(gs.typedDependenciesCCprocessed(), "xml", includeTags, pw); print(gs.typedDependenciesCollapsedTree(), "xml", includeTags, pw); print(gs.typedDependenciesCCprocessed(), "xml", includeTags, pw); GrammaticalStructure gs = gsf.newGrammaticalStructure(outputTree); if (basicDependencies) { print(gs.typedDependencies(), includeTags, pw); print(gs.allTypedDependencies(), includeTags, pw); print(gs.allTypedDependencies(), "separator", includeTags, pw); print(gs.typedDependenciesCollapsed(GrammaticalStructure.Extras.MAXIMAL), includeTags, pw); print(gs.typedDependenciesCCprocessed(), includeTags, pw); print(gs.typedDependenciesCollapsedTree(), includeTags, pw); print(gs.typedDependenciesCCprocessed(), includeTags, pw);
DependencyReader altDepReader = loadAlternateDependencyReader(altDepReaderName); try { gsBank = altDepReader.readDependencies(altDepReaderFilename); String parserOpts = props.getProperty("parserOpts"); boolean tokenized = props.getProperty("tokenized") != null; Function<List<? extends HasWord>, Tree> lp = loadParser(parserFile, parserOpts, makeCopulaHead); trees = new LazyLoadTreesByParsing(sentFileName, encoding, tokenized, lp); altDepPrinter = loadAlternateDependencyPrinter(altDepPrinterName); tree = gs.root(); // recover tree System.out.println(StringUtils.join(gs.typedDependencies(false), "\n")); System.out.println(StringUtils.join(gs.typedDependencies(true), "\n")); System.out.println(StringUtils.join(gs.typedDependenciesCollapsed(true), "\n")); System.out.println(StringUtils.join(gs.typedDependenciesCollapsedTree(), "\n")); System.out.println(StringUtils.join(gs.typedDependenciesCCprocessed(true), "\n")); boolean connected = GrammaticalStructure.isConnected(gs.typedDependenciesCollapsed(true)); System.out.println("collapsed dependencies form a connected graph: " + connected); if (!connected) { System.out.println("possible offending nodes: " + GrammaticalStructure.getRoots(gs.typedDependenciesCollapsed(true))); printDependencies(gs, gs.typedDependencies(false), tree, conllx, false);
/** * Get the typed dependencies after collapsing them. * The "collapsed" option corresponds to calling this method with argument * {@code true}. * * @param includeExtras If true, the list of typed dependencies * returned may include "extras", like controlling subjects * @return collapsed dependencies */ public List<TypedDependency> typedDependenciesCollapsed(boolean includeExtras) { List<TypedDependency> tdl = typedDependencies(false); // Adds stuff to the basic dependencies. // We don't want to simply call typedDependencies with // "includeExtras" because the collapseDependencies method may add // the extras in a way that makes more logical sense. For // example, the English dependencies, when CC processed, have more // nsubjs than they originally do. If we wait until that occurs // to add xsubj for xcomp dependencies, we get better coverage. // TODO: this might not be necessary any more if (includeExtras) { getExtras(tdl); getTreeDeps(root(), tdl, new NoPunctTypedDependencyFilter(puncFilter), extraTreeDepFilter()); } collapseDependencies(tdl, false, includeExtras); return tdl; }
/** * Get a list of the typed dependencies, including extras like control * dependencies, collapsing them and distributing relations across * coordination. This method is generally recommended for best * representing the semantic and syntactic relations of a sentence. In * general it returns a directed graph (i.e., the output may not be a tree * and it may contain (small) cycles). * The "CCPropagated" option corresponds to calling this method. * * @return collapsed dependencies with CC processed */ public List<TypedDependency> typedDependenciesCCprocessed() { return typedDependenciesCCprocessed(Extras.MAXIMAL); }
List<TypedDependency> basicDep = Generics.newArrayList(); for (Dependency<Label, Label, Object> d : dependencies()) { TreeGraphNode gov = (TreeGraphNode) d.governor(); TreeGraphNode dep = (TreeGraphNode) d.dependent(); GrammaticalRelation reln = getGrammaticalRelation(gov, dep); dependencyRoot.setIndex(0); TreeGraphNode rootDep = null; Collection<TypedDependency> roots = getRoots(basicDep); if (roots.size() == 0) { rootDep = root().headWordNode(); if (rootDep == null) { List<Tree> leaves = Trees.leaves(root()); if (leaves.size() > 0) { Tree leaf = leaves.get(0); postProcessDependencies(basicDep); getExtras(basicDep); getTreeDeps(root(), basicDep, puncTypedDepFilter, extraTreeDepFilter());
/** * Returns all the typed dependencies of this grammatical structure. * These are like the basic (uncollapsed) dependencies, but may include * extra arcs for control relationships, etc. This corresponds to the * "nonCollapsed" option. */ public Collection<TypedDependency> allTypedDependencies() { return typedDependencies(Extras.MAXIMAL); }
/** * Get the typed dependencies after collapsing them. * Collapsing dependencies refers to turning certain function words * such as prepositions and conjunctions into arcs, so they disappear from * the set of nodes. * There is no guarantee that the dependencies are a tree. While the * dependencies are normally tree-like, the collapsing may introduce * not only re-entrancies but even small cycles. * * @return A set of collapsed dependencies */ public Collection<TypedDependency> typedDependenciesCollapsed() { return typedDependenciesCollapsed(Extras.NONE); }
List<TypedDependency> basicDep = Generics.newArrayList(); for (Dependency<Label, Label, Object> d : dependencies()) { TreeGraphNode gov = (TreeGraphNode) d.governor(); TreeGraphNode dep = (TreeGraphNode) d.dependent(); GrammaticalRelation reln = getGrammaticalRelation(gov, dep); Collection<TypedDependency> roots = getRoots(basicDep); assert(roots.size() == 1); TreeGraphNode root = new TreeGraphNode(new Word("ROOT")); TreeGraphNode rootTree = root(); getDep(rootTree, basicDep, f); // adds stuff to basicDep
/** * Read in typed dependencies in CoNLLX format. * * @param filename * @throws IOException */ static protected List<Collection<TypedDependency>> readDepsCoNLLX(String filename) throws IOException { List<GrammaticalStructure> gss = GrammaticalStructure.readCoNLLXGrammaticalStructureCollection(filename, new fakeShortNameToGRel(), new GraphLessGrammaticalStructureFactory()); List<Collection<TypedDependency>> readDeps = new ArrayList<>(gss.size()); for (GrammaticalStructure gs : gss) { Collection<TypedDependency> deps = gs.typedDependencies(); readDeps.add(deps); } return readDeps; }
System.out.println("------------- basic dependencies ---------------"); printDependencies(gs, gs.typedDependencies(false), t, conllx, false); System.out.println("------------- noncollapsed dependencies ---------------"); printDependencies(gs, gs.typedDependencies(true), t, conllx, false); System.out.println("----------- collapsed dependencies -----------"); printDependencies(gs, gs.typedDependenciesCollapsed(true), t, conllx, false);
private static Graph getGraph(Tree tree) throws Exception { ArrayList<TaggedWord> words = tree.taggedYield(); GrammaticalStructure gs = gsf.newGrammaticalStructure(tree); Collection<TypedDependency> tdl = gs.typedDependencies(); Graph g = new Graph(words); for (TypedDependency td : tdl) { g.addEdge(td.gov().index() - 1, td.dep().index() - 1, td.reln().toString()); } try { g.setRoot(GrammaticalStructure.getRoots(tdl).iterator().next().gov().toString()); } catch (Exception ex) { //System.err.println("Cannot find dependency graph root. Setting root to first"); if (g.nodes.size() > 0) { g.setRoot(g.nodes.get(0).label); } } return g; }
/** * 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); }
/** * Checks if all the typeDependencies are connected * @param list a list of typedDependencies * @return true if the list represents a connected graph, false otherwise */ public static boolean isConnected(Collection<TypedDependency> list) { return getRoots(list).size() <= 1; // there should be no more than one root to have a connected graph // there might be no root in the way we look when you have a relative clause // ex.: Apple is a society that sells computers // (the root "society" will also be the nsubj of "sells") }
analyzeNode(root, root, relations); typedDependencies = getDeps(false, puncTypedDepFilter); allTypedDependencies = getDeps(true, puncTypedDepFilter);