-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_test.go
126 lines (96 loc) · 2.91 KB
/
matrix_test.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
115
116
117
118
119
120
121
122
123
124
125
126
package levenshtein_test
import "fmt"
import "testing"
import "github.com/neurlang/levenshtein"
//Calculate the edit distance between unicode strings.
func TestUnicode(t *testing.T) {
const word1 = "☺"
const word2 = "Ö"
var mat = levenshtein.Matrix[float32](uint(len([]rune(word1))), uint(len([]rune(word2))),
nil, nil,
levenshtein.OneSlice[rune, float32]([]rune(word1), []rune(word2)), nil)
for x := 0; x <= len([]rune(word2)); x++ {
for y := 0; y <= len([]rune(word1)); y++ {
var pos = x + y*(len([]rune(word2))+1)
fmt.Print(mat[pos], " ")
}
fmt.Println()
}
fmt.Println("Edit distance is:", *levenshtein.Distance(mat))
if *levenshtein.Distance(mat) != 1 {
t.Fail()
}
}
//Calculate the edit distance between raw strings.
func TestRaw(t *testing.T) {
const word1 = "☺"
const word2 = "Ö"
var mat = levenshtein.Matrix[float32](uint(len((word1))), uint(len((word2))),
nil, nil,
levenshtein.OneString[float32]((word1), (word2)), nil)
for x := 0; x <= len((word2)); x++ {
for y := 0; y <= len((word1)); y++ {
var pos = x + y*(len((word2))+1)
fmt.Print(mat[pos], " ")
}
fmt.Println()
}
fmt.Println("Edit distance is:", *levenshtein.Distance(mat))
if *levenshtein.Distance(mat) != 3 {
t.Fail()
}
}
//Calculate the transposed edit distance between strings.
func TestTransposed(t *testing.T) {
const word1 = "1234567"
const word2 = "137"
var mat = levenshtein.Matrix[float32](uint(len((word1))), uint(len((word2))),
nil, nil,
levenshtein.OneString[float32]((word1), (word2)), nil)
var matt = levenshtein.MatrixT[float32](uint(len((word1))), uint(len((word2))),
nil, nil,
levenshtein.OneString[float32]((word1), (word2)), nil)
for x := 0; x <= len((word2)); x++ {
for y := 0; y <= len((word1)); y++ {
var pos = x + y*(len((word2))+1)
fmt.Print(mat[pos], " ")
}
fmt.Println()
}
for x := 0; x <= len((word1)); x++ {
for y := 0; y <= len((word2)); y++ {
var pos = x + y*(len((word1))+1)
fmt.Print(matt[pos], " ")
}
fmt.Println()
}
for x := 0; x <= len((word2)); x++ {
for y := 0; y <= len((word1)); y++ {
var pos = x + y*(len((word2))+1)
var post = y + x*(len((word1))+1)
if mat[pos] != matt[post] {
t.Fail()
}
}
}
fmt.Println("Edit distance is:", *levenshtein.Distance(mat))
fmt.Println("Transposed Edit distance is:", *levenshtein.Distance(matt))
if *levenshtein.Distance(mat) != *levenshtein.Distance(matt) {
t.Fail()
}
}
//Calculate the transposed edit distance between slices.
func TestTransposedSlices(t *testing.T) {
var array1 = []string{"0", "1", "2"}
var array2 = []string{"0", "2"}
var matt = levenshtein.MatrixTSlices[float32, string](array1, array2,
nil, nil, nil, nil)
for x := 0; x <= len((array1)); x++ {
for y := 0; y <= len((array2)); y++ {
var pos = x + y*(len((array1))+1)
fmt.Print(matt[pos], " ")
}
fmt.Println()
}
fmt.Println("Transposed Edit distance is:", *levenshtein.Distance(matt))
}