예제 #1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public static String[] toStringArray(org.maltparser.core.syntaxgraph.DependencyGraph graph, org.maltparser.core.io.dataformat.DataFormatInstance dataFormatInstance, org.maltparser.core.symbol.SymbolTableHandler symbolTables) throws org.maltparser.core.exception.MaltChainedException
        public static string[] toStringArray(DependencyGraph graph, DataFormatInstance dataFormatInstance, SymbolTableHandler symbolTables)
        {
            string[]      tokens = new string[graph.NTokenNode()];
            StringBuilder sb     = new StringBuilder();
            IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();

            foreach (int?index in graph.TokenIndices)
            {
                sb.Length = 0;
                if (index <= tokens.Length)
                {
                    ColumnDescription column = null;
                    TokenNode         node   = graph.GetTokenNode(index.Value);
                    while (columns.MoveNext())
                    {
                        column = columns.Current;
                        if (column.Category == ColumnDescription.INPUT)
                        {
                            if (!column.Name.Equals("ID"))
                            {
                                if (node.hasLabel(symbolTables.getSymbolTable(column.Name)) && node.getLabelSymbol(symbolTables.getSymbolTable(column.Name)).Length > 0)
                                {
                                    sb.Append(node.getLabelSymbol(symbolTables.getSymbolTable(column.Name)));
                                }
                                else
                                {
                                    sb.Append('_');
                                }
                            }
                            else
                            {
                                sb.Append(index.ToString());
                            }
                            sb.Append('\t');
                        }
                    }
                    sb.Length         = sb.Length - 1;
                    tokens[index - 1] = sb.ToString();
                    columns           = dataFormatInstance.GetEnumerator();
                }
            }
            return(tokens);
        }
예제 #2
0
        /// <summary>
        /// Parses an array of tokens and returns a dependency structure.
        ///
        /// Note: To call this method requires that a parser model has been initialized by using the initializeParserModel().
        /// </summary>
        /// <param name="tokens"> an array of tokens </param>
        /// <returns> a dependency structure </returns>
        /// <exception cref="MaltChainedException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public org.maltparser.core.syntaxgraph.DependencyStructure parse(String[] tokens) throws org.maltparser.core.exception.MaltChainedException
        public virtual IDependencyStructure parse(string[] tokens)
        {
            if (!initialized)
            {
                throw new MaltChainedException("No parser model has been initialized. Please use the method initializeParserModel() before invoking this method.");
            }
            if (tokens == null || tokens.Length == 0)
            {
                throw new MaltChainedException("Nothing to parse. ");
            }

            IDependencyStructure outputGraph = new DependencyGraph(singleMalt.SymbolTables);

            for (int i = 0; i < tokens.Length; i++)
            {
                IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
                DependencyNode node  = outputGraph.AddDependencyNode(i + 1);
                string[]       items = tokens[i].Split("\t", true);
                for (int j = 0; j < items.Length; j++)
                {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                    if (columns.hasNext())
                    {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                        ColumnDescription column = columns.next();
                        if (column.Category == ColumnDescription.INPUT && node != null)
                        {
                            outputGraph.AddLabel(node, column.Name, items[j]);
                        }
                    }
                }
            }
            outputGraph.SetDefaultRootEdgeLabel(outputGraph.SymbolTables.getSymbolTable("DEPREL"), "ROOT");
            // Invoke parse with the output graph
            singleMalt.Parse(outputGraph);
            return(outputGraph);
        }
예제 #3
0
        /// <summary>
        /// Converts an array of tokens to a dependency structure
        /// </summary>
        /// <param name="tokens"> tokens an array of tokens </param>
        /// <param name="dataFormatSpecification"> a data format specification </param>
        /// <returns> a dependency structure </returns>
        /// <exception cref="MaltChainedException"> </exception>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public org.maltparser.core.syntaxgraph.DependencyStructure toDependencyStructure(String[] tokens, org.maltparser.core.io.dataformat.DataFormatSpecification dataFormatSpecification) throws org.maltparser.core.exception.MaltChainedException
        public virtual IDependencyStructure toDependencyStructure(string[] tokens, DataFormatSpecification dataFormatSpecification)
        {
            // Creates a symbol table handler
            SymbolTableHandler symbolTables = new HashSymbolTableHandler();

            // Initialize data format instance
            DataFormatInstance dataFormatInstance = dataFormatSpecification.createDataFormatInstance(symbolTables, "none");

            // Creates a dependency graph
            if (tokens == null || tokens.Length == 0)
            {
                throw new MaltChainedException("Nothing to convert. ");
            }
            IDependencyStructure outputGraph = new DependencyGraph(symbolTables);

            for (int i = 0; i < tokens.Length; i++)
            {
                IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
                DependencyNode node  = outputGraph.AddDependencyNode(i + 1);
                string[]       items = tokens[i].Split("\t", true);
                Edge           edge  = null;
                for (int j = 0; j < items.Length; j++)
                {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                    if (columns.hasNext())
                    {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                        ColumnDescription column = columns.next();
                        if (column.Category == ColumnDescription.INPUT && node != null)
                        {
                            outputGraph.AddLabel(node, column.Name, items[j]);
                        }
                        else if (column.Category == ColumnDescription.HEAD)
                        {
                            if (column.Category != ColumnDescription.IGNORE && !items[j].Equals("_"))
                            {
                                edge = ((IDependencyStructure)outputGraph).AddDependencyEdge(int.Parse(items[j]), i + 1);
                            }
                        }
                        else if (column.Category == ColumnDescription.DEPENDENCY_EDGE_LABEL && edge != null)
                        {
                            outputGraph.AddLabel(edge, column.Name, items[j]);
                        }
                    }
                }
            }
            outputGraph.SetDefaultRootEdgeLabel(outputGraph.SymbolTables.getSymbolTable("DEPREL"), "ROOT");
            return(outputGraph);
        }
예제 #4
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public boolean readSentence(org.maltparser.core.syntaxgraph.TokenStructure syntaxGraph) throws org.maltparser.core.exception.MaltChainedException
        public virtual bool readSentence(ITokenStructure syntaxGraph)
        {
            if (syntaxGraph == null || dataFormatInstance == null)
            {
                return(false);
            }
            syntaxGraph.Clear();
            syntaxGraph.SymbolTables.cleanUp();
            Element node = null;

            Edge.Edge edge = null;


            List <string> tokens = new List <string>();

            try
            {
                string line;
                while (!ReferenceEquals((line = reader.ReadLine()), null))
                {
                    if (line.Trim().Length == 0)
                    {
                        break;
                    }
                    else
                    {
                        tokens.Add(line.Trim());
                    }
                }
            }
            catch (IOException e)
            {
                close();
                throw new DataFormatException("Error when reading from the input file. ", e);
            }

            int terminalCounter = 0;

            for (int i = 0; i < tokens.Count; i++)
            {
                string token = tokens[i];

                if (token[0] == '#')
                {
                    syntaxGraph.AddComment(token, terminalCounter + 1);
                    continue;
                }
                string[] columns = token.Split("\t", true);
                if (columns[0].Contains("-") || columns[0].Contains("."))
                {
                    syntaxGraph.AddComment(token, terminalCounter + 1);
                    continue;
                }
                terminalCounter++;
                node = syntaxGraph.AddTokenNode(terminalCounter);

                IEnumerator <ColumnDescription> columnDescriptions = dataFormatInstance.GetEnumerator();
                for (int j = 0; j < columns.Length; j++)
                {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                    ColumnDescription columnDescription = columnDescriptions.next();

                    if (columnDescription.Category == ColumnDescription.INPUT && node != null)
                    {
                        syntaxGraph.AddLabel(node, columnDescription.Name, columns[j]);
                    }
                    else if (columnDescription.Category == ColumnDescription.HEAD)
                    {
                        if (syntaxGraph is IDependencyStructure)
                        {
                            if (columnDescription.Category != ColumnDescription.IGNORE && !columns[j].Equals(IGNORE_COLUMN_SIGN))
                            {
                                edge = ((IDependencyStructure)syntaxGraph).AddDependencyEdge(int.Parse(columns[j]), terminalCounter);
                            }
                        }
                        else
                        {
                            close();
                            throw new DataFormatException("The input graph is not a dependency graph and therefore it is not possible to add dependncy edges. ");
                        }
                    }
                    else if (columnDescription.Category == ColumnDescription.DEPENDENCY_EDGE_LABEL && edge != null)
                    {
                        syntaxGraph.AddLabel(edge, columnDescription.Name, columns[j]);
                    }
                }
            }

            if (!syntaxGraph.HasTokens())
            {
                return(false);
            }
            sentenceCount++;
            return(true);
        }
예제 #5
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public boolean readSentence(org.maltparser.core.syntaxgraph.TokenStructure syntaxGraph) throws org.maltparser.core.exception.MaltChainedException
        public virtual bool readSentence(ITokenStructure syntaxGraph)
        {
            if (syntaxGraph == null || !(syntaxGraph is PhraseStructure))
            {
                return(false);
            }
            syntaxGraph.Clear();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.syntaxgraph.PhraseStructure phraseStructure = (org.maltparser.core.syntaxgraph.PhraseStructure)syntaxGraph;
            PhraseStructure phraseStructure = (PhraseStructure)syntaxGraph;
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.symbol.SymbolTableHandler symbolTables = phraseStructure.getSymbolTables();
            SymbolTableHandler  symbolTables = phraseStructure.SymbolTables;
            PhraseStructureNode parent       = null;
            PhraseStructureNode child        = null;

            currentHeaderTable = NegraTables.UNDEF;
            string line = null;

            syntaxGraph.Clear();
            nonterminals.Clear();
            try
            {
                while (true)
                {
                    line = reader.ReadLine();
                    if (ReferenceEquals(line, null))
                    {
                        if (syntaxGraph.HasTokens())
                        {
                            sentenceCount++;
                            if (syntaxGraph is MappablePhraseStructureGraph)
                            {
                                ((MappablePhraseStructureGraph)syntaxGraph).Mapping.updateDependenyGraph(((MappablePhraseStructureGraph)syntaxGraph), ((PhraseStructure)syntaxGraph).PhraseStructureRoot);
                            }
                        }
                        if (cIterations < nIterations)
                        {
                            cIterations++;
                            reopen();
                            return(true);
                        }
                        return(false);
                    }
                    else if (line.StartsWith("#EOS", StringComparison.Ordinal))
                    {
                        currentTerminalSize    = 0;
                        currentNonTerminalSize = 0;
                        currentHeaderTable     = NegraTables.UNDEF;
                        if (syntaxGraph is MappablePhraseStructureGraph)
                        {
                            ((MappablePhraseStructureGraph)syntaxGraph).Mapping.updateDependenyGraph(((MappablePhraseStructureGraph)syntaxGraph), ((PhraseStructure)syntaxGraph).PhraseStructureRoot);
                        }
                        return(true);
                    }
                    else if (line.StartsWith("#BOS", StringComparison.Ordinal))
                    {
                        currentHeaderTable = NegraTables.SENTENCE;
                        int s = -1, e = -1;
                        for (int i = 5, n = line.Length; i < n; i++)
                        {
                            if (char.IsDigit(line[i]) && s == -1)
                            {
                                s = i;
                            }
                            if (line[i] == ' ')
                            {
                                e = i;
                                break;
                            }
                        }
                        if (s != e && s != -1 && e != -1)
                        {
                            phraseStructure.SentenceID = int.Parse(line.Substring(s, e - s));
                        }
                        sentenceCount++;
                    }
                    else if (currentHeaderTable == NegraTables.SENTENCE)
                    {
                        if (line.Length >= 2 && line[0] == '#' && char.IsDigit(line[1]))
                        {                         // Non-terminal
                            IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
                            ColumnDescription column = null;
                            currentNonTerminalSize++;
                            char[] lineChars      = line.ToCharArray();
                            int    start          = 0;
                            int    secedgecounter = 0;
                            for (int i = 0, n = lineChars.Length; i < n; i++)
                            {
                                if (lineChars[i] == '\t' && start == i)
                                {
                                    start++;
                                }
                                else if (lineChars[i] == '\t' || i == n - 1)
                                {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                                    if (columns.hasNext())
                                    {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                                        column = columns.next();
                                    }
                                    if (column.Position == 0)
                                    {
                                        int index = int.Parse((i == n - 1)?line.Substring(start + 1):line.Substring(start + 1, i - (start + 1)));
                                        child = nonterminals[index];
                                        if (child == null)
                                        {
                                            if (index != 0)
                                            {
                                                child = ((PhraseStructure)syntaxGraph).addNonTerminalNode(index - START_ID_OF_NONTERMINALS + 1);
                                            }
                                            nonterminals[index] = child;
                                        }
                                    }
                                    else if (column.Position == 2 && child != null)
                                    {
                                        syntaxGraph.AddLabel(child, "CAT", (i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                    }
                                    else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL)
                                    {
                                        edgelabelSymbol.Length = 0;
                                        edgelabelSymbol.Append((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                        edgelabelTableName.Length = 0;
                                        edgelabelTableName.Append(column.Name);
                                    }
                                    else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL && child != null)
                                    {
                                        int index = int.Parse((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                        parent = nonterminals[index];
                                        if (parent == null)
                                        {
                                            if (index == 0)
                                            {
                                                parent = phraseStructure.PhraseStructureRoot;
                                            }
                                            else
                                            {
                                                parent = phraseStructure.addNonTerminalNode(index - START_ID_OF_NONTERMINALS + 1);
                                            }
                                            nonterminals[index] = parent;
                                        }
                                        Edge.Edge e = phraseStructure.addPhraseStructureEdge(parent, child);
                                        syntaxGraph.AddLabel(e, edgelabelTableName.ToString(), edgelabelSymbol.ToString());
                                    }
                                    else if (column.Category == ColumnDescription.SECONDARY_EDGE_LABEL && child != null)
                                    {
                                        if (secedgecounter % 2 == 0)
                                        {
                                            edgelabelSymbol.Length = 0;
                                            edgelabelSymbol.Append((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                            secedgecounter++;
                                        }
                                        else
                                        {
                                            int index = int.Parse((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                            if (index == 0)
                                            {
                                                parent = phraseStructure.PhraseStructureRoot;
                                            }
                                            else if (index < START_ID_OF_NONTERMINALS)
                                            {
                                                parent = phraseStructure.GetTokenNode(index);
                                            }
                                            else
                                            {
                                                parent = nonterminals[index];
                                                if (parent == null)
                                                {
                                                    parent = phraseStructure.addNonTerminalNode(index - START_ID_OF_NONTERMINALS + 1);
                                                    nonterminals[index] = parent;
                                                }
                                            }
                                            Edge.Edge e = phraseStructure.addSecondaryEdge(parent, child);
                                            e.addLabel(symbolTables.getSymbolTable(column.Name), edgelabelSymbol.ToString());
                                            secedgecounter++;
                                        }
                                    }
                                    start = i + 1;
                                }
                            }
                        }
                        else
                        {                         // Terminal
                            IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
                            ColumnDescription column = null;

                            currentTerminalSize++;
                            child = syntaxGraph.AddTokenNode(currentTerminalSize);
                            char[] lineChars      = line.ToCharArray();
                            int    start          = 0;
                            int    secedgecounter = 0;
                            for (int i = 0, n = lineChars.Length; i < n; i++)
                            {
                                if (lineChars[i] == '\t' && start == i)
                                {
                                    start++;
                                }
                                else if (lineChars[i] == '\t' || i == n - 1)
                                {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                                    if (columns.hasNext())
                                    {
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                                        column = columns.next();
                                    }
                                    if (column.Category == ColumnDescription.INPUT && child != null)
                                    {
                                        syntaxGraph.AddLabel(child, column.Name, (i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                    }
                                    else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL && child != null)
                                    {                                     // && column.getName().equals("EDGELABEL")) {
                                        edgelabelSymbol.Length = 0;
                                        edgelabelSymbol.Append((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                        edgelabelTableName.Length = 0;
                                        edgelabelTableName.Append(column.Name);
                                    }
                                    else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL && child != null)
                                    {
                                        int index = int.Parse((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                        parent = nonterminals[index];
                                        if (parent == null)
                                        {
                                            if (index == 0)
                                            {
                                                parent = phraseStructure.PhraseStructureRoot;
                                            }
                                            else
                                            {
                                                parent = phraseStructure.addNonTerminalNode(index - START_ID_OF_NONTERMINALS + 1);
                                            }
                                            nonterminals[index] = parent;
                                        }

                                        Edge.Edge e = phraseStructure.addPhraseStructureEdge(parent, child);
                                        syntaxGraph.AddLabel(e, edgelabelTableName.ToString(), edgelabelSymbol.ToString());
                                    }
                                    else if (column.Category == ColumnDescription.SECONDARY_EDGE_LABEL && child != null)
                                    {
                                        if (secedgecounter % 2 == 0)
                                        {
                                            edgelabelSymbol.Length = 0;
                                            edgelabelSymbol.Append((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                            secedgecounter++;
                                        }
                                        else
                                        {
                                            int index = int.Parse((i == n - 1)?line.Substring(start):line.Substring(start, i - start));
                                            if (index == 0)
                                            {
                                                parent = phraseStructure.PhraseStructureRoot;
                                            }
                                            else if (index < START_ID_OF_NONTERMINALS)
                                            {
                                                parent = phraseStructure.GetTokenNode(index);
                                            }
                                            else
                                            {
                                                parent = nonterminals[index];
                                                if (parent == null)
                                                {
                                                    parent = phraseStructure.addNonTerminalNode(index - START_ID_OF_NONTERMINALS + 1);
                                                    nonterminals[index] = parent;
                                                }
                                            }
                                            Edge.Edge e = phraseStructure.addSecondaryEdge(parent, child);
                                            e.addLabel(symbolTables.getSymbolTable(column.Name), edgelabelSymbol.ToString());
                                            secedgecounter++;
                                        }
                                    }
                                    start = i + 1;
                                }
                            }
                        }
                    }
                    else if (line.StartsWith("%%", StringComparison.Ordinal))
                    {                     // comment skip
                    }
                    else if (line.StartsWith("#FORMAT", StringComparison.Ordinal))
                    {
                        //				int index = line.indexOf(' ');
                        //				if (index > -1) {
                        //					try {
                        //						formatVersion = Integer.parseInt(line.substring(index+1));
                        //					} catch (NumberFormatException e) {
                        //
                        //					}
                        //				}
                    }
                    else if (line.StartsWith("#BOT", StringComparison.Ordinal))
                    {
                        //				int index = line.indexOf(' ');
                        //				if (index > -1) {
                        //					if (line.substring(index+1).equals("ORIGIN")) {
                        //						currentHeaderTable = NegraTables.ORIGIN;
                        //					} else if (line.substring(index+1).equals("EDITOR")) {
                        //						currentHeaderTable = NegraTables.EDITOR;
                        //					} else if (line.substring(index+1).equals("WORDTAG")) {
                        //						currentHeaderTable = NegraTables.WORDTAG;
                        //					} else if (line.substring(index+1).equals("MORPHTAG")) {
                        //						currentHeaderTable = NegraTables.MORPHTAG;
                        //					} else if (line.substring(index+1).equals("NODETAG")) {
                        //						currentHeaderTable = NegraTables.NODETAG;
                        //					} else if (line.substring(index+1).equals("EDGETAG")) {
                        //						currentHeaderTable = NegraTables.EDGETAG;
                        //					} else if (line.substring(index+1).equals("SECEDGETAG")) {
                        //						currentHeaderTable = NegraTables.SECEDGETAG;
                        //					} else {
                        //						currentHeaderTable = NegraTables.UNDEF;
                        //					}
                        //				}
                    }
                    else if (line.StartsWith("#EOT", StringComparison.Ordinal))
                    {
                        currentHeaderTable = NegraTables.UNDEF;
                    }
                }
            }
            catch (IOException e)
            {
                throw new DataFormatException("Error when reading from the input file. ", e);
            }
        }
예제 #6
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: public void writeSentence(org.maltparser.core.syntaxgraph.TokenStructure syntaxGraph) throws org.maltparser.core.exception.MaltChainedException
        public virtual void writeSentence(ITokenStructure syntaxGraph)
        {
            if (syntaxGraph == null || dataFormatInstance == null || !syntaxGraph.HasTokens())
            {
                return;
            }
            IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.symbol.SymbolTableHandler symbolTables = syntaxGraph.getSymbolTables();
            SymbolTableHandler symbolTables = syntaxGraph.SymbolTables;

            foreach (int i in syntaxGraph.TokenIndices)
            {
                writeComments(syntaxGraph, i);
                try
                {
                    ColumnDescription column = null;
                    while (columns.MoveNext())
                    {
                        column = columns.Current;

                        if (column.Category == ColumnDescription.INPUT)
                        {                         // && column.getType() != ColumnDescription.IGNORE) {
                            TokenNode node = syntaxGraph.GetTokenNode(i);
                            if (!column.Name.Equals("ID"))
                            {
                                if (node.hasLabel(symbolTables.getSymbolTable(column.Name)))
                                {
                                    output.Append(node.getLabelSymbol(symbolTables.getSymbolTable(column.Name)));
                                    if (output.Length != 0)
                                    {
                                        writer.Write(output.ToString());
                                    }
                                    else
                                    {
                                        writer.BaseStream.WriteByte('_');
                                    }
                                }
                                else
                                {
                                    writer.BaseStream.WriteByte('_');
                                }
                            }
                            else
                            {
                                writer.Write(Convert.ToString(i));
                            }
                        }
                        else if (column.Category == ColumnDescription.HEAD && syntaxGraph is IDependencyStructure)
                        {
                            if (((IDependencyStructure)syntaxGraph).GetDependencyNode(i).hasHead())
                            {
                                writer.Write(Convert.ToString(((IDependencyStructure)syntaxGraph).GetDependencyNode(i).Head.Index));
                            }
                            else
                            {
                                writer.Write(Convert.ToString(0));
                            }
                        }
                        else if (column.Category == ColumnDescription.DEPENDENCY_EDGE_LABEL && syntaxGraph is IDependencyStructure)
                        {
                            if (((IDependencyStructure)syntaxGraph).GetDependencyNode(i).hasHead() && ((IDependencyStructure)syntaxGraph).GetDependencyNode(i).hasHeadEdgeLabel(symbolTables.getSymbolTable(column.Name)))
                            {
                                output.Append(((IDependencyStructure)syntaxGraph).GetDependencyNode(i).getHeadEdgeLabelSymbol(symbolTables.getSymbolTable(column.Name)));
                            }
                            else
                            {
                                output.Append(((IDependencyStructure)syntaxGraph).GetDefaultRootEdgeLabelSymbol(symbolTables.getSymbolTable(column.Name)));
                            }

                            if (output.Length != 0)
                            {
                                writer.Write(output.ToString());
                            }
                        }
                        else
                        {
                            writer.Write(column.DefaultOutput);
                        }
//JAVA TO C# CONVERTER TODO TASK: Java iterators are only converted within the context of 'while' and 'for' loops:
                        if (columns.hasNext())
                        {
                            writer.BaseStream.WriteByte(TAB);
                        }
                        output.Length = 0;
                    }
                    writer.BaseStream.WriteByte(NEWLINE);
                    columns = dataFormatInstance.GetEnumerator();
                }
                catch (IOException e)
                {
                    close();
                    throw new DataFormatException("Could not write to the output file. ", e);
                }
            }
            writeComments(syntaxGraph, syntaxGraph.NTokenNode() + 1);
            try
            {
                writer.BaseStream.WriteByte('\n');
                writer.Flush();
            }
            catch (IOException e)
            {
                close();
                throw new DataFormatException("Could not write to the output file. ", e);
            }
        }
예제 #7
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void writeTerminals(org.maltparser.core.syntaxgraph.PhraseStructure phraseStructure) throws org.maltparser.core.exception.MaltChainedException
        private void writeTerminals(PhraseStructure phraseStructure)
        {
            try
            {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.symbol.SymbolTableHandler symbolTables = phraseStructure.getSymbolTables();
                SymbolTableHandler symbolTables = phraseStructure.SymbolTables;
                foreach (int index in phraseStructure.TokenIndices)
                {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.maltparser.core.syntaxgraph.node.PhraseStructureNode terminal = phraseStructure.getTokenNode(index);
                    PhraseStructureNode terminal = phraseStructure.GetTokenNode(index);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final java.util.Iterator<org.maltparser.core.io.dataformat.ColumnDescription> columns = dataFormatInstance.iterator();
                    IEnumerator <ColumnDescription> columns = dataFormatInstance.GetEnumerator();
                    ColumnDescription column = null;
                    int ti = 1;
                    while (columns.MoveNext())
                    {
                        column = columns.Current;
                        if (column.Category == ColumnDescription.INPUT)
                        {
                            SymbolTable table = symbolTables.getSymbolTable(column.Name);
                            writer.Write(terminal.getLabelSymbol(table));
                            int nTabs = 1;
                            if (ti == 1 || ti == 2)
                            {
                                nTabs = 3 - (terminal.getLabelSymbol(table).Length / 8);
                            }
                            else if (ti == 3)
                            {
                                nTabs = 1;
                            }
                            else if (ti == 4)
                            {
                                nTabs = 2 - (terminal.getLabelSymbol(table).Length / 8);
                            }
                            if (nTabs < 1)
                            {
                                nTabs = 1;
                            }
                            for (int j = 0; j < nTabs; j++)
                            {
                                writer.BaseStream.WriteByte('\t');
                            }
                            ti++;
                        }
                        else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_EDGE_LABEL)
                        {
                            SymbolTable table = symbolTables.getSymbolTable(column.Name);
                            if (terminal.Parent != null && terminal.hasParentEdgeLabel(table))
                            {
                                writer.Write(terminal.getParentEdgeLabelSymbol(table));
                                writer.BaseStream.WriteByte('\t');
                            }
                            else
                            {
                                writer.Write("--\t");
                            }
                        }
                        else if (column.Category == ColumnDescription.PHRASE_STRUCTURE_NODE_LABEL)
                        {
                            if (terminal.Parent == null || terminal.Parent == phraseStructure.PhraseStructureRoot)
                            {
                                writer.BaseStream.WriteByte('0');
                            }
                            else
                            {
                                writer.Write(Convert.ToString(nonTerminalIndexMap.get(terminal.Parent.Index)));
                                //							writer.write(Integer.toString(terminal.getParent().getIndex()+START_ID_OF_NONTERMINALS-1));
                            }
                        }
                    }
                    SymbolTable table = symbolTables.getSymbolTable(column.Name);
                    foreach (Edge.Edge e in terminal.IncomingSecondaryEdges)
                    {
                        if (e.hasLabel(table))
                        {
                            writer.BaseStream.WriteByte('\t');
                            writer.Write(e.getLabelSymbol(table));
                            writer.BaseStream.WriteByte('\t');
                            if (e.Source is NonTerminalNode)
                            {
                                writer.Write(Convert.ToString(nonTerminalIndexMap.get(e.Source.Index)));
                                //							writer.write(Integer.toString(e.getSource().getIndex()+START_ID_OF_NONTERMINALS-1));
                            }
                            else
                            {
                                writer.Write(Convert.ToString(e.Source.Index));
                            }
                        }
                    }
                    writer.Write("\n");
                }
            }
            catch (IOException e)
            {
                throw new DataFormatException("The Negra writer is not able to write. ", e);
            }
        }