-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (85 loc) · 2.52 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/xavier268/autocluster/cluster"
"github.com/xavier268/autocluster/distance"
)
func main() {
const VERSION = "0.8.3"
const COPYRIGHT = "(c) xavier268<at>github.com, 2023"
var ( // flags
FLAGLINK string // type of linkage
FLAGMINSIZE int // minimum cluster size (for display)
FLAGHELP bool
FLAGDEND bool // print dendrogramme
FLAGTREE bool // print tree
FLAGNAMES bool // print file names
FLAGMATRIX bool // print distance matrix (truncated)
)
flag.StringVar(&FLAGLINK, "link", "complete", "select type of linkage from single, complete, upgma")
flag.IntVar(&FLAGMINSIZE, "min", 0, "set this value to a high number to get less clusters")
flag.StringVar(&distance.CACHEFILENAME, "cache", filepath.Join(os.TempDir(), "fileDistance.cache"), "cache file location")
flag.BoolVar(&FLAGHELP, "h", false, "print version, usage and exit")
flag.BoolVar(&FLAGDEND, "d", true, "print dendrogramme view")
flag.BoolVar(&FLAGTREE, "t", true, "print tree view")
flag.BoolVar(&FLAGNAMES, "f", true, "list file names")
flag.BoolVar(&FLAGMATRIX, "dm", true, "print distance matrix")
flag.BoolVar(&cluster.FLAGMEDOID, "med", true, "compute and print medoïd view")
flag.Parse()
args := flag.Args()
if FLAGHELP {
fmt.Printf("Unsupervised clustering of files\n%s - v%s\nUsage :\n", COPYRIGHT, VERSION)
flag.PrintDefaults()
return
}
files := []string{}
for _, f := range args {
files = append(files, distance.FilesInFolder(f)...)
}
if FLAGNAMES {
fmt.Println("\nList of files to process :\n\n id\tfull path name")
for i, f := range files {
fmt.Printf("%4d\t%s\n", i, f)
}
}
mat := distance.ComputeFiles(files...)
if FLAGMATRIX {
fmt.Println()
fmt.Println(mat)
}
var cc *cluster.CContext
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
switch strings.ToLower(FLAGLINK) {
case "single":
cc = cluster.NewCContexMatrix(ctx, mat, cluster.SingleLinkage)
case "complete":
cc = cluster.NewCContexMatrix(ctx, mat, cluster.CompleteLinkage)
case "upgma":
cc = cluster.NewCContexMatrix(ctx, mat, cluster.UPGMALinkage)
default:
fmt.Fprintln(os.Stderr, "you selected an invalid linkage option")
flag.PrintDefaults()
return
}
cc.MergeAll()
root := cc.Root()
if FLAGDEND {
fmt.Println()
fmt.Println(root.Dendrogram(files, FLAGMINSIZE))
}
if FLAGTREE {
fmt.Println()
fmt.Println(root.Tree())
}
if cluster.FLAGMEDOID {
fmt.Println()
cc.DumpMedoids()
}
}