示例#1
0
        public WordNode GetRoot()
        {
            WordNode node = this;

            while (node.Parent != null)
            {
                node = node.Parent;
            }
            return(node);
        }
示例#2
0
        private static WordNode GetWordTree(WordNode initial, int maxDepth = int.MaxValue)
        {
            if (initial.Depth == maxDepth)
            {
                return(initial);
            }

            Queue <WordNode> nodes = new Queue <WordNode>();

            nodes.Enqueue(initial);

            HashSet <string> tested = new HashSet <string>()
            {
                initial.Entry.Word
            };

            while (nodes.Any())
            {
                WordNode node = nodes.Dequeue();
                if (node.Depth == maxDepth)
                {
                    continue;
                }

                // Get definitions
                List <string> defs = node.Entry.GetNounDefinitions();

                foreach (string def in defs)
                {
                    var             definingWords      = def.GetLemmatizedNouns(tested).GetAwaiter().GetResult().OfType <EntryDTO>();
                    List <WordNode> definingWordsNodes = definingWords.Select(e => new WordNode(e)).ToList();

                    if (definingWordsNodes.Any())
                    {
                        node.AddChildren(definingWordsNodes);
                        foreach (var defNode in definingWordsNodes)
                        {
                            tested.Add(defNode.Entry.Word);
                            nodes.Enqueue(defNode);
                        }
                    }
                }
            }

            return(initial);
        }
示例#3
0
        public static async void AnalyseWord()
        {
            string initialWord = "matter";

            Stopwatch sw = new Stopwatch();

            sw.Start();
            // Get initial entry
            EntryDTO entry = await APIManager.GetWordEntry(initialWord);

            WordNode      root   = new WordNode(entry);
            List <string> tested = new List <string> {
                initialWord
            };

            GetWordTree(root);
            var a = root.Traverse().Select(n => n.Entry.Word);

            sw.Stop();
            var time = sw.Elapsed;
        }
示例#4
0
        public static void ToGephiCSV(this WordNode node, string filenameNodes, string filenameEdges, bool fromRoot = true)
        {
            WordNode initialNode = fromRoot ? node.GetRoot() : node;
            string   nodecsv     = "Id,Label,Size\n";
            string   edgecsv     = "Source,Target\n";

            initialNode.Traverse().Where(n => n.Parent != null).OrderBy(n => n.Depth).ToList().AssignIds().ForEach(n =>
            {
                nodecsv = string.Concat(nodecsv, n.Id, ",", n.Entry.Word, ",", n.Depth, "\n");
                edgecsv = string.Concat(edgecsv, n.Parent.Id, ",", n.Id, "\n");
            });

            using (StreamWriter sw = new StreamWriter(filenameNodes))
            {
                sw.Write(nodecsv);
            }

            using (StreamWriter sw = new StreamWriter(filenameEdges))
            {
                sw.Write(edgecsv);
            }
        }
示例#5
0
 private void InitChild(WordNode child)
 {
     child.Parent = this;
     child.Depth  = this.Depth + 1;
 }