Last active
December 19, 2015 01:28
-
-
Save jbowles/5876142 to your computer and use it in GitHub Desktop.
Part two of Levenshtein Distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
// PART TWO: step through each string character and vector/cell of dynamic programming table to determine difference. | |
//// This handles the case where both characters are an exact match, and only the "no-change" condition is used. | |
func LevenshteinTwo(s1, s2 string) { | |
m := len(s1) | |
n := len(s2) | |
width := n - 1 | |
fmt.Println("width =",width) | |
vcell := make([]int, m * n) | |
// cells for i of m(s1) | |
for i := 1; i < m; i++ { | |
vcell[i * width + 0] = i | |
fmt.Println("i+=",i, "for len(str1)==",m, "at vector cell", vcell[i * width + 0]*width) | |
} | |
// cell for j of n(s2) | |
for j := 1; j < n; j++ { | |
vcell[0 * width + j] = j | |
fmt.Println("j+=",j, "for len(str2)==",n, "at vector cell", vcell[0 * width + j]) | |
} | |
fmt.Println("Vector Cell:\n", vcell) | |
for j := 1; j < n; j++ { | |
for i := 1; i < m; i++ { | |
if s1[i] == s2[j] { | |
//a fancy way of not changing cell by essentially | |
//vcell[i * width + j] = vcell[(i - 1) * width + (j - 1)] | |
fmt.Println("\n\nChecking Index",(i * width + j), "with value =", vcell[i * width + j], ) | |
fmt.Println("doing math for ...:", "((",i, "- 1) =", i -1, "*", width, "+", "(", j, "-1) =", j - 1,")", "==", ((i -1)*width+(j-1)), "THIS IS THE INDEX WE OPERATE ON") | |
fmt.Println("******* RESULT: Set index", (i*width+j), "To value=", vcell[(i - 1) * width + (j - 1)], "for comparison of string indexes", i, "," ,j) | |
// find index and set result of maths to its value | |
vcell[i * width + j] = vcell[(i - 1) * width + (j - 1)] | |
fmt.Println(" indexes i/j are same char:", string(s1[i]), string(s2[j])) | |
fmt.Println("Therefore make no changes", vcell) | |
} else { | |
} | |
} | |
} | |
} | |
func main() { | |
LevenshteinTwo("string","string") | |
} | |
/* | |
*** OUTPUT | |
width = 5 | |
i+= 1 for len(str1)== 6 at vector cell 5 | |
i+= 2 for len(str1)== 6 at vector cell 10 | |
i+= 3 for len(str1)== 6 at vector cell 15 | |
i+= 4 for len(str1)== 6 at vector cell 20 | |
i+= 5 for len(str1)== 6 at vector cell 25 | |
j+= 1 for len(str2)== 6 at vector cell 1 | |
j+= 2 for len(str2)== 6 at vector cell 2 | |
j+= 3 for len(str2)== 6 at vector cell 3 | |
j+= 4 for len(str2)== 6 at vector cell 4 | |
j+= 5 for len(str2)== 6 at vector cell 5 | |
Vector Cell: | |
[0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
Checking Index 6 with value = 0 | |
doing math for ...: (( 1 - 1) = 0 * 5 + ( 1 -1) = 0 ) == 0 THIS IS THE INDEX WE OPERATE ON | |
******* RESULT: Set index 6 To value= 0 for comparison of string indexes 1 , 1 | |
indexes i/j are same char: t t | |
Therefore make no changes [0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
Checking Index 12 with value = 0 | |
doing math for ...: (( 2 - 1) = 1 * 5 + ( 2 -1) = 1 ) == 6 THIS IS THE INDEX WE OPERATE ON | |
******* RESULT: Set index 12 To value= 0 for comparison of string indexes 2 , 2 | |
indexes i/j are same char: r r | |
Therefore make no changes [0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
Checking Index 18 with value = 0 | |
doing math for ...: (( 3 - 1) = 2 * 5 + ( 3 -1) = 2 ) == 12 THIS IS THE INDEX WE OPERATE ON | |
******* RESULT: Set index 18 To value= 0 for comparison of string indexes 3 , 3 | |
indexes i/j are same char: i i | |
Therefore make no changes [0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
Checking Index 24 with value = 0 | |
doing math for ...: (( 4 - 1) = 3 * 5 + ( 4 -1) = 3 ) == 18 THIS IS THE INDEX WE OPERATE ON | |
******* RESULT: Set index 24 To value= 0 for comparison of string indexes 4 , 4 | |
indexes i/j are same char: n n | |
Therefore make no changes [0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
Checking Index 30 with value = 0 | |
doing math for ...: (( 5 - 1) = 4 * 5 + ( 5 -1) = 4 ) == 24 THIS IS THE INDEX WE OPERATE ON | |
******* RESULT: Set index 30 To value= 0 for comparison of string indexes 5 , 5 | |
indexes i/j are same char: g g | |
Therefore make no changes [0 1 2 3 4 5 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment