Esempio n. 1
0
        public void WhenGivenAQuery_Scoring_ShouldGiveMoreWeightToStartOfNewWord(
            string queryString, string compareString1, string compareString2)
        {
            // When
            var matcher = new StringMatcher {
                UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular
            };

            // Given
            var compareString1Result = matcher.FuzzyMatch(queryString, compareString1);
            var compareString2Result = matcher.FuzzyMatch(queryString, compareString2);

            Debug.WriteLine("");
            Debug.WriteLine("###############################################");
            Debug.WriteLine($"QueryString: \"{queryString}\"{Environment.NewLine}");
            Debug.WriteLine($"CompareString1: \"{compareString1}\", Score: {compareString1Result.Score}{Environment.NewLine}");
            Debug.WriteLine($"CompareString2: \"{compareString2}\", Score: {compareString2Result.Score}{Environment.NewLine}");
            Debug.WriteLine("###############################################");
            Debug.WriteLine("");

            // Should
            Assert.True(compareString1Result.Score > compareString2Result.Score,
                        $"Query: \"{queryString}\"{Environment.NewLine} " +
                        $"CompareString1: \"{compareString1}\", Score: {compareString1Result.Score}{Environment.NewLine}" +
                        $"Should be greater than{ Environment.NewLine}" +
                        $"CompareString2: \"{compareString2}\", Score: {compareString1Result.Score}{Environment.NewLine}");
        }
        public void FuzzyMatchingScoreShouldBeHigherWhenPreceedingCharacterIsSpace(string firstCompareStr, string secondCompareStr, string query)
        {
            // Arrange
            var matcher = new StringMatcher();

            // Act
            var firstScore  = matcher.FuzzyMatch(query, firstCompareStr).Score;
            var secondScore = matcher.FuzzyMatch(query, secondCompareStr).Score;

            // Assert
            Assert.IsTrue(firstScore > secondScore);
        }
Esempio n. 3
0
        public void WhenGivenStringsAndAppliedPrecisionFilteringThenShouldReturnGreaterThanPrecisionScoreResults(string searchTerm)
        {
            var results = new List <Result>();
            var matcher = new StringMatcher();

            foreach (var str in GetSearchStrings())
            {
                results.Add(new Result
                {
                    Title = str,
                    Score = matcher.FuzzyMatch(searchTerm, str).Score
                });
            }

            foreach (var precisionScore in GetPrecisionScores())
            {
                var filteredResult = results.Where(result => result.Score >= precisionScore).Select(result => result).OrderByDescending(x => x.Score).ToList();

                Debug.WriteLine("");
                Debug.WriteLine("###############################################");
                Debug.WriteLine("SEARCHTERM: " + searchTerm + ", GreaterThanSearchPrecisionScore: " + precisionScore);
                foreach (var item in filteredResult)
                {
                    Debug.WriteLine("SCORE: " + item.Score.ToString() + ", FoundString: " + item.Title);
                }
                Debug.WriteLine("###############################################");
                Debug.WriteLine("");

                Assert.IsFalse(filteredResult.Any(x => x.Score < precisionScore));
            }
        }
Esempio n. 4
0
        public void MatchTest()
        {
            var sources = new List <string>
            {
                "file open in browser-test",
                "Install Package",
                "add new bsd",
                "Inste",
                "aac"
            };

            var results = new List <Result>();
            var matcher = new StringMatcher();

            foreach (var str in sources)
            {
                results.Add(new Result
                {
                    Title = str,
                    Score = matcher.FuzzyMatch("inst", str).RawScore
                });
            }

            results = results.Where(x => x.Score > 0).OrderByDescending(x => x.Score).ToList();

            Assert.IsTrue(results.Count == 3);
            Assert.IsTrue(results[0].Title == "Inste");
            Assert.IsTrue(results[1].Title == "Install Package");
            Assert.IsTrue(results[2].Title == "file open in browser-test");
        }
Esempio n. 5
0
        public void WhenGivenQueryShouldReturnResultsContainingAllQuerySubstrings(
            string queryString,
            string compareString,
            StringMatcher.SearchPrecisionScore expectedPrecisionScore,
            bool expectedPrecisionResult)
        {
            // When
            var matcher = new StringMatcher {
                UserSettingSearchPrecision = expectedPrecisionScore
            };

            // Given
            var matchResult = matcher.FuzzyMatch(queryString, compareString);

            Debug.WriteLine("");
            Debug.WriteLine("###############################################");
            Debug.WriteLine($"QueryString: {queryString}     CompareString: {compareString}");
            Debug.WriteLine($"RAW SCORE: {matchResult.RawScore.ToString()}, PrecisionLevelSetAt: {expectedPrecisionScore} ({(int)expectedPrecisionScore})");
            Debug.WriteLine("###############################################");
            Debug.WriteLine("");

            // Should
            Assert.AreEqual(expectedPrecisionResult, matchResult.IsSearchPrecisionScoreMet(),
                            $"Query:{queryString}{Environment.NewLine} " +
                            $"Compare:{compareString}{Environment.NewLine}" +
                            $"Raw Score: {matchResult.RawScore}{Environment.NewLine}" +
                            $"Precision Score: {(int)expectedPrecisionScore}");
        }
        public void WhenGivenDesiredPrecisionThenShouldReturnAllResultsGreaterOrEqual(
            string queryString,
            string compareString,
            StringMatcher.SearchPrecisionScore expectedPrecisionScore,
            bool expectedPrecisionResult)
        {
            // When
            var matcher = new StringMatcher {
                UserSettingSearchPrecision = expectedPrecisionScore
            };

            // Given
            var matchResult = matcher.FuzzyMatch(queryString, compareString);

            Debug.WriteLine(string.Empty);
            Debug.WriteLine("###############################################");
            Debug.WriteLine($"QueryString: {queryString}     CompareString: {compareString}");
            Debug.WriteLine($"RAW SCORE: {matchResult.RawScore}, PrecisionLevelSetAt: {expectedPrecisionScore} ({(int)expectedPrecisionScore})");
            Debug.WriteLine("###############################################");
            Debug.WriteLine(string.Empty);

            // Should
            Assert.AreEqual(
                expectedPrecisionResult,
                matchResult.IsSearchPrecisionScoreMet(),
                $"{$"Query:{queryString}{Environment.NewLine} "}{$"Compare:{compareString}{Environment.NewLine}"}{$"Raw Score: {matchResult.RawScore}{Environment.NewLine}"}{$"Precision Score: {(int)expectedPrecisionScore}"}");
        }
Esempio n. 7
0
        public void WhenGivenNotAllCharactersFoundInSearchStringThenShouldReturnZeroScore(string searchString)
        {
            var compareString = "Can have rum only in my glass";
            var matcher       = new StringMatcher();
            var scoreResult   = matcher.FuzzyMatch(searchString, compareString).RawScore;

            Assert.True(scoreResult == 0);
        }
        [TestCase("sql  manag", MicrosoftSqlServerManagementStudio, 99)]//double spacing intended
        public void WhenGivenQueryStringThenShouldReturnCurrentScoring(string queryString, string compareString, int expectedScore)
        {
            // When, Given
            var matcher  = new StringMatcher();
            var rawScore = matcher.FuzzyMatch(queryString, compareString).RawScore;

            // Should
            Assert.AreEqual(expectedScore, rawScore, $"Expected score for compare string '{compareString}': {expectedScore}, Actual: {rawScore}");
        }
Esempio n. 9
0
        public void WhenGivenAnAcronymQuery_ShouldReturnAcronymScore(string queryString, string compareString,
                                                                     int desiredScore)
        {
            var matcher = new StringMatcher();
            var score   = matcher.FuzzyMatch(queryString, compareString).Score;

            Assert.IsTrue(score == desiredScore,
                          $@"Query: ""{queryString}""
                   CompareString: ""{compareString}""
                   Score: {score}
                   Desired Score: {desiredScore}");
        }
Esempio n. 10
0
        [TestCase("sql  manag", MicrosoftSqlServerManagementStudio, 121)] //double spacing intended
        public void WhenGivenQueryString_ThenShouldReturn_TheDesiredScoring(
            string queryString, string compareString, int expectedScore)
        {
            // When, Given
            var matcher = new StringMatcher {
                UserSettingSearchPrecision = SearchPrecisionScore.Regular
            };
            var rawScore = matcher.FuzzyMatch(queryString, compareString).RawScore;

            // Should
            Assert.AreEqual(expectedScore, rawScore,
                            $"Expected score for compare string '{compareString}': {expectedScore}, Actual: {rawScore}");
        }
        public void WhenGivenChinese(string queryString, string stringToCompare)
        {
            Settings.Instance.ShouldUsePinyin = true;
            var matcher = new StringMatcher {
                UserSettingSearchPrecision = StringMatcher.SearchPrecisionScore.Regular
            };

            var matchResult = matcher.FuzzyMatch(queryString, stringToCompare);

            Debug.WriteLine("");
            Debug.WriteLine("###############################################");
            string output = $"QueryString: {queryString}{Environment.NewLine}" +
                            $"CompareString: {stringToCompare}{Environment.NewLine}" +
                            $"Score: {matchResult.RawScore} {matchResult.Score}{Environment.NewLine}" +
                            $"MatchData: {matchResult.MatchData}{Environment.NewLine}";

            Debug.WriteLine(output);
            Debug.WriteLine("###############################################");
            Debug.WriteLine("");

            // Should
            Assert.AreEqual(true, matchResult.IsSearchPrecisionScoreMet(), output);
        }
Esempio n. 12
0
        public void WhenMultipleResults_ExactMatchingResult_ShouldHaveGreatestScore(string queryString, string firstName, string firstDescription, string firstExecutableName, string secondName, string secondDescription, string secondExecutableName)
        {
            // Act
            var matcher                  = new StringMatcher();
            var firstNameMatch           = matcher.FuzzyMatch(queryString, firstName).RawScore;
            var firstDescriptionMatch    = matcher.FuzzyMatch(queryString, firstDescription).RawScore;
            var firstExecutableNameMatch = matcher.FuzzyMatch(queryString, firstExecutableName).RawScore;

            var secondNameMatch           = matcher.FuzzyMatch(queryString, secondName).RawScore;
            var secondDescriptionMatch    = matcher.FuzzyMatch(queryString, secondDescription).RawScore;
            var secondExecutableNameMatch = matcher.FuzzyMatch(queryString, secondExecutableName).RawScore;

            var firstScore = new[] { firstNameMatch, firstDescriptionMatch, firstExecutableNameMatch }.Max();
            var secondScore = new[] { secondNameMatch, secondDescriptionMatch, secondExecutableNameMatch }.Max();

            // Assert
            Assert.IsTrue(firstScore > secondScore);
        }
Esempio n. 13
0
        public void WhenMultipleResults_ExactMatchingResult_ShouldHaveGreatestScore(
            string queryString, string firstName, string firstDescription, string firstExecutableName,
            string secondName, string secondDescription, string secondExecutableName)
        {
            // Act
            var matcher                  = new StringMatcher();
            var firstNameMatch           = matcher.FuzzyMatch(queryString, firstName).RawScore;
            var firstDescriptionMatch    = matcher.FuzzyMatch(queryString, firstDescription).RawScore;
            var firstExecutableNameMatch = matcher.FuzzyMatch(queryString, firstExecutableName).RawScore;

            var secondNameMatch           = matcher.FuzzyMatch(queryString, secondName).RawScore;
            var secondDescriptionMatch    = matcher.FuzzyMatch(queryString, secondDescription).RawScore;
            var secondExecutableNameMatch = matcher.FuzzyMatch(queryString, secondExecutableName).RawScore;

            var firstScore = new[] { firstNameMatch, firstDescriptionMatch, firstExecutableNameMatch }.Max();
            var secondScore = new[] { secondNameMatch, secondDescriptionMatch, secondExecutableNameMatch }.Max();

            // Assert
            Assert.IsTrue(firstScore > secondScore,
                          $"Query: \"{queryString}\"{Environment.NewLine} " +
                          $"Name of first: \"{firstName}\", Final Score: {firstScore}{Environment.NewLine}" +
                          $"Should be greater than{ Environment.NewLine}" +
                          $"Name of second: \"{secondName}\", Final Score: {secondScore}{Environment.NewLine}");
        }