/
zurichess.go
62 lines (52 loc) · 1.12 KB
/
zurichess.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
package dataset
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/ChizhovVadim/CounterGo/internal/domain"
)
// Separate validation dataset gives us general "unit of measurement" for cost error
type ZurichessDatasetProvider struct {
FilePath string
}
func (dp *ZurichessDatasetProvider) Load(
ctx context.Context,
dataset chan<- domain.DatasetItem,
) error {
file, err := os.Open(dp.FilePath)
if err != nil {
return err
}
defer file.Close()
var scanner = bufio.NewScanner(file)
for scanner.Scan() {
var s = scanner.Text()
var index = strings.Index(s, "\"")
if index < 0 {
return fmt.Errorf("zurichessParser failed %v", s)
}
var fen = s[:index]
var strScore = s[index+1:]
var prob float64
if strings.HasPrefix(strScore, "1/2-1/2") {
prob = 0.5
} else if strings.HasPrefix(strScore, "1-0") {
prob = 1.0
} else if strings.HasPrefix(strScore, "0-1") {
prob = 0.0
} else {
return fmt.Errorf("zurichessParser failed %v", s)
}
select {
case <-ctx.Done():
return ctx.Err()
case dataset <- domain.DatasetItem{
Fen: fen,
Target: prob,
}:
}
}
return nil
}