示例#1
0
        /// <summary>
        /// Method to parsing one sentence of given text
        /// </summary>
        /// <param name="sentence">Sentence to parse</param>
        /// <returns>List of parsed elements</returns>
        public SentenceGraph ParseSentence(string sentence)
        {
            Dictionary <int, List <IProcessable> > dependencyTree;
            SentenceGraph graph = new SentenceGraph();

            // recreating dependency tree given as REST API response from UDPipe
            var validLines = this.Client.GetResponse(sentence);

            dependencyTree = GetDependencyTree(validLines);

            // new root element
            var root = this.ElementFactory.CreateRoot(sentence);

            // compressing dependency tree into graph
            this.Comparer.Tree = dependencyTree;
            IProcessable element = CompressDependencyTree(dependencyTree, graph, root).FinalizeProcessing(graph);

            // Clear graph if nothing were found for safety
            if (element is Root)
            {
                graph.Clear();
            }

            // Adding last processed vertex (is added only if its only vertex in sentence)
            if (element is IDrawable drawable)
            {
                graph.AddVertex(drawable);
            }

            return(graph);
        }
示例#2
0
        /// <summary>
        /// Method for compressing dependency tree edges into other representation
        /// </summary>
        /// <param name="tree">tree to be compressed</param>
        /// <param name="graph">new graph to be created</param>
        /// <param name="element">actual processed element</param>
        /// <returns>Element with processed its dependencies</returns>
        private IProcessable CompressDependencyTree(Dictionary <int, List <IProcessable> > tree, SentenceGraph graph, IProcessable element)
        {
            if (!tree.ContainsKey(element.Id))
            {
                return(element);
            }

            // Adpositions have different priorities
            if (element is Adposition)
            {
                this.Comparer.IsAdposition = true;
            }
            else
            {
                this.Comparer.IsAdposition = false;
            }

            // sorting dependencies before they are processed
            tree[element.Id].Sort(this.Comparer);

            // processing each dependency
            foreach (var vertex in tree[element.Id])
            {
                element = element.Process(CompressDependencyTree(tree, graph, vertex), graph);
            }

            return(element);
        }