Commit f37b1f06 authored by 李维's avatar 李维

添加关键词按序匹配

parent 929dfd03
......@@ -1809,7 +1809,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
let configInputText = contentData.inputText;
if(contentData.keyWordMatch) {
// 关键词匹配,只有回答文字中包含全部关键词,就算对
right = this.fuzzyMatchingString(userInputText, configInputText, contentData.isCaseInsensitive);
right = this.fuzzyMatchingString(userInputText, configInputText, {
isCaseInsensitive: contentData.isCaseInsensitive ? true : false,
keyWordMatchInOrder: contentData.keyWordMatchInOrder ? true : false,
notAdaptContraction: contentData.notAdaptContraction ? true : false
});
} else if(contentData.openAnswer) {
// 开放型回答 只要有内容就算对
right = userInputText != "";
......@@ -1894,7 +1898,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
let configInputText = option.inputText;
if(option.keyWordMatch) {
// 关键词匹配,只有回答文字中包含全部关键词,就算对
right = this.fuzzyMatchingString(userInputText, configInputText, option.isCaseInsensitive);
right = this.fuzzyMatchingString(userInputText, configInputText, {
isCaseInsensitive: option.isCaseInsensitive ? true : false,
keyWordMatchInOrder: option.keyWordMatchInOrder ? true : false,
notAdaptContraction: option.notAdaptContraction ? true : false
});
} else if(option.openAnswer) {
// 开放型回答 只要有内容就算对
right = userInputText != "";
......@@ -2420,14 +2428,18 @@ export default class SceneComponent extends MyCocosSceneComponent {
let right = false;
// 判断是否启用关键词匹配
if(contentData.keyWordMatch) {
right = this.fuzzyMatchingString(recordText, evaText, contentData.isCaseInsensitive)
right = this.fuzzyMatchingString(recordText, evaText, {
isCaseInsensitive: contentData.isCaseInsensitive ? true : false,
keyWordMatchInOrder: contentData.keyWordMatchInOrder ? true : false,
notAdaptContraction: contentData.notAdaptContraction ? true : false
})
} else if(contentData.openAnswer) {
// 开放型回答 只要有内容就算对
right = recordText != ""
}else {
if(contentData.isCaseInsensitive) {
// 不区分大小写
right = recordText.toLocaleLowerCase() == evaText.toLocaleLowerCase();
right = recordText.toLowerCase() == evaText.toLowerCase();
} else {
// 区分大小写 完全相等
right = recordText == evaText;
......@@ -2910,7 +2922,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
}
// 模糊匹配字符串 1,2,3
fuzzyMatchingString(testString, matchString, isCaseInsensitive){
fuzzyMatchingString(testString, matchString, options){
const {
isCaseInsensitive,
keyWordMatchInOrder,
notAdaptContraction
} = options;
matchString.replace(/,/g,",");
const _keyWordGroup = matchString.split("|"); // 大的分组 任何一个组匹配了 都算对
......@@ -2921,27 +2939,61 @@ export default class SceneComponent extends MyCocosSceneComponent {
});
let rightInGroup = false;
keyWordGroup.forEach(gpKeys => {
// 如果没有正确分组 进行查找
if(!rightInGroup) {
// 分组查找
const result = gpKeys.find(key=>{
// console.log(key)
// console.log(testString.toLocaleLowerCase())
// console.log(testString.toLocaleLowerCase().indexOf(key.toLocaleLowerCase()))
if(isCaseInsensitive) {
// 不区分大小写
return testString.toLocaleLowerCase().indexOf(key.toLocaleLowerCase()) == -1
} else {
// 区分大小写
return testString.indexOf(key) == -1
if(keyWordMatchInOrder) {
// 关键词按序匹配
keyWordGroup.forEach(gpKeys => {
// 如果没有正确分组 进行查找
if(!rightInGroup) {
// 分组查找
let keyIndex = -1;
let matched = true;
const result = gpKeys.forEach(key=>{
// 判断当前组是否还符合匹配顺序
if(matched) {
let _keyIndex = -1;
if(isCaseInsensitive) {
// 不区分大小写
_keyIndex = testString.toLowerCase().indexOf(key.toLowerCase())
} else {
// 区分大小写
_keyIndex = testString.indexOf(key)
}
// 如果找到的索引 比 上一次找到的索引小 则认为不符合
if(_keyIndex < keyIndex) {
matched = false;
} else {
keyIndex = _keyIndex;
}
}
})
// 如果符合 并且索引大于0
if(matched && keyIndex >= 0) {
rightInGroup = true;
}
})
if(result == undefined) {
rightInGroup = true;
}
}
});
});
} else {
keyWordGroup.forEach(gpKeys => {
// 如果没有正确分组 进行查找
if(!rightInGroup) {
// 分组查找
const result = gpKeys.find(key=>{
if(isCaseInsensitive) {
// 不区分大小写
return testString.toLowerCase().indexOf(key.toLowerCase()) == -1
} else {
// 区分大小写
return testString.indexOf(key) == -1
}
})
if(result == undefined) {
rightInGroup = true;
}
}
});
}
return rightInGroup;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment