docs: #317 finding poem rhythms according to sections

This commit is contained in:
Hamid Reza Mohammadi 2022-05-05 18:06:04 +04:30
parent c850fd756b
commit 70278d80c6
3 changed files with 61 additions and 19 deletions

View File

@ -13631,6 +13631,14 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.FilterSectionVerses(RMuseum.Models.Ganjoor.GanjoorPoemSection,System.Collections.Generic.List{RMuseum.Models.Ganjoor.GanjoorVerse})">
<summary>
filter secion verses
</summary>
<param name="section"></param>
<param name="verses"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetCoupletSectionsAsync(System.Int32,System.Int32)">
<summary>
get couplet sections

View File

@ -30,36 +30,63 @@ namespace RMuseum.Services.Implementation
GanjoorMetre preDeterminedMetre = string.IsNullOrEmpty(rhythm) ? null : metres.Where(m => m.Rhythm == rhythm).Single();
var poems = await context.GanjoorPoems.Where(p => p.CatId == catId).ToListAsync();
var poems = await context.GanjoorPoems.AsNoTracking().Where(p => p.CatId == catId).ToListAsync();
int i = 0;
using (HttpClient httpClient = new HttpClient())
{
foreach (var poem in poems)
{
if (retag || poem.GanjoorMetreId == null)
var sections = await context.GanjoorPoemSections.Where(p => p.PoemId == poem.Id).ToListAsync();
foreach (var section in sections.Where(s => s.GanjoorMetreRefSectionIndex == null).ToList())
{
await jobProgressServiceEF.UpdateJob(job.Id, i++);
if (preDeterminedMetre == null)
if (retag || section.GanjoorMetreId == null)
{
var res = await _FindPoemRhythm(poem.Id, context, httpClient, rhythms);
if (!string.IsNullOrEmpty(res.Result))
await jobProgressServiceEF.UpdateJob(job.Id, i++);
if (preDeterminedMetre == null)
{
poem.GanjoorMetreId = metres.Where(m => m.Rhythm == res.Result).Single().Id;
context.GanjoorPoems.Update(poem);
var res = await _FindSectionRhythm(section, context, httpClient, rhythms);
if (!string.IsNullOrEmpty(res.Result))
{
section.GanjoorMetreId = metres.Where(m => m.Rhythm == res.Result).Single().Id;
context.GanjoorPoemSections.Update(section);
await context.SaveChangesAsync();
}
}
else
{
section.GanjoorMetreId = preDeterminedMetre.Id;
context.GanjoorPoemSections.Update(section);
await context.SaveChangesAsync();
}
}
else
{
poem.GanjoorMetreId = preDeterminedMetre.Id;
context.GanjoorPoems.Update(poem);
await context.SaveChangesAsync();
}
if (poem.GanjoorMetreId != null && !string.IsNullOrEmpty(poem.RhymeLetters))
{
await _UpdateRelatedPoems(context, (int)poem.GanjoorMetreId, poem.RhymeLetters);
if(section.GanjoorMetreId != null)
{
var dependentSections = sections.Where(s => s.GanjoorMetreRefSectionIndex == section.Id).ToList();
foreach (var dsection in dependentSections)
{
dsection.GanjoorMetreId = section.GanjoorMetreId;
context.GanjoorPoemSections.Update(dsection);
await context.SaveChangesAsync();
}
}
if (section.GanjoorMetreId != null && !string.IsNullOrEmpty(section.RhymeLetters))
{
await _UpdateRelatedSections(context, (int)section.GanjoorMetreId, section.RhymeLetters);
}
if (section.GanjoorMetreId != null)
{
var dependentSections = sections.Where(s => s.GanjoorMetreRefSectionIndex == section.Id).ToList();
foreach (var dsection in dependentSections)
{
if(dsection.GanjoorMetreId != null && !string.IsNullOrEmpty(dsection.RhymeLetters))
{
await _UpdateRelatedSections(context, (int)dsection.GanjoorMetreId, dsection.RhymeLetters);
}
}
}
}
}
}

View File

@ -3149,11 +3149,18 @@ namespace RMuseum.Services.Implementation
var metres = (await GetGanjoorMetres()).Result.Select(m => m.Rhythm).ToArray();
return await _FindPoemRhythm(id, _context, _httpClient, metres);
}
private async Task<RServiceResult<string>> _FindPoemRhythm(int id, RMuseumDbContext context, HttpClient httpClient, string[] metres, bool alwaysReturnaAResult = false)
{
var section = await context.GanjoorPoemSections.AsNoTracking().Where(s => s.PoemId == id && s.SectionType == PoemSectionType.WholePoem && s.VerseType == VersePoemSectionType.First).OrderBy(s => s.Index).FirstOrDefaultAsync();
return await _FindSectionRhythm(section, context, httpClient, metres, alwaysReturnaAResult);
}
private async Task<RServiceResult<string>> _FindSectionRhythm(GanjoorPoemSection section, RMuseumDbContext context, HttpClient httpClient, string[] metres, bool alwaysReturnaAResult = false)
{
try
{
var verses = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == id).OrderBy(v => v.VOrder).ToListAsync();
var poemVerses = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == section.PoemId).OrderBy(v => v.VOrder).ToListAsync();
var verses = FilterSectionVerses(section, poemVerses);
Dictionary<string, int> rhytmCounter = new Dictionary<string, int>();