/// <summary> /// Creates a immutable dependency graph /// </summary> /// <param name="dataFormat"> a data format that describes the label types (or the columns in the input and output) </param> /// <param name="sourceGraph"> a dependency graph that implements the interface org.maltparser.core.syntaxgraph.DependencyStructure </param> /// <param name="defaultRootLabel"> the default root label </param> /// <exception cref="MaltChainedException"> </exception> public ConcurrentDependencyGraph(DataFormat.DataFormat dataFormat, IDependencyStructure sourceGraph, string defaultRootLabel) { DataFormat = dataFormat; nodes = new ConcurrentDependencyNode[sourceGraph.NDependencyNode()]; // Add nodes nodes[0] = new ConcurrentDependencyNode(this, 0, null); // ROOT foreach (int index in sourceGraph.TokenIndices) { DependencyNode gNode = sourceGraph.GetDependencyNode(index); string[] columns = new string[dataFormat.NumberOfColumns()]; for (int i = 0; i < dataFormat.NumberOfColumns(); i++) { ColumnDescription column = dataFormat.GetColumnDescription(i); if (!column.Internal) { if (column.Category == ColumnDescription.Input) { columns[i] = gNode.getLabelSymbol(sourceGraph.SymbolTables.getSymbolTable(column.Name)); } else if (column.Category == ColumnDescription.Head) { if (gNode.hasHead()) { columns[i] = Convert.ToString(gNode.HeadEdge.Source.Index); } else { columns[i] = Convert.ToString(-1); } } else if (column.Category == ColumnDescription.DependencyEdgeLabel) { SymbolTable sourceTable = sourceGraph.SymbolTables.getSymbolTable(column.Name); if (gNode.HeadEdge.hasLabel(sourceTable)) { columns[i] = gNode.HeadEdge.getLabelSymbol(sourceTable); } else { columns[i] = defaultRootLabel; } } else { columns[i] = "_"; } } } nodes[index] = new ConcurrentDependencyNode(this, index, columns); } }
internal ConcurrentDependencyGraph(DataFormat.DataFormat dataFormat, IReadOnlyList <ConcurrentDependencyNode> inputNodes) { DataFormat = dataFormat; nodes = new ConcurrentDependencyNode[inputNodes.Count]; // Add nodes for (int i = 0; i < inputNodes.Count; i++) { nodes[i] = inputNodes[i]; } // Check graph if (nodes.Any(t => t.HeadIndex >= nodes.Length)) { throw new ConcurrentGraphException("Not allowed to add a head node that doesn't exists"); } }
/// <summary> /// Creates a immutable dependency graph /// </summary> /// <param name="dataFormat"> a data format that describes the label types (or the columns in the input and output) </param> /// <param name="inputTokens"> a string array of tokens. Each label is separated by a tab-character and must follow the order in the data format </param> /// <exception cref="ConcurrentGraphException"> </exception> public ConcurrentDependencyGraph(DataFormat.DataFormat dataFormat, IReadOnlyList <string> inputTokens) { DataFormat = dataFormat; nodes = new ConcurrentDependencyNode[inputTokens.Count + 1]; // Add nodes nodes[0] = new ConcurrentDependencyNode(this, 0, null); // ROOT for (int i = 0; i < inputTokens.Count; i++) { string[] columns = inputTokens[i].Split(tabSign, true); nodes[i + 1] = new ConcurrentDependencyNode(this, i + 1, columns); } // Check graph if (nodes.Any(t => t.HeadIndex >= nodes.Length)) { throw new ConcurrentGraphException("Not allowed to add a head node that doesn't exists"); } }
internal ConcurrentDependencyEdge(DataFormat.DataFormat dataFormat, ConcurrentDependencyNode source, ConcurrentDependencyNode target, SortedDictionary <int, string> labels) { Source = source ?? throw new ConcurrentGraphException("Not allowed to have an edge without a source node"); Target = target ?? throw new ConcurrentGraphException("Not allowed to have an edge without a target node"); if (Target.Index == 0) { throw new ConcurrentGraphException("Not allowed to have an edge target as root node"); } this.labels = new SortedDictionary <int, string>(); if (labels != null) { foreach (int i in labels.Keys) { if (dataFormat.GetColumnDescription(i).Category == ColumnDescription.DependencyEdgeLabel) { this.labels[i] = labels[i]; } } } }