0

我正在制作一个脚本,它接收一个字符串并将其分隔在较小的字符串上。

例如:“这句话很长,我把它分成更小的部分。拉拉拉”

它将返回“This is a long sentence”、“我会将它分成更小的部分”、“Lalala”

这样做的目的是使用谷歌翻译器将文本转换为语音,但这个功能有大约 70-80 个字符的限制,所以如果字符串太大,我需要将其截断。

首先,我用点(“.”)分隔句子,然后如果句子仍然太长,我用逗号(“,”)分隔它们,如果字符串仍然太长,我将它们分隔成唯一的单词。

一切都很好,直到我尝试加入一些单词以使音频变得更加连续。由于某种原因,用逗号分隔的字符串再次连接起来。我不知道为什么。

这是代码:

编辑:相关部分拆分并格式化

function talk(text){
    var audios = document.createElement('audio');
audios.setAttribute('id','audio_speech');
var playlist = new Array()
if(text.length >= 75) {
    playlist = text.split(".");     
    for (var i = 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;     
               }        
        }
//...

编辑:完整的原始代码

function talk(text)
{
    var audios = document.createElement('audio');
    audios.setAttribute('id','audio_speech');
    var playlist = new Array()
    if(text.length >= 75) {
        playlist = text.split(".");

        for (var i = 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);
                i = 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>
4

1 回答 1

1

试试这个,修改它以使用您的常量和参数。

var LIMIT = 20;

var res = new Array()

//strats with spliting by dot
var dotArr = "This is a long sentence. and I will separate it into smaller parts. Lalala".split(/[.]/);

for (var i = 0; i < dotArr.length; i++) {
  if (dotArr[i].length > LIMIT){

    //only when have to, split by comma
    var comArr = dotArr[i].split(/[,]/);
    for (var j = 0; j < comArr.length; j++) {
       //only when have to and that a space exists, split by space
       if (comArr[j].length > LIMIT && comArr[j].indexOf(" ") != -1 ){
         var spaceArr = comArr[j].split(/[ ]/);
         //accomulate words until we reach the limit and then push the value to res
         for (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 all optimized sentences.
于 2013-03-01T16:52:20.040 回答