【问题标题】:How can I validate a sentence in a javascript using Levenshtein Distance?如何使用 Levenshtein Distance 验证 javascript 中的句子?
【发布时间】:2021-06-22 03:57:34
【问题描述】:

我正在尝试使用 Levenshtein Distance 比较两个句子(“Kat”并输入“Spat”)。如果句子足够相似,我希望出现“正确”的文本。我复制的脚本运行良好,但我在使用 If 语句时遇到了问题。如果 Levenshtein 距离测量为“2”(与“Kat”和“Spat”一样),我希望出现“正确”的文本,但我不知道应该将哪个变量设置为“ 2"。

//Codes by Jared Stilwell
var matrix;
var first = "Kat";

function levenshteinDistance(a, b) {
  if (b.length == 0) return a.length;

  matrix = [];

  // increment along the first column of each row
  for (var i = 0; i <= b.length; i++) {
    matrix[i] = [i];
  }

  // increment each column in the first row
  for (var j = 0; j <= a.length; j++) {
    matrix[0][j] = j;
  }

  // fill in the rest of the matrix
  for (i = 1; i <= b.length; i++) {
    for (j = 1; j <= a.length; j++) {
      if (b.charAt(i - 1) == a.charAt(j - 1)) {
        matrix[i][j] = matrix[i - 1][j - 1];
      } else {
        matrix[i][j] = Math.min(
          matrix[i - 1][j - 1] + 1, // substitution
          Math.min(
            matrix[i][j - 1] + 1, // insertion
            matrix[i - 1][j] + 1
          )
        ); // deletion
      }
    }
  }

  return matrix[b.length][a.length];
}

function updateDistance() {
  var second = $(".term-2").val();
  $(".js-distance").text(levenshteinDistance(first, second));
}

$(".term-2").on("keyup", function(ev) {
  ev.preventDefault();
  updateDistance();
});

updateDistance();


//My own part which I can't figure out
function testknap() {
  if (matrix == 2) // I CAN'T FIGURE OUT THIS PART?
    document.getElementById("correct_text_here").innerHTML = "Correct";
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="js-distance">0</div>
<!-- The distance points -->
<!-- Textform -->
<input class="term-2" value="Spat" placeholder="Skriv her">


<button onclick="testknap()">Tryk</button>
<!-- A submit button -->
<!-- Text which appear if the input is correct -->
<p id="correct_text_here"></p>

【问题讨论】:

    标签: javascript levenshtein-distance


    【解决方案1】:

    只需读取值

    function testknap() {
      if (document.querySelector(".js-distance").textContent == 2)
        document.getElementById("correct_text_here").innerHTML = "Correct";
    };
    

    //Codes by Jared Stilwell
    var matrix;
    var first = "Kat";
    
    function levenshteinDistance(a, b) {
      if (b.length == 0) return a.length;
    
      matrix = [];
    
      // increment along the first column of each row
      for (var i = 0; i <= b.length; i++) {
        matrix[i] = [i];
      }
    
      // increment each column in the first row
      for (var j = 0; j <= a.length; j++) {
        matrix[0][j] = j;
      }
    
      // fill in the rest of the matrix
      for (i = 1; i <= b.length; i++) {
        for (j = 1; j <= a.length; j++) {
          if (b.charAt(i - 1) == a.charAt(j - 1)) {
            matrix[i][j] = matrix[i - 1][j - 1];
          } else {
            matrix[i][j] = Math.min(
              matrix[i - 1][j - 1] + 1, // substitution
              Math.min(
                matrix[i][j - 1] + 1, // insertion
                matrix[i - 1][j] + 1
              )
            ); // deletion
          }
        }
      }
    
      return matrix[b.length][a.length];
    }
    
    function updateDistance() {
      var second = $(".term-2").val();
      $(".js-distance").text(levenshteinDistance(first, second));
    }
    
    $(".term-2").on("keyup", function(ev) {
      ev.preventDefault();
      updateDistance();
    });
    
    updateDistance();
    
    
    
    function testknap() {
      if (document.querySelector(".js-distance").textContent == 2)
        document.getElementById("correct_text_here").innerHTML = "Correct";
    };
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="js-distance">0</div>
    <!-- The distance points -->
    <!-- Textform -->
    <input class="term-2" value="Spat" placeholder="Skriv her">
    
    
    <button onclick="testknap()">Tryk</button>
    <!-- A submit button -->
    <!-- Text which appear if the input is correct -->
    <p id="correct_text_here"></p>

    【讨论】:

      最近更新 更多