-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_test.go
More file actions
79 lines (62 loc) · 2.8 KB
/
sort_test.go
File metadata and controls
79 lines (62 loc) · 2.8 KB
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
package fuzzy
import (
"fmt"
"testing"
"github.com/ItsMeSamey/go_fuzzy/heuristics"
"github.com/ItsMeSamey/go_fuzzy/transformers"
"golang.org/x/text/transform"
)
func TestReadmeSort(t *testing.T) {
target := "apple"
candidates := []string{"aple", "application", "orange", "banana", "appel"}
sorter := Sorter[float64, string, string]{
Threshold: 0.6, // Only include strings with similarity >= 0.6
}
fmt.Println("Unsorted:", candidates)
count := sorter.Sort(candidates, target)
fmt.Println("Sorted (and filtered):", candidates[:count]) // Only the first 'count' elements are sorted, rest are still shuffled
fmt.Println("Score: ", sorter.Score(candidates, target))
}
func TestReadmeScorer(t *testing.T) {
strs := []string{"hello world", "Hello fuzzy world", "Hello World 2"}
query := "Hello World"
scorer := Scorer[float64, string, string]{
ScoreFn: heuristics.LevenshteinSimilarityPercentage[float64, string, string],
Transformer: transform.Chain(transformers.UnicodeNormalize(), transformers.Lowercase()), // Should always UnicodeNormalize before Lowercase
}
var scores []float64 = scorer.Score(strs, query)
fmt.Println(scores)
}
func TestReadmeStringLike(t *testing.T) {
string1 := "hello world"
string2 := "hello world 2"
byteArray1 := []byte("hello byte world")
var score32 float32 = heuristics.DiceSorensenCoefficient[float32](string1, string2)
fmt.Printf("Dice-Sorensen Similarity: %f\n", score32)
var score64 float64 = heuristics.DiceSorensenCoefficient[float64](byteArray1, string2)
fmt.Printf("Dice-Sorensen Similarity: %f\n", score64)
}
func TestReadmeSortAny(t *testing.T) {
type Product struct {
Name string
Company string
Description string
}
target := "apple"
candidates := []Product{
{Name: "aple", Company: "Misspelling Corp", Description: "A misspelling of apple"},
{Name: "appel", Company: "Misspelling Corp", Description: "Another misspelling of apple"},
{Name: "application", Company: "Light Corp", Description: "A software application"},
{Name: "orange", Company: "Fruit Corp", Description: "A fruit"},
{Name: "banana", Company: "Fruit Corp", Description: "A fruit"},
{Name: "iphone", Company: "Apple", Description: "A smartphone"},
}
sorter := Sorter[float32, string, string]{Threshold: 0.8}
fmt.Println("Unsorted:", candidates)
// Note: `SortAny` for single key
count := sorter.SortAny(ToSwapper(candidates, func(p Product) string { return p.Name }), target)
fmt.Println("Sorted (and filtered):", candidates[:count]) // output:
// Note: `SortAnyArr` for multiple keys (best match is used)
count = sorter.SortAnyArr(ToSwapper(candidates, func(p Product) []string { return []string{p.Name, p.Company, p.Description} }), target)
fmt.Println("Sorted (and filtered):", candidates[:count]) // output:
}