From c9585b5dc96f8c6397bf112b3eef39907c99b255 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Wed, 16 Jun 2021 18:42:58 +0430 Subject: [PATCH] #148 find rhyme api --- RMuseum/Controllers/GanjoorController.cs | 23 ++- .../ViewModels/GanjooRhymeAnalysisResult.cs | 18 ++ RMuseum/RMuseum.xml | 44 +++++ RMuseum/Services/IGanjoorService.cs | 7 + .../Services/Implementation/GanjoorService.cs | 19 ++ .../Services/Implementation/LanguageUtils.cs | 162 +++++++++++++++++- 6 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 RMuseum/Models/Ganjoor/ViewModels/GanjooRhymeAnalysisResult.cs diff --git a/RMuseum/Controllers/GanjoorController.cs b/RMuseum/Controllers/GanjoorController.cs index ec7b8cc2..9af1ed44 100644 --- a/RMuseum/Controllers/GanjoorController.cs +++ b/RMuseum/Controllers/GanjoorController.cs @@ -1218,7 +1218,28 @@ namespace RMuseum.Controllers return Ok(res.Result); } - + /// + /// find poem rhyme + /// + /// + /// + [HttpGet] + [Route("poem/analysisrhyme/{id}")] + [AllowAnonymous] + [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GanjooRhymeAnalysisResult))] + [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] + + public async Task FindPoemRhyme(int id) + { + var res = await _ganjoorService.FindPoemRhyme(id); + + if (!string.IsNullOrEmpty(res.ExceptionString)) + return BadRequest(res.ExceptionString); + + return Ok(res.Result); + } + + /// /// examine site pages for broken links diff --git a/RMuseum/Models/Ganjoor/ViewModels/GanjooRhymeAnalysisResult.cs b/RMuseum/Models/Ganjoor/ViewModels/GanjooRhymeAnalysisResult.cs new file mode 100644 index 00000000..e058d7b2 --- /dev/null +++ b/RMuseum/Models/Ganjoor/ViewModels/GanjooRhymeAnalysisResult.cs @@ -0,0 +1,18 @@ +namespace RMuseum.Models.Ganjoor.ViewModels +{ + /// + /// Rhyme Analysis Result + /// + public class GanjooRhymeAnalysisResult + { + /// + /// rhyme + /// + public string Rhyme { get; set; } + + /// + /// the verse analysis stopped at + /// + public string FailVerse { get; set; } + } +} diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index db3791b9..96f9cec6 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -1009,6 +1009,13 @@ + + + find poem rhyme + + + + examine site pages for broken links @@ -5137,6 +5144,21 @@ this can be used by clients + + + Rhyme Analysis Result + + + + + rhyme + + + + + the verse analysis stopped at + + modify page view model @@ -7426,6 +7448,13 @@ + + + find poem rhyme + + + + IArtifactService implementation @@ -8766,6 +8795,13 @@ + + + find poem rhyme + + + + make plain text @@ -8880,6 +8916,14 @@ + + + find rhyme + + + + + Music Catalgue Service Implementation diff --git a/RMuseum/Services/IGanjoorService.cs b/RMuseum/Services/IGanjoorService.cs index ef974a7f..18d6cd4f 100644 --- a/RMuseum/Services/IGanjoorService.cs +++ b/RMuseum/Services/IGanjoorService.cs @@ -376,5 +376,12 @@ namespace RMuseum.Services /// /// Task> BatchRenameCatPoemTitles(int catId, GanjoorBatchNamingModel model, Guid userId); + + /// + /// find poem rhyme + /// + /// + /// + Task> FindPoemRhyme(int id); } } diff --git a/RMuseum/Services/Implementation/GanjoorService.cs b/RMuseum/Services/Implementation/GanjoorService.cs index a367f561..9e228abf 100644 --- a/RMuseum/Services/Implementation/GanjoorService.cs +++ b/RMuseum/Services/Implementation/GanjoorService.cs @@ -2864,6 +2864,25 @@ namespace RMuseum.Services.Implementation } } + /// + /// find poem rhyme + /// + /// + /// + public async Task> FindPoemRhyme(int id) + { + try + { + return new RServiceResult( + LanguageUtils.FindRhyme(await _context.GanjoorVerses.Where(v => v.PoemId == id).OrderBy(v => v.VOrder).ToListAsync()) + ); + } + catch(Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + /// /// make plain text /// diff --git a/RMuseum/Services/Implementation/LanguageUtils.cs b/RMuseum/Services/Implementation/LanguageUtils.cs index 18e4b7ed..c676c68c 100644 --- a/RMuseum/Services/Implementation/LanguageUtils.cs +++ b/RMuseum/Services/Implementation/LanguageUtils.cs @@ -1,4 +1,9 @@ -namespace RMuseum.Services.Implementation +using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; +using System.Collections.Generic; +using System.Linq; + +namespace RMuseum.Services.Implementation { /// /// Language Utilitis @@ -52,7 +57,162 @@ .Replace("؟", "") .Replace("ٔ", "") .Replace(":", "") + .Replace("ئ", "ی") + .Replace("؛", "") + .Replace(";", "") + .Replace("*", "") + .Replace(")", "") + .Replace("(", "") ; } + + public static string PrepareTextForFindingRhyme(string text) + { + return MakeTextSearchable(text) + .Replace("لله", "للاه") + .Replace("آ", "ا") + .Replace("‍", "") + .Replace("‏", "") + .Replace("‌", "") + .Replace(" ", "") + .Trim() + ; + ; + } + + + /// + /// find rhyme + /// + /// + /// + /// + public static GanjooRhymeAnalysisResult FindRhyme(List verses, bool secondPhase = false) + { + List verseTextList = verses.Count == 2 ? verses.Select(v => v.Text).ToList() + : verses.Where(v => v.VersePosition == VersePosition.Left).Select(v => v.Text).ToList(); + if(verseTextList.Count > 1) + { + string rhyme = PrepareTextForFindingRhyme(verseTextList[0]); + if(string.IsNullOrEmpty(rhyme)) + { + return new GanjooRhymeAnalysisResult() + { + Rhyme = "", + FailVerse = verseTextList[0] + }; + } + if(secondPhase) + { + if (secondPhase) + if (rhyme[rhyme.Length - 1] == 'ی') + rhyme = rhyme.Remove(rhyme.Length - 1); + } + + for (int j = 1; j < verseTextList.Count; j++) + { + string verseText = PrepareTextForFindingRhyme(verseTextList[j]); + if (secondPhase) + { + if (verseText[verseText.Length - 1] == 'ی') + { + verseText = verseText.Remove(verseText.Length - 1); + } + } + string oldRhyme = rhyme; + rhyme = ""; + int i = oldRhyme.Length - 1; + while ( + (oldRhyme[i] == verseText[verseText.Length - oldRhyme.Length + i]) + || + ( + (oldRhyme[i] == 'ذ') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'د') + ) + || + ( + (oldRhyme[i] == 'د') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ذ') + ) + || + + ( + (oldRhyme[i] == 'ی') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ا') + ) + || + ( + (oldRhyme[i] == 'ا') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ی') + ) + + || + + (oldRhyme[i] == verseText[verseText.Length - oldRhyme.Length + i]) + || + ( + (oldRhyme[i] == 'پ') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ب') + ) + || + ( + (oldRhyme[i] == 'ب') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'پ') + ) + + || + (oldRhyme[i] == 'ة') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ت') + + || + (oldRhyme[i] == 'ت') + && + (verseText[verseText.Length - oldRhyme.Length + i] == 'ة') + + + + ) + { + rhyme = oldRhyme[i] + rhyme; + i--; + if ((i == -1)) + break; + } + if (rhyme.Length == 0) + { + return new GanjooRhymeAnalysisResult() + { + Rhyme = "", + FailVerse = verseText + }; + } + + } + + if(!secondPhase) + { + if(string.IsNullOrEmpty(rhyme)) + { + return FindRhyme(verses, true); + } + } + + return new GanjooRhymeAnalysisResult() + { + Rhyme = rhyme, + FailVerse = "" + }; + + } + + return new GanjooRhymeAnalysisResult() { Rhyme = "", FailVerse = "" }; + } } }