-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchecker.go
65 lines (51 loc) · 1.62 KB
/
checker.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
package clustering
import "log"
// Checker implements the decision criteria used to stop clustering.
// Note that this interface may also be used to collect the hierarchical
// clustering tree produced by the agglomerations.
type Checker interface {
// Check decides wether or not to continue merging cluster nodes, based on
// the current set of clusters and the next best merge score.
//
// Returns true to continue clustering, false to stop.
Check(clusters ClusterSet, i, j int, nextScore float64) bool
}
// MaxClusters returns a Checker that limits total number of output clusters.
func MaxClusters(t int) Checker {
return limitClustersCount{t}
}
// Threshold returns a Checker that stops before a merge threshold is passed.
func Threshold(t float64) Checker {
return simpleThreshold{t}
}
// TreeLog prints the merge decisions that occur at each step of the tree.
func TreeLog(c Checker) Checker {
return clusterTreeLog{c}
}
/////////////
type simpleThreshold struct {
val float64
}
func (t simpleThreshold) Check(clusters ClusterSet, i, j int, nextScore float64) bool {
return nextScore <= t.val
}
/////////////
type clusterTreeLog struct {
chk Checker
}
func (c clusterTreeLog) Check(clusters ClusterSet, i, j int, nextScore float64) bool {
t := c.chk.Check(clusters, i, j, nextScore)
if t {
log.Printf(" merge (%d,%d) ~~ %f %v", i, j, nextScore, clusters)
} else {
log.Printf(" STOP (%d,%d) ~~ %f", i, j, nextScore)
}
return t
}
//////////////
type limitClustersCount struct {
val int
}
func (t limitClustersCount) Check(clusters ClusterSet, i, j int, nextScore float64) bool {
return clusters.Count() > t.val
}