-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouper.go
171 lines (125 loc) · 4.48 KB
/
grouper.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"io/ioutil"
"math"
"os"
"sort"
"strings"
)
func vectorize(a []string) (words map[string]int) {
// Convert a string into a map of words:word-counts
var returned_words map[string]int
returned_words = make(map[string]int)
for _, word := range a {
aCount := 0
for _, subA := range a {
if subA == word {
aCount = aCount + 1
}
}
returned_words[word] = aCount
returned_words[word] = aCount
}
words = returned_words
return
}
func cosine(a, b []string) (cosineSimilarity float64) {
aa := vectorize(a)
bb := vectorize(b)
// All of this to make a set...
//
var keys []string
for k := range aa {
pf := 1
for _, y := range keys {
if k == y {
pf = 0
}
}
if pf == 1 {
keys = append(keys, k)
}
}
for k := range bb {
pf := 1
for _, y := range keys {
if k == y {
pf = 0
}
}
if pf == 1 {
keys = append(keys, k)
}
}
// Here's our set
numerator := 0
denominatorA := 0.0
denominatorB := 0.0
for _, y := range keys {
powerA := math.Pow(float64(aa[y]), 2)
powerB := math.Pow(float64(bb[y]), 2)
denominatorA = denominatorA + powerA
denominatorB = denominatorB + powerB
numerator = numerator + (aa[y] * bb[y])
}
denominator := math.Sqrt(denominatorA) * math.Sqrt(denominatorB)
cosineSimilarity = float64(numerator) / denominator
return
}
func main() {
arguments := os.Args[1:]
sort.Strings(arguments)
var pairings map[string]string
pairings = make(map[string]string)
for _, topitem := range arguments {
var matched_files []string
matched_files = append(matched_files, topitem)
for _, bottomitem := range arguments {
if topitem != bottomitem {
topType, _ := os.Stat(topitem)
botType, _ := os.Stat(bottomitem)
if topType.Mode().IsRegular() && botType.Mode().IsRegular() {
aDat, _ := ioutil.ReadFile(topitem)
a := strings.Fields(string(aDat))
bDat, _ := ioutil.ReadFile(bottomitem)
b := strings.Fields(string(bDat))
if int(cosine(a, b)*100) > 80 {
pairings[topitem] = bottomitem
}
}
}
}
}
var chatter []string
// Group individual pairs into larger sets
//
for _, index := range pairings {
var groups []string
groups = append(groups, "")
for a, b := range pairings {
if b == index && groups[len(groups)-1] != a {
groups = append(groups, a)
}
if a == index && groups[len(groups)-1] != b {
groups = append(groups, b)
}
sort.Strings(groups)
}
groups = append(groups, index)
sort.Strings(groups)
group := strings.Join(groups, " ")
// This process is repetitive
//
chattering := 0
for _, repeated := range chatter {
if strings.Contains(repeated, group) {
chattering = 1
}
}
chatter = append(chatter, group)
if chattering == 0 {
fmt.Println(group)
}
}
}