首页 > 解决方案 > 比较两个句子并识别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;
}

标签: javascript

解决方案


我将通过以下步骤递归地解决问题:

  1. 尝试在每个句子中找到最接近的匹配词
  2. 如果找到匹配:在匹配的单词处拆分每个句子并在每一侧运行相同的函数
  3. 如果未找到匹配项:两个句子没有任何共同点,您可以返回以您想要的方式格式化的每个句子

注意:我开始并尝试找到最长的匹配词,因为它们最能指示句子结构,而不是搜索“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)


推荐阅读