A Pen by Santhosh Thottingal on CodePen.
Created
December 10, 2017 11:39
-
-
Save santhoshtr/14e2a0d19d01ba7803a7d241a4079f07 to your computer and use it in GitHub Desktop.
Malayalam number parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<input id="num" type="number" placeholder="Enter a number" /> | |
<div id="result"></div> | |
<div id="analysis"></div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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