javascript - 比较两个句子并识别javascript中的错误单词
问题描述
我想比较两个句子。这是示例:
正确:专家认为工业发展将有助于经济。
鉴于:Xperts 确实相信发展不会经济。
预期输出:
专家 Xperts 确实相信工业 发展不会帮助经济。_ _
我试图通过拆分两个单词并检查它们来比较字符串。
let given= "Xperts does believe that development won't economy.";
let correct= "Experts believe industrial development will help the economy.";
function checkSentence(given,correct){
let final='';
for(i=0; i<given.length; i++){
if(given[i].trim()==correct[i].trim()){
final += given[i]+' ';
}else{
final += "<i>given[i]</i> <b>correct[i]</b>";
}
}
return final;
}
解决方案
我将通过以下步骤递归地解决问题:
- 尝试在每个句子中找到最接近的匹配词
- 如果找到匹配:在匹配的单词处拆分每个句子并在每一侧运行相同的函数
- 如果未找到匹配项:两个句子没有任何共同点,您可以返回以您想要的方式格式化的每个句子
注意:我开始并尝试找到最长的匹配词,因为它们最能指示句子结构,而不是搜索“it”和“and”
const correct= "Experts believe industrial development will help the economy.";
const given= "Xperts does believe development that won't economy.";
const correctArray = correct.split(" ")
const givenArray = given.split(" ")
// Returns [correctIndex, givenIndex] if match found or [-1, -1] if no match found
function findIndexOfLongestMatchingWord(correctArray, givenArray) {
// Create an array of word length and its index for the correct word array
const correctWordLengthIndexArray = correctArray.map((word, index) => [word.length, index]).sort(([length1, index1], [length2, index2]) => length2 - length1)
for(let matchingIndex = 0; matchingIndex < correctArray.length; matchingIndex++) {
const correctArrayIndex = correctWordLengthIndexArray[matchingIndex][1]
const correctArrayWord = correctArray[correctArrayIndex]
const foundIndexOfGivenWord = givenArray.findIndex(givenWord => givenWord === correctArrayWord)
if(foundIndexOfGivenWord != -1) return [correctArrayIndex, foundIndexOfGivenWord];
}
return [-1,-1]
}
function formatCorrectArray(correctArray) {
return correctArray.length == 0 ? "" : `<b>${correctArray.join(" ")}</b>`
}
function formatGivenArray(givenArray) {
return givenArray.length == 0 ? "" : `<i>${givenArray.join(" ")}</i>`
}
function findDifferenceRecursively(correctArray, givenArray) {
// If either array empty there is nothing to compare, return each one formatted
if(correctArray.length == 0 || givenArray.length == 0) {
return formatCorrectArray(correctArray) + formatGivenArray(givenArray)
}
const [correctIndex, givenIndex] = findIndexOfLongestMatchingWord(correctArray, givenArray);
if (correctIndex != -1) {
// Split each string at their index and run find difference on each side of the indexes;
const leftCorrect = correctArray.slice(0, correctIndex)
const rightCorrect = correctArray.slice(correctIndex + 1)
const leftGiven = givenArray.slice(0, givenIndex)
const rightGiven = givenArray.slice(givenIndex + 1)
// Run function on words on each side
return findDifferenceRecursively(leftCorrect, leftGiven) + ` ${correctArray[correctIndex]} ` + findDifferenceRecursively(rightCorrect, rightGiven)
} else {
return formatCorrectArray(correctArray) + formatGivenArray(givenArray)
}
}
const result = findDifferenceRecursively(correctArray, givenArray)
// Returns "<b>Experts</b><i>Xperts does</i> believe <b>industrial</b> development <b>will help the</b><i>that won't</i> economy. "
console.log(result)
推荐阅读
- docker - 在 Alpine Linux 上运行“chpasswd”时出现“PAM:身份验证失败”
- ruby-on-rails - 如何在生产中将示例数据填充到我的 Rails 应用程序?
- reactjs - React-Redux - 动作返回未定义
- forms - 具有 bloc 模式的 TextFormField 是 Subbmiting null
- reactjs - TypeError:无法读取未定义的 mapStateToProps 的属性
- javascript - 在 Angular 应用程序中全局使用 ngx-toastr
- c# - 从 C# 运行 PowerShell 脚本时 Add-PSSnapin 的路径不正确
- javascript - 将 highcharts / stockchart 导出为图像与 Web 浏览器中的不同
- python - 如何在另一个模型中从 django 用户获取特定用户
- hibernate - JPA:尝试从双向多对多关系中删除实体