Example #1
0
 public NegraReader()
 {
     currentHeaderTable = NegraTables.UNDEF;
     edgelabelSymbol    = new StringBuilder();
     edgelabelTableName = new StringBuilder();
     nonterminals       = new SortedDictionary <int, PhraseStructureNode>();
     nIterations        = 1;
     cIterations        = 1;
 }
Example #2
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);
            }
        }