Browse Source

Initial commit

main
Harish Karumuthil 3 years ago
commit
bfc107e7e1
  1. 12
      run.sh
  2. 54
      server.py
  3. 78
      static/app.js
  4. 29
      static/index.html
  5. 33
      test.py

12
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 "$@"

54
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())

78
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");

29
static/index.html

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Ascii to unicode converter</title>
<link rel="stylesheet" href="https://unpkg.com/mvp.css@1.12/mvp.css" />
<style type="text/css" media="screen">
</style>
</head>
<body>
<main id="app">
<hr />
<form accept-charset="utf-8" v-on:submit.prevent="onSubmit">
<select v-model="form.font">
<option v-for="font in allFonts" :value="font">{{font}}</option>
</select>
<textarea v-model="form.text" rows="4">
</textarea>
<button type="submit">Submit</button>
<textarea rows="8" :style="{'font-family': asciiFont}" v-model="asciiTxt" ></textarea>
</form>
</main>
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
<script src="./app.js" charset="utf-8"></script>
</body>
</html>

33
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()
Loading…
Cancel
Save