Exemplo n.º 1
0
        protected void MatchChar(int patternIndex, int offset)
        {
            indices.Push(offset);

            int distance = 0;

            if (indices.Count > 1)
            {
                distance = offset - indices.ElementAt(1);
            }

            int newScore = 0;

            if (distance == 0)
            {
                consecutiveMatchCounts.Push(0);
            }
            else if (distance == 1)
            {
                // Consecutive match.
                var consecutiveCount = consecutiveMatchCounts.ElementAt(0) + 1;
                var consecutiveScore = consecutiveCount * scoreConfig.ConsecutiveMatchBonus;
                consecutiveMatchCounts.Push(consecutiveCount);

                newScore = scores.ElementAt(0) + consecutiveScore;
            }
            else
            {
                consecutiveMatchCounts.Push(0);

                newScore = scores.ElementAt(0) - ((distance - 1) * scoreConfig.DistancePenalty);
            }

            scores.Push(newScore);

            int maxScore = FuzzySearch.CalculateMaxScore(
                pattern.Length - (indices.Count - 1),
                consecutiveMatchCounts.ElementAt(0),
                scoreConfig
                );

            if (newScore + maxScore < bestMatch.Score)
            {
                PopAll();
                return;
            }

            if (patternIndex >= pattern.Length)
            {
                ScoreCurrent();
                PopAll();
                return;
            }
            char patternChar = pattern[patternIndex];

            var occurences = GetOccurences(patternChar, charMap, offset + 1);

            if (occurences == null || occurences.Count == 0)
            {
                PopAll();
                return;
            }

            foreach (var o in occurences)
            {
                MatchChar(patternIndex + 1, o);
            }

            PopAll();
        }
Exemplo n.º 2
0
 public static int CalculateMaxScore(int count, ScoreConfig config)
 {
     return(FuzzySearch.CalculateMaxScore(count, 0, config));
 }
Exemplo n.º 3
0
 public FuzzySearch(String pattern, String target)
 {
     this.pattern = Regex.Replace(pattern, @"\s+", "");
     charMap      = FuzzySearch.BuildCharMap(target);
 }