2016-04-03 1 views
1

私はp5.jsのRiTaライブラリを使用して、魔法のような "狂った"ライブラリを作っています。文字を正しく出力するために、rita.tokenize関数を使用して、各単語/文字を配列の値にしました。次に、文字列を適切に出力するためにtextWidth()を使用し、単語がすべて実行されないようにすべての配列値を ""追加します。しかし、これを行うことによって、コンマ、ピリオド、疑問符などの周りに余分なスペースを入れてしまい、それが奇妙に見えます。どのようにこれらを選択的に削除するのですか?私はJavaScriptでそれを調べていましたが、p5.jsとRiTaのためのものは特に見つかりませんでした。句読点を特定し、これを修正するためにブール値文を使用する方法はありますか?または、私は配列を捨てて文字列を使ってこれをハードコードする必要がありますか?手伝ってくれてありがとう! (私がコメントしたビットを無視してください、私はいくつかの余分なテキストで遊んでいます)。 ClaireP5.jsの特殊文字の周りのスペースを削除する

//Generative Madlib poetry: Find a prose poetry text from Wikisource and 
//replace all nouns and adjectives with random nouns and adjectives. Try adding a rhymescheme. 

var whatupwalt = ("What is it then between us? What is the count of the scores or hundreds of years between us? Whatever it is, it avails not, distance avails not, and place avails not, I too lived, Brooklyn of ample hills was mine, I too walkd the streets of Manhattan island, and bathed in the waters around it.");  
var words = []; 
var lexicon; 
var wordPosX = 10; 
var wordPosY = 25; 
var credit = ("- Walt Whitman"); 
//var click = ("Walt Whitman MadLibs! Click to switch up the poem"); 


function setup() { 
    createCanvas(800,800); 
    lexicon = new RiLexicon(); 


    textSize(20); 
    fill(200,0, 20); 



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

    text(words[i], wordPosX, wordPosY); 
    textWidth(words[i], 10); 
    wordPosX += textWidth(words[i]+" "); 
    text(credit, 600, 100); 

    if(wordPosX > 700){ 
    wordPosX = 10; 
    wordPosY += 20; 
    } 
    } 
} 

function mousePressed() { 
    madLibs(); 

    textSize(12); 
    textAlign(LEFT, TOP); 

function madLibs() { 
    var words = "What is " + 
    lexicon.randomWord("nn") + 
    " then between us? What is the " + 
    lexicon.randomWord("nn") + 
    " of the " + 
    lexicon.randomWord("nn") + 
    " or hundreds of " + 
    lexicon.randomWord("nn") + 
    " between us? Whatever " + 
    lexicon.randomWord("nn") + 
    " is, " + 
    lexicon.randomWord("nn") + 
    " avails not, " + 
    lexicon.randomWord("nn") + 
    " avails not, and " + 
    lexicon.randomWord("nn") + 
    " avails not, I too lived, " + 
    lexicon.randomWord("nn") + 
    " of " + 
    lexicon.randomWord("jj") + " " + 
    lexicon.randomWord("nn") + 
    " was mine, I too walkd the " + 
    lexicon.randomWord("nn") + 
    " of " + 
    lexicon.randomWord("nn") + " " + 
    lexicon.randomWord("nn") + 
    " , and bathed in the " + 
    lexicon.randomWord("nn") + 
    " around " + 
    lexicon.randomWord("nn") + "." 


    background(255); 
    textSize(20); 
    text(words, 10, 10, 800-20, 800-20); 
    credit = "-" + " " + lexicon.randomWord("nn") + " " + lexicon.randomWord("jj") 
    text(credit, 580, 120); 
    //rect(375, 240, 375, 220, 400, 240, 400, 220); 
    //text(click, 400, 250) 

答えて

-1

単純なif文を使用して連結すると、スペースを削除できます。このような何か:

var wordOne = "one"; 
var wordTwo = ", three"; 

var combined; 
if(wordTwo.startsWidth(",")){ 
    combined = wordOne + wordTwo; 
} 
else{ 
    combined = wordOne + " " + wordTwo; 
} 

あなたはまた、単にあなたがそれを作成した後、あなたの文字列を修正するために、正規表現とreplace()機能を使用することができます。

var wordOne = "one"; 
var wordTwo = ", three"; 
combined = wordOne + " " + wordTwo; 
combined = combined.replace(/ ,/g, ','); 
+0

ありがとう、ケビン!私は論理を理解しようとしています... "one"と "three"を何かに置き換えるか、このコードをそのままにしますか? – Claire

+0

@Claireこのコードは単なる例にすぎません。あなたは、あなたが参加しているにもかかわらず、ロジックを変更するでしょう。しかし、基本は同じです:ifステートメントを使用して、適切なときだけスペースを追加します。 –

+0

申し訳ありませんが、私はまだ混乱しています...非常に新しいp5.js!だから私は言葉でこの言葉をしなければならなかったでしょうか?インスタンスごとに?どういうわけか何かを文法的なキャラクターとして認識し、それを取り除く方法はありません。 – Claire

-1

だけのアレイに単語を追加、とリタを使用.untokenize();

var words = ['one', ',', 'two', ',', 'three', '.']; 
var result = RiTa.untokenize(words); 
console.log(result); // -> "one, two, three." 
関連する問題