Search code examples
javascriptlevenshtein-distance

How can I validate a sentence in a javascript using Levenshtein Distance?


I am trying to compare two sentences ("Kat" and input "Spat") using Levenshtein Distance. If the sentences are similar enough, I want a "correct" text to appear. The script, which I have copied, works fine, but I am having troubles with the If-statement. I want a "correct" text to appear, if the Levenshtein distance is measured to "2" (As is the case with "Kat" and "Spat"), but I don't know which variable should be set as equal to "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>


Solution

  • Just read the value

    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>