tajik api fix

This commit is contained in:
Hamid Reza Mohammadi 2026-08-07 16:28:35 +03:30
parent 23cb549953
commit a6e7d4f4e6
2 changed files with 105 additions and 0 deletions

View File

@ -149,7 +149,17 @@ namespace RMuseum.Services.Implementation
var nextPoem = await _context.TajikPoems.AsNoTracking().Where(p => p.Id == page.Poem.Next.Id).SingleOrDefaultAsync(); var nextPoem = await _context.TajikPoems.AsNoTracking().Where(p => p.Id == page.Poem.Next.Id).SingleOrDefaultAsync();
if(nextPoem != null) if(nextPoem != null)
{ {
// real Tajik translation exists for the neighbouring poem - use it
page.Poem.Next.Title = nextPoem.TajikTitle; page.Poem.Next.Title = nextPoem.TajikTitle;
page.Poem.Next.Excerpt = ExtractExcerpt(nextPoem.TajikPlainText);
}
else
{
// no Tajik translation for this poem yet - fall back to a simple
// best-effort letter transliteration rather than showing raw
// Perso-Arabic script (see LanguageUtils.TransliteratePersianToTajik)
page.Poem.Next.Title = LanguageUtils.TransliteratePersianToTajik(page.Poem.Next.Title);
page.Poem.Next.Excerpt = LanguageUtils.TransliteratePersianToTajik(page.Poem.Next.Excerpt);
} }
} }
if (page.Poem.Previous != null) if (page.Poem.Previous != null)
@ -158,6 +168,12 @@ namespace RMuseum.Services.Implementation
if (prePoem != null) if (prePoem != null)
{ {
page.Poem.Previous.Title = prePoem.TajikTitle; page.Poem.Previous.Title = prePoem.TajikTitle;
page.Poem.Previous.Excerpt = ExtractExcerpt(prePoem.TajikPlainText);
}
else
{
page.Poem.Previous.Title = LanguageUtils.TransliteratePersianToTajik(page.Poem.Previous.Title);
page.Poem.Previous.Excerpt = LanguageUtils.TransliteratePersianToTajik(page.Poem.Previous.Excerpt);
} }
} }
} }
@ -169,6 +185,16 @@ namespace RMuseum.Services.Implementation
} }
} }
/// <summary>
/// short plain-text excerpt (first non-empty line) for a Tajik poem's next/previous nav link
/// </summary>
private static string ExtractExcerpt(string plainText)
{
if (string.IsNullOrWhiteSpace(plainText)) return "";
var firstLine = plainText.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? "";
return firstLine.Length > 80 ? firstLine.Substring(0, 80) + "…" : firstLine;
}
/// <summary> /// <summary>

View File

@ -92,6 +92,85 @@ namespace RMuseum.Services.Implementation
; ;
} }
/// <summary>
/// simple, best-effort Perso-Arabic -> Tajik Cyrillic letter substitution.
/// NOT a linguistically correct transliteration: Perso-Arabic script does not
/// write short vowels, so a letter-by-letter map cannot recover them (that's
/// why a dedicated tool like persian-tajik.ir exists, and why the bulk of
/// TajikGanjoor's content comes from real TajikPoems/TajikCats/TajikPoets rows
/// rather than on-the-fly conversion). This exists only as a fallback for
/// spots - like a neighbouring poem's excerpt on the next/previous nav - that
/// don't have a proper Tajik translation in the database yet, so at least
/// something in Cyrillic is shown instead of raw Perso-Arabic script.
/// </summary>
private static readonly Dictionary<char, string> _persianToTajikMap = new Dictionary<char, string>()
{
['آ'] = "о",
['ا'] = "о",
['ب'] = "б",
['پ'] = "п",
['ت'] = "т",
['ث'] = "с",
['ج'] = "ҷ",
['چ'] = "ч",
['ح'] = "ҳ",
['خ'] = "х",
['د'] = "д",
['ذ'] = "з",
['ر'] = "р",
['ز'] = "з",
['ژ'] = "ж",
['س'] = "с",
['ش'] = "ш",
['ص'] = "с",
['ض'] = "з",
['ط'] = "т",
['ظ'] = "з",
['ع'] = "ъ",
['غ'] = "ғ",
['ف'] = "ф",
['ق'] = "қ",
['ک'] = "к",
['ك'] = "к",
['گ'] = "г",
['ل'] = "л",
['م'] = "м",
['ن'] = "н",
['و'] = "в",
['ه'] = "ҳ",
['ی'] = "ӣ",
['ي'] = "ӣ",
['ئ'] = "ъ",
['ؤ'] = "в",
['ء'] = "ъ",
['ة'] = "ат",
['؟'] = "?",
['،'] = ",",
['؛'] = ";",
['۰'] = "0",
['۱'] = "1",
['۲'] = "2",
['۳'] = "3",
['۴'] = "4",
['۵'] = "5",
['۶'] = "6",
['۷'] = "7",
['۸'] = "8",
['۹'] = "9",
};
public static string TransliteratePersianToTajik(string text)
{
if (string.IsNullOrEmpty(text)) return text;
text = CleanTextForTransileration(text);
var sb = new System.Text.StringBuilder(text.Length);
foreach (char c in text)
{
sb.Append(_persianToTajikMap.TryGetValue(c, out string mapped) ? mapped : c.ToString());
}
return sb.ToString();
}
public static string SameSoundLetters(string text) public static string SameSoundLetters(string text)
{ {