// ============================================================================
// RynthAi - Features/FeatureSearch.cs
// The Find a Feature search (pure; RynthAiHostTests' FeatureSearchTests).
//
// Every typed word must match somewhere in the entry: its name, keywords,
// summary, where paths, command / open, group or plugin. Case doesn't matter;
// a word matches a word of the entry it is the start of ("vend" finds Vendors),
// after light stemming on both sides (a plural s, -ing, -ed: "dying" stays,
// "deaths" finds Death, "looting" finds Loot). Ranked by where each word hit:
// the name over the keywords over the command, group, summary and where. No
// words: every entry, grouped by its group.
// ============================================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace RynthCore.Plugin.RynthAi.Features;
/// One result: the entry and its score (higher first).
internal readonly record struct FeatureHit(FeatureEntry Entry, int Score);
/// One group of the browse view (no words typed): its name and its entries by name.
internal sealed record FeatureGroup(string Name, List Entries);
internal static class FeatureSearch
{
// Weights per field a word hit in (the best one counts for that word).
public const int NameWeight = 100, KeywordWeight = 60, CommandWeight = 45, GroupWeight = 40,
SummaryWeight = 30, WhereWeight = 25, PluginWeight = 20;
/// A word that is a whole word of the field (not just its start) scores this much more.
public const int WholeWordBonus = 10;
/// The schema's groups, in the order the browse view shows them (others after, by name).
public static readonly string[] GroupOrder =
{
"Help", "Windows", "Combat", "Buffs", "Hunting", "Navigation", "Looting", "Vendors", "Inventory", "Crafting",
"Metas", "Character", "Quests", "Chat", "HUD", "Death", "Fellowship", "Pets", "Scripts", "Server", "Settings",
};
/// The query's words, lower case, without punctuation ("/ra hunt" is "ra", "hunt").
public static List Words(string? text)
{
var words = new List();
if (string.IsNullOrEmpty(text)) return words;
var sb = new StringBuilder();
foreach (char ch in text)
{
if (char.IsLetterOrDigit(ch)) sb.Append(char.ToLowerInvariant(ch));
else if (ch == '\'' || ch == '’') continue; // "don't" is "dont"
else Flush(sb, words);
}
Flush(sb, words);
return words;
}
private static void Flush(StringBuilder sb, List words)
{
if (sb.Length == 0) return;
words.Add(sb.ToString());
sb.Clear();
}
/// Light stemming: a plural s ("vendors"), -ing ("looting"), -ed ("looted"), a doubled last letter after them ("stopped").
public static string Stem(string word)
{
string w = word;
if (w.Length > 5 && w.EndsWith("ing", StringComparison.Ordinal)) w = Undouble(w[..^3]);
else if (w.Length > 4 && w.EndsWith("ed", StringComparison.Ordinal)) w = Undouble(w[..^2]);
else if (w.Length > 4 && w.EndsWith("ies", StringComparison.Ordinal)) w = w[..^3] + "y";
else if (w.Length > 3 && w.EndsWith('s') && !w.EndsWith("ss", StringComparison.Ordinal) && !w.EndsWith("us", StringComparison.Ordinal)) w = w[..^1];
return w;
}
/// "stopp" is "stop"; "kill", "pass" and "buzz" keep their double letter.
private static string Undouble(string w) =>
w.Length >= 3 && w[^1] == w[^2] && !IsVowel(w[^1]) && w[^1] is not ('l' or 's' or 'z') ? w[..^1] : w;
private static bool IsVowel(char c) => c is 'a' or 'e' or 'i' or 'o' or 'u';
///
/// How well the typed word matches the field word :
/// 0 no match, 1 the start of it, 2 the whole word (after stemming).
///
public static int WordMatch(string q, string w)
{
if (q.Length == 0 || w.Length == 0) return 0;
string sq = Stem(q), sw = Stem(w);
if (sq == sw || q == w) return 2;
if (sw.StartsWith(sq, StringComparison.Ordinal) || w.StartsWith(q, StringComparison.Ordinal)) return 1;
return 0;
}
/// The best match of against any word of .
private static int FieldMatch(string q, List field)
{
int best = 0;
foreach (string w in field)
{
int m = WordMatch(q, w);
if (m > best) { best = m; if (best == 2) break; }
}
return best;
}
/// An entry's words, by field (built per search; the catalog is a few hundred entries).
private sealed class Fields
{
public readonly List Name, Keywords, Command, Group, Summary, Where, Plugin;
public Fields(FeatureEntry e)
{
Name = Words(e.Name);
Keywords = new List();
foreach (string k in e.Keywords) Keywords.AddRange(Words(k));
Command = Words(e.Command);
Command.AddRange(Words(e.Open));
Group = Words(e.Group);
Summary = Words(e.Summary);
Where = new List();
foreach (string p in e.Where) Where.AddRange(Words(p));
Plugin = Words(e.Plugin);
}
}
/// The score of for , or 0 when a word matches nowhere.
public static int Score(FeatureEntry e, IReadOnlyList words)
{
var f = new Fields(e);
int total = 0;
foreach (string q in words)
{
int best = 0;
best = Math.Max(best, Weigh(FieldMatch(q, f.Name), NameWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Keywords), KeywordWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Command), CommandWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Group), GroupWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Summary), SummaryWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Where), WhereWeight));
best = Math.Max(best, Weigh(FieldMatch(q, f.Plugin), PluginWeight));
if (best == 0) return 0; // every word must match somewhere
total += best;
}
return total;
}
private static int Weigh(int match, int weight) => match == 0 ? 0 : match == 2 ? weight + WholeWordBonus : weight;
///
/// The entries every word of matches, best first (ties by name). No words:
/// every entry in browse order (by group, then name), each with score 0.
///
public static List Search(IReadOnlyList all, string? query)
{
List words = Words(query);
var hits = new List();
if (words.Count == 0)
{
foreach (FeatureGroup g in Grouped(all))
foreach (FeatureEntry e in g.Entries) hits.Add(new FeatureHit(e, 0));
return hits;
}
foreach (FeatureEntry e in all)
{
int s = Score(e, words);
if (s > 0) hits.Add(new FeatureHit(e, s));
}
hits.Sort((a, b) => a.Score != b.Score ? b.Score.CompareTo(a.Score) : CompareNames(a.Entry, b.Entry));
return hits;
}
/// Every entry by group (the schema's order, then others by name; no group is "Other"), each group by name.
public static List Grouped(IReadOnlyList all)
{
var byName = new Dictionary(StringComparer.OrdinalIgnoreCase);
foreach (FeatureEntry e in all)
{
string g = e.Group.Trim().Length > 0 ? e.Group.Trim() : "Other";
if (!byName.TryGetValue(g, out FeatureGroup? group)) byName[g] = group = new FeatureGroup(g, new List());
group.Entries.Add(e);
}
var groups = new List(byName.Values);
groups.Sort((a, b) =>
{
int ia = GroupRank(a.Name), ib = GroupRank(b.Name);
return ia != ib ? ia.CompareTo(ib) : string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
});
foreach (FeatureGroup g in groups) g.Entries.Sort(CompareNames);
return groups;
}
private static int GroupRank(string g)
{
int i = Array.FindIndex(GroupOrder, x => x.Equals(g, StringComparison.OrdinalIgnoreCase));
return i >= 0 ? i : GroupOrder.Length;
}
private static int CompareNames(FeatureEntry a, FeatureEntry b)
{
int c = string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
return c != 0 ? c : string.Compare(a.Plugin, b.Plugin, StringComparison.OrdinalIgnoreCase);
}
/// "12 features match 'death'", "1 feature matches 'death'", "No feature matches 'xyz'", "40 features".
public static string CountLine(int count, string? query)
{
string q = (query ?? string.Empty).Trim();
if (Words(q).Count == 0) return count == 1 ? "1 feature" : $"{count} features";
return count switch
{
0 => $"No feature matches '{q}'",
1 => $"1 feature matches '{q}'",
_ => $"{count} features match '{q}'",
};
}
}