From bfc107e7e1c893f9a4a0cd2f5c203801a3d01f41 Mon Sep 17 00:00:00 2001 From: Harish Karumuthil Date: Wed, 15 Feb 2023 10:38:53 +0530 Subject: [PATCH] Initial commit --- run.sh | 12 ++++++++ server.py | 54 ++++++++++++++++++++++++++++++++ static/app.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++ static/index.html | 29 ++++++++++++++++++ test.py | 33 ++++++++++++++++++++ 5 files changed, 206 insertions(+) create mode 100755 run.sh create mode 100644 server.py create mode 100644 static/app.js create mode 100644 static/index.html create mode 100644 test.py diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..07b5a4b --- /dev/null +++ b/run.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +THIS_DIR=$(dirname $(readlink -f $0)) + +. $THIS_DIR/venv/bin/activate + +PYTHONPATH="$THIS_DIR/payyans" +PYTHONPATH+=":$THIS_DIR/normalizer" +export PYTHONPATH + +bindAddress=${BIND_ADDRESS:-'127.0.0.1:8444'} +gunicorn server:api -b $bindAddress "$@" diff --git a/server.py b/server.py new file mode 100644 index 0000000..0831a7d --- /dev/null +++ b/server.py @@ -0,0 +1,54 @@ +import falcon +import json +import os +from libindic.payyans import Payyans +instance = Payyans() + +all_fonts = [ + 'ambili', + 'charaka', + 'haritha', + 'indulekha', + 'karthika', + 'manorama', + 'matweb', + 'nandini', + 'panchari', + 'revathi', + 'template', + 'uma', + ] + +def convert(txt, font): + # txt = txt.replace("ൺ", "ണ്‍") \ + # .replace("ൻ", "ന്‍") \ + # .replace("ർ", "ര്‍") \ + # .replace("ൽ", "ല്‍") \ + # .replace("ൾ", "ള്‍") \ + # .replace("ോ", "ോ") \ + # .replace("ൊ", "ൊ") + + return instance.Unicode2ASCII(txt, font) + +class QResource: + def on_post(self, req, resp): + body = req.get_media() + text = body.get( 'text' ) + font_name = body.get( 'font' ) + if( font_name not in all_fonts ): + raise falcon.HTTPBadRequest('Invalid font') + resp.set_header('content-type', 'application/json; charset=UTF-8') + data = { } + data['ascii'] = convert(text, font_name ) + resp.body = json.dumps( data, default=str ) + + +class RedirectingResource: + def on_get(self, req, resp): + raise falcon.HTTPMovedPermanently('/index.html') + +api = falcon.API() +api.add_route('/', RedirectingResource()) +api.add_static_route('/', os.path.dirname(os.path.realpath(__file__)) + '/static' ) +api.add_route('/api/a2u', QResource()) + diff --git a/static/app.js b/static/app.js new file mode 100644 index 0000000..3728b4a --- /dev/null +++ b/static/app.js @@ -0,0 +1,78 @@ +const { createApp } = Vue; + +const fontsMap = { + ambili: "FML-TTambili", + charaka: "FML-TTRevathi", + haritha: "haritha", + indulekha: "FML-TTindulekha", + karthika: "FML-TTkarthika", + manorama: "manorama", + matweb: "matweb", + nandini: "FML-TTnandini", + panchari: "panchari", + revathi: "FML-TTrevathi", + uma: "FML-TTkaumudi", +}; + +createApp({ + data() { + return { + form: { + text: `ചെണ്ടമേളം +കുഞ്ചൻ നമ്പ്യാർ +മഞ്ഞൾ പ്രസാദം +തിങ്കളാഴ്ച +`, + font: "karthika", + }, + allFonts: Object.keys(fontsMap), + asciiTxt: "", + asciiFont: "", + }; + }, + + methods: { + async onSubmit() { + const res = await fetch("/api/a2u", { + method: "post", + body: JSON.stringify(this.form), + headers: { + "Content-type": "application/json", + }, + }); + const data = await res.json(); + this.asciiTxt = data.ascii; + this.asciiFont = fontsMap[this.form.font]; + }, + correctOldChillu(e) { + return (e = e + .replace("ൺ", "ണ്‍") + .replace("ൻ", "ന്‍") + .replace("ർ", "ര്‍") + .replace("ൽ", "ല്‍") + .replace("ൾ", "ള്‍") + .replace("ോ", "ോ") + .replace("ൊ", "ൊ")); + }, + correctNewChillu(e) { + return (e = e + .replaceAll("ണ്‍", "ൺ") + .replaceAll("ന്‍", "ൻ") + .replaceAll("ര്‍", "ർ") + .replaceAll("ല്‍", "ൽ") + .replaceAll("ള്‍", "ൾ") + .replaceAll("ോ", "ോ") + .replaceAll("ൊ", "ൊ") + .replaceAll("൦", "0") + .replaceAll("൧", "1") + .replaceAll("൨", "2") + .replaceAll("൩", "3") + .replaceAll("൪", "4") + .replaceAll("൫", "5") + .replaceAll("൬", "6") + .replaceAll("൭", "7") + .replaceAll("൮", "8") + .replaceAll("൯", "9")); + }, + }, +}).mount("#app"); diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..e36c23e --- /dev/null +++ b/static/index.html @@ -0,0 +1,29 @@ + + + + + + Ascii to unicode converter + + + + +
+
+ +
+ + + + +
+
+ + + + + diff --git a/test.py b/test.py new file mode 100644 index 0000000..8f02da9 --- /dev/null +++ b/test.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +from libindic.payyans import Payyans +instance = Payyans() + +from libindic.normalizer import Normalizer +normalizer = Normalizer() + +test_words = [ + 'തിങ്കളാഴ്ച ', + # 'ചെണ്ടമേളം ', + # ' കുഞ്ചൻ നമ്പ്യാർ ', + ] + + + +def test_normalizer(): + for txt in test_words: + out = normalizer.normalize(txt) + print(list(txt), list(out)) + +def test_u2a(): + for txt in test_words: + out = instance.Unicode2ASCII(txt, 'karthika') + print(list(txt), list(out)) + + +def main(): + test_normalizer() + test_u2a() + + +main()