forked from texttheater/golang-levenshtein
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoperations.go
114 lines (88 loc) · 1.99 KB
/
operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package editdistance
type (
Match struct {
}
Insertion struct {
Cost int
}
Deletion struct {
Cost int
}
Substitution struct {
Cost int
}
Transposition struct {
Cost int
}
)
var (
_ EditOperation = Match{}
_ EditOperation = Insertion{}
_ EditOperation = Deletion{}
_ EditOperation = Substitution{}
_ EditOperation = Transposition{}
)
func (o Match) Apply(seqPair SequencePair, matrix Matrix, i, j int) (int, bool) {
if i > 0 && j > 0 {
if seqPair.Equal(i-1, j-1) {
return matrix[i-1][j-1], true
}
}
return 0, false
}
func (o Match) Backtrack(matrix Matrix, i, j int) (int, int) {
return i - 1, j - 1
}
func (o Match) String() string {
return "match"
}
func (o Insertion) Apply(seqPair SequencePair, matrix Matrix, i, j int) (int, bool) {
if i >= 0 && j > 0 {
return matrix[i][j-1] + o.Cost, true
}
return 0, false
}
func (o Insertion) Backtrack(matrix Matrix, i, j int) (int, int) {
return i, j - 1
}
func (o Insertion) String() string {
return "ins"
}
func (o Deletion) Apply(seqPair SequencePair, matrix Matrix, i, j int) (int, bool) {
if i > 0 && j >= 0 {
return matrix[i-1][j] + o.Cost, true
}
return 0, false
}
func (o Deletion) Backtrack(matrix Matrix, i, j int) (int, int) {
return i - 1, j
}
func (o Deletion) String() string {
return "del"
}
func (o Substitution) Apply(seqPair SequencePair, matrix Matrix, i, j int) (int, bool) {
if i > 0 && j > 0 {
return matrix[i-1][j-1] + o.Cost, true
}
return 0, false
}
func (o Substitution) Backtrack(matrix Matrix, i, j int) (int, int) {
return i - 1, j - 1
}
func (o Substitution) String() string {
return "subs"
}
func (o Transposition) Apply(seqPair SequencePair, matrix Matrix, i, j int) (int, bool) {
if i > 1 && j > 1 {
if seqPair.Equal(i-1, j-2) && seqPair.Equal(i-2, j-1) {
return matrix[i-2][j-2] + o.Cost, true
}
}
return 0, false
}
func (o Transposition) Backtrack(matrix Matrix, i, j int) (int, int) {
return i - 2, j - 2
}
func (o Transposition) String() string {
return "trp"
}