私はRiTaライブラリのDaniel Shiffmanのデモからインスパイアされたワードジェネレータに取り組んでいます。今、コード行を使用して、すべての単語や句読点の間にスペースを追加します。RiTa.jsランダムワードジェネレータからスペースを削除するには?
output += " ";
私はスペースが(このような期間など)句読点の間で表示されないようにコードを変更する方法を見つけ出すしようとしていると言葉。これを行う最も簡単な方法は、句読点を変更せずに単語にスペースを追加するif/else文を使用することですが、文法と同様にwhat functions from the Rita library to use for thisを調べるのは苦労しています。
アイデア?今すぐ私のコードがあります:
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);
}
>「あなただけに、正規表現を使用することができます文字列が句読文字と一致するかどうかをテストするか、気にする句読点ごとに一連の等価チェックを使用します。私はこれを行うことを考えていましたが、私はどのように機能を設定するのか分かりません。私はいろいろ試しましたが、コードを壊したり、スペースをずらしたりしました(元のコードのPOSの後にスペースを追加するだけです)。 –