/// <summary> /// Convert a dependency graph to a format expected as input to /// <see cref="IWriter.Set(string, object)"/> /// . /// </summary> private static object BuildDependencyTree(SemanticGraph graph) { // It's lying; we need the "redundant" casts (as of 2014-09-08) if (graph != null) { return(IStream.Concat(graph.GetRoots().Stream().Map(null), graph.EdgeListSorted().Stream().Map(null))); } else { // Roots // Regular edges return(null); } }
private static Element BuildDependencyTreeInfo(string dependencyType, SemanticGraph graph, IList <CoreLabel> tokens, string curNS) { if (graph != null) { Element depInfo = new Element("dependencies", curNS); depInfo.AddAttribute(new Attribute("type", dependencyType)); // The SemanticGraph doesn't explicitly encode the ROOT node, // so we print that out ourselves foreach (IndexedWord root in graph.GetRoots()) { string rel = GrammaticalRelation.Root.GetLongName(); rel = rel.ReplaceAll("\\s+", string.Empty); // future proofing int source = 0; int target = root.Index(); string sourceWord = "ROOT"; string targetWord = tokens[target - 1].Word(); bool isExtra = false; AddDependencyInfo(depInfo, rel, isExtra, source, sourceWord, null, target, targetWord, null, curNS); } foreach (SemanticGraphEdge edge in graph.EdgeListSorted()) { string rel = edge.GetRelation().ToString(); rel = rel.ReplaceAll("\\s+", string.Empty); int source = edge.GetSource().Index(); int target = edge.GetTarget().Index(); string sourceWord = tokens[source - 1].Word(); string targetWord = tokens[target - 1].Word(); int sourceCopy = edge.GetSource().CopyCount(); int targetCopy = edge.GetTarget().CopyCount(); bool isExtra = edge.IsExtra(); AddDependencyInfo(depInfo, rel, isExtra, source, sourceWord, sourceCopy, target, targetWord, targetCopy, curNS); } return(depInfo); } return(null); }