I’m trying to split words up so I can count the syllable in each one, but i can’t seem to isolate the words at the moment, i can only count the whole number of syllables in the sentence,
var text = $(this).val();
var words = new Array(text.replace(/\s/g, ' ').split(' '));
function countSyllables(words) {
word = words.toString();
var lWords = word.toLowerCase();
var wordsArray=lWords.split(",");
for (var z = 0; z < lWords.length; z++) {
console.log(wordsArray, "- array of words");
console.log(wordsArray[z], "- one item[z]");
My problem seems to be that i have to convert the array to a string to lower it, but then i can’t refer to each individual word, in both cases, to count the syllables:
[16:04:02.726] ["this", "is", "test", "text", "", "the", "words", "need", "to", "be", "separately", "analysed.", "hello"] - array of words
[16:04:02.726] o - one item[z]
This is the last character, when i want to refer to the word
I’m sure it’s a simple solution but i can’t wrap my head around it
This line is wrong on many levels:
What you’re after is, AFAIK, something like this:
This splits the string, using all white-space, dots, brackets and other punctuation-things as delimiters. I’ve excluded the dash (
-), to avoid counting words likeice-creamas two words. If you don’t care about that, or if you feel that the result is more reliable when you do treat dashes as delimiters, just leave them out:On the converting to lower-case thing: why not convert the entire text to lower-case from the off?
Oh, and the output of this code does not leave you with empty strings in the array: