Keyword Groups Match & Diversion
What is your Result?
- When user types the question (i.e. Do you support WhatsApp Chatbot Solution?), the FAQ module will scan the entire data source to see which entry has the highest number of keyword group(s) that matched with the input (score).
Data Source Example of FAQ Chatbot with Keyword Groups Match & Diversion
- Once matched with an FAQ entry, the chatbot will display the answer of the corresponding entry (In this case, a text response.).
Example of FAQ Chatbot with Keyword Groups Match & Diversion 1
- If there are multiple entries with the same score, the chatbot will display all matched questions (diversion).
Example of FAQ Chatbot with Keyword Groups Match & Diversion 2
FAQ Data Source Format
Please refer to level one procedure for the the FAQ Data Source Format. To allow more variations, you may just add additional columns for extra keyword groups on the data source.
You may download the sample FAQ data source in .CSV here.
Getting Hands-on
Sample Tree Structure
Tree Structure of FAQ Chatbot with Keyword Groups Match & Diversion
Edit the Tree Node for FAQ Module
Before proceeding, you should have completed the level two FAQ Chatbot with Exact Keyword Match and Diversion. You may edit the level two tree directly or duplicate a new tree.
Edit the existing pre-action (this is for handling the keyword groups matching logic) in the FAQ Module tree node with the following code (Remember to apply the Data Source ID to collectionName in the code):
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)
}
})
- Check and see if you can produce the expected outcome.