public virtual string toStringNonTerminalNode(NonTerminalNode node) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(); sb.Append(node.ToString().Trim()); sb.Append('\n'); IEnumerator <Edge.Edge> ie = ((Node.Node)node).OutgoingEdgeIterator; while (ie.MoveNext()) { Edge.Edge e = ie.Current; if (e.Target is TokenNode) { sb.Append(" T"); sb.Append(e.Target.Index); } if (e.Target is NonTerminalNode) { sb.Append(" N"); sb.Append(e.Target.Index); } sb.Append('\t'); sb.Append(e.ToString()); sb.Append('\n'); } return(sb.ToString()); }
public static NonTerminalNode CreateNode(Rule rule) { Debug.Assert(rule != null); NonTerminalNode node = _implTypes.ContainsKey(rule.Index) ? _implTypes.CreateInstance(rule.Index) : new NonTerminalNode(); node.Rule = rule; return(node); }
private Node ApplyDistributiveProperty(Node current) { if ((current == null) || (current is TerminalNode)) { return(current); } else if (!(current is And)) { NonTerminalNode nt = (NonTerminalNode)current; nt.left = ApplyDistributiveProperty(nt.left); nt.right = ApplyDistributiveProperty(nt.right); return(nt); } else { And and = (And)current; // Check for an OR child Or childOr = (and.left as Or); Node otherChild = and.right; if (childOr == null) { childOr = (and.right as Or); otherChild = and.left; } // If neither child is an OR simply recurse if (childOr == null) { and.left = ApplyDistributiveProperty(and.left); and.right = ApplyDistributiveProperty(and.right); return(and); } // If either child is an OR // convert (child.left OR child.right) AND otherChild into // (child.left AND otherChild) OR (child.right AND otherChild) else { // Create new operator nodes Or or = new Or(new OrOperator()); And left = new And(new AndOperator()); And right = new And(new AndOperator()); // Populate new terms left.left = childOr.left; left.right = otherChild; right.left = childOr.right; right.right = otherChild; // Recursively apply distributive rule to new terms or.left = ApplyDistributiveProperty(left); or.right = ApplyDistributiveProperty(right); return(or); } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public org.maltparser.core.syntaxgraph.node.PhraseStructureNode getHeadChild(org.maltparser.core.syntaxgraph.node.NonTerminalNode nt) throws org.maltparser.core.exception.MaltChainedException public virtual PhraseStructureNode getHeadChild(NonTerminalNode nt) { PhraseStructureNode headChild = null; for (int i = 0, n = Count; i < n; i++) { headChild = this[i].getHeadChild(nt, direction); if (headChild != null) { break; } } return(headChild); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public org.maltparser.core.syntaxgraph.node.PhraseStructureNode getHeadChild(org.maltparser.core.syntaxgraph.node.NonTerminalNode nt, Direction direction) throws org.maltparser.core.exception.MaltChainedException public virtual PhraseStructureNode getHeadChild(NonTerminalNode nt, Direction direction) { bool match = false; if (direction == Direction.LEFT) { foreach (PhraseStructureNode child in nt.Children) { for (int j = 0; j < Count; j++) { match = matchHeadChild(child, this[j]); if (match == true) { if (j + 1 >= Count) { return(child); } else if (this[j].RelationToPrevMember != RelationToPrevMember.CONJUNCTION) { return(child); } } } } } else if (direction == Direction.RIGHT) { for (int i = nt.nChildren() - 1; i >= 0; i--) { PhraseStructureNode child = nt.getChild(i); for (int j = 0; j < Count; j++) { match = matchHeadChild(child, this[j]); if (match == true) { if (j + 1 >= Count) { return(child); } else if (this[j].RelationToPrevMember != RelationToPrevMember.CONJUNCTION) { return(child); } } } } } return(null); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void calculateIndices(org.maltparser.core.syntaxgraph.PhraseStructure phraseStructure) throws org.maltparser.core.exception.MaltChainedException private void calculateIndices(PhraseStructure phraseStructure) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final java.util.SortedMap<int,int> heights = new java.util.TreeMap<int,int>(); SortedDictionary <int, int> heights = new SortedDictionary <int, int>(); foreach (int index in phraseStructure.NonTerminalIndices) { heights[index] = ((NonTerminalNode)phraseStructure.getNonTerminalNode(index)).Height; } bool done = false; int h = 1; int ntid = START_ID_OF_NONTERMINALS; nonTerminalIndexMap.clear(); while (!done) { done = true; foreach (int index in phraseStructure.NonTerminalIndices) { if (heights[index] == h) { NonTerminalNode nt = (NonTerminalNode)phraseStructure.getNonTerminalNode(index); nonTerminalIndexMap.put(nt.Index, ntid++); // nonTerminalIndexMap.put(nt.getIndex(), nt.getIndex()+START_ID_OF_NONTERMINALS-1); done = false; } } h++; } // boolean done = false; // int h = 1; //// int ntid = START_ID_OF_NONTERMINALS; //// nonTerminalIndexMap.clear(); // while (!done) { // done = true; // for (int index : phraseStructure.getNonTerminalIndices()) { // if (heights.get(index) == h) { // NonTerminalNode nt = (NonTerminalNode)phraseStructure.getNonTerminalNode(index); //// nonTerminalIndexMap.put(nt.getIndex(), ntid++); // nonTerminalIndexMap.put(nt.getIndex(), nt.getIndex()+START_ID_OF_NONTERMINALS-1); // done = false; // } // } // h++; // } }
public void ShoulEnumerateTerminals() { NonTerminalNode <char> parent, child; child = new NonTerminalNode <char>(); child.Nodes.Add(new TerminalNode <char>(new MockedTerminalInput('b'))); child.Nodes.Add(new TerminalNode <char>(new MockedTerminalInput('c'))); child.Nodes.Add(new TerminalNode <char>(new MockedTerminalInput('d'))); parent = new NonTerminalNode <char>(); parent.Nodes.Add(new TerminalNode <char>(new MockedTerminalInput('a'))); parent.Nodes.Add(child); parent.Nodes.Add(new TerminalNode <char>(new MockedTerminalInput('e'))); Assert.AreEqual("abcde", new string( parent.EnumerateInputs().OfType <MockedTerminalInput>().Select(item => item.Value).ToArray())); }
public static NonTerminalNode CreateNode(Rule rule) { Debug.Assert(rule != null); NonTerminalNode node = null; if (_nodeImplTypeMap.ContainsKey(rule.Index)) { node = _nodeImplTypeMap.CreateInstance(rule.Index); } else { node = new NonTerminalNode();// if no type is bound to the rule then just create a base non-terminal node } node.Rule = rule; return(node); }
private Node SearchForNot(Node current) { if ((current == null) || (current is TerminalNode)) { return(current); } else if (!(current is Not)) { NonTerminalNode nt = (NonTerminalNode)current; nt.left = SearchForNot(nt.left); nt.right = SearchForNot(nt.right); return(nt); } else { return(ApplyDeMorgan((current as Not).left)); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public Direction getDefaultDirection(org.maltparser.core.syntaxgraph.node.NonTerminalNode nt) throws org.maltparser.core.exception.MaltChainedException public virtual Direction getDefaultDirection(NonTerminalNode nt) { HeadRule rule = null; if (nt.hasLabel(nonTerminalSymbolTable)) { rule = this[nonTerminalSymbolTable.Name + ":" + nt.getLabelSymbol(nonTerminalSymbolTable)]; } if (rule == null && nt.hasParentEdgeLabel(edgelabelSymbolTable)) { rule = this[edgelabelSymbolTable.Name + ":" + nt.getParentEdgeLabelSymbol(edgelabelSymbolTable)]; } if (rule != null) { return(rule.DefaultDirection); } return(Direction.LEFT); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public org.maltparser.core.syntaxgraph.node.PhraseStructureNode getHeadChild(org.maltparser.core.syntaxgraph.node.NonTerminalNode nt) throws org.maltparser.core.exception.MaltChainedException public virtual PhraseStructureNode getHeadChild(NonTerminalNode nt) { HeadRule rule = null; if (nt.hasLabel(nonTerminalSymbolTable)) { rule = this[nonTerminalSymbolTable.Name + ":" + nt.getLabelSymbol(nonTerminalSymbolTable)]; } if (rule == null && nt.hasParentEdgeLabel(edgelabelSymbolTable)) { rule = this[edgelabelSymbolTable.Name + ":" + nt.getParentEdgeLabelSymbol(edgelabelSymbolTable)]; } if (rule != null) { return(rule.getHeadChild(nt)); } return(null); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void writeNonTerminals(org.maltparser.core.syntaxgraph.PhraseStructure phraseStructure) throws org.maltparser.core.exception.MaltChainedException public virtual void writeNonTerminals(PhraseStructure phraseStructure) { try { SortedDictionary <int, int> heights = new SortedDictionary <int, int>(); foreach (int index in phraseStructure.NonTerminalIndices) { heights[index] = ((NonTerminalNode)phraseStructure.getNonTerminalNode(index)).Height; } writer.Write(" <nonterminals>\n"); bool done = false; int h = 1; while (!done) { done = true; foreach (int index in phraseStructure.NonTerminalIndices) { if (heights[index] == h) { NonTerminalNode nt = (NonTerminalNode)phraseStructure.getNonTerminalNode(index); tmpID.Length = 0; tmpID.Append(sentenceID); tmpID.Append('_'); tmpID.Append(Convert.ToString(nt.Index + START_ID_OF_NONTERMINALS - 1)); writeNonTerminal(phraseStructure.SymbolTables, nt, tmpID.ToString()); done = false; } } h++; } writeNonTerminal(phraseStructure.SymbolTables, (NonTerminalNode)phraseStructure.PhraseStructureRoot, rootID.ToString()); writer.Write(" </nonterminals>\n"); } catch (IOException e) { throw new DataFormatException("The TigerXML writer is not able to write. ", e); } }
public bool Parse(TextReader sourceReader) { Debug.Assert(sourceReader != null); m_parser = ParserFactory.CreateParser(sourceReader); m_parser.TrimReductions = true; while (true) { switch (m_parser.Parse()) { case ParseMessage.LexicalError: ErrorString = string.Format("Lexical Error. Line {0}. Token {1} was not expected.", m_parser.LineNumber, m_parser.TokenText); return(false); case ParseMessage.SyntaxError: StringBuilder text = new StringBuilder(); foreach (Symbol tokenSymbol in m_parser.GetExpectedTokens()) { text.AppendFormat(" {0}", tokenSymbol); } ErrorString = string.Format("Syntax Error. Line {0}. Expecting: {1}.", m_parser.LineNumber, text); return(false); case ParseMessage.Reduction: NonTerminalNode nonTerminal = SyntaxRuleFactory.CreateNode(m_parser.ReductionRule); m_parser.TokenSyntaxNode = nonTerminal; for (int i = 0; i < m_parser.ReductionCount; i++) { nonTerminal.AppendChildNode(m_parser.GetReductionSyntaxNode(i) as SyntaxNode); } // post parsing syntax check (used to segregate the difference between HAVING and WHERE // expressions in terms of the validtity of aggregate expressions) nonTerminal.CheckSyntax(); break; case ParseMessage.TokenRead: m_parser.TokenSyntaxNode = new TerminalNode(m_parser); break; case ParseMessage.Accept: SyntaxTree = m_parser.TokenSyntaxNode as NonTerminalNode; ErrorString = null; return(true); case ParseMessage.InternalError: ErrorString = "Internal Error. Something is horribly wrong."; return(false); case ParseMessage.NotLoadedError: ErrorString = "Grammar Table is not loaded."; return(false); case ParseMessage.CommentError: ErrorString = "Comment Error. Unexpected end of input."; return(false); case ParseMessage.CommentBlockRead: case ParseMessage.CommentLineRead: // don't do anything break; } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void writeNonTerminal(org.maltparser.core.symbol.SymbolTableHandler symbolTables, org.maltparser.core.syntaxgraph.node.NonTerminalNode nt, String id) throws org.maltparser.core.exception.MaltChainedException public virtual void writeNonTerminal(SymbolTableHandler symbolTables, NonTerminalNode nt, string id) { try { writer.Write(" <nt"); writer.Write(" id=\""); writer.Write(id); writer.Write("\" "); foreach (ColumnDescription column in dataFormatInstance.PhraseStructureNodeLabelColumnDescriptionSet) { if (nt.hasLabel(symbolTables.getSymbolTable(column.Name))) { writer.Write(column.Name.ToLower()); writer.Write("="); writer.Write("\""); writer.Write(Util.xmlEscape(nt.getLabelSymbol(symbolTables.getSymbolTable(column.Name)))); writer.Write("\" "); } } writer.Write(">\n"); for (int i = 0, n = nt.nChildren(); i < n; i++) { PhraseStructureNode child = nt.getChild(i); writer.Write(" <edge "); foreach (ColumnDescription column in dataFormatInstance.PhraseStructureEdgeLabelColumnDescriptionSet) { if (child.hasParentEdgeLabel(symbolTables.getSymbolTable(column.Name))) { writer.Write(column.Name.ToLower()); writer.Write("=\""); writer.Write(Util.xmlEscape(child.getParentEdgeLabelSymbol(symbolTables.getSymbolTable(column.Name)))); writer.Write("\" "); } } if (child is TokenNode) { if (!labeledTerminalID) { tmpID.Length = 0; tmpID.Append(sentenceID); tmpID.Append('_'); tmpID.Append(Convert.ToString(child.Index)); writer.Write(" idref=\""); writer.Write(tmpID.ToString()); writer.Write("\""); } else { writer.Write(" idref=\""); writer.Write(child.getLabelSymbol(symbolTables.getSymbolTable("ID"))); writer.Write("\""); } } else { tmpID.Length = 0; tmpID.Append(sentenceID); tmpID.Append('_'); tmpID.Append(Convert.ToString(child.Index + START_ID_OF_NONTERMINALS - 1)); writer.Write(" idref=\""); writer.Write(tmpID.ToString()); writer.Write("\""); } writer.Write(" />\n"); } writer.Write(" </nt>\n"); } catch (IOException e) { throw new DataFormatException("The TigerXML writer is not able to write. ", e); } }
public bool Parse(TextReader sourceReader) { Debug.Assert(sourceReader != null); m_parser = ParserFactory.CreateParser(sourceReader); m_parser.TrimReductions = true; while (true) { switch (m_parser.Parse()) { case ParseMessage.LexicalError: ErrorString = string.Format("Lexical Error. Line {0}. Token {1} was not expected.", m_parser.LineNumber, m_parser.TokenText); return false; case ParseMessage.SyntaxError: StringBuilder text = new StringBuilder(); foreach (Symbol tokenSymbol in m_parser.GetExpectedTokens()) text.AppendFormat(" {0}", tokenSymbol); ErrorString = string.Format("Syntax Error. Line {0}. Expecting: {1}.", m_parser.LineNumber, text); return false; case ParseMessage.Reduction: NonTerminalNode nonTerminal = SyntaxRuleFactory.CreateNode(m_parser.ReductionRule); m_parser.TokenSyntaxNode = nonTerminal; for (int i = 0; i < m_parser.ReductionCount; i++) nonTerminal.AppendChildNode(m_parser.GetReductionSyntaxNode(i) as SyntaxNode); // post parsing syntax check (used to do things like segregate the difference between HAVING and WHERE // expressions in terms of the validtity of aggregate expressions) nonTerminal.CheckSyntax(); break; case ParseMessage.TokenRead: m_parser.TokenSyntaxNode = new TerminalNode(m_parser); break; case ParseMessage.Accept: SyntaxTree = m_parser.TokenSyntaxNode as NonTerminalNode; ErrorString = null; return true; case ParseMessage.InternalError: ErrorString = "Internal Error. Something is horribly wrong."; return false; case ParseMessage.NotLoadedError: ErrorString = "Grammar Table is not loaded."; return false; case ParseMessage.CommentError: ErrorString = "Comment Error. Unexpected end of input."; return false; case ParseMessage.CommentBlockRead: case ParseMessage.CommentLineRead: // don't do anything break; } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void writeElement(org.maltparser.core.symbol.SymbolTableHandler symbolTables, org.maltparser.core.syntaxgraph.node.PhraseStructureNode element, int depth) throws org.maltparser.core.exception.MaltChainedException private void writeElement(SymbolTableHandler symbolTables, PhraseStructureNode element, int depth) { try { if (element is TokenNode) { PhraseStructureNode t = (PhraseStructureNode)element; SymbolTable table = null; writer.Write("\n" + getIndentation(depth) + STARTING_BRACKET); int i = 0; foreach (string inputColumn in inputColumns.Keys) { if (i != 0) { writer.BaseStream.WriteByte(INPUT_SEPARATOR); } table = symbolTables.getSymbolTable(inputColumns[inputColumn].Name); if (t.hasLabel(table)) { writer.Write(encodeString(t.getLabelSymbol(table))); } if (i == 0) { foreach (string edgeLabelColumn in edgeLabelColumns.Keys) { table = symbolTables.getSymbolTable(edgeLabelColumns[edgeLabelColumn].Name); if (t.hasParentEdgeLabel(table) && !t.Parent.Root && !t.getParentEdgeLabelSymbol(table).Equals(EMPTY_EDGELABEL)) { writer.BaseStream.WriteByte(EDGELABEL_SEPARATOR); writer.Write(t.getParentEdgeLabelSymbol(table)); } } } i++; } writer.BaseStream.WriteByte(CLOSING_BRACKET); } else { NonTerminalNode nt = (NonTerminalNode)element; writer.Write("\n" + getIndentation(depth) + STARTING_BRACKET); SymbolTable table = null; int i = 0; foreach (string phraseLabelColumn in phraseLabelColumns.Keys) { if (i != 0) { writer.BaseStream.WriteByte(INPUT_SEPARATOR); } table = symbolTables.getSymbolTable(phraseLabelColumns[phraseLabelColumn].Name); if (nt.hasLabel(table)) { writer.BaseStream.WriteByte(nt.getLabelSymbol(table)); } if (i == 0) { foreach (string edgeLabelColumn in edgeLabelColumns.Keys) { table = symbolTables.getSymbolTable(edgeLabelColumns[edgeLabelColumn].Name); if (nt.hasParentEdgeLabel(table) && !nt.Parent.Root && !nt.getParentEdgeLabelSymbol(table).Equals(EMPTY_EDGELABEL)) { writer.BaseStream.WriteByte(EDGELABEL_SEPARATOR); writer.Write(nt.getParentEdgeLabelSymbol(table)); } } } i++; } foreach (PhraseStructureNode node in ((NonTerminalNode)element).Children) { writeElement(symbolTables, node, depth + 1); } writer.Write("\n" + getIndentation(depth) + CLOSING_BRACKET); } } catch (IOException e) { throw new DataFormatException("Could not write to the output file. ", e); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void writeNonTerminals(org.maltparser.core.syntaxgraph.PhraseStructure phraseStructure) throws org.maltparser.core.exception.MaltChainedException private void writeNonTerminals(PhraseStructure phraseStructure) { //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 nonTerminalIndexMap.Keys) { // for (int index : phraseStructure.getNonTerminalIndices()) { NonTerminalNode nonTerminal = (NonTerminalNode)phraseStructure.getNonTerminalNode(index); if (nonTerminal == null || nonTerminal.Root) { return; } try { writer.BaseStream.WriteByte('#'); // writer.write(Integer.toString(index+START_ID_OF_NONTERMINALS-1)); writer.Write(Convert.ToString(nonTerminalIndexMap.get(index))); writer.Write("\t\t\t--\t\t\t"); if (nonTerminal.hasLabel(symbolTables.getSymbolTable("CAT"))) { writer.BaseStream.WriteByte(nonTerminal.getLabelSymbol(symbolTables.getSymbolTable("CAT"))); } else { writer.Write("--"); } writer.Write("\t--\t\t"); if (nonTerminal.hasParentEdgeLabel(symbolTables.getSymbolTable("LABEL"))) { writer.Write(nonTerminal.getParentEdgeLabelSymbol(symbolTables.getSymbolTable("LABEL"))); } else { writer.Write("--"); } writer.BaseStream.WriteByte('\t'); if (nonTerminal.Parent == null || nonTerminal.Parent.Root) { writer.BaseStream.WriteByte('0'); } else { // writer.write(Integer.toString(nonTerminal.getParent().getIndex()+START_ID_OF_NONTERMINALS-1)); writer.Write(Convert.ToString(nonTerminalIndexMap.get(nonTerminal.Parent.Index))); } foreach (Edge.Edge e in nonTerminal.IncomingSecondaryEdges) { if (e.hasLabel(symbolTables.getSymbolTable("SECEDGELABEL"))) { writer.BaseStream.WriteByte('\t'); writer.Write(e.getLabelSymbol(symbolTables.getSymbolTable("SECEDGELABEL"))); writer.BaseStream.WriteByte('\t'); if (e.Source is NonTerminalNode) { // writer.write(Integer.toString(e.getSource().getIndex()+START_ID_OF_NONTERMINALS-1)); writer.Write(Convert.ToString(nonTerminalIndexMap.get(e.Source.Index))); } 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 the non-terminals. ", e); } } }