Exemplo n.º 1
0
        //	private void updateDependenyLabels(MappablePhraseStructureGraph graph, PhraseStructureNode top) throws MaltChainedException {
        //		if (top == null) {
        //			return;
        //		}
        //		DependencyNode head = null;
        //		DependencyNode dependent = null;
        //		if (top instanceof NonTerminalNode) {
        //			for (PhraseStructureNode node : ((NonTerminalNode)top).getChildren()) {
        //				if (node instanceof NonTerminalNode) {
        //					updateDependenyLabels(graph, node);
        //				} else {
        //					head = ((NonTerminalNode)top).getLexicalHead(headRules);
        //					dependent = (DependencyNode)node;
        //					if (head != null && dependent != null && head != dependent) {
        //						lockUpdate = true;
        //						if (dependent.hasHead()) {
        //							Edge e = dependent.getHeadEdge();
        //							labelDependencyEdge(graph, e, node);
        //						}
        //						lockUpdate = false;
        //					}
        //				}
        //			}
        //		}
        //
        //		dependent = null;
        //		if (top instanceof NonTerminalNode) {
        //			dependent = ((NonTerminalNode)top).getLexicalHead(headRules);
        //		}
        //
        //		if (dependent != null) {
        //			lockUpdate = true;
        //			if (dependent.hasHead()) {
        //				Edge e = dependent.getHeadEdge();
        //				labelDependencyEdge(graph, e, top);
        //			}
        //			lockUpdate = false;
        //		}
        //	}

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: private void labelDependencyEdge(org.maltparser.core.syntaxgraph.MappablePhraseStructureGraph graph, org.maltparser.core.syntaxgraph.edge.Edge e, org.maltparser.core.syntaxgraph.node.PhraseStructureNode top) throws org.maltparser.core.exception.MaltChainedException
        private void labelDependencyEdge(MappablePhraseStructureGraph graph, Edge.Edge e, PhraseStructureNode top)
        {
            if (e == null)
            {
                return;
            }
            SymbolTableHandler symbolTables = graph.SymbolTables;

            deprel.Length  = 0;
            phrase.Length  = 0;
            headrel.Length = 0;

            e.removeLabel(symbolTables.getSymbolTable(DEPREL));
            e.removeLabel(symbolTables.getSymbolTable(HEADREL));
            e.removeLabel(symbolTables.getSymbolTable(PHRASE));
            e.removeLabel(symbolTables.getSymbolTable(ATTACH));

            int i = 0;
            SortedDictionary <string, SymbolTable> edgeLabelSymbolTables = phraseStructuretDataFormatInstance.getPhraseStructureEdgeLabelSymbolTables(symbolTableHandler);
            SortedDictionary <string, SymbolTable> nodeLabelSymbolTables = phraseStructuretDataFormatInstance.getPhraseStructureNodeLabelSymbolTables(symbolTableHandler);

            if (!top.Root)
            {
                foreach (string name in edgeLabelSymbolTables.Keys)
                {
                    if (top.hasParentEdgeLabel(symbolTables.getSymbolTable(name)))
                    {
                        deprel.Append(top.getParentEdgeLabelSymbol(symbolTables.getSymbolTable(name)));
                    }
                    else
                    {
                        deprel.Append(EMPTY_LABEL);
                    }
                    i++;
                    if (i < edgeLabelSymbolTables.Count)
                    {
                        deprel.Append(LABEL_ELEMENT_SEPARATOR);
                    }
                }
                if (deprel.Length != 0)
                {
                    e.addLabel(symbolTables.getSymbolTable(DEPREL), deprel.ToString());
                }
            }
            else
            {
                string deprelDefaultRootLabel = graph.GetDefaultRootEdgeLabelSymbol(symbolTables.getSymbolTable(DEPREL));
                if (!ReferenceEquals(deprelDefaultRootLabel, null))
                {
                    e.addLabel(symbolTables.getSymbolTable(DEPREL), deprelDefaultRootLabel);
                }
                else
                {
                    e.addLabel(symbolTables.getSymbolTable(DEPREL), EMPTY_LABEL);
                }
            }
            PhraseStructureNode tmp = (PhraseStructureNode)e.Target;

            while (tmp != top && tmp.Parent != null)
            {             // && !tmp.getParent().isRoot()) {
                i = 0;
                foreach (string name in edgeLabelSymbolTables.Keys)
                {
                    if (tmp.hasParentEdgeLabel(symbolTables.getSymbolTable(name)))
                    {
                        headrel.Append(tmp.getParentEdgeLabelSymbol(symbolTables.getSymbolTable(name)));
                    }
                    else
                    {
                        headrel.Append(EMPTY_LABEL);
                    }
                    i++;
                    if (i < edgeLabelSymbolTables.Count)
                    {
                        headrel.Append(LABEL_ELEMENT_SEPARATOR);
                    }
                }
                i = 0;
                headrel.Append(SPINE_ELEMENT_SEPARATOR);
                foreach (string name in nodeLabelSymbolTables.Keys)
                {
                    if (tmp.Parent.hasLabel(symbolTables.getSymbolTable(name)))
                    {
                        phrase.Append(tmp.Parent.getLabelSymbol(symbolTables.getSymbolTable(name)));
                    }
                    else
                    {
                        if (tmp.Parent.Root)
                        {
                            string deprelDefaultRootLabel = graph.GetDefaultRootEdgeLabelSymbol(symbolTables.getSymbolTable(PHRASE));
                            if (!ReferenceEquals(deprelDefaultRootLabel, null))
                            {
                                phrase.Append(deprelDefaultRootLabel);
                            }
                            else
                            {
                                phrase.Append(EMPTY_LABEL);
                            }
                        }
                        else
                        {
                            phrase.Append(EMPTY_LABEL);
                        }
                    }
                    i++;
                    if (i < nodeLabelSymbolTables.Count)
                    {
                        phrase.Append(LABEL_ELEMENT_SEPARATOR);
                    }
                }
                phrase.Append(SPINE_ELEMENT_SEPARATOR);
                tmp = tmp.Parent;
            }
            if (phrase.Length == 0)
            {
                headrel.Append(EMPTY_SPINE);
                phrase.Append(EMPTY_SPINE);
            }
            else
            {
                headrel.Length = headrel.Length - 1;
                phrase.Length  = phrase.Length - 1;
            }
            e.addLabel(symbolTables.getSymbolTable(HEADREL), headrel.ToString());
            e.addLabel(symbolTables.getSymbolTable(PHRASE), phrase.ToString());
            int a = 0;

            tmp = (PhraseStructureNode)e.Source;
            while (top.Parent != null && tmp.Parent != null && tmp.Parent != top.Parent)
            {
                a++;
                tmp = tmp.Parent;
            }
            e.addLabel(symbolTables.getSymbolTable(ATTACH), Convert.ToString(a));
        }
Exemplo n.º 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;
            PhraseStructureNode parent          = null;
            PhraseStructureNode child           = null;

            //		if (header == null) {
            //			header = new TigerXMLHeader(syntaxGraph.getSymbolTables());
            //		}

            try
            {
                while (true)
                {
                    int @event = reader.next();
                    if (@event == XMLStreamConstants.START_ELEMENT)
                    {
                        if (reader.LocalName.length() == 0)
                        {
                            continue;
                        }
                        if (reader.LocalName.charAt(0) == 'e')
                        {
                            // e -> edge, edgelabel
                            if (reader.LocalName.length() == 4)
                            {                             //edge
                                int childid  = -1;
                                int indexSep = reader.getAttributeValue(null, "idref").IndexOf('_');

                                try
                                {
                                    if (indexSep != -1)
                                    {
                                        childid = int.Parse(reader.getAttributeValue(null, "idref").substring(indexSep + 1));
                                    }
                                    else
                                    {
                                        childid = int.Parse(reader.getAttributeValue(null, "idref"));
                                    }
                                    if (childid == -1)
                                    {
                                        throw new SyntaxGraphException("The tiger reader couldn't recognize the idref attribute '" + reader.getAttributeValue(null, "idref") + "' of the edge element. ");
                                    }
                                }
                                catch (System.FormatException)
                                {
                                    throw new SyntaxGraphException("The tiger reader couldn't recognize the idref attribute '" + reader.getAttributeValue(null, "idref") + "' of the edge element. ");
                                }

                                if (childid < START_ID_OF_NONTERMINALS)
                                {
                                    child = phraseStructure.GetTokenNode(childid);
                                }
                                else
                                {
                                    child = phraseStructure.getNonTerminalNode(childid - START_ID_OF_NONTERMINALS + 1);
                                }

                                Edge.Edge e = phraseStructure.addPhraseStructureEdge(parent, child);
                                SortedDictionary <string, SymbolTable> inputTables = dataFormatInstance.getPhraseStructureEdgeLabelSymbolTables(phraseStructure.SymbolTables);
                                foreach (string name in inputTables.Keys)
                                {
                                    e.addLabel(inputTables[name], reader.getAttributeValue(null, name.ToLower()));
                                }
                            }
                            else if (reader.LocalName.Equals("edgelabel"))
                            {                             // edgelabel
                                //							domain = Domain.EL;
                            }
                        }
                        else if (reader.LocalName.charAt(0) == 'n')
                        {
                            // n -> nt, nonterminals, name
                            if (reader.LocalName.length() == 2)
                            {                             // nt
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String id = reader.getAttributeValue(null, "id");
                                string id = reader.getAttributeValue(null, "id");
                                if (graphRootID.Length == id.Length && graphRootID.ToString().Equals(id))
                                {
                                    parent = phraseStructure.PhraseStructureRoot;
                                }
                                else
                                {
                                    int index = id.IndexOf('_');
                                    if (index != -1)
                                    {
                                        parent = phraseStructure.addNonTerminalNode(int.Parse(id.Substring(index + 1)) - START_ID_OF_NONTERMINALS + 1);
                                    }
                                }
                                SortedDictionary <string, SymbolTable> inputTables = dataFormatInstance.getPhraseStructureNodeLabelSymbolTables(phraseStructure.SymbolTables);
                                foreach (string name in inputTables.Keys)
                                {
                                    parent.addLabel(inputTables[name], reader.getAttributeValue(null, name.ToLower()));
                                }
                            }
                            else if (reader.LocalName.Equals("name"))
                            {                             // name
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                        }
                        else if (reader.LocalName.charAt(0) == 't')
                        {
                            // t -> t, terminals
                            if (reader.LocalName.length() == 1)
                            {                             // t
                                SortedDictionary <string, SymbolTable> inputTables = dataFormatInstance.getInputSymbolTables(phraseStructure.SymbolTables);
                                child = syntaxGraph.AddTokenNode();
                                foreach (string name in inputTables.Keys)
                                {
                                    child.addLabel(inputTables[name], reader.getAttributeValue(null, name.ToLower()));
                                }
                            }
                        }
                        else if (reader.LocalName.charAt(0) == 's')
                        {
                            // s -> subcorpus, secedge, s, secedgelabel
                            if (reader.LocalName.length() == 1)
                            {                             // s
                                string id        = reader.getAttributeValue(null, "id");
                                bool   indexable = false;
                                int    index     = -1;
                                if (!ReferenceEquals(id, null) && id.Length > 0)
                                {
                                    for (int i = 0, n = id.Length; i < n; i++)
                                    {
                                        if (char.IsDigit(id[i]))
                                        {
                                            if (index == -1)
                                            {
                                                index = i;
                                            }
                                            indexable = true;
                                        }
                                    }
                                }
                                if (indexable)
                                {
                                    phraseStructure.SentenceID = int.Parse(id.Substring(index));
                                }
                                else
                                {
                                    phraseStructure.SentenceID = sentenceCount + 1;
                                }
                            }
                        }
                        else if (reader.LocalName.charAt(0) == 'v')
                        {
                            // v -> variable, value
                            //						if (reader.getLocalName().equals("value")) {
                            //							valueName.setLength(0);
                            //							valueName.append(reader.getAttributeValue(null, "name"));
                            //							elementContent.setLength(0);
                            //							collectChar = true;
                            //						}
                        }
                        else
                        {
                            //						 a -> annotation, author
                            //						 b -> body
                            //						 c -> corpus
                            //						 d -> date, description,
                            //						 f -> feature, format
                            //						 g -> graph
                            //						 h -> head, history
                            //						 m -> matches, match
                            if (reader.LocalName.Equals("graph"))
                            {
                                graphRootID.Length = 0;
                                graphRootID.Append(reader.getAttributeValue(null, "root"));
                            }
                            else if (reader.LocalName.Equals("corpus"))
                            {
                                //							header.setCorpusID(reader.getAttributeValue(null, "id"));
                                //							header.setCorpusID(reader.getAttributeValue(null, "version"));
                            }
                            else if (reader.LocalName.Equals("feature"))
                            {
                                //							if (header != null) {
                                //								currentFeatureName.setLength(0);
                                //								currentFeatureName.append(reader.getAttributeValue(null, "name"));
                                //								header.addFeature(reader.getAttributeValue(null, "name"), reader.getAttributeValue(null, "domain"));
                                //							}
                                //							domain = Domain.valueOf(reader.getAttributeValue(null, "domain"));
                            }
                            else if (reader.LocalName.Equals("secedgelabel"))
                            {
                                //							domain = Domain.SEL;
                            }
                            else if (reader.LocalName.Equals("author"))
                            {
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                            else if (reader.LocalName.Equals("date"))
                            {
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                            else if (reader.LocalName.Equals("description"))
                            {
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                            else if (reader.LocalName.Equals("format"))
                            {
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                            else if (reader.LocalName.Equals("history"))
                            {
                                //							elementContent.setLength(0);
                                //							collectChar = true;
                            }
                        }
                    }
                    else if (@event == XMLStreamConstants.END_ELEMENT)
                    {
                        if (reader.LocalName.length() == 0)
                        {
                            continue;
                        }
                        if (reader.LocalName.charAt(0) == 'e')
                        {
                            // e -> edge, edgelabel
                        }
                        else if (reader.LocalName.charAt(0) == 'n')
                        {
                            // n -> nt, nonterminals, name
                            if (reader.LocalName.Equals("nt"))
                            {
                                ntid.Length = 0;
                            }
                            else if (reader.LocalName.Equals("nonterminals"))
                            {
                                if (phraseStructure.NTokenNode() == 1 && phraseStructure.nNonTerminals() == 0 && ((NonTerminalNode)phraseStructure.PhraseStructureRoot).nChildren() == 0)
                                {
                                    Edge.Edge e = phraseStructure.addPhraseStructureEdge(phraseStructure.PhraseStructureRoot, phraseStructure.GetTokenNode(1));
                                    SortedDictionary <string, SymbolTable> inputTables = dataFormatInstance.getPhraseStructureEdgeLabelSymbolTables(phraseStructure.SymbolTables);
                                    foreach (string name in inputTables.Keys)
                                    {
                                        e.addLabel(inputTables[name], "--");
                                    }
                                }
                            }
                            //						else if (reader.getLocalName().equals("name")) {
                            //							if (header != null) {
                            //								header.setMetaName(elementContent.toString());
                            //							}
                            //							collectChar = false;
                            //						}
                        }
                        else if (reader.LocalName.charAt(0) == 't')
                        {
                            // t -> t, terminals
                        }
                        else if (reader.LocalName.charAt(0) == 's')
                        {
                            // s -> subcorpus, secedge, s, secedgelabel
                            if (reader.LocalName.Equals("s"))
                            {
                                if (syntaxGraph.HasTokens())
                                {
                                    sentenceCount++;
                                }
                                if (syntaxGraph is MappablePhraseStructureGraph)
                                {
                                    ((MappablePhraseStructureGraph)syntaxGraph).Mapping.updateDependenyGraph(((MappablePhraseStructureGraph)syntaxGraph), ((PhraseStructure)syntaxGraph).PhraseStructureRoot);
                                }
                                return(true);
                            }
                        }
                        else if (reader.LocalName.charAt(0) == 'v')
                        {
                            // v -> variable, value
                            //						if (reader.getLocalName().equals("value")) {
                            //							if (header != null) {
                            //								if (domain == Domain.T || domain == Domain.NT || domain == Domain.FREC) {
                            //									header.addFeatureValue(currentFeatureName.toString(), valueName.toString(), elementContent.toString());
                            //								} else if (domain == Domain.EL) {
                            //									header.addEdgeLabelValue(valueName.toString(), elementContent.toString());
                            //								} else if (domain == Domain.SEL) {
                            //									header.addSecEdgeLabelValue(valueName.toString(), elementContent.toString());
                            //								}
                            //							}
                            //							collectChar = false;
                            //						}
                        }
                        else
                        {
                            //						 a -> annotation, author
                            //						 b -> body
                            //						 c -> corpus
                            //						 d -> date, description,
                            //						 f -> feature, format
                            //						 g -> graph
                            //						 h -> head, history
                            //						 m -> matches, match
                            if (reader.LocalName.Equals("body"))
                            {
                                //sentence = dataStructures.getSentence();
                                //phraseTree = dataStructures.getInPhraseTree();
                                //sentence.clear();
                                //phraseTree.clear();
                                //dataStructures.setLastProcessObject(true);
                            }
                            else if (reader.LocalName.Equals("author"))
                            {
                                //							if (header != null) {
                                //								header.setMetaAuthor(elementContent.toString());
                                //							}
                                //							collectChar = false;
                            }
                            else if (reader.LocalName.Equals("date"))
                            {
                                //							if (header != null) {
                                //								header.setMetaInDate(elementContent.toString());
                                //							}
                                //							collectChar = false;
                            }
                            else if (reader.LocalName.Equals("description"))
                            {
                                //							if (header != null) {
                                //								header.setMetaDescription(elementContent.toString());
                                //							}
                                //							collectChar = false;
                            }
                            else if (reader.LocalName.Equals("format"))
                            {
                                //							if (header != null) {
                                //								header.setMetaFormat(elementContent.toString());
                                //							}
                                //							collectChar = false;
                            }
                            else if (reader.LocalName.Equals("history"))
                            {
                                //							if (header != null) {
                                //								header.setMetaHistory(elementContent.toString());
                                //							}
                                //							collectChar = false;
                            }                             /* else if (reader.getLocalName().equals("annotation")) {
                                                           *    if (header != null) {
                                                           *            System.out.println(header.toTigerXML());
                                                           *    }
                                                           *    collectChar = false;
                                                           * } */
                        }
                    }
                    else if (@event == XMLStreamConstants.END_DOCUMENT)
                    {
                        if (syntaxGraph.HasTokens())
                        {
                            sentenceCount++;
                        }
                        if (cIterations < nIterations)
                        {
                            cIterations++;
                            reopen();
                            return(true);
                        }
                        return(false);
                    }
                    else if (@event == XMLStreamConstants.CHARACTERS)
                    {
                        //					if (collectChar) {
                        //						char[] ch = reader.getTextCharacters();
                        //						final int size = reader.getTextStart()+reader.getTextLength();
                        //						for (int i = reader.getTextStart(); i < size; i++) {
                        //							elementContent.append(ch[i]);
                        //						}
                        //					}
                    }
                }
            }
            catch (XMLStreamException e)
            {
                throw new DataFormatException("", e);
            }
        }