Exemple #1
0
        public int Run()
        {
            var keywords     = File.ReadAllLines(this.keywordsPath);
            var searchEngine = new SearchEngine(keywords, new DocumentWithGroupSanitizer());

            foreach (var rawDocument in File.ReadAllText(this.documentDbPath).Replace("\r", "").Split("\n\n"))
            {
                searchEngine.AddDocument(rawDocument);
            }
            try
            {
                var groups = searchEngine.Cluster();
                Console.WriteLine("Assigned group                  Original group");
                Console.WriteLine(new String('-', 50));
                foreach (var group in groups)
                {
                    foreach (var document in group.Value)
                    {
                        Console.WriteLine($"{group.Key, -30}  {document.Group}");
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(1);
            }

            return(0);
        }
        private SearchEngine CreateSearchEngine()
        {
            var keywords  = this.Configuration["keywords.txt"].Replace("\r", "").Split("\n");
            var documents = this.Configuration["documents.txt"].Replace("\r", "").Split("\n\n");

            var searchEngine = new SearchEngine(keywords, new DocumentSanitizer());

            foreach (var rawDocument in documents)
            {
                searchEngine.AddDocument(rawDocument);
            }
            return(searchEngine);
        }
        public int Run()
        {
            var keywords     = File.ReadAllLines(this.keywordsPath);
            var searchEngine = new SearchEngine(keywords, new DocumentSanitizer());

            foreach (var rawDocument in File.ReadAllText(this.documentDbPath).Replace("\r", "").Split("\n\n"))
            {
                searchEngine.AddDocument(rawDocument);
            }
            while (true)
            {
                var query = Prompt.GetString("What are you looking for?", promptColor: ConsoleColor.Green);
                if (query?.Length == 0)
                {
                    break;
                }
                try
                {
                    var results = searchEngine.Search(query).Results.Take(this.numberOfResults).Where(x => x.similarity > 0).ToList();
                    if (results.Count == 0)
                    {
                        Console.WriteLine("No documents retrieved for your query :(");
                    }
                    for (int i = 0; i < results.Count; i++)
                    {
                        var(document, similarity) = results[i];
                        Console.WriteLine($"{i + 1}. similarity: {similarity:f2}");
                        Console.WriteLine(document.RawContents);
                        Console.WriteLine();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return(1);
                }
            }
            return(0);
        }