Answers.Body: <p>Maybe try this:</p>
  
  <pre><code>from functools import lru_cache
  from itertools import product
  
  @lru_cache(maxsize=4095)
  def ld(s, t):
      """
      Levenshtein distance memoized implementation from Rosetta code:
      https://rosettacode.org/wiki/Levenshtein_distance#Python
      """
      if not s: return len(t)
      if not t: return len(s)
      if s[0] == t[0]: return ld(s[1:], t[1:])
      l1 = ld(s, t[1:])      # Deletion.
      l2 = ld(s[1:], t)      # Insertion.
      l3 = ld(s[1:], t[1:])  # Substitution.
      return 1 + min(l1, l2, l3)
  
  
  a = "this is a sentence".split()
  b = "yet another cat thing".split()
  
  # To get the triplets.
  for i, j in product(a, b):
      print((i, j, ld(i, j)))
  </code></pre>
  
  <p>To get a matrix:</p>
  
  <pre><code>from scipy.sparse import coo_matrix
  import numpy as np
  
  a = "this is a sentence".split()
  b = "yet another cat thing , yes".split()
  
  tripets = np.array([(i, j, ld(w1, w2)) for (i, w1) , (j, w2) in product(enumerate(a), enumerate(b))])
  row, col, data = [np.squeeze(splt) for splt in np.hsplit(tripets, tripets.shape[-1])]
  coo_matrix((data, (row, col))).toarray()
  </code></pre>
  
  <p>[out]:</p>
  
  <pre><code>array([[4, 5, 4, 2, 4, 3],
         [3, 7, 3, 4, 2, 2],
         [3, 6, 2, 5, 1, 3],
         [6, 7, 7, 7, 8, 7]])
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-11T15:29:04.943Z
Answers.LastActivityDate: 2020-05-11T15:37:10.960Z
Answers.LastEditDate: 2020-05-11T15:37:10.960Z
Answers.UserDisplayName: alvas
Answers.Body: <p>well... simply put a <code>.split()</code> at the end of your first two lines:</p>
  
  <pre><code>a = "This is a dog.".split()
  b = "This is a cat.".split()
  </code></pre>
  
  <p>Your algorithm works with the iterables, and the string is broken into it's characters. You do the split, and <code>a,b</code> would be a list of words, then your algorithm works on the word-level</p>
  
  <p>Output on your example:</p>
  
  <pre><code>[[0. 1. 2. 3. 4.]
   [1. 0. 1. 2. 3.]
   [2. 1. 0. 1. 2.]
   [3. 2. 1. 0. 1.]
   [4. 3. 2. 1. 1.]]
  
  1.0
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-11T15:20:08.190Z
Answers.LastActivityDate: 2020-05-11T15:20:08.190Z
Answers.UserDisplayName: Alireza
Body: <p>From <a href="https://codereview.stackexchange.com/questions/217065/calculate-levenshtein-distance-between-two-strings-in-python">Calculate Levenshtein distance between two strings in Python</a> it is possible to calculate distance and similarity between two given strings(sentences).</p>
  
  <p>And from <a href="https://stackabuse.com/levenshtein-distance-and-text-similarity-in-python/" rel="nofollow noreferrer">Levenshtein Distance and Text Similarity in Python</a> to return the matrix for each character and distance for two strings.</p>
  
  <p>Are there any ways to calculate distance and similarity between each word in a string and print the matrix for each word in a string(sentences)?</p>
  
  <pre><code>a = "This is a dog."
  b = "This is a cat."
  
  from difflib import ndiff
  
  def levenshtein(seq1, seq2):
      size_x = len(seq1) + 1
      size_y = len(seq2) + 1
      matrix = np.zeros ((size_x, size_y))
      for x in range(size_x):
          matrix [x, 0] = x
      for y in range(size_y):
          matrix [0, y] = y
  
      for x in range(1, size_x):
          for y in range(1, size_y):
              if seq1[x-1] == seq2[y-1]:
                  matrix [x,y] = min(
                      matrix[x-1, y] + 1,
                      matrix[x-1, y-1],
                      matrix[x, y-1] + 1
                  )
              else:
                  matrix [x,y] = min(
                      matrix[x-1,y] + 1,
                      matrix[x-1,y-1] + 1,
                      matrix[x,y-1] + 1
                  )
      print (matrix)
      return (matrix[size_x - 1, size_y - 1])
  
  levenshtein(a, b)
  </code></pre>
  
  <p><strong>Outputs</strong></p>
  
  <pre><code>&gt;&gt; 3
  </code></pre>
  
  <p><strong>Matrix</strong></p>
  
  <pre><code>[[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14.]
   [ 1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13.]
   [ 2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12.]
   [ 3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]
   [ 4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
   [ 5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
   [ 6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.]
   [ 7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.]
   [ 8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.]
   [ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.]
   [10.  9.  8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.]
   [11. 10.  9.  8.  7.  6.  5.  4.  3.  2.  1.  1.  2.  3.  4.]
   [12. 11. 10.  9.  8.  7.  6.  5.  4.  3.  2.  2.  2.  3.  4.]
   [13. 12. 11. 10.  9.  8.  7.  6.  5.  4.  3.  3.  3.  3.  4.]
   [14. 13. 12. 11. 10.  9.  8.  7.  6.  5.  4.  4.  4.  4.  3.]]
  </code></pre>
  
  <p>General Levenshtein distance for character level shown in below fig. 
  <a href="https://i.sstatic.net/wYCIl.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/wYCIl.gif" alt="enter image description here"></a></p>
  
  <blockquote>
    <p><strong>Is it possible to calculate Levenshtein Distance for Word Level?</strong></p>
  </blockquote>
  
  <p><strong>Required Matrix</strong></p>
  
  <pre><code>          This is a cat
  
  This
  is
  a
  dog
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-24T10:59:06.513Z
Comments.Text: Do you mean other distance metrics, or other ways to code the levenshtein distance process? if former, there are methods such as word embeddings like word2vec that work like a charm, if the latter, why not use existing libraries? `NLTK` library has Levenshtein function. take a look http://www.nltk.org/howto/metrics.html . Please elaborate if you are looking for a more precise answer
Comments.UserDisplayName: Alireza
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-04-30T04:35:41.677Z
Comments.Text: @Alireza Elaborated the Question. Please do have a look
Comments.UserDisplayName: Pluviophile
Tags: <natural-language-processing><similarity>
Title: Levenshtein Distance between each word in a given string
UserDisplayName: Pluviophile
ThreadText: # Levenshtein Distance between each word in a given string
  
  (Asked by: username_0 on 2020-04-24)
  
  From Calculate Levenshtein distance between two strings in Python it is possible to calculate distance and similarity between two given strings(sentences).
  And from Levenshtein Distance and Text Similarity in Python to return the matrix for each character and distance for two strings.
  Are there any ways to calculate distance and similarity between each word in a string and print the matrix for each word in a string(sentences)?
  a = "This is a dog."
  b = "This is a cat."
  
  from difflib import ndiff
  
  def levenshtein(seq1, seq2):
      size_x = len(seq1) + 1
      size_y = len(seq2) + 1
      matrix = np.zeros ((size_x, size_y))
      for x in range(size_x):
          matrix [x, 0] = x
      for y in range(size_y):
          matrix [0, y] = y
  
      for x in range(1, size_x):
          for y in range(1, size_y):
              if seq1[x-1] == seq2[y-1]:
                  matrix [x,y] = min(
                      matrix[x-1, y] + 1,
                      matrix[x-1, y-1],
                      matrix[x, y-1] + 1
                  )
              else:
                  matrix [x,y] = min(
                      matrix[x-1,y] + 1,
                      matrix[x-1,y-1] + 1,
                      matrix[x,y-1] + 1
                  )
      print (matrix)
      return (matrix[size_x - 1, size_y - 1])
  
  levenshtein(a, b)
  
  Outputs
  >> 3
  
  Matrix
  [[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14.]
   [ 1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13.]
   [ 2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12.]
   [ 3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]
   [ 4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
   [ 5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.  9.]
   [ 6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.  8.]
   [ 7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.  7.]
   [ 8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.  6.]
   [ 9.  8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.  5.]
   [10.  9.  8.  7.  6.  5.  4.  3.  2.  1.  0.  1.  2.  3.  4.]
   [11. 10.  9.  8.  7.  6.  5.  4.  3.  2.  1.  1.  2.  3.  4.]
   [12. 11. 10.  9.  8.  7.  6.  5.  4.  3.  2.  2.  2.  3.  4.]
   [13. 12. 11. 10.  9.  8.  7.  6.  5.  4.  3.  3.  3.  3.  4.]
   [14. 13. 12. 11. 10.  9.  8.  7.  6.  5.  4.  4.  4.  4.  3.]]
  
  General Levenshtein distance for character level shown in below fig. 
  
  
  Is it possible to calculate Levenshtein Distance for Word Level?
  
  Required Matrix
            This is a cat
  
  This
  is
  a
  dog
  
  **Tags:** natural-language-processing, similarity
  > **Comments:**
  > **username_1**: Do you mean other distance metrics, or other ways to code the levenshtein distance process? if former, there are methods such as word embeddings like word2vec that work like a charm, if the latter, why not use existing libraries? `NLTK` library has Levenshtein function. take a look http://www.nltk.org/howto/metrics.html . Please elaborate if you are looking for a more precise answer  
  > **username_0**: @Alireza Elaborated the Question. Please do have a look  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-05-11. Score: 0 | Accepted answer
  
  Maybe try this:
  from functools import lru_cache
  from itertools import product
  
  @lru_cache(maxsize=4095)
  def ld(s, t):
      """
      Levenshtein distance memoized implementation from Rosetta code:
      https://rosettacode.org/wiki/Levenshtein_distance#Python
      """
      if not s: return len(t)
      if not t: return len(s)
      if s[0] == t[0]: return ld(s[1:], t[1:])
      l1 = ld(s, t[1:])      # Deletion.
      l2 = ld(s[1:], t)      # Insertion.
      l3 = ld(s[1:], t[1:])  # Substitution.
      return 1 + min(l1, l2, l3)
  
  
  a = "this is a sentence".split()
  b = "yet another cat thing".split()
  
  # To get the triplets.
  for i, j in product(a, b):
      print((i, j, ld(i, j)))
  
  To get a matrix:
  from scipy.sparse import coo_matrix
  import numpy as np
  
  a = "this is a sentence".split()
  b = "yet another cat thing , yes".split()
  
  tripets = np.array([(i, j, ld(w1, w2)) for (i, w1) , (j, w2) in product(enumerate(a), enumerate(b))])
  row, col, data = [np.squeeze(splt) for splt in np.hsplit(tripets, tripets.shape[-1])]
  coo_matrix((data, (row, col))).toarray()
  
  [out]:
  array([[4, 5, 4, 2, 4, 3],
         [3, 7, 3, 4, 2, 2],
         [3, 6, 2, 5, 1, 3],
         [6, 7, 7, 7, 8, 7]])
  
  
  ---
  
  ### Answer by username_1 on 2020-05-11. Score: 0
  
  well... simply put a .split() at the end of your first two lines:
  a = "This is a dog.".split()
  b = "This is a cat.".split()
  
  Your algorithm works with the iterables, and the string is broken into it's characters. You do the split, and a,b would be a list of words, then your algorithm works on the word-level
  Output on your example:
  [[0. 1. 2. 3. 4.]
   [1. 0. 1. 2. 3.]
   [2. 1. 0. 1. 2.]
   [3. 2. 1. 0. 1.]
   [4. 3. 2. 1. 1.]]
  
  1.0
  
  
  ---
  
  