From 9efa4f9e3213c513871e1ad36d198f29cd55cc99 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Tue, 28 Jan 2025 12:57:52 +0330 Subject: [PATCH] stats for poems couplet counts in categories #464 --- .../GanjoorService-Stats.cs | 71 +++++++++++++++---- 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Stats.cs b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Stats.cs index 77e8eb2b..2d2f9ef0 100644 --- a/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Stats.cs +++ b/RMuseum/Services/Implementation/GanjoorService-Partials/GanjoorService-Stats.cs @@ -83,7 +83,7 @@ namespace RMuseum.Services.Implementation Dictionary metreCounts = new Dictionary(); int secondMetreCoupletCount = 0; - Dictionary coupletCounts = new Dictionary(); + Dictionary groupedCoupletCounts = new Dictionary(); foreach (var section in wholePoemSections) { int coupletCount = await context.GanjoorVerses.AsNoTracking() @@ -103,7 +103,7 @@ namespace RMuseum.Services.Implementation ) ).CountAsync();//GanjoorPoemSection.CoupletsCount added later - if (coupletCounts.TryGetValue(coupletCount, out int groupedCoupletCount)) + if (groupedCoupletCounts.TryGetValue(coupletCount, out int groupedCoupletCount)) { groupedCoupletCount++; } @@ -111,7 +111,7 @@ namespace RMuseum.Services.Implementation { groupedCoupletCount = 1; } - coupletCounts[coupletCount] = groupedCoupletCount; + groupedCoupletCounts[coupletCount] = groupedCoupletCount; var metreId = section.GanjoorMetreId == null ? 0 : (int)section.GanjoorMetreId; if (metreCounts.TryGetValue(metreId, out int sectionCoupletCount)) @@ -326,7 +326,7 @@ namespace RMuseum.Services.Implementation List coupletCountsList = new List(); - foreach (var coupletCount in coupletCounts) + foreach (var coupletCount in groupedCoupletCounts) { coupletCountsList.Add(new SectionCoupletCount() { CoupletCount = coupletCount.Key, Count = coupletCount.Value }); } @@ -395,11 +395,12 @@ namespace RMuseum.Services.Implementation var wholePoemSections = await context.GanjoorPoemSections.Include(v => v.Poem).ThenInclude(p => p.Cat).ThenInclude(c => c.Poet).AsNoTracking() .Where(s => catIdList.Contains(s.Poem.CatId) && (string.IsNullOrEmpty(s.Language) || s.Language == "fa-IR") && s.Poem.Cat.Poet.Published && s.SectionType == PoemSectionType.WholePoem) - .Select(s => new { s.PoemId, s.Index, s.GanjoorMetreId, Versetype = s.VerseType }) + .Select(s => new { s.PoemId, s.Index, s.GanjoorMetreId, s.VerseType }) .ToListAsync(); Dictionary metreCounts = new Dictionary(); int secondMetreCoupletCount = 0; + Dictionary groupedCoupletCounts = new Dictionary(); foreach (var section in wholePoemSections) { int coupletCount = await context.GanjoorVerses.AsNoTracking() @@ -409,15 +410,25 @@ namespace RMuseum.Services.Implementation (v.VersePosition == VersePosition.Right || v.VersePosition == VersePosition.CenteredVerse1) && ( - (section.Versetype == VersePoemSectionType.First && v.SectionIndex1 == section.Index) + (section.VerseType == VersePoemSectionType.First && v.SectionIndex1 == section.Index) || - (section.Versetype == VersePoemSectionType.Second && v.SectionIndex2 == section.Index) + (section.VerseType == VersePoemSectionType.Second && v.SectionIndex2 == section.Index) || - (section.Versetype == VersePoemSectionType.Third && v.SectionIndex3 == section.Index) + (section.VerseType == VersePoemSectionType.Third && v.SectionIndex3 == section.Index) || - (section.Versetype == VersePoemSectionType.Forth && v.SectionIndex4 == section.Index) + (section.VerseType == VersePoemSectionType.Forth && v.SectionIndex4 == section.Index) ) - ).CountAsync(); + ).CountAsync();//GanjoorPoemSection.CoupletsCount added later + if (groupedCoupletCounts.TryGetValue(coupletCount, out int groupedCoupletCount)) + { + groupedCoupletCount++; + } + else + { + groupedCoupletCount = 1; + } + groupedCoupletCounts[coupletCount] = groupedCoupletCount; + var metreId = section.GanjoorMetreId == null ? 0 : (int)section.GanjoorMetreId; if (metreCounts.TryGetValue(metreId, out int sectionCoupletCount)) { @@ -428,7 +439,7 @@ namespace RMuseum.Services.Implementation sectionCoupletCount = coupletCount; } metreCounts[metreId] = sectionCoupletCount; - if (metreId != 0 && section.Versetype != VersePoemSectionType.First) + if (metreId != 0 && section.VerseType != VersePoemSectionType.First) { secondMetreCoupletCount += coupletCount; } @@ -640,10 +651,46 @@ namespace RMuseum.Services.Implementation } htmlText += $"{Environment.NewLine}"; + List coupletCountsList = new List(); + foreach (var coupletCount in groupedCoupletCounts) + { + coupletCountsList.Add(new SectionCoupletCount() { CoupletCount = coupletCount.Key, Count = coupletCount.Value }); + } + coupletCountsList.Sort((a, b) => b.Count - a.Count); + if (coupletCountsList.Count > 0) + { + htmlText += $"

آمار فراوانی تعداد ابیات اشعار این بخش به شرح زیر است (این آمار برای مثنوی‌ها که بخش‌بندی آنها ممکن است به سلیقهٔ گردآورنده صورت گرفته باشد):

{Environment.NewLine}"; + + htmlText += $"{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}" + + $"{Environment.NewLine}"; + + for (int i = 0; i < coupletCountsList.Count; i++) + { + if (coupletCountsList[i].Count == 0) continue; + if (i % 2 == 0) + htmlText += $"{Environment.NewLine}"; + else + htmlText += $"{Environment.NewLine}"; + + htmlText += $"{Environment.NewLine}"; + htmlText += $"{Environment.NewLine}"; + htmlText += $"{Environment.NewLine}"; + htmlText += $"{Environment.NewLine}"; + + htmlText += $"{Environment.NewLine}"; + } + htmlText += $"
ردیفتعداد ابیات شعرفراوانیدرصد از کل
{(i + 1).ToPersianNumbers()}{coupletCountsList[i].CoupletCount.ToPersianNumbers()}{LanguageUtils.FormatMoney(coupletCountsList[i].Count)}{(coupletCountsList[i].Count * 100.0 / wholeCoupletsCount).ToString("N2", new CultureInfo("fa-IR")).ToPersianNumbers()}
{Environment.NewLine}"; + } + //آمار شعرهای زیربخشها - + var subCats = await context.GanjoorCategories.AsNoTracking().Where(c => c.ParentId == catId).ToListAsync(); subCats.Add(await context.GanjoorCategories.AsNoTracking().Where(c => c.Id == catId).SingleAsync());