예제 #1
0
        public double Score(string subject, string query, StringScorerOptions options)
        {
            if (!(options.AllowErrors || IsMatch(subject, options.PreparedQuery.CoreLw, options.PreparedQuery.CoreUp)))
            {
                return(0);
            }

            var stringLw = subject.ToLower();
            var score    = ComputeScore(subject, stringLw, options.PreparedQuery);

            return(Math.Ceiling(score));
        }
예제 #2
0
        private double ScorePath(string subject, string subjectLw, double score, StringScorerOptions options)
        {
            if (score == 0)
            {
                return(0);
            }

            var pathSeparator = Convert.ToChar(options.PathSeparator);
            var end           = subject.Length - 1;

            while (subject[end] == pathSeparator)
            {
                end--;
            }

            var basePos    = subject.LastIndexOf(pathSeparator, end);
            var fileLength = end - basePos;
            var extAdjust  = 1.0;

            if (options.UseExtensionBonus)
            {
                extAdjust += GetExtensionScore(subjectLw, options.PreparedQuery.Ext, basePos, end, 2);
                score     *= extAdjust;
            }

            if (basePos == -1)
            {
                return(score);
            }

            var depth = options.PreparedQuery.Depth;

            while (basePos > -1 && depth-- > 0)
            {
                basePos = subject.LastIndexOf(options.PathSeparator, basePos - 1);
            }

            double basePathScore = 0.0;

            if (basePos == -1)
            {
                basePathScore = score;
            }
            else
            {
                basePathScore = extAdjust * Scorer.ComputeScore(subject.Slice(basePos + 1, end + 1), subjectLw.Slice(basePos + 1, end + 1), options.PreparedQuery);
            }

            var alpha = 0.5 * CONST_TAU_DEPTH / (CONST_TAU_DEPTH + ScorerUtil.CountDir(subject, end + 1, Convert.ToChar(options.PathSeparator)));

            return(alpha * basePathScore + (1 - alpha) * score * Scorer.ScoreSize(0, CONST_FILE_COEFF * (fileLength)));
        }
예제 #3
0
        public void ReturnMostAccurateLimitedResultTest()
        {
            var opts       = new StringScorerOptions(maxResults: 1);
            var candidates = new List <string> {
                "GruntFile", "filter", "bile"
            };

            var result = StringScorer.Filter(candidates, "file", opts);
            var words  = result.Select(x => x.Word).ToList();

            Assert.Single(result);
            Assert.Equal(new [] { "GruntFile" }, words);
        }
예제 #4
0
        public static HighlightedResult Wrap(string subject, string query, StringScorerOptions options)
        {
            var highlight = new int[subject.Length];

            if (subject == query)
            {
                int j = -1;
                while (++j < highlight.Length)
                {
                    highlight[j] = j;
                }
                return(new HighlightedResult(subject, highlight));
            }

            var matchPositions = Match(subject, query, options);

            return(new HighlightedResult(subject, matchPositions));
        }
예제 #5
0
        public static int[] Match(string subject, string query, StringScorerOptions options)
        {
            if (!(options.AllowErrors || Scorer.IsMatch(subject, options.PreparedQuery.CoreLw, options.PreparedQuery.CoreUp)))
            {
                return(new int[] { });
            }

            var string_lw = subject.ToLower();
            var matches   = ComputeMatch(subject, string_lw, options.PreparedQuery);

            if (matches.Length == 0)
            {
                return(matches);
            }

            if (subject.IndexOf(options.PathSeparator) > -1)
            {
                var baseMatches = BasenameMatch(subject, string_lw, options.PreparedQuery, options.PathSeparator);
                matches = MergeMatches(matches, baseMatches);
            }

            return(matches);
        }
예제 #6
0
        public static IResultCandidate[] FilterCandidates(ICandidate[] candidates, string query,
                                                          StringScorerOptions options)
        {
            var scoredCandidates = new List <ResultCandidate>();

            var spotLeft = candidates.Length + 1;

            if (options.MaxInners.HasValue && options.MaxInners > 0)
            {
                spotLeft = options.MaxInners.Value;
            }

            for (var i = 0; i < candidates.Length; i++)
            {
                double score = 0.0;

                score = options.ScorerEngine.Score(candidates[i].Word, query, options);

                if (score > 0)
                {
                    scoredCandidates.Add(new ResultCandidate(candidates[i], score));
                    if (--spotLeft == 0)
                    {
                        break;
                    }
                }
            }

            var result = scoredCandidates.OrderByDescending(x => x.Score);

            if (options.MaxResults.HasValue)
            {
                return(result.Take(options.MaxResults.Value).ToArray());
            }

            return(result.ToArray());
        }
예제 #7
0
 private IResultSubject[] Filter(List <string> candidates, string query, StringScorerOptions options = null)
 {
     return(StringScorer.Filter(candidates, query, options));
 }
예제 #8
0
 private string ReturnBestMatch(List <string> candidates, string query, StringScorerOptions options = null)
 {
     return(Filter(candidates, query, options)[0].Word);
 }