Commit f37b1f06 authored by 李维's avatar 李维

添加关键词按序匹配

parent 929dfd03
...@@ -1809,7 +1809,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1809,7 +1809,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
let configInputText = contentData.inputText; let configInputText = contentData.inputText;
if(contentData.keyWordMatch) { 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) { } else if(contentData.openAnswer) {
// 开放型回答 只要有内容就算对 // 开放型回答 只要有内容就算对
right = userInputText != ""; right = userInputText != "";
...@@ -1894,7 +1898,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1894,7 +1898,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
let configInputText = option.inputText; let configInputText = option.inputText;
if(option.keyWordMatch) { 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) { } else if(option.openAnswer) {
// 开放型回答 只要有内容就算对 // 开放型回答 只要有内容就算对
right = userInputText != ""; right = userInputText != "";
...@@ -2420,14 +2428,18 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -2420,14 +2428,18 @@ export default class SceneComponent extends MyCocosSceneComponent {
let right = false; let right = false;
// 判断是否启用关键词匹配 // 判断是否启用关键词匹配
if(contentData.keyWordMatch) { 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) { } else if(contentData.openAnswer) {
// 开放型回答 只要有内容就算对 // 开放型回答 只要有内容就算对
right = recordText != "" right = recordText != ""
}else { }else {
if(contentData.isCaseInsensitive) { if(contentData.isCaseInsensitive) {
// 不区分大小写 // 不区分大小写
right = recordText.toLocaleLowerCase() == evaText.toLocaleLowerCase(); right = recordText.toLowerCase() == evaText.toLowerCase();
} else { } else {
// 区分大小写 完全相等 // 区分大小写 完全相等
right = recordText == evaText; right = recordText == evaText;
...@@ -2910,7 +2922,13 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -2910,7 +2922,13 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
// 模糊匹配字符串 1,2,3 // 模糊匹配字符串 1,2,3
fuzzyMatchingString(testString, matchString, isCaseInsensitive){ fuzzyMatchingString(testString, matchString, options){
const {
isCaseInsensitive,
keyWordMatchInOrder,
notAdaptContraction
} = options;
matchString.replace(/,/g,","); matchString.replace(/,/g,",");
const _keyWordGroup = matchString.split("|"); // 大的分组 任何一个组匹配了 都算对 const _keyWordGroup = matchString.split("|"); // 大的分组 任何一个组匹配了 都算对
...@@ -2921,27 +2939,61 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -2921,27 +2939,61 @@ export default class SceneComponent extends MyCocosSceneComponent {
}); });
let rightInGroup = false; let rightInGroup = false;
keyWordGroup.forEach(gpKeys => { if(keyWordMatchInOrder) {
// 如果没有正确分组 进行查找 // 关键词按序匹配
if(!rightInGroup) { keyWordGroup.forEach(gpKeys => {
// 分组查找 // 如果没有正确分组 进行查找
const result = gpKeys.find(key=>{ if(!rightInGroup) {
// console.log(key) // 分组查找
// console.log(testString.toLocaleLowerCase()) let keyIndex = -1;
// console.log(testString.toLocaleLowerCase().indexOf(key.toLocaleLowerCase())) let matched = true;
if(isCaseInsensitive) { const result = gpKeys.forEach(key=>{
// 不区分大小写 // 判断当前组是否还符合匹配顺序
return testString.toLocaleLowerCase().indexOf(key.toLocaleLowerCase()) == -1 if(matched) {
} else { let _keyIndex = -1;
// 区分大小写 if(isCaseInsensitive) {
return testString.indexOf(key) == -1 // 不区分大小写
_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; 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