Javascript - String split does not work well -


i making script receives string , separate on smaller strings.

ex: "this long sentence, , separate smaller parts. lalala"

it return "this long sentence","and separate smaller parts","lalala"

the aim of use google translator transform text speech, feature has limit of 70-80 chars, if string large need chop it.

first chop in sentences separated dot ("."), if there still long sentences, split them commas (",") , if there still long strings separate them in unique words.

everything works until try join words audio become more continuous. reason strings separated commas joined again. not know why.

this code:

edit: relevant section split out , formatted

function talk(text){     var audios = document.createelement('audio'); audios.setattribute('id','audio_speech'); var playlist = new array() if(text.length >= 75) {     playlist = text.split(".");          (var = 0;i<playlist.length;i++) {         if (playlist[i].length >= 75) {         auxarr = playlist[i].split(",");         //alert(auxarr.length);          for(var j=0;j<auxarr.length;j++) {             auxarr2 = auxarr[j].split(" ");             document.write(auxarr2+"<br>");              if (auxarr[j].length >= 75) {             auxarr2 = auxarr[j].split(" ");                  for(var x=0; x < auxarr2.length; x++){             if(auxarr2[x].length < 50) {                 aux = auxarr2[x];                 while (aux.length < 50 && auxarr2[x+1]) {                     aux = aux + " " + auxarr2[x+1];                 auxarr2.splice(x,1);                 auxarr2[x]=aux;                     }                 } //... 

edit: full original code

function talk(text) {     var audios = document.createelement('audio');     audios.setattribute('id','audio_speech');     var playlist = new array()     if(text.length >= 75) {         playlist = text.split(".");          (var = 0;i<playlist.length;i++) {             if (playlist[i].length >= 75) {                  auxarr = playlist[i].split(",");                 //alert(auxarr.length);                  for(var j=0;j<auxarr.length;j++) {                     auxarr2 = auxarr[j].split(" ");                     document.write(auxarr2+"<br>");                      if (auxarr[j].length >= 75) {                         auxarr2 = auxarr[j].split(" ");                          for(var x=0; x < auxarr2.length; x++){                              if(auxarr2[x].length < 50) {                                  aux = auxarr2[x];                                  while (aux.length < 50 && auxarr2[x+1]) {                                         aux = aux + " " + auxarr2[x+1];                                         auxarr2.splice(x,1);                                     }                                 auxarr2[x]=aux;                              }                          }                          auxarr_end = auxarr.slice(j+1,auxarr.length);                          auxarr_begin = auxarr.slice(0,j);                         document.write("<br>"+auxarr+"<br> aca");                                                document.write("<br>"+auxarr_end+"<br> aca1");                         document.write("<br>"+auxarr_begin+"<br> aca2");                                                 auxarr.splice(j,1);                          auxarr_begin = auxarr_begin.concat(auxarr2);                         j = auxarr.length;                         auxarr = auxarr_begin.concat(auxarr_end);                         alert(auxarr);                       }                  }                  //alert("current: "+playlist[i]);                 //alert("current length:"+playlist[i].length);                 //alert("auxarr: "+auxarr);                 playlist_end = playlist.slice(i+1,playlist.length);                 playlist_begin = playlist.slice(0, i);                  playlist.splice(i,1);                  playlist_begin = playlist_begin.concat(auxarr);                 = playlist.length;                 playlist = playlist_begin.concat(playlist_end);                 //alert("new "+playlist[i]);                  }          }         /*do {             textaux = text.substring(0, 74);             text = text.substring(textaux.length, text.length);             playlist.push(textaux);         }while(text.length >= 75);*/     } else {         playlist.push(text);     }     //      //playlist.push(text);      /*for(var a=0; a<playlist.length;a++){     document.write(playlist[a]+"<br>");}*/     audios.setattribute('src', 'http://translate.google.com/translate_tts?tl=es&q=' + encodeuricomponent(playlist[0]));     playlist.splice(0,1);     audios.load();     audios.play(); /*       */     audios.addeventlistener('ended', function(){         if (playlist[0]){         audios.setattribute('src', 'http://translate.google.com/translate_tts?tl=es&q=' + encodeuricomponent(playlist[0]));         playlist.splice(0,1);         audios.play();         }     }, false);   } </script> 

try this, modify work constants , parameters.

var limit = 20;  var res = new array()  //strats spliting dot var dotarr = "this long sentence. , separate smaller parts. lalala".split(/[.]/);  (var = 0; < dotarr.length; i++) {   if (dotarr[i].length > limit){      //only when have to, split comma     var comarr = dotarr[i].split(/[,]/);     (var j = 0; j < comarr.length; j++) {        //only when have , space exists, split space        if (comarr[j].length > limit && comarr[j].indexof(" ") != -1 ){          var spacearr = comarr[j].split(/[ ]/);          //accomulate words until reach limit , push value res          (var k = 0; k < spacearr.length;){             var smerge = spacearr[k++];             while (k < spacearr.length && smerge.length + spacearr[k].length + 1 < limit){                smerge = smerge + " " + spacearr[k];                k++;             }             res.push(smerge)          }        }else{          res.push(comarr[j]);        }     }   }else{     res.push(dotarr[i]);   } }  //res contain optimized sentences. 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -