namespace LyricFlow.Core.Services; public static class NltkPathResolver { // MARK: - Public Resolution #region Public Resolution public static string? ResolveCmudictPath(string? configuredPath = null) { return ResolveResourceFile(configuredPath, "LYRICFLOW_CMUDICT_PATH", ["cmudict", "cmudict"]); } public static string? ResolveWordNetPath(string? configuredPath = null) { foreach (var candidate in EnumerateCandidates(configuredPath, "LYRICFLOW_WORDNET_PATH", ["wordnet"])) { if (Directory.Exists(candidate)) { return candidate; } } foreach (var candidate in EnumerateZipCandidates(configuredPath, "LYRICFLOW_WORDNET_PATH")) { if (File.Exists(candidate)) { return candidate; } } return null; } #endregion // MARK: - Shared Resolution Helpers #region Shared Resolution Helpers private static string? ResolveResourceFile(string? configuredPath, string envVar, string[] resourceSegments) { foreach (var candidate in EnumerateCandidates(configuredPath, envVar, resourceSegments)) { if (File.Exists(candidate)) { return candidate; } } return null; } #endregion // MARK: - Candidate Enumeration #region Candidate Enumeration private static IEnumerable EnumerateCandidates(string? configuredPath, string envVar, string[] resourceSegments) { var roots = new List(); if (!string.IsNullOrWhiteSpace(configuredPath)) { roots.Add(configuredPath); } var envPath = Environment.GetEnvironmentVariable(envVar); if (!string.IsNullOrWhiteSpace(envPath)) { roots.Add(envPath); } var nltkDataEnv = Environment.GetEnvironmentVariable("NLTK_DATA"); if (!string.IsNullOrWhiteSpace(nltkDataEnv)) { roots.AddRange( nltkDataEnv.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select(root => Path.Combine(root, "corpora", Path.Combine(resourceSegments))) ); } var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); if (!string.IsNullOrWhiteSpace(appData)) { roots.Add(Path.Combine(appData, "nltk_data", "corpora", Path.Combine(resourceSegments))); } var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (!string.IsNullOrWhiteSpace(home)) { roots.Add(Path.Combine(home, "nltk_data", "corpora", Path.Combine(resourceSegments))); } roots.Add(Path.Combine(AppContext.BaseDirectory, "nltk_data", "corpora", Path.Combine(resourceSegments))); return roots .Where(root => !string.IsNullOrWhiteSpace(root)) .Select(Path.GetFullPath); } #endregion // MARK: - Zip Candidates #region Zip Candidates private static IEnumerable EnumerateZipCandidates(string? configuredPath, string envVar) { var roots = new List(); if (!string.IsNullOrWhiteSpace(configuredPath)) { roots.Add(configuredPath); } var envPath = Environment.GetEnvironmentVariable(envVar); if (!string.IsNullOrWhiteSpace(envPath)) { roots.Add(envPath); } var nltkDataEnv = Environment.GetEnvironmentVariable("NLTK_DATA"); if (!string.IsNullOrWhiteSpace(nltkDataEnv)) { roots.AddRange( nltkDataEnv.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Select(root => Path.Combine(root, "corpora", "wordnet.zip")) ); } var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); if (!string.IsNullOrWhiteSpace(appData)) { roots.Add(Path.Combine(appData, "nltk_data", "corpora", "wordnet.zip")); } var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (!string.IsNullOrWhiteSpace(home)) { roots.Add(Path.Combine(home, "nltk_data", "corpora", "wordnet.zip")); } roots.Add(Path.Combine(AppContext.BaseDirectory, "nltk_data", "corpora", "wordnet.zip")); return roots .Where(root => !string.IsNullOrWhiteSpace(root)) .Select(Path.GetFullPath); } #endregion }