public void BestScoreTest() { int score = FuzzyMatching.CalculateScoreForMatches(new List <int>() { 1, 2, 3, 4 }); Assert.IsTrue(score == -3); }
/// <summary> /// Calculates the score for how closely this window matches the search string /// </summary> /// <remarks> /// Higher Score is better /// </remarks> private void CalculateScore() { if (FuzzyMatching.CalculateScoreForMatches(SearchMatchesInProcessName) > FuzzyMatching.CalculateScoreForMatches(SearchMatchesInTitle)) { Score = FuzzyMatching.CalculateScoreForMatches(SearchMatchesInProcessName); BestScoreSource = TextType.ProcessName; } else { Score = FuzzyMatching.CalculateScoreForMatches(SearchMatchesInTitle); BestScoreSource = TextType.WindowTitle; } }
/// <summary> /// Finds the best match (the one with the most /// number of letters adjecent to each other) and /// returns the index location of each of the letters /// of the matches /// </summary> /// <param name="text">The text to search inside of</param> /// <param name="searchText">the text to search for</param> /// <returns></returns> public static List <int> FindBestFuzzyMatch(string text, string searchText, int searchStartIndex = 0) { List <int> matchIndexes = new List <int>(); searchText = searchText.ToLower(); text = text.ToLower(); // Create a grid to march matches like // eg. // a b c a d e c f g // a x x // c x x bool[,] matches = new bool[text.Length, searchText.Length]; for (int firstIndex = 0; firstIndex < text.Length; firstIndex++) { for (int secondIndex = 0; secondIndex < searchText.Length; secondIndex++) { matches[firstIndex, secondIndex] = searchText[secondIndex] == text[firstIndex] ? true : false; } } // use this table to get all the possible matches List <List <int> > allMatches = FuzzyMatching.GetAllMatchIndexes(matches); // return the score that is the max int maxScore = allMatches.Count > 0 ? FuzzyMatching.CalculateScoreForMatches(allMatches[0]) : 0; List <int> bestMatch = allMatches.Count > 0 ? allMatches[0] : new List <int>(); foreach (var match in allMatches) { int score = FuzzyMatching.CalculateScoreForMatches(match); if (score > maxScore) { bestMatch = match; maxScore = score; } } return(bestMatch); }