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

如何在Node.js自制selfbot中提取用户提及的目标词汇?

手动实现问句中的目标词提取方案

嘿,这个需求我太懂了——做selfbot的时候,要从用户的自然问句里抓核心词汇,其实不用上复杂的NLP库,靠正则模式匹配就能搞定,毕竟你要处理的是特定类型的查询句式,手动写规则完全够用。

思路拆解

首先先梳理你要覆盖的常见问句模式,比如你提到的这两个:

  • "What does the word [词] mean"
  • "What is the meaning of [词]"
    再加上一些常见变体,比如"What's the meaning of [词]?"、"What does [词] mean?",把这些句式转化为正则表达式,就能精准捕获目标词。

代码实现(Node.js)

直接上可运行的代码,你可以根据自己的需求扩展更多模式:

function extractTargetTerm(userInput) {
  // 定义所有需要匹配的问句正则,i修饰符忽略大小写
  const queryPatterns = [
    /what does the word (\w+) mean/i,
    /what is the meaning of (\w+)/i,
    /what does (\w+) mean/i,
    /what's the meaning of (\w+)/i
  ];

  // 遍历每个模式尝试匹配
  for (const pattern of queryPatterns) {
    const matchResult = userInput.match(pattern);
    if (matchResult) {
      // 捕获组1就是我们要找的目标词
      return matchResult[1];
    }
  }

  // 没有匹配到的情况,返回null或者自定义提示
  return null;
}

// 测试几个例子
console.log(extractTargetTerm("What does the word baking mean")); // 输出: baking
console.log(extractTargetTerm("What is the meaning of baking ?")); // 输出: baking
console.log(extractTargetTerm("What does cooking mean?")); // 输出: cooking
console.log(extractTargetTerm("Hey, how are you?")); // 输出: null

扩展优化建议

如果之后需要支持短语(比如"What does 'apple pie' mean?"),可以调整正则的捕获组,把(\w+)改成([\w\s']+),这样就能匹配带空格和单引号的短语了。另外,还可以加入更多句式,比如"What's [词] definition?"之类的,只要把对应的正则加到queryPatterns数组里就行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 07:20:15