/
CoOccurance.go
163 lines (129 loc) · 2.89 KB
/
CoOccurance.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
package main
import (
"fmt"
"strings"
"sort"
)
type CoOccuranceField struct {
Fields map [string] int
}
func (t *CoOccuranceField) Report(a string) {
for k,v := range( t.Fields) {
fmt.Printf("\tCO '%s' -> '%s' %d\n", a,k,v)
}
}
func (t *CoOccuranceField) Field(b string) {
if t.Fields == nil {
t.Fields = make(map [string] int)
}
if val, ok := t.Fields[b]; ok {
t.Fields[b] = val + 1
} else {
t.Fields[b]=1
}
}
type CoOccurance struct {
TheFields map [string] *CoOccuranceField
}
func (t *CoOccurance) Report() {
fmt.Printf("CoOccurance report\n" ) // total per field
// fields with the same count for cooccurance
coccurance := map[int][]string{}
// for each field, get the coccurance
for k,v := range( t.TheFields) {
//fmt.Printf("\tCHECK FIELD '%s' -> '%#v'\n", k,v)
var total int // total per field
total = 0
//v.Report(k)
for k2,v2 := range( v.Fields) { // count with other fields
//fmt.Printf("\tCHECK FIELD2 '%s' -> '%s'\n", k2,v2)
key := fmt.Sprintf("\t\tCO '%s' -> '%s'", k,k2)
coccurance[v2] = append(coccurance[v2], key)
total = total + v2
}
fmt.Printf("\tFIELD: '%s' : %d\n", k, total ) // total per field
// now compare with others
for k2,_ := range( t.TheFields) {
//if k2 not in v.Fields
if k2 != k {
// now swap them if k is less than k2
a := k
b := k2
if strings.Compare(a, b) < 0 {
c :=a
a=b
b=c
}
// now look up in the matrix from the beginning
if count, ok := t.TheFields[a].Fields[b]; ! ok {
fmt.Printf("\t\tNCO '%s' '%s'\n", k,k2)
} else {
fmt.Printf("\t\tCOO '%s' '%s' -> %d\n", k,k2,count)
}
}
}
}
var a []int
for k := range coccurance {
a = append(a, k)
}
//
sort.Sort(sort.Reverse(sort.IntSlice(a)))
for _, k := range a {
for _, s := range coccurance[k] {
fmt.Printf("COOCCURANCE : %s, %d\n", s, k)
}
}
}
func (t *CoOccurance) Fields(a string, b string) {
// skip the digit fields
if (a[0]>= '0' && a[0] <= '9'){
return
}
if (b[0]>= '0' && b[0] <= '9'){
return
}
if strings.Compare(a, b) < 0 {
c :=a
a=b
b=c
}
if t.TheFields == nil {
t.TheFields = make(map [string] *CoOccuranceField)
}
if val, ok := t.TheFields[a]; ok {
val.Field(b)
} else {
o := &CoOccuranceField{}
t.TheFields[a]=o
o.Field(b)
}
}
func (t *CoOccuranceField) AppendTo(t2 *CoOccuranceField) {
if t2.Fields == nil {
t2.Fields = make(map [string] int)
}
for k,v := range( t.Fields) {
if val, ok := t2.Fields[k]; ok {
t2.Fields[k] = val + v
} else {
t2.Fields[k]=v
}
}
}
func (t *CoOccurance) AppendTo(t2 *CoOccurance) {
if t2.TheFields == nil {
t2.TheFields = make(map [string] *CoOccuranceField)
}
//
for k,v := range( t.TheFields) {
var o * CoOccuranceField
if val, ok := t2.TheFields[k]; ok {
o = val
} else {
o = &CoOccuranceField{}
t2.TheFields[k]=o
}
v.AppendTo(o)
}
}