/** * 【高复用】分析文本行为类型天气 / 行动 / 感受 / 未知 * param {string} text - OCR/对话转义后的文本 * returns {string} 行为类型 */ function analyzeBehaviorType(text) { const lower text?.toLowerCase() || ; // 行为关键词库可自行扩展 const weatherWords [天气, 阳光, 明媚, 微风, 晴朗, 下雨]; const actionWords [出门, 散步, 走走, 游玩, 运动, 逛街]; const moodWords [舒服, 开心, 惬意, 轻松, 愉快, 幸福]; if (weatherWords.some(w lower.includes(w))) return weather; if (actionWords.some(w lower.includes(w))) return action; if (moodWords.some(w lower.includes(w))) return mood; return unknown; } /** * 【高复用】根据上下文预测下一词概率模拟输入法/AI生成 * param {string} context - 当前上下文文本 * param {string[]} wordList - 你的候选词组库 * returns {Array{word:string, prob:number}} 按概率降序 */ function predictNextWords(context, wordList) { const type analyzeBehaviorType(context); const isLong context?.length 8; const result []; for (const word of wordList) { let prob 0.02; // 行为类型 → 自动分配概率权重 if (type weather) { if ([阳光, 明媚, 微风].includes(word)) prob 0.25; if ([适合, 出门].includes(word)) prob 0.15; } if (type action) { if ([散步, 走走, 游玩].includes(word)) prob 0.28; if ([舒服, 开心, 惬意].includes(word)) prob 0.18; } if (type mood) { if (word 。) prob 0.35; } // 长文本 → 提高结束符概率 if (isLong word 。) prob 0.2; result.push({ word, prob: Math.min(prob, 1) }); } return result.sort((a, b) b.prob - a.prob); } /** * 【工具】打印候选词调试用 */ function showPredict(context, wordList) { console.log(); console.log(输入文本:, context); console.log(行为类型:, analyzeBehaviorType(context)); const list predictNextWords(context, wordList); list.forEach((i, idx) { console.log(${idx1}. ${i.word} (${i.prob.toFixed(2)})); }); console.log(\n); }1.analyzeBehaviorType(text)输入任意文本OCR / 对话输出行为类型天气 / 行动 / 感受 / 未知2.predictNextWords(text, wordList)输入任意文本 你的词库输出按概率排序的下一词列表完全模拟输入法