forked from sajari/word2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
104 lines (85 loc) · 2.27 KB
/
cluster.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
package word2vec
import (
"fmt"
"github.com/biogo/cluster/meanshift"
)
// Feature a
type Feature struct {
id string
data []float64
}
// Features of the data
type Features []Feature
// Len as
func (f Features) Len() int {
return len(f)
}
// Values a
func (f Features) Values(i int) []float64 {
return []float64(f[i].data)
}
func buildFeatureArray(m *Model, a, b Expr) (Features, error) {
f := make(Features, len(a)+len(b))
realCount := 0
for word := range a {
vec, ok := m.getWord(word)
if !ok {
continue
}
castVec := make([]float64, len(vec))
for i := range vec {
castVec[i] = float64(vec[i])
}
f[realCount] = Feature{word, castVec}
realCount++
}
for word := range b {
vec, ok := m.getWord(word)
if !ok {
continue
}
castVec := make([]float64, len(vec))
for i := range vec {
castVec[i] = float64(vec[i])
}
f[realCount] = Feature{word, castVec}
realCount++
}
ret := make(Features, realCount)
for i := 0; i < realCount; i++ {
ret[i] = f[i]
}
return ret, nil
}
func doCluster(m *Model, main, related Expr, shifter meanshift.Shifter) {
fmt.Println("============================")
//fmt.Println(buildFeatureArray(m, e))
features, _ := buildFeatureArray(m, main, related)
ms := meanshift.New(features, shifter, 0.10, 10)
//ms := meanshift.New(features, meanshift.NewUniform(1.0), 0.10, 8)
err := ms.Cluster()
if err != nil {
panic(err)
}
for _, c := range ms.Centers() {
fmt.Println("")
for _, i := range c.Members() {
f := features[i]
fmt.Println(f.id)
}
}
}
// Cluster similar words
func Cluster(m *Model, main, related Expr) {
fmt.Println("\n\n*******************************************\n******************************************\narticle:")
//ushifter = []meanshift.Shifter{meanshift.NewUniform(1.0), meanshift.NewUniform(1.5), meanshift.NewUniform(2.0)}
//gshifter = []dd
//fmt.Println("\n*******median:")
//doCluster(m, main, related, meanshift.NewUniform(1.0))
//doCluster(m, main, related, meanshift.NewUniform(2.0))
//doCluster(m, main, related, meanshift.NewUniform(3.0))
//fmt.Println("\n*******gaus:")
//doCluster(m, main, related, meanshift.NewTruncGauss(0.55, 4.0))
//doCluster(m, main, related, meanshift.NewTruncGauss(0.6, 4.0))
//doCluster(m, main, related, meanshift.NewTruncGauss(0.65, 4.0))
}