forked from ymgaq/Pyaq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsgf.py
201 lines (171 loc) · 5.88 KB
/
sgf.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import os
from sys import stderr
from board import *
import numpy as np
class sgf_data(object):
def __init__(self):
self.size = BSIZE
self.komi = KOMI
self.handicap = 0
self.result = 0
self.handicap_stones = []
self.history = []
self.move_cnt = 0
def sgf2ev(self, v_sgf):
if len(v_sgf) != 2:
return (self.size + 2) ** 2
labels = "abcdefghijklmnopqrs"
x = labels.find(v_sgf[0]) + 1
y = labels.find(v_sgf[1]) + 1
return x + (self.size + 1 - y) * (self.size + 2)
def import_file(self, file_path):
key = ""
f = open(file_path, 'r', encoding='utf-8')
lines = f.readlines()
for line in lines:
str = line.rstrip("\n")
while len(str) > 3:
open_br = str.find("[")
close_br = str.find("]")
if open_br < 0 or close_br < 0:
break
elif close_br == 0:
str = str[close_br + 1:]
continue
if key == "AB" and open_br == 0:
pass
else:
key = str[0:open_br].lstrip(";")
val = str[open_br + 1:close_br]
if key == "SZ":
self.size = int(val)
elif key == "KM":
self.komi = float(val)
elif key == "HA":
self.handicap = int(val)
elif key == "RE":
if val.find("B") >= 0:
self.result = 1
elif val.find("W") >= 0:
self.result = -1
else:
self.result = 0
elif key == "AB":
if self.sgf2ev(val) != (self.size + 2) ** 2:
self.handicap_stones.append(self.sgf2ev(val))
elif key == "B" or key == "W":
self.history.append(self.sgf2ev(val))
self.move_cnt += 1
str = str[close_br + 1:]
if self.result == 0 and len(self.history) >= 2:
pass_ = (self.size + 2) ** 2
if self.history[-1] != pass_ or self.history[-2] != pass_:
self.result = 1 if len(self.history) % 2 == 1 else -1
def import_sgf_text(self, sgf):
key = ""
str = sgf.rstrip("\n")
while len(str) > 3:
open_br = str.find("[")
close_br = str.find("]")
if open_br < 0 or close_br < 0:
break
elif close_br == 0:
str = str[close_br + 1:]
continue
if key != "AB" and open_br != 0:
key = str[0:open_br].lstrip(";")
val = str[open_br + 1:close_br]
if key == "SZ":
self.size = int(val)
elif key == "KM":
self.komi = float(val)
elif key == "HA":
self.handicap = int(val)
elif key == "RE":
if val.find("B") >= 0:
self.result = 1
elif val.find("W") >= 0:
self.result = -1
else:
self.result = 0
elif key == "AB":
self.handicap_stones.append(self.sgf2ev(val))
elif key == "B" or key == "W":
self.history.append(self.sgf2ev(val))
self.move_cnt += 1
str = str[close_br + 1:]
if self.result == 0 and len(self.history) >= 2:
pass_ = (self.size + 2) ** 2
if self.history[-1] != pass_ or self.history[-2] != pass_:
self.result = 1 if len(self.history) % 2 == 1 else -1
def import_sgf(dir_path):
dir_path = os.path.join(dir_path, "*.sgf")
# file_list = glob.glob(dir_path)
sd_list = []
# b = Board()
for f in glob.glob(dir_path):
sd_list.append(sgf_data())
sd_list[-1].import_file(f)
# b.clear()
# for v in sd_list[-1].history:
# err = b.play(v, not_fill_eye=False)
# if err:
# stderr.write("file %d\n" % len(sd_list))
# b.showboard()
# stderr.write("move=(%d,%d)\n" % ev2xy(v))
# raw_input()
# if len(sd_list) % 5000 == 0:
# stderr.write(".")
return sd_list
def import_sgfs(dir_path):
dir_path += "/*.sgfs"
file_list = glob.glob(dir_path)
sd_list = []
# b = Board()
for f in file_list:
with open(f, 'r', encoding='utf-8') as rf:
content = rf.read()
sgf_list = content.split('\n')
for sgf in sgf_list:
if sgf is None:
continue
sd_list.append(sgf_data())
sd_list[-1].import_sgf_text(sgf)
# b.clear()
# for v in sd_list[-1].history:
# err = b.play(v, not_fill_eye=False)
# if err:
# stderr.write("file %d\n" % len(sd_list))
# b.showboard()
# stderr.write("move=(%d,%d)\n" % ev2xy(v))
# raw_input()
# if len(sd_list) % 5000 == 0:
# stderr.write(".")
return sd_list
def sgf2feed(sgf_list):
total_cnt = 0
for s in sgf_list:
total_cnt += s.move_cnt
feature = np.zeros((total_cnt, BVCNT, FEATURE_CNT), dtype=np.uint8)
move = np.zeros((total_cnt, BVCNT + 1), dtype=np.uint8)
result = np.zeros((total_cnt), dtype=np.int8)
train_idx = 0
b = Board()
for s in sgf_list:
if s.size != BSIZE:
continue
b.clear()
for hs in s.handicap_stones:
b.turn = 1
b.play(hs, False)
b.turn = 1
for v in s.history:
feature[train_idx] = b.feature()
move[train_idx, ev2rv(v)] = 1
result[train_idx] = s.result * (2 * b.turn - 1)
b.play(v, False)
train_idx += 1
return feature, move, result