Add TeamClipboardMatcher utility and corresponding tests for fuzzy team name matching

This commit introduces the TeamClipboardMatcher class, which provides functionality to match team names from clipboard text using fuzzy matching techniques. The class includes methods for extracting team names and finding the best match based on a specified threshold. Additionally, comprehensive unit tests are added in TeamClipboardMatcher_Tests to validate various matching scenarios, including exact matches, fuzzy matches, and handling of different clipboard formats. This enhancement improves the application's ability to efficiently match teams from user input.
This commit is contained in:
2026-01-20 22:49:09 -05:00
parent 455be30821
commit 48861eb6a6
4 changed files with 597 additions and 24 deletions
+91
View File
@@ -0,0 +1,91 @@
using Core.Entities;
using FuzzySharp;
namespace Core.Utility;
/// <summary>
/// Utility class for matching team names from clipboard text using fuzzy matching.
/// </summary>
public static class TeamClipboardMatcher
{
/// <summary>
/// Matches team names from clipboard text against available teams using fuzzy matching.
/// </summary>
/// <param name="clipboardText">The text content from the clipboard.</param>
/// <param name="availableTeams">The collection of available teams to match against.</param>
/// <param name="matchThreshold">The minimum fuzzy match score threshold (0-100). Default is 85.</param>
/// <returns>A list of teams that match the clipboard text, ordered by match quality.</returns>
public static List<Team> MatchTeamsFromClipboard(
string clipboardText,
IEnumerable<Team> availableTeams,
int matchThreshold = 85)
{
var matchedTeams = new List<Team>();
var teamsList = availableTeams.ToList();
if (string.IsNullOrWhiteSpace(clipboardText) || !teamsList.Any())
{
return matchedTeams;
}
// Split clipboard text by newlines
var lines = clipboardText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
// Extract team name portion (before " - " if present, as clipboard format includes student lists)
var teamName = ExtractTeamNameFromLine(line);
// Skip empty lines or lines that look like headers/metadata
if (ShouldSkipLine(teamName))
{
continue;
}
// Find best match using fuzzy matching
var matchedTeam = FindBestMatch(teamName, teamsList, matchThreshold);
if (matchedTeam != null && !matchedTeams.Any(t => t.Id == matchedTeam.Id))
{
matchedTeams.Add(matchedTeam);
}
}
return matchedTeams;
}
private static string ExtractTeamNameFromLine(string line)
{
var dashIndex = line.IndexOf(" - ");
if (dashIndex > 0)
{
return line.Substring(0, dashIndex).Trim();
}
return line.Trim();
}
private static bool ShouldSkipLine(string teamName)
{
return string.IsNullOrWhiteSpace(teamName) ||
teamName.StartsWith("--") ||
teamName.Equals("Unscheduled", StringComparison.OrdinalIgnoreCase);
}
private static Team? FindBestMatch(string teamName, List<Team> availableTeams, int matchThreshold)
{
// Normalize both strings to lowercase for case-insensitive comparison
var normalizedTeamName = teamName.ToLowerInvariant();
var bestMatch = availableTeams
.Select(team => new
{
Team = team,
Score = Fuzz.Ratio(normalizedTeamName, team.ToString().ToLowerInvariant())
})
.Where(x => x.Score >= matchThreshold)
.OrderByDescending(x => x.Score)
.FirstOrDefault();
return bestMatch?.Team;
}
}