Skip to content

Instantly share code, notes, and snippets.

@skorytnicki
Created July 18, 2016 21:14
Show Gist options
  • Save skorytnicki/bd74d76e9e974d84ed858f5141496d04 to your computer and use it in GitHub Desktop.
Save skorytnicki/bd74d76e9e974d84ed858f5141496d04 to your computer and use it in GitHub Desktop.
Approximate the number of syllabes in the string
// coursera NLP lesson 02.04
// approximating number of syllables in a string
function countSyllables(s) {
'use strict';
var k = 0;
if (s.length < 1) return k;
s = s.toLowerCase();
s = s.replace(/\W+/g, "");
s = s.replace(/[aeiou]/ig,'\~');
s = s.replace(/[qwrtypsdfghjklzxcvbnm]/ig,'C');
// lol, not tested it yet
// count occurencies of VC in a given string
while (s.length > 1) {
let i = s.indexOf('~C');
if (i == -1) break;
k = k+1;
s = s.substr(i+2,s.length);
}
return k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment