using Core.Entities;
using FuzzySharp;
namespace Core.Parsers;
///
/// Fuzzy-matches a CSV or pasted name to existing students.
///
public static class FuzzyStudentMatcher
{
public const int MatchThreshold = 90;
public static (Student Student, int Score)? Find(ICollection students, string name)
{
var ranked = students
.Select(s => (Student: s, Score: Score(s, name)))
.Where(x => x.Score >= MatchThreshold)
.OrderByDescending(x => x.Score)
.ToList();
return ranked.Count == 0 ? null : ranked[0];
}
public static int Score(Student student, string name)
{
var candidates = new[] { student.Name, student.FirstNameLastName, student.LastNameFirstName };
return candidates.Max(candidate => Math.Max(Fuzz.Ratio(candidate, name), Fuzz.TokenSetRatio(candidate, name)));
}
}