You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

p5.js使用RiTa.js时报错RiLexicon未定义问题求助

错误原因

两个报错直接关联,核心原因如下:

  • 你使用的是2.x及以上版本的RiTa.js,该版本已移除RiLexicon类,原RiLexicon的所有能力都整合到全局RiTa对象中,直接调用new RiLexicon()会触发未定义错误
  • 你在全局作用域初始化lexicon变量,此时RiTa库还未完成加载,变量本身为undefined,后续调用isNoun方法自然会触发属性读取失败错误
  • 额外需确认:项目入口html中已正确引入RiTa.js库,且加载顺序早于你的sketch代码
修复方案

无需新建lexicon实例,所有原来调用lexicon的方法直接替换为RiTa对象调用即可,修改后可运行的完整代码如下:

let poem = "STOPPING BY WOODS ON A SNOWY EVENING WHOSE woods these are I think I know. His house is in the village though; He will not see me stopping here To watch his woods fill up with snow. My little horse must think it queer To stop without a farmhouse near Between the woods and frozen lake The darkest evening of the year. He gives his harness bells a shake To ask if there is some mistake. The only other sound's the sweep Of easy wind and downy flake. The woods are lovely, dark and deep. But I have promises to keep, And miles to go before I sleep, And miles to go before I sleep."

let words;
let newPoem = "";

function setup() {
    createCanvas(windowWidth, windowHeight);
    background(100);
    
    // 将诗歌字符串分词为单词
    words = RiTa.tokenize(poem);
    // 遍历所有单词,替换名词和形容词为随机同词性单词
    for (i=0; i<words.length; i++){
        let currentWord = words[i];
        if (RiTa.isNoun(currentWord)){
            currentWord = RiTa.randomWord("nn");
        }
        else if (RiTa.isAdjective(currentWord)){
            currentWord = RiTa.randomWord("jj");
        }
        newPoem += currentWord + " ";
    }
    print(newPoem);
}
function draw() {
    textSize(16);
    text(newPoem, width/20, height/4, 500, 800);
}

内容的提问来源于stack exchange,提问作者Nareh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 01:06:03