-
Notifications
You must be signed in to change notification settings - Fork 1
/
counter.go
60 lines (54 loc) · 1.16 KB
/
counter.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
package siw
import (
"fmt"
"time"
)
//clog := ClerkLog{log.New(os.Stderr, "Counter: ", log.Lshortfile)}
// T == Type or Token
func (doc *Document) TFreqNorm(tk string) float64 {
var wc = float64(len(doc.words))
var counter float64
for _, t := range doc.words {
switch t {
case tk:
counter += 1
}
}
return counter / wc
}
// T == Type or Token
func (doc *Document) TFreq(tk string) float64 {
var timer time.Duration
timer = time.Nanosecond
tfreq := make(chan float64)
var wc = float64(len(doc.words))
go func(doc *Document) {
var counter float64
for _, t := range doc.words {
switch t {
case tk:
counter += 1
}
}
tfreq <- counter
}(doc)
for {
select {
case <-time.After(timer):
//clog.Println(os.Stderr, "TFreq", log.Lshortfile)
//log.Printf(" %v counting... ", timer)
case res := <-tfreq:
return res / wc
}
}
}
func (doc *Document) TypeFrequencyChan(tf_c chan []string) {
this := []string{}
var counter = 0
for _, tok := range doc.words {
tok_freq := doc.TFreq(tok)
counter += 1
this = append(this, fmt.Sprintf("\n\nToken '%s', frequency %f, for Document: %s\n", tok, tok_freq, doc.label))
}
tf_c <- this
}