public static string FormatKnGraphIndexesForPrinting(KnowledgeGraph graph)
        {
            StringBuilder sb = new StringBuilder();

            foreach (KnowledgeGraphNode node in graph.KnGraph)
            {
                sb.Append(node.Index + ": ");
                foreach (KnowledgeGraphNode neighbor in node.Neighbors)
                {
                    sb.Append(neighbor.Index + ", ");
                }

                sb.AppendLine();
            }

            return(sb.ToString());
        }
        public static string FormatKnGraphLabelsForPrinting(KnowledgeGraph graph)
        {
            // we'll use DFS to print the names according to the graph and with indentation
            List <bool> visited = new List <bool>();

            graph.KnGraph.ForEach(x => visited.Add(false));
            StringBuilder sb = new StringBuilder();

            foreach (KnowledgeGraphNode node in graph.KnGraph)
            {
                if (!visited[node.Index])
                {
                    DFSRecursive(graph, visited, node, 0, ref sb);
                }
            }

            return(sb.ToString());
        }
        public static string FormatKnGraphIndexAndLabelsForPrinting(KnowledgeGraph graph)
        {
            StringBuilder sb = new StringBuilder();

            foreach (KnowledgeGraphNode node in graph.KnGraph)
            {
                sb.AppendLine(node.OriginalGraphType + "-" + node.Index + ": " + node.HtmlName + ": " + node.Label + ", " + node.LinkToPage);
                sb.Append(node.Index + ": ");
                foreach (KnowledgeGraphNode neighbor in node.Neighbors)
                {
                    sb.Append(neighbor.Index + ", ");
                }

                sb.AppendLine();
            }

            return(sb.ToString());
        }
        private static void DFSRecursive(KnowledgeGraph graph, List <bool> visited, KnowledgeGraphNode currentNode, int level, ref StringBuilder sb)
        {
            sb.Append(currentNode.Label);
            sb.AppendLine();
            visited[currentNode.Index] = true;

            level++;

            foreach (KnowledgeGraphNode neighbor in graph.KnGraph[currentNode.Index].Neighbors)
            {
                if (!visited[neighbor.Index])
                {
                    PrintLevelSpaces(level, ref sb);
                    DFSRecursive(graph, visited, neighbor, level, ref sb);
                }
            }

            level--;
        }
        public static void DownloadAllPagesInKnGraph()
        {
            KnowledgeGraph knowledgeGraph = WKGE.ExtractKnGraphFromUris(WKGE.WikipediaPagesToParse);

            // Download the html files
            SaveUriToHtmlFile(WKGE.GetWikipediaListOfAlgorithmsPageUri(), "../../../DownloadedHtmlPages/listOfAlgos.html");
            SaveUriToHtmlFile(WKGE.GetWikipediaListOfDataStructuresPageUri(), "../../../DownloadedHtmlPages/listOfDataStructures.html");

            // Download the content of each link in the nodes of the kn graph
            foreach (KnowledgeGraphNode node in knowledgeGraph.KnGraph.Where(x => x.LinkToPage != null))
            {
                string filePath = GetFilePathFromNodeLinkTopage(node.LinkToPage);

                if (!File.Exists(filePath))
                {
                    SaveUriPageContentToTxtFile(node.LinkToPage, filePath);
                    Console.WriteLine("Wrote file with name " + Path.GetFileName(filePath));
                }
                else
                {
                    Console.WriteLine("!!! ALREADYEXISTS !!! " + filePath);
                }
            }
        }
 private static void AddEdgeToKnowledgeGraph(ref KnowledgeGraph graph, int nodeIndex, KnowledgeGraphNode nodeToAdd)
 {
     graph.KnGraph[nodeIndex].Neighbors.Add(new KnowledgeGraphNode(nodeToAdd));
 }
        private static Dictionary <string, KnowledgeGraphNode> GetUniqueDataStructureNodes(KnowledgeGraph graph)
        {
            List <KnowledgeGraphNode> dataStructureNodesWithDuplicates = graph.KnGraph.Where(n => n.OriginalGraphType == OriginalGraphType.DataStructuresKnGraph).ToList();
            Dictionary <string, KnowledgeGraphNode> result             = new Dictionary <string, KnowledgeGraphNode>();

            foreach (KnowledgeGraphNode node in dataStructureNodesWithDuplicates)
            {
                string nodeLabel = node.Label.ToLowerInvariant();
                if (!result.ContainsKey(nodeLabel))
                {
                    result.Add(nodeLabel, node);
                }
            }

            return(result);
        }
        public static List <WordCountByNodeIndex> GetDataStructureWordCountsFromGraph(KnowledgeGraph knowledgeGraph)
        {
            // compute the list of DS words
            List <string> dataStructureWords   = Utilities.GetDataStructureWordsFromGraph(knowledgeGraph);
            List <WordCountByNodeIndex> result = new List <WordCountByNodeIndex>();

            // foreach node in the graph which has a link to page and downloaded file for that link, get the ds word count
            knowledgeGraph.KnGraph
            .Where(node => node.OriginalGraphType == OriginalGraphType.AlgorithmsKnGraph && node.LinkToPage != null && File.Exists(Utilities.GetFilePathFromNodeLinkTopage(node.LinkToPage))).ToList()
            .ForEach(node =>
            {
                // make empty ds words dictionary for this node
                Dictionary <string, int> dsWordsCountDictForOneNode = new Dictionary <string, int>();
                dataStructureWords.ForEach(dsWord =>
                {
                    dsWordsCountDictForOneNode.Add(dsWord, 0);
                });

                // get file content and count the ds words in it
                string fileContent        = File.ReadAllText(Utilities.GetFilePathFromNodeLinkTopage(node.LinkToPage));
                string[] fileContentWords = fileContent.Split(' ');
                foreach (string word in fileContentWords)
                {
                    if (dsWordsCountDictForOneNode.ContainsKey(word))
                    {
                        dsWordsCountDictForOneNode[word]++;
                    }
                }

                // remove words with 0 count from words count dictionary
                dsWordsCountDictForOneNode = dsWordsCountDictForOneNode.Where(x => x.Value > 0).ToDictionary(y => y.Key, y => y.Value);

                // only add it to the list if there is at least one ds word
                if (dsWordsCountDictForOneNode.Any())
                {
                    WordCountByNodeIndex wordCountForNode = new WordCountByNodeIndex()
                    {
                        Index      = node.Index,
                        WordsCount = new Dictionary <string, int>(dsWordsCountDictForOneNode)
                    };

                    result.Add(wordCountForNode);
                }
            });

            return(result);
        }