Utils: Fix GetSearchScore for multiple occurances of keyword in haystack
This commit is contained in:
@ -193,28 +193,27 @@ namespace Elwig.Helpers {
|
||||
if (!searchKeywords.Any())
|
||||
return 0;
|
||||
|
||||
return words
|
||||
.Select(w => {
|
||||
return searchKeywords
|
||||
.Select(k => {
|
||||
k = k.ToLower();
|
||||
var scores = words.Select(w => {
|
||||
w = w?.ToLower();
|
||||
var p = w?.ToLower()?.Split(" ");
|
||||
if (w == null || p == null)
|
||||
if (w == null || p == null) {
|
||||
return 0;
|
||||
|
||||
var t1 = searchKeywords.Where(f => w == f).Select(f => f.Length).OrderDescending().FirstOrDefault(0);
|
||||
var t2 = searchKeywords.Where(f => p.Any(a => a == f)).Select(f => f.Length).OrderDescending().FirstOrDefault(0);
|
||||
var t3 = searchKeywords.Where(f => p.Any(a => a.StartsWith(f))).Select(f => f.Length).OrderDescending().FirstOrDefault(0);
|
||||
var t4 = searchKeywords.Where(f => w.Contains(f)).Select(f => f.Length).OrderDescending().FirstOrDefault(0);
|
||||
if (t1 > 0) {
|
||||
return 4 + t1;
|
||||
} else if (t2 > 0) {
|
||||
return 3 + t2;
|
||||
} else if (t3 > 0) {
|
||||
return 2 + t3;
|
||||
} else if (t4 > 0) {
|
||||
return 1 + t4;
|
||||
} else if (k == w) {
|
||||
return 4 + k.Length;
|
||||
} else if (p.Any(a => a == k)) {
|
||||
return 3 + k.Length;
|
||||
} else if (p.Any(a => a.StartsWith(k))) {
|
||||
return 2 + k.Length;
|
||||
} else if (w.Contains(k)) {
|
||||
return 1 + k.Length;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
return scores.Max() + scores.Count(s => s > 0);
|
||||
})
|
||||
.Sum();
|
||||
}
|
||||
|
Reference in New Issue
Block a user