最高关键词组匹配和问题转移
预期结果?
- 当用户输入问题(即您是否支持WhatsApp Chatbot Solution?)时,FAQ 模块会扫描整个数据源以查看哪个条目具有与输入匹配的最高关键字组数 .
Data Source Example of FAQ Chatbot with Keyword Groups Match & Diversion
- 一旦与 FAQ 条目匹配,聊天机器人将显示相应条目的答案(在这种情况下,文本响应。)。
Example of FAQ Chatbot with Keyword Groups Match & Diversion 1
3.如果有多个条目与相同的最高关键字组数匹配,则聊天机器人将显示相似匹配的问题(转移)。
Example of FAQ Chatbot with Keyword Groups Match & Diversion 2
FAQ 数据源格式
FAQ数据源格式请参考一级程序。 唯一的区别是关键字组的数量最多为 7 个,以允许更多变体。
动手实践
示例树结构
Tree Structure of FAQ Chatbot with Keyword Groups Match & Diversion
编辑常见问题模块的树节点
在继续之前,您应该已经完成了带有精确关键字匹配和转移的第二级常见问题聊天机器人 . 您可以直接编辑二级树或复制为新树。
编辑FAQ Module树节点中已有的pre-action(这是为了处理关键词组匹配逻辑),代码如下(记得在代码中将Data Source ID应用到collectionName):
return new Promise(async (resolve, reject) => {
try {
let text = this.messageEvent.data.text
text = text.replace(/\/n/g, "")
// text = text.replace(/(\/|\.|\*|\?|\+)/g, "")
let result = await this.fetchDataFromDataSource({
collectionName: "enter the Data Source ID here",
filter: {}
})
let maxScore = 0
result = result.map((doc) => {
try {
let score = 0
if (this.lodash.isArray(doc["Keyword Group 1"]) && this.lodash.get(doc, "Keyword Group 1.length")) {
let keywords = doc["Keyword Group 1"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 2"]) && this.lodash.get(doc, "Keyword Group 2.length")) {
let keywords = doc["Keyword Group 2"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 3"]) && this.lodash.get(doc, "Keyword Group 3.length")) {
let keywords = doc["Keyword Group 3"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 4"]) && this.lodash.get(doc, "Keyword Group 4.length")) {
let keywords = doc["Keyword Group 4"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 5"]) && this.lodash.get(doc, "Keyword Group 5.length")) {
let keywords = doc["Keyword Group 5"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 6"]) && this.lodash.get(doc, "Keyword Group 6.length")) {
let keywords = doc["Keyword Group 6"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (this.lodash.isArray(doc["Keyword Group 7"]) && this.lodash.get(doc, "Keyword Group 7.length")) {
let keywords = doc["Keyword Group 7"]
keywords = keywords.map((str) => {
str = str.replace("+", "\\+")
str = str.replace("$", "\\$")
str = str.replace(".", "\\.")
str = str.replace("!", "\\!")
return str
})
let reg = new RegExp(keywords.join("|"), "i")
if (reg.test(text)) {
score++
}
}
if (score > maxScore) {
maxScore = score
}
doc.score = score
} catch (error) {
console.log("doc", doc)
console.log("error", error)
}
return doc
})
console.log("maxScore", maxScore)
let ans = result.filter((doc) => {
return doc.score === maxScore && doc.score > 0
})
ans = ans.slice(0, 10)
console.log("ans", ans)
this.member.botMeta.tempData.faqAns = ans
resolve({
member: this.member,
})
} catch (e) {
reject(e)
}
})
3.检查是否可以产生预期结果.