From b40c05812480c7ac4af01dbfae961e1555d60bd9 Mon Sep 17 00:00:00 2001 From: Karthik Madathil Date: Mon, 1 Aug 2022 12:43:06 -0700 Subject: [PATCH] Potential fix for issue #179 --- docs/ui/sanskrit_parser.js | 83 ++++++++++++++++-------------- sanskrit_parser/rest_api/api_v1.py | 14 +++-- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/docs/ui/sanskrit_parser.js b/docs/ui/sanskrit_parser.js index aacf45b..466e183 100644 --- a/docs/ui/sanskrit_parser.js +++ b/docs/ui/sanskrit_parser.js @@ -15,8 +15,14 @@ function reject_split(id) { $("#card"+id).addClass("d-none") } +function end_s_visarga(s) { + sr = s.replace(/स्$/,"ः").replace(/र्$/,"ः(र्)") + return sr +} + function parse_split(id) { - split = $("#header"+id).text() + //split = $("#header"+id).text() + split = $("#header"+id).attr("rawtext") url = urlbase + "sanskrit_parser/v1/parse-presegmented/" + split var btn = $(this); var btxt = btn.text(); @@ -63,13 +69,13 @@ function parse_split(id) { }) } -function createSplitPanel(heading, urlbase, id) { +function createSplitPanel(heading, urlbase, id, rawtext) { "use strict;" // var cardClass = id % 2 ? "bg-secondary" : "bg-primary"; var cardClass = "border-dark mb-3"; var expanded = id === 0 ? "show" : ""; var h = "
"; - h += ""; + h += ""; h += heading + ""; h += "" h += "" @@ -81,39 +87,39 @@ function createSplitPanel(heading, urlbase, id) { return h; } -function createPanel(heading, row, dot, urlbase, id) { - "use strict;" - var cardClass = id % 2 ? "bg-secondary" : "bg-primary"; - var expanded = id === 0 ? "show" : ""; - var h = "
"; - h += ""; - h += heading + ""; -/* h += heading + "

(View Graph)

"; */ -/* h += urlbase + "sanskrit_parser/v1/graph/" + imgbase + "\">(View Graph)

"; */ -/* h += encodeURI("https://image-charts.com/chart?cht=gv:dot&chl=" + dot["split"]) + "\">(View Graph)

"; */ - h += "
"; - h += "
"; - h += "
    "; - row.forEach(function (sitem, index) { - h += "
  1. "; -// h += "

    (View Parse Graph)

    " -// h += encodeURI("https://image-charts.com/chart?cht=gv:dot&chl=" + dot[index]) + "\">(View Parse Graph)

    " - h += "
    "; - h += ""; - sitem.forEach(function (item) { - h += ""; - }); - h += "
    WordPossible InterpretationsRoleLinked To
    " + item[0] + ""; - h += item[1][0] + " - " + item[1][1] + ""; - h += item[2] + ""; - h += item[3] + "
  2. "; - }); - h += "
"; - h += "
"; - return h; -} +// function createPanel(heading, row, dot, urlbase, id) { +// "use strict;" +// var cardClass = id % 2 ? "bg-secondary" : "bg-primary"; +// var expanded = id === 0 ? "show" : ""; +// var h = "
"; +// h += ""; +// h += heading + ""; +// /* h += heading + "

(View Graph)

"; */ +// /* h += urlbase + "sanskrit_parser/v1/graph/" + imgbase + "\">(View Graph)

"; */ +// /* h += encodeURI("https://image-charts.com/chart?cht=gv:dot&chl=" + dot["split"]) + "\">(View Graph)

"; */ +// h += "
"; +// h += "
"; +// h += "
    "; +// row.forEach(function (sitem, index) { +// h += "
  1. "; +// // h += "

    (View Parse Graph)

    " +// // h += encodeURI("https://image-charts.com/chart?cht=gv:dot&chl=" + dot[index]) + "\">(View Parse Graph)

    " +// h += "
    "; +// h += ""; +// sitem.forEach(function (item) { +// h += ""; +// }); +// h += "
    WordPossible InterpretationsRoleLinked To
    " + item[0] + ""; +// h += item[1][0] + " - " + item[1][1] + ""; +// h += item[2] + ""; +// h += item[3] + "
  2. "; +// }); +// h += "
"; +// h += "
"; +// return h; +// } // write small plugin for keypress enter detection $.fn.enterKey = function (fnc) { @@ -226,8 +232,9 @@ $(document).ready( function () { return a.length > b.length; }); splits.forEach(function (res) { - var item = res.join(" "); - restable += createSplitPanel(item, urlbase, panelID); + var rawtext = res.join(" ") + var item = res.map(end_s_visarga).join(" "); + restable += createSplitPanel(item, urlbase, panelID, rawtext); panelID += 1; }); $("#restable").append(restable); diff --git a/sanskrit_parser/rest_api/api_v1.py b/sanskrit_parser/rest_api/api_v1.py index 9159abd..33695f0 100644 --- a/sanskrit_parser/rest_api/api_v1.py +++ b/sanskrit_parser/rest_api/api_v1.py @@ -82,7 +82,10 @@ def get(self, v): g = analyzer.getSandhiSplits(vobj) if g: splits = g.find_all_paths(10) - jsplits = [[ss.devanagari(strict_io=False) for ss in s] for s in splits] + # We don't get output with visargas here. + # Why? Because this will need to be parsed later, and we need to distinguish s/r + # We rely on the UI to handle display correctly + jsplits = [[ss.devanagari(strict_io=True) for ss in s] for s in splits] else: jsplits = [] r = {"input": v, "devanagari": vobj.devanagari(), "splits": jsplits} @@ -97,11 +100,14 @@ def get(self, v): if request.args.get("strict") == "false": strict_p = False vobj = SanskritObject(v, strict_io=strict_p, replace_ending_visarga=None) + # We set strict_io to False so we get output with visargas + # If further processing is desired, this needs to be set to True + # And the UI must handle display parser = Parser(input_encoding=sanscript.SLP1, - output_encoding="Devanagari", + output_encoding=sanscript.DEVANAGARI, + strict_io=False, replace_ending_visarga='s') mres = [] - print(v) for split in parser.split(vobj.canonical(), limit=10, pre_segmented=True): parses = list(split.parse(limit=10)) sdot = split.to_dot() @@ -119,7 +125,7 @@ def get(self, v): """ Presegmented Split """ vobj = SanskritObject(v, strict_io=True, replace_ending_visarga=None) parser = Parser(input_encoding=sanscript.SLP1, - output_encoding="Devanagari", + output_encoding=sanscript.DEVANAGARI, replace_ending_visarga='s') splits = parser.split(vobj.canonical(), limit=10, pre_segmented=True) r = {"input": v, "devanagari": vobj.devanagari(), "splits": [x.serializable()['split'] for x in splits]}