docs: #367 api for regenerating single couplets rhymes

This commit is contained in:
Hamid Reza Mohammadi 2023-03-09 21:38:07 +03:30
parent 1cbe489a71
commit 524477c7c0
5 changed files with 111 additions and 4 deletions

View File

@ -2437,6 +2437,24 @@ namespace RMuseum.Controllers
return BadRequest(res.ExceptionString);
}
/// <summary>
/// start finding rhymes
/// </summary>
/// <returns></returns>
[HttpPost]
[Route("singlecouplets/startfindingrhymes")]
[Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + RMuseumSecurableItem.ImportOperationShortName)]
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))]
[ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))]
public IActionResult StartFindingRhymesForSingleCouplets()
{
RServiceResult<bool> res =
_ganjoorService.StartFindingRhymesForSingleCouplets();
if (res.Result)
return Ok();
return BadRequest(res.ExceptionString);
}
/// <summary>
/// separate verses in poem.PlainText with Environment.NewLine instead of SPACE
/// </summary>

View File

@ -1594,6 +1594,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.StartFindingRhymesForSingleCouplets">
<summary>
start finding rhymes
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Controllers.GanjoorController.RegerneratePoemsPlainText(System.Int32)">
<summary>
separate verses in poem.PlainText with Environment.NewLine instead of SPACE
@ -12724,6 +12730,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.StartFindingRhymesForSingleCouplets">
<summary>
start finding rhymes for single couplets
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.IGanjoorService.RegerneratePoemsPlainText(System.Int32)">
<summary>
separate verses in poem.PlainText with Environment.NewLine instead of SPACE
@ -14848,6 +14860,12 @@
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartFindingRhymesForSingleCouplets">
<summary>
start finding rhymes for single couplets
</summary>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.TransferMeterAsync(System.Int32,System.Int32)">
<summary>
transfer poems and sections from a meter to another one

View File

@ -587,6 +587,12 @@ namespace RMuseum.Services
/// <returns></returns>
RServiceResult<bool> RegeneratePoemsFullTitles();
/// <summary>
/// start finding rhymes for single couplets
/// </summary>
/// <returns></returns>
RServiceResult<bool> StartFindingRhymesForSingleCouplets();
/// <summary>
/// separate verses in poem.PlainText with Environment.NewLine instead of SPACE
/// </summary>

View File

@ -728,5 +728,64 @@ namespace RMuseum.Services.Implementation
return new RServiceResult<bool>(true);
}
/// <summary>
/// start finding rhymes for single couplets
/// </summary>
/// <returns></returns>
public RServiceResult<bool> StartFindingRhymesForSingleCouplets()
{
_backgroundTaskQueue.QueueBackgroundWorkItem
(
async token =>
{
using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions<RMuseumDbContext>())) //this is long running job, so _context might be already been freed/collected by GC
{
LongRunningJobProgressServiceEF jobProgressServiceEF = new LongRunningJobProgressServiceEF(context);
var job = (await jobProgressServiceEF.NewJob("FindingRhymesForSingleCouplets", "Query data")).Result;
try
{
var sections = await context.GanjoorPoemSections.AsNoTracking().Include(s => s.GanjoorMetre).Where(s => s.GanjoorMetre != null && (s.RhymeLetters.Length >= 30 || s.RhymeLetters.Length < 2)).ToListAsync();
int percent = 0;
for (int i = 0; i < sections.Count; i++)
{
if (i * 100 / sections.Count > percent)
{
percent++;
await jobProgressServiceEF.UpdateJob(job.Id, percent);
}
var res = await _FindSectionRhyme(context, sections[i].Id);
if(string.IsNullOrEmpty(res.ExceptionString))
{
if(!string.IsNullOrEmpty(res.Result.Rhyme) && res.Result.Rhyme != sections[i].RhymeLetters)
{
var oldRhyme = sections[i].RhymeLetters;
sections[i].RhymeLetters = res.Result.Rhyme;
context.Update(sections[i]);
await context.SaveChangesAsync();
await _UpdateRelatedSections(context, (int)sections[i].GanjoorMetreId, oldRhyme, jobProgressServiceEF, job, percent);
await _UpdateRelatedSections(context, (int)sections[i].GanjoorMetreId, res.Result.Rhyme, jobProgressServiceEF, job, percent);
}
}
}
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true);
}
catch (Exception exp)
{
await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString());
}
}
}
);
return new RServiceResult<bool>(true);
}
}
}

View File

@ -3328,15 +3328,21 @@ namespace RMuseum.Services.Implementation
/// <returns></returns>
public async Task<RServiceResult<GanjooRhymeAnalysisResult>> FindSectionRhyme(int id)
{
var section = await _context.GanjoorPoemSections.Include(s => s.GanjoorMetre).AsNoTracking().Where(s => s.Id == id).FirstOrDefaultAsync();
return await _FindSectionRhyme(_context, id);
}
private async Task<RServiceResult<GanjooRhymeAnalysisResult>> _FindSectionRhyme(RMuseumDbContext context, int id)
{
var section = await context.GanjoorPoemSections.Include(s => s.GanjoorMetre).AsNoTracking().Where(s => s.Id == id).FirstOrDefaultAsync();
if (section == null)
return new RServiceResult<GanjooRhymeAnalysisResult>(null, "no sections");
var verses = await _context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == section.PoemId).OrderBy(v => v.VOrder).ToListAsync();
var verses = await context.GanjoorVerses.AsNoTracking().Where(v => v.PoemId == section.PoemId).OrderBy(v => v.VOrder).ToListAsync();
var rhymeAnalysisResult = LanguageUtils.FindRhyme(FilterSectionVerses(section, verses));
if(rhymeAnalysisResult.Rhyme.Length > 30 && verses.Count == 2 && section.GanjoorMetre != null)//single verse
{
var rhymingSection = await _context.GanjoorPoemSections.AsNoTracking()
.Where(s => s.GanjoorMetreId == section.GanjoorMetreId && section.RhymeLetters != null && rhymeAnalysisResult.Rhyme.Contains(s.RhymeLetters))
var rhymingSection = await context.GanjoorPoemSections.AsNoTracking()
.Where(s => s.GanjoorMetreId == section.GanjoorMetreId && section.RhymeLetters != null && s.RhymeLetters.Length < 15 && rhymeAnalysisResult.Rhyme.Contains(s.RhymeLetters))
.OrderByDescending(s => s.RhymeLetters.Length)
.FirstOrDefaultAsync();
if(rhymingSection != null)