简体   繁体   English

如何从RiTa.js随机字生成器中删除空格?

[英]How to remove space from RiTa.js random word generator?

I'm working on a word generator inspired by Daniel Shiffman's demo of the RiTa library. 我正在研究一个由Daniel Shiffman的RiTa库演示启发的单词生成器。 Right now, the code adds a space between all words and punctuation using the line: 现在,代码使用以下行在所有单词和标点符号之间添加一个空格:

output += " ";

I have been trying to figure out how to change the code so that space does not appear between punctuation (such as periods) and words. 我一直试图找出如何更改代码,以便在标点符号(如句点)和单词之间不显示空格。 I think the simplest way to do this would be to use an if/else statement that leaves punctuation unaltered but adds space to words, but I am having a hard time figuring out what functions from the Rita library to use for this , as well as the syntax. 我认为最简单的方法是使用if / else语句使标点符号保持不变但为文字增加空间,但我很难弄清楚Rita库中的哪些函数用于此 ,以及语法。

Any ideas? 有任何想法吗? Here's my code right now: 这是我现在的代码:

var input;
var button;
var lexicon;

function setup() {
  noCanvas();
  lexicon = new RiLexicon();

  input = createInput('As I say a noun is the name of a thing.');
  button = createButton('submit');
  input.changed(processRita);
  button.mousePressed(processRita);
  input.size(400);
}

function processRita() {
var s = input.value();
var rs = new RiString(s);
var words = rs.words();
var pos = rs.pos();
console.log(words);
console.log(pos);

var output = '';

for (var i = 0; i < words.length; i++) { 
   if (/nn.*/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      if(alliterations.length == 0){
        output+=words[i];
      }else{
         output += alliterations[Math.floor(Math.random() * alliterations.length)];
      }
      //console.log("noun");
      //console.log(alliterations.length);
    } else if (/jj.*/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      output += alliterations[Math.floor(Math.random() * alliterations.length)];
      //console.log("adjective");
    } else if (/vb/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      output += alliterations[Math.floor(Math.random() * alliterations.length)];
      //console.log("verbs");
    } 
    else {
      //console.log(words[i]);
      output += words[i];
  } {
       output += " ";
  }
  }
  createP(output);

}

Why do you need a library for this? 为什么你需要一个库? Can't you just use the regular String functions to test whether a String is a punctuation mark? 难道你不能只使用常规的String函数来测试String是否是标点符号?

You could just use a regular expression to test whether a String matches a punctuation character. 您可以使用正则表达式来测试String是否与标点符号匹配。 Or just use a series of equality checks against each punctuation mark you care about. 或者只对你关心的每个标点符号使用一系列相等检查。

You might also check out the startsWith() function and the endsWith() function . 您还可以查看startsWith()函数endsWith()函数

after much trial and error, I had help from a coding professor who helped me to solve this problem, which was more complicated than I originally anticipated. 经过多次反复试验,我得到了一位帮助我解决这个问题的编码教授的帮助,这个问题比我原先预想的要复杂得多。 In order to get this code to work, we added this bit toward the beginning of the for loop: 为了使这段代码有效,我们将这一位添加到for循环的开头:

if(words[i] == "." || words[i] == "," || words[i] == "?" || words[i] == "!"){ output += words[i]; if(words [i] ==“。”|| words [i] ==“,”|| words [i] ==“?”|| words [i] ==“!”){output + = words [一世]; }else{ output += " "; } else {output + =“”;

So the entire code now looks like this: 所以整个代码现在看起来像这样:

for (var i = 0; i < words.length; i++) { 



if(words[i] == "." || words[i] == "," || words[i] == "?" || words[i] == "!"){
    output += words[i];
  }else{
    output += " ";
   if (/nn.*/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      if(alliterations.length == 0){
        output+=words[i];
      }else{
         output += alliterations[Math.floor(Math.random() * alliterations.length)];
      }
      //console.log("noun");
      //console.log(alliterations.length);
    } else if (/jj.*/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      output += alliterations[Math.floor(Math.random() * alliterations.length)];
      //console.log("adjective");
    } else if (/vb/.test(pos[i])) {
      var alliterations = lexicon.alliterations(words[i]);
      output += alliterations[Math.floor(Math.random() * alliterations.length)];
      //console.log("verbs");
    } 
    else {
      //console.log(words[i]);
      output += words[i];
  }

}


  }
  createP(output);

}

Its much simpler if you use the RiTa library functions: 如果您使用RiTa库函数,它更简单:

function processRita() {

  var all, output = [],
    words = RiTa.tokenize(input.value()),
    pos = RiTa.getPosTags(words);

  for (var i = 0; i < words.length; i++) {
    if (/[nn|kk|vb|jj].*/.test(pos[i]) && (all = lexicon.alliterations(words[i])).length) {
      output.push(RiTa.randomItem(all));
    } else {
      output.push(words[i]);
    }
  }

  createP(RiTa.untokenize(output));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM