Skip to content

Instantly share code, notes, and snippets.

@santhoshtr
Created December 10, 2017 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santhoshtr/14e2a0d19d01ba7803a7d241a4079f07 to your computer and use it in GitHub Desktop.
Save santhoshtr/14e2a0d19d01ba7803a7d241a4079f07 to your computer and use it in GitHub Desktop.
Malayalam number parser
<div class="container">
<input id="num" type="number" placeholder="Enter a number" />
<div id="result"></div>
<div id="analysis"></div>
</div>
var onesStr = [
"",
"ഒന്ന്",
"രണ്ട്",
"മൂന്ന്",
"നാല്",
"അഞ്ച്",
"ആറ്",
"ഏഴ്",
"എട്ട്",
"ഒമ്പത്"
];
function positionValues(value) {
var result = "";
var crores = value > 10000000 ? parseInt(value / 10000000) : 0;
var lakhs = parseInt((value % 10000000) / 100000);
var thousands = parseInt((value % 100000) / 1000);
var hundreds = parseInt((value % 1000) / 100);
var tens = parseInt((value % 100) / 10);
var ones = parseInt((value % 10) / 1);
result =
(crores > 0 ? positionValues(crores) + "<crores>" : "") +
(lakhs > 0 ? positionValues(lakhs) + "<lakhs>" : "") +
(thousands > 0 ? positionValues(thousands) + "<thousands>" : "") +
(hundreds > 0 ? positionValues(hundreds) + "<hundreds>" : "") +
(tens > 0 ? positionValues(tens) + "<tens>" : "") +
(ones > 0 ? onesStr[ones] + "<ones>" : "");
// Some cleanup for better results
result = result.replace("<ones><hundreds>", "<hundreds>");
result = result.replace("<ones><tens>", "<tens>");
result = result.replace("ഒന്ന്<ones><hundredsa>", "<hundreds>");
result = result.replace("ഒന്ന്<ones><thousands>", "<thousands>");
return result;
}
function onNumberChange() {
var value = document.getElementById("num").value;
value = Number(value);
numberGen = "";
numberGen = positionValues(value) + "<cardinal>";
$("#analysis").text(numberGen);
$.getJSON(
"https://morph.smc.org.in/api/generate",
{
word: numberGen
},
function(data) {
var result = data.result && data.result[0];
$("#result").text(result);
}
);
}
$(document).ready(function($) {
$("#num").on("change keyup", onNumberChange);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
#num,
#result {
color: #333;
font-size: 2em;
}
#analysis {
color: darkgray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment