-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
example_test.go
82 lines (63 loc) · 2.16 KB
/
example_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
package strutil_test
import (
"fmt"
"github.com/adrg/strutil"
"github.com/adrg/strutil/metrics"
)
func ExampleSimilarity() {
sim := strutil.Similarity("riddle", "needle", metrics.NewJaroWinkler())
fmt.Printf("(riddle, needle) similarity: %.2f\n", sim)
// Output:
// (riddle, needle) similarity: 0.56
}
func ExampleCommonPrefix() {
fmt.Println("(answer, anvil):", strutil.CommonPrefix("answer", "anvil"))
// Output:
// (answer, anvil): an
}
func ExampleUniqueSlice() {
sample := []string{"a", "b", "a", "b", "b", "c"}
fmt.Println("[a b a b b c]:", strutil.UniqueSlice(sample))
// Output:
// [a b a b b c]: [a b c]
}
func ExampleSliceContains() {
terms := []string{"a", "b", "c"}
fmt.Println("([a b c], b):", strutil.SliceContains(terms, "b"))
fmt.Println("([a b c], d):", strutil.SliceContains(terms, "d"))
// Output:
// ([a b c], b): true
// ([a b c], d): false
}
func ExampleNgramCount() {
fmt.Println("abbcd n-gram count (size 2):", strutil.NgramCount("abbcd", 2))
fmt.Println("abbcd n-gram count (size 3):", strutil.NgramCount("abbcd", 3))
// Output:
// abbcd n-gram count (size 2): 4
// abbcd n-gram count (size 3): 3
}
func ExampleNgrams() {
fmt.Println("abbcd n-grams (size 2):", strutil.Ngrams("abbcd", 2))
fmt.Println("abbcd n-grams (size 3):", strutil.Ngrams("abbcd", 3))
// Output:
// abbcd n-grams (size 2): [ab bb bc cd]
// abbcd n-grams (size 3): [abb bbc bcd]
}
func ExampleNgramMap() {
// 2 character n-gram map.
ngrams, total := strutil.NgramMap("abbcabb", 2)
fmt.Printf("abbcabb n-gram map (size 2): %v (%d ngrams)\n", ngrams, total)
// 3 character n-gram map.
ngrams, total = strutil.NgramMap("abbcabb", 3)
fmt.Printf("abbcabb n-gram map (size 3): %v (%d ngrams)\n", ngrams, total)
// Output:
// abbcabb n-gram map (size 2): map[ab:2 bb:2 bc:1 ca:1] (6 ngrams)
// abbcabb n-gram map (size 3): map[abb:2 bbc:1 bca:1 cab:1] (5 ngrams)
}
func ExampleNgramIntersection() {
ngrams, common, totalA, totalB := strutil.NgramIntersection("ababc", "ababd", 2)
fmt.Printf("(ababc, ababd) n-gram intersection: %v (%d/%d n-grams)\n",
ngrams, common, totalA+totalB)
// Output:
// (ababc, ababd) n-gram intersection: map[ab:2 ba:1] (3/8 n-grams)
}