From c8b915af01ebccf565c61d6b61bad918f5cff739 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 17 Jun 2021 12:04:34 +0430 Subject: [PATCH] #151 api for assigning or analyzing prosody information of a poem --- RMuseum/Controllers/GanjoorController.cs | 25 ++++- RMuseum/RMuseum.xml | 29 +++++- RMuseum/Services/IGanjoorService.cs | 9 ++ .../Services/Implementation/GanjoorService.cs | 91 +++++++++++++++++-- 4 files changed, 146 insertions(+), 8 deletions(-) diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index be9b9a2e..c3ff1109 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -432,7 +432,7 @@ namespace RMuseum.Controllers [HttpPut] [Route("cat/startassigningrhymes/{id}/{retag}")] [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] - [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjoorPageCompleteViewModel))] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] [ProducesResponseType((int)HttpStatusCode.NotFound)] public IActionResult FindCategoryPoemsRhymes(int id, bool retag) @@ -445,6 +445,29 @@ namespace RMuseum.Controllers return Ok(res.Result); } + /// + /// start assigning poem rhuthmes + /// + /// + /// + /// + /// + [HttpPut] + [Route("cat/startassigningrhythms/{id}/{retag}")] + [Authorize(Policy = RMuseumSecurableItem.GanjoorEntityShortName + ":" + SecurableItem.ModifyOperationShortName)] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(bool))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + [ProducesResponseType((int)HttpStatusCode.NotFound)] + public IActionResult FindCategoryPoemsRhythms(int id, bool retag, string rhythm = "") + { + + RServiceResult res = + _ganjoorService.FindCategoryPoemsRhythms(id, retag, rhythm); + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + return Ok(res.Result); + } + /// /// page by url /// diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 5d4f02e7..4a163b99 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -778,6 +778,15 @@ + + + start assigning poem rhuthmes + + + + + + page by url @@ -7503,6 +7512,15 @@ + + + find category poem rhymes + + + + + + IArtifactService implementation @@ -8850,6 +8868,14 @@ + + + find category poem rhymes + + + + + find poem rhythm @@ -8857,12 +8883,13 @@ - + find category poem rhymes + diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index f7f1e208..c3a2b4bb 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -398,5 +398,14 @@ namespace RMuseum.Services /// /// Task> FindPoemRhythm(int id); + + /// + /// find category poem rhymes + /// + /// + /// + /// + /// + RServiceResult FindCategoryPoemsRhythms(int catId, bool retag, string rhythm = ""); } } diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index 20f5740c..1a0c6374 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2929,6 +2929,31 @@ namespace RMuseum.Services.Implementation } } + /// + /// find category poem rhymes + /// + /// + /// + /// + public RServiceResult FindCategoryPoemsRhymes(int catId, bool retag) + { + try + { + _backgroundTaskQueue.QueueBackgroundWorkItem + ( + async token => + { + await _FindCategoryPoemsRhymesInternal(catId, retag); + }); + + return new RServiceResult(true); + } + catch (Exception exp) + { + return new RServiceResult(false, exp.ToString()); + } + } + /// /// find poem rhythm /// @@ -2939,14 +2964,14 @@ namespace RMuseum.Services.Implementation try { var metres = (await GetGanjoorMetres()).Result.Select(m => m.Rhythm).ToArray(); - return await _FindPoemRhythm(id, _context, metres); + return await _FindPoemRhythm(id, _context, _httpClient, metres); } catch (Exception exp) { return new RServiceResult(null, exp.ToString()); } } - private async Task> _FindPoemRhythm(int id, RMuseumDbContext context, string[] metres) + private async Task> _FindPoemRhythm(int id, RMuseumDbContext context, HttpClient httpClient, string[] metres) { try { @@ -2958,7 +2983,7 @@ namespace RMuseum.Services.Implementation { try { - var response = await _httpClient.GetAsync($"http://sorud.info/?Text={HttpUtility.UrlEncode(LanguageUtils.MakeTextSearchable(verse.Text))}"); + var response = await httpClient.GetAsync($"http://sorud.info/?Text={HttpUtility.UrlEncode(LanguageUtils.MakeTextSearchable(verse.Text))}"); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); if (result.IndexOf("آهنگِ همه‌ی بندها شناسایی نشد.") != -1) @@ -3037,13 +3062,65 @@ namespace RMuseum.Services.Implementation } } + private async Task _FindCategoryPoemsRhythmsInternal(int catId, bool retag, string rhythm) + { + using (RMuseumDbContext context = new RMuseumDbContext(new DbContextOptions())) //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($"FindCategoryPoemsRhythms Cat {catId}", "Query data")).Result; + try + { + var metres = await context.GanjoorMetres.OrderBy(m => m.Rhythm).AsNoTracking().ToArrayAsync(); + var rhythms = metres.Select(m => m.Rhythm).ToArray(); + + GanjoorMetre preDeterminedMetre = string.IsNullOrEmpty(rhythm) ? null : metres.Where(m => m.Rhythm == rhythm).Single(); + + var poems = await context.GanjoorPoems.Where(p => p.CatId == catId).ToListAsync(); + + int i = 0; + using(HttpClient httpClient = new HttpClient()) + { + foreach (var poem in poems) + { + if (retag || poem.GanjoorMetreId == null) + { + await jobProgressServiceEF.UpdateJob(job.Id, i++); + if (preDeterminedMetre == null) + { + var res = await _FindPoemRhythm(poem.Id, context, httpClient, rhythms); + if (!string.IsNullOrEmpty(res.Result)) + { + poem.GanjoorMetreId = metres.Where(m => m.Rhythm == res.Result).Single().Id; + context.GanjoorPoems.Update(poem); + } + } + else + { + poem.GanjoorMetreId = preDeterminedMetre.Id; + context.GanjoorPoems.Update(poem); + } + } + } + } + await jobProgressServiceEF.UpdateJob(job.Id, 99); + await context.SaveChangesAsync(); + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", true); + } + catch (Exception exp) + { + await jobProgressServiceEF.UpdateJob(job.Id, 100, "", false, exp.ToString()); + } + } + } + /// /// find category poem rhymes /// /// /// + /// /// - public RServiceResult FindCategoryPoemsRhymes(int catId, bool retag) + public RServiceResult FindCategoryPoemsRhythms(int catId, bool retag, string rhythm = "") { try { @@ -3051,17 +3128,19 @@ namespace RMuseum.Services.Implementation ( async token => { - await _FindCategoryPoemsRhymesInternal(catId, retag); + await _FindCategoryPoemsRhythmsInternal(catId, retag, rhythm); }); return new RServiceResult(true); } - catch(Exception exp) + catch (Exception exp) { return new RServiceResult(false, exp.ToString()); } } + + /// /// make plain text ///