Skip to content

Commit

Permalink
add algorithm to calculate edit distance (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 29, 2023
1 parent 1259b11 commit 288c07d
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions internal/diff/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package diff

type Diff struct {
textLong []rune
textShort []rune
lenTextLong int
lenTextShort int
isReversed bool
editDistance int
}

func NewDiff(text1, text2 []rune) *Diff {
diff := &Diff{}
if len(text1) > len(text2) {
diff.textLong = text1
diff.textShort = text2
diff.lenTextLong = len(text1)
diff.lenTextShort = len(text2)
} else {
diff.textLong = text2
diff.textShort = text1
diff.lenTextLong = len(text2)
diff.lenTextShort = len(text1)
diff.isReversed = true
}

return diff
}

func (d *Diff) Compose() {
delta := d.lenTextLong - d.lenTextShort
offset := d.lenTextShort + 1
fp := make([]int, d.lenTextLong+d.lenTextShort+3)
for i := range fp {
fp[i] = -1
}

for p := 0; ; p++ {
for k := -p; k < delta; k++ {
fp[k+offset] = d.snake(k, max(fp[k+offset-1]+1, fp[k+offset+1]))
}
for k := delta + p; k > delta; k-- {
fp[k+offset] = d.snake(k, max(fp[k+offset-1]+1, fp[k+offset+1]))
}
fp[delta+offset] = d.snake(delta+offset, max(fp[delta+offset-1]+1, fp[delta+offset+1]))
if fp[delta+offset] == d.lenTextLong {
d.editDistance = p
}
}
}

func (d *Diff) snake(k, y int) int {
x := y - k
for x < d.lenTextShort && y < d.lenTextLong && d.textLong[y] == d.textShort[x] {
x++
y++
}

return y
}

func max(x, y int) int {
if x > y {
return x
}
return y
}

0 comments on commit 288c07d

Please sign in to comment.