Skip to content

Instantly share code, notes, and snippets.

@imerkle
Created September 2, 2017 19:55
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 imerkle/13c85945d10ea5a9812676abc79a6ae7 to your computer and use it in GitHub Desktop.
Save imerkle/13c85945d10ea5a9812676abc79a6ae7 to your computer and use it in GitHub Desktop.
uncomment the xhr https://repl.it/KeAf
function countSyllables(word)
{
var vowels = [ 'a', 'e', 'i', 'o', 'u', 'y' ];
var currentWord = word.match(/\S/g);
var numVowels = 0;
var lastWasVowel = false;
var sylabels = [];
var charset = "";
currentWord.map((wc, ix) => {
wc = wc.toLowerCase();
charset += wc;
var foundVowel = false;
vowels.map(v =>
{
//don't count diphthongs
var isBreak = false;
if ((v == wc) && lastWasVowel && ix > 0)
{
if(!isBreak){
foundVowel = true;
lastWasVowel = true;
}
isBreak = true;
}
else if (v == wc && !lastWasVowel && ix > 0)
{
if(!isBreak){
numVowels++;
sylabels.push(charset);
charset = "";
foundVowel = true;
lastWasVowel = true;
}
isBreak = true;
}
});
// If full cycle and no vowel found, set lastWasVowel to false;
if (!foundVowel)
lastWasVowel = false;
});
// Remove es, it's _usually? silent
if (word.length > 2 &&
word.substring(word.length - 2) == "es"){
numVowels--;
}
// remove silent e
else if (word.length > 1 &&
word.substring(word.length - 1) == "e"){
numVowels--;
}
return {numVowels: numVowels, sylabels: sylabels};
}
function wordCorrection(str){
str = str.toLowerCase();
if(str.substring(str.length - 1) == "y"){
str = str.substring(0, str.length - 1) + "i";
}
var replaceObj = {
"acha": "aka",
"oo": "u",
"ei": "i",
"on": "an",
}
for(var key in replaceObj) {
if(replaceObj.hasOwnProperty(key)) {
str = str.replace(key,replaceObj[key]);
}
};
return str;
}
console.log(countSyllables(wordCorrection("Onsooh")));
const jsonurl = "https://gist.githubusercontent.com/dsslimshaddy/c72922eadd7829b0cbde04a716f2584a/raw/edeaadc26123ced6cf6d958f47b4cd0718650b45/eve.json";
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
let data = JSON.stringify(parseData(JSON.parse(xhttp.responseText)));
console.log(data);
}
};
xhttp.open("GET", jsonurl, true);
xhttp.send();
function parseData(data){
data.map((o,i)=>{
let yo;
if(!o.FIELD4){
yo = countSyllables(wordCorrection(o.FIELD1));
o.FIELD3 = yo.numVowels;
o.FIELD4 = yo.sylabels.join("-");
yo = countSyllables(o.FIELD1);
const tmp_field6 = yo.sylabels.join("-");
if(tmp_field6 != o.FIELD4){
o.FIELD5 = yo.numVowels;
o.FIELD6 = tmp_field6;
}
}
})
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment