stan44 e0f298ba36 Add LyricFlow .NET backend API and Python bridge integration
- introduce `LyricFlow.Core.Backend` with shared DTOs, rhyme/spellcheck engines, and REST endpoints
- wire Python GUI/core to run and call the backend via new bridge/client modules
- add backend parity/integration tests and update packaging/ignore settings
2026-03-15 01:44:56 -05:00

148 lines
4.6 KiB
C#

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<string> EnumerateCandidates(string? configuredPath, string envVar, string[] resourceSegments)
{
var roots = new List<string>();
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<string> EnumerateZipCandidates(string? configuredPath, string envVar)
{
var roots = new List<string>();
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
}