-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation.go
57 lines (51 loc) · 1.18 KB
/
evaluation.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
package main
import (
"errors"
"runtime"
"sync"
)
func dependencyAccuracy(golds [][]int, predictions [][]int) (float64, error) {
if len(golds) != len(predictions) {
return 0.0, errors.New("length of golds and that of predictions is not same")
}
sum := 0.0
count := 0.0
for idx, gold := range golds {
pred := predictions[idx]
if len(gold) != len(pred) {
return 0.0, errors.New("length of gold and that of pred is not same")
}
for i, g := range gold {
if g == pred[i] {
sum += 1.0
}
count += 1.0
}
}
return sum / count, nil
}
func DependencyAccuracy(w *[]float64, sents []*Sentence) float64 {
wg := &sync.WaitGroup{}
goldHeads := make([][]int, 0)
for _, sent := range sents {
goldHeads = append(goldHeads, sent.ExtractHeads())
}
predHeads := make([][]int, 0)
cpus := runtime.NumCPU()
semaphore := make(chan int, cpus)
for _, sent := range sents {
wg.Add(1)
go func(sent *Sentence) {
defer wg.Done()
semaphore <- 1
Decode(w, sent)
<-semaphore
}(sent)
}
wg.Wait()
for _, sent := range sents {
predHeads = append(predHeads, sent.ExtractPredictedHeads())
}
accuracy, _ := dependencyAccuracy(goldHeads, predHeads)
return accuracy
}