Пример #1
0
        public void BinarySearch_EmptyListInserted_EmptyListReturned()
        {
            List <Word> wordList = new List <Word>();
            var         result   = SearchEngine <Word> .BinarySearch(wordList, false, "bb");

            Assert.AreEqual(0, result.Count);
        }
Пример #2
0
 private void btnSearch_Click(object sender, EventArgs e)
 {
     foreach (KeyValuePair <string, int> word in SearchEngine <Word> .BinarySearch(sortedWordsList, true, tbxSearch.Text))
     {
         dataSearchResults.Rows.Add(tbxSearch.Text, word.Value, word.Key.ToString());
     }
 }
Пример #3
0
        public void BinarySearch_SeveralMatchesExists_FindsAll()
        {
            WordExtractor extractor = new WordExtractor();

            string path1 = $"{AppDomain.CurrentDomain.BaseDirectory}TestFiles\\TextFile1.txt";
            string text1 = InputOutput.ReadFile(path1);

            string path2 = $"{AppDomain.CurrentDomain.BaseDirectory}TestFiles\\TextFile2.txt";
            string text2 = InputOutput.ReadFile(path2);

            string path3 = $"{AppDomain.CurrentDomain.BaseDirectory}TestFiles\\TextFile3.txt";
            string text3 = InputOutput.ReadFile(path3);

            string path4 = $"{AppDomain.CurrentDomain.BaseDirectory}TestFiles\\TextFile4.txt";
            string text4 = InputOutput.ReadFile(path4);

            extractor.ExtractWordsFromTextFile(text1, path1);
            extractor.ExtractWordsFromTextFile(text2, path2);
            extractor.ExtractWordsFromTextFile(text3, path3);
            extractor.ExtractWordsFromTextFile(text4, path4);

            List <Word> list = extractor.GetCompoundedList();

            SearchEngine <Word> .QuickSort(list, 0, list.Count - 1);

            var result = SearchEngine <Word> .BinarySearch(list, true, "is");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add(path1, 1);
            expected.Add(path2, 2);
            expected.Add(path3, 1);
            expected.Add(path4, 1);
            Assert.AreEqual(expected, result);
        }
Пример #4
0
        public void BinarySearch_SearchForNonExistingWord_ReturnsEmptyList()
        {
            List <Word> wordList = new List <Word> {
                new Word("aa", "txt"), new Word("bb", "txt"), new Word("cc", "txt"), new Word("dd", "txt"), new Word("ee", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "pp");

            Assert.AreEqual(0, result.Count);
        }
Пример #5
0
        public void BinarySearch_ListNotSorted_FindsCorrectWord()
        {
            List <Word> wordList = new List <Word> {
                new Word("jj", "txt"), new Word("ee", "txt"), new Word("pp", "txt"), new Word("aa", "txt"), new Word("bb", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, false, "bb");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("txt", 1);
            Assert.AreEqual(expected, result);
        }
Пример #6
0
        public void BinarySearch_EnterWeirdCharacters_ReturnsEmptyList()
        {
            List <Word> wordList = new List <Word> {
                new Word("aa", "txt"), new Word("bb", "txt"), new Word("cc", "txt"), new Word("dd", "txt"), new Word("ee", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "");

            Assert.AreEqual(0, result.Count);
            var result2 = SearchEngine <Word> .BinarySearch(wordList, true, null);

            Assert.AreEqual(0, result2.Count);
        }
Пример #7
0
        public void BinarySearch_CaseDoesntMatch_FindsTheWord()
        {
            List <Word> wordList = new List <Word> {
                new Word("aa", "txt"), new Word("bB", "txt"), new Word("cc", "txt"), new Word("dd", "txt"), new Word("ee", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "Bb");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("txt", 1);
            Assert.AreEqual(expected, result);
        }
Пример #8
0
        public void BinarySearch_OnlyOneMatchExists_FindsTheWord()
        {
            List <Word> wordList = new List <Word> {
                new Word("a", "txt"), new Word("b", "txt"), new Word("c", "txt"), new Word("d", "txt"), new Word("e", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "b");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("txt", 1);
            Assert.AreEqual(expected, result);
        }
Пример #9
0
        public void BinarySearch_SeveralMatchesExists_FindsAll()
        {
            List <Word> wordList = new List <Word> {
                new Word("aa", "txt"), new Word("bb", "txt"), new Word("bb", "txt"), new Word("bb", "txt"), new Word("bb", "txt"),
                new Word("cc", "txt"), new Word("dd", "txt"), new Word("ee", "txt")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "bb");

            Assert.AreEqual(4, result["txt"]);
            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("txt", 4);
            Assert.AreEqual(expected, result);
        }
Пример #10
0
        public void BinarySearch_SeveralMatchesFromDifferentFiles_FindsAllAndPresentsThemSeparately()
        {
            List <Word> wordList = new List <Word> {
                new Word("aa", "txt1"), new Word("bb", "txt2"), new Word("bb", "txt3"), new Word("bb", "txt4"), new Word("bb", "txt5"),
                new Word("cc", "txt7"), new Word("dd", "txt8"), new Word("ee", "txt10")
            };
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "bb");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("txt2", 1);
            expected.Add("txt3", 1);
            expected.Add("txt4", 1);
            expected.Add("txt5", 1);
            Assert.AreEqual(expected, result);
        }
Пример #11
0
        public void BinarySearch_LoadsEmptyTextFile_ReturnsEmptyDictionary()
        {
            WordExtractor extractor = new WordExtractor();

            string path1 = $"{AppDomain.CurrentDomain.BaseDirectory}TestFiles\\EmptyTextFile.txt";
            string text1 = InputOutput.ReadFile(path1);

            extractor.ExtractWordsFromTextFile(text1, path1);

            List <Word> list = extractor.GetCompoundedList();

            SearchEngine <Word> .QuickSort(list, 0, list.Count - 1);

            var result = SearchEngine <Word> .BinarySearch(list, true, "");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            Assert.AreEqual(expected, result);
        }
Пример #12
0
        public void BinarySearch_LoadsNull_ReturnsEmptyDictionary()
        {
            WordExtractor extractor = new WordExtractor();

            string path1 = null;
            string text1 = InputOutput.ReadFile(path1);

            extractor.ExtractWordsFromTextFile(text1, path1);

            List <Word> list = extractor.GetCompoundedList();

            SearchEngine <Word> .QuickSort(list, 0, list.Count - 1);

            var result = SearchEngine <Word> .BinarySearch(list, true, "could");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            Assert.AreEqual(expected, result);
        }
Пример #13
0
        public void BinarySearch_ListIsBig_ReturnsCorrectList()
        {
            List <Word> wordList = new List <Word>();

            Random rnd = new Random();

            for (int i = 0; i < 100000; i++)
            {
                wordList.Add(new Word($"{(char)rnd.Next('c', 'z')}{(char)rnd.Next('c', 'z')}", "txt"));
            }

            wordList.Add(new Word("bb", "text"));

            for (int i = 0; i < 1000000; i++)
            {
                wordList.Add(new Word($"{(char)rnd.Next('c', 'z')}{(char)rnd.Next('c', 'z')}", "txt"));
            }

            DateTime start = DateTime.Now;

            SearchEngine <Word> .QuickSort(wordList, 0, wordList.Count - 1);

            TimeSpan span = DateTime.Now - start;

            Debug.WriteLine($"Time of sort was {span.TotalSeconds} seconds");

            start = DateTime.Now;
            var result = SearchEngine <Word> .BinarySearch(wordList, true, "bb");

            span = DateTime.Now - start;
            Debug.WriteLine($"Time of search was {span.TotalSeconds} seconds");

            Dictionary <string, int> expected = new Dictionary <string, int>();

            expected.Add("text", 1);
            Assert.AreEqual(expected, result);
        }
Пример #14
0
        static void Main(string[] args)
        {
            WordExtractor wordExtractor  = new WordExtractor();
            List <Word>   compoundedList = new List <Word>();

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("se-SE");
            Console.WriteLine($"This program lets you search for single words from text files. To begin your search, start by entering a file. {Environment.NewLine}");
            while (compoundedList.Count <= 0)
            {
                //Reads file from file path. Extracts words from file. Sorts words in ascending alphabetical order.
                compoundedList = LoadFileToMain(wordExtractor, compoundedList);
            }
            while (compoundedList.Count > 0)
            {
                Console.WriteLine($"To read a new file:\t\t\t\t\t Press [1] + [Enter]");
                Console.WriteLine($"To search for a word:\t\t\t\t\t Press [2] + [Enter]");
                Console.WriteLine($"To save all words in the document(s) in a sorted list:\t Press [3] + [Enter]");
                Console.WriteLine($"To print all words in the document(s):\t\t\t Press [4] + [Enter]");
                Console.WriteLine($"To exit the program:\t\t\t\t\t Press [0] + [Enter]");
                Console.WriteLine();
                Console.Write(">: ");
                switch (Console.ReadLine())
                {
                //Reads file from file path. Extracts words from file. Sorts words in ascending alphabetical order.
                case "1":
                    //Console.WriteLine("File Location exp \"C\\User\\... \"");
                    compoundedList = LoadFileToMain(wordExtractor, compoundedList);
                    break;

                //Searches for a word selected by user input. Prints existance of word in all files to console.
                case "2":
                    if (compoundedList.Count > 0)
                    {
                        Console.WriteLine();
                        Console.Write("Type the word you want to search: ");
                        var searchWord    = Console.ReadLine().ToLower();
                        var searchedWords = SearchEngine <Word> .BinarySearch(compoundedList, true, searchWord);

                        if (searchedWords.Count > 0)
                        {
                            foreach (var item in searchedWords)
                            {
                                Console.WriteLine($"{searchWord} exists {item.Value} times in {item.Key}");
                                Console.WriteLine();
                            }
                        }
                        else
                        {
                            Console.WriteLine($"{searchWord} does not exist in the document(s)");
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        Console.WriteLine("No file read yet");
                        Console.WriteLine();
                    }
                    break;

                //Creates a new .txt-file. Saves sorted list of all words in all files read to this file.
                case "3":
                    if (compoundedList.Count > 0)
                    {
                        Console.WriteLine();
                        Console.WriteLine($"Press [1] and [Enter] if you would like to save in a new file.{Environment.NewLine}Press [2] and [Enter] if you would like to create a new default file:");
                        Console.Write(">: ");
                        var saveOption = Console.ReadLine();
                        Console.WriteLine();
                        string fullFilePath;
                        string sortedFileContent = wordExtractor.BuildStringFromListOfWords(compoundedList);
                        if (saveOption == "1")
                        {
                            Console.WriteLine("Enter a location where you would like to save your file:");
                            Console.Write(">: ");
                            string directoryPath = Console.ReadLine();
                            Console.WriteLine();
                            Console.WriteLine("Enter a name for your file:");
                            Console.Write(">: ");
                            fullFilePath = InputOutput.SaveFile(sortedFileContent, directoryPath, Console.ReadLine());
                            Console.WriteLine();
                            if (fullFilePath == "Could not save file." || fullFilePath == "You don't have access, your authority level is to low")
                            {
                                Console.WriteLine(fullFilePath);
                            }
                            else
                            {
                                Console.WriteLine($"Finished saving file at {fullFilePath}");
                            }
                        }
                        else if (saveOption == "2")
                        {
                            fullFilePath = InputOutput.SaveFile(sortedFileContent, compoundedList[0].File + "new.txt");
                            if (fullFilePath == "Could not save file." || fullFilePath == "You don't have access, your authority level is to low")
                            {
                                Console.WriteLine(fullFilePath);
                                Console.WriteLine();
                            }
                            else
                            {
                                Console.WriteLine($"Finished saving file at {fullFilePath}");
                                Console.WriteLine();
                            }
                        }
                        else
                        {
                            Console.WriteLine("Illegal command!");
                        }
                    }
                    else
                    {
                        Console.WriteLine("No file read yet");
                    }
                    break;

                //Prints the sorted list to console.
                case "4":
                    if (compoundedList.Count > 10000)
                    {
                        Console.WriteLine("Too many words to print in console. To see all words, try saving to a text file instead.");
                        Console.WriteLine();
                    }
                    else if (compoundedList.Count > 0)
                    {
                        Console.WriteLine(wordExtractor.BuildStringFromListOfWords(compoundedList));
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("No file read yet");
                    }

                    break;

                //Exits the program.
                case "0":
                    Environment.Exit(0);
                    break;

                //Prints a message to the console when input is erroneous.
                default:
                    Console.WriteLine("Wrong input please try again.");
                    break;
                }
            }
        }