Search code examples
javascriptvariablesprocessingp5.js

Cannot Figure out Why Variable is Undefined in p5.js


I'm sure this is the simplest question ever, but my friend and I are pair coding in p5.js and cannot figure out why the below code is throwing the error: Uncaught ReferenceError: RiLexicon is not defined

We've done a similar project with all code in basically the exact same place, and it works. Maybe we've been staring at this too long and are going insane? Please help! Thank you!

var mermaid = ("Fishes, both large and small, glide between the branches, as birds fly among the trees here upon land. In the deepest spot of all, stands the castle of the Sea King. Its walls are built of coral, and the long, gothic windows are of the clearest amber. The roof is formed of shells, that open and close as the water flows over them. Their appearance is very beautiful, for in each lies a glittering pearl, which would be fit for the diadem of a queen.");
var story = []; 
var lexicon;


function setup() {

    createCanvas(650,400);
    lexicon = new RiLexicon();
    story = RiTa.tokenize(mermaid);
    //partsOfSpeech = RiTa.getPosTags(story);
    textSize(15);
    fill(255, 100, 100);


function draw(){
 // background(255);

 var wordPosX = 10;
var wordPosY = width/8;


    for(var i=0; i < story.length; i++){
      text(story[i], wordPosX, wordPosY)
      textWidth(story[i]+5,30);

      //we check whether each parts of speech exists
      //in our array
      //if(partsOfSpeech[i] != null){
      //  text(partsOfSpeech[i], wordPosX, wordPosY+20, textWidth(story[i]), 20);
     // fill(100,175,175);  


      wordPosX += textWidth(story[i])+ 3;

      //if wordPosX goes beyond our width,
      //move the text down to a new line
      if(wordPosX +30 > width){
        wordPosX = 10;
        wordPosY += 50;
      }
    }
}
      function mousePressed(){
        changingWords();
        textSize(15);

        function changingWords (){
        var story = "Fishes," + 
          lexicon.randomWord("nn") + "" + 
          lexicon.randomWord("jj") + 
          "and" + lexicon.randomWord("jj") + 
          ", glide between the branches, as birds fly among the trees here upon" + 
          lexicon.randomWord("nn") + 
          ". In the deepest" + lexicon.randomWord("nn") + 
          "of all, stands the" + lexicon.randomWord("nn") + 
          "of the Sea King. Its walls are built of" + 
          lexicon.randomWord("jj") + 
          ", and the" + lexicon.randomWord("jj") +
          "," + lexicon.randomWord("jj") + 
          "windows are of the clearest" + 
          lexicon.randomWord("jj") + 
          ". The" + lexicon.randomWord("nn") + 
          "is formed of shells, that" + 
          lexicon.randomWord("jj") + 
          "and close as the" + 
          lexicon.randomWord("nn") + 
          "flows over them. Their" + 
          lexicon.randomWord("nn") + 
          "is very" + lexicon.randomWord("jj") + 
          ", for in each lies a glittering" + 
          lexicon.randomWord("nn") + 
          ", which would be fit for the" + 
          lexicon.randomWord("nn") + 
          "of a queen."
        }  
    }
 }

Solution

  • Like the comments said, you need to make sure you're loading the RiTa library.

    It sounds like maybe your previous sketch already loaded the library, and then you copied some code from that sketch into a new one. The problem is, that's not enough to make your new sketch work. You have to load the library into your new sketch so that you can access it.

    Here is a really great tutorial on how to load a library, but I'll try to copy the basics here:

    Step 1: Download RiTa from here.

    Step 2: Find a file named rita-full.min.js (you could also download it directly from here).

    Step 3: Copy that file into your sketch directory. It should be accessible by your html file.

    Step 4: Edit your html file to load the library by putting this line in the <head> section:

    <script src="your/path/to/rita-full.min.js" type="text/javascript"></script>
    

    Step 5: Now when you load your html page, the library will be loaded, and your code will be able to access it.

    You can find more information on RiTaJS's GitHub page here, or in this RiTaJS tutorial.