-
Notifications
You must be signed in to change notification settings - Fork 2
/
log2sgfmerge.scala
executable file
·261 lines (233 loc) · 5.68 KB
/
log2sgfmerge.scala
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import java.io.File
import java.io.FileReader
import java.io.PrintStream
import java.io.BufferedReader
import scala.io.Source
import scala.collection.mutable.ArrayBuffer
val logFile = args(0)
val sgfFile = args(1)
val myColor = args(2)
val oppColor = if (myColor == "B") "W" else "B"
val sgfLines = Source.fromFile(sgfFile).getLines.mkString.split("[;()]").filter(_.size > 0).toList
if (false) {
for (l <- sgfLines)
println(l)
sys.exit(0)
}
case class MoveLog(move: String, po: Int, vn: Int, winRateSim: Double, policy: Double,
winRateValue: Option[Double], winRate: Option[Double],
bestSeq: String) {
}
case class LogElement(self: ArrayBuffer[MoveLog], ops: ArrayBuffer[MoveLog], comment: String) {
}
def toMoveLog(line: String): MoveLog = {
val tokens = line.split("\\|", -1).map(_.trim).tail
if (tokens.size != 8)
println("ERRROR " + line)
val winRateValue = if (tokens(5) isEmpty) None else Some(tokens(5).toDouble)
val winRate = if (tokens(6) isEmpty) None else Some(tokens(6).toDouble)
MoveLog(tokens(0), tokens(1).toInt, tokens(2).toInt,
tokens(3).toDouble, tokens(4).toDouble,
winRateValue, winRate,
tokens(7))
}
def gtpToSgf(p: String) = {
if (p == "PASS") {
"tt"
} else {
val c = p.toUpperCase.charAt(0)
val x = if (c >= 'I') c - 'A' -1 else c - 'A'
val y = 19 - p.substring(1).toInt
('a' + x).toChar + "" + ('a' + y).toChar
}
}
class LogReader(val r: BufferedReader) {
var buffer: Option[String] = None
var comment = new ArrayBuffer[String]
def readLine() = {
buffer match {
case Some(line) => {
buffer = None
line
}
case None => {
val line = r.readLine
comment += line
line
}
}
}
def unreadLine(l: String): Unit = {
buffer = Some(l)
}
def readMoveLine(): Option[MoveLog] = {
val line = readLine()
if (line == null) {
None
} else if (line.startsWith("|")) {
Some(toMoveLog(line))
} else {
unreadLine(line)
None
}
}
def readMoveLines(buf: scala.collection.mutable.Buffer[MoveLog]): Unit = {
readMoveLine() match {
case Some(m) => {
buf += m
readMoveLines(buf)
//println(buf)
}
case None => ()
}
}
def readLog() = {
var line = readLine
val log = new ArrayBuffer[LogElement]
while (line != null) {
if (line startsWith "All Playouts ") {
comment.clear
comment += line
}
if (line startsWith "|Move") {
val moves = new ArrayBuffer[MoveLog]
readMoveLines(moves)
line = readLine
if (!(line startsWith "Opponent Moves"))
throw new IllegalArgumentException(line)
line = readLine
if (!(line startsWith "|Move"))
throw new IllegalArgumentException(line)
val oppMoves = new ArrayBuffer[MoveLog]
readMoveLines(oppMoves)
val p = LogElement(moves, oppMoves, comment.mkString("\n"))
log += p
}
line = readLine
}
log
}
def printLog(log: ArrayBuffer[LogElement]): Unit = {
print("(")
printLog(log, 0, 0, None)
print(")")
}
def printLog(log: ArrayBuffer[LogElement], i: Int, j: Int, opp: Option[ArrayBuffer[MoveLog]]): Unit = {
//println(log)
if (i < sgfLines.size) {
val sgfElement = sgfLines(i)
if (sgfElement.startsWith(myColor + "[") && j < log.size) {
val LogElement(self, opp, comment) = log(j)
print("(;")
printLog1(self)
println(sgfElement)
printf("C[%s]", comment)
//printf("C[%d/%d]", i, j)
printLog(log, i + 1, j + 1, Some(opp))
println(")")
println
if (i + 1 < sgfLines.size)
printLog2(self, myColor)
println
//j += 1
} else {
//if (false)
opp match {
case Some(o) =>
print("(;")
printLog1(o)
case None =>
print(";")
}
println(sgfElement)
printLog(log, i + 1, j, None)
opp match {
case Some(o) =>
print(")")
printLog2(o, oppColor)
case None =>
}
println
}
}
}
def printLog1(self: ArrayBuffer[MoveLog]) {
var first = true
for (m <- self if m.move != "PASS") {
m.winRate match {
case Some(r) =>
if (first)
print("LB")
first = false
printf("[%s:%.1f]", gtpToSgf(m.move), r)
case None => ()
}
}
println
}
def printBestSeq(moves: List[Array[String]]) {
var blist = ""
var wlist = ""
var first = true
for ((mm, i) <- moves zipWithIndex) {
if (mm(1) != "PASS") {
if (first)
print("LB")
first = false
printf("[%s:%d]", gtpToSgf(mm(1)), (i + 1))
if (mm(0) == "B") {
blist += "[" + gtpToSgf(mm(1)) + "]"
} else {
wlist += "[" + gtpToSgf(mm(1)) + "]"
}
}
}
if (blist.size > 0)
print("CR" + blist)
if (wlist.size > 0)
print("MA" + wlist)
/*
var blist = ""
var wlist = ""
print("(;LB")
for ((mm, i) <- moves zipWithIndex) {
if (mm(1) != "PASS") {
printf("[%s:%d]", gtpToSgf(mm(1)), (i + 1))
if (mm(0) == "B") {
blist += "[" + gtpToSgf(mm(1)) + "]"
} else {
wlist += "[" + gtpToSgf(mm(1)) + "]"
}
}
}
if (blist.size > 0)
print("AB" + blist)
if (wlist.size > 0)
print("AW" + wlist)
println(")")
*/
//print("(")
for (mm <- moves) {
printf(";%s[%s]", mm(0), gtpToSgf(mm(1)))
}
//print(")")
}
def printLog2(self: ArrayBuffer[MoveLog], color: String) {
for (m <- self) {
if (!m.bestSeq.trim.isEmpty) {
val moves = m.bestSeq.split(" ").grouped(2).toList
printf("(;%s[%s]", color, gtpToSgf(m.move))
printBestSeq(moves)
println(")")
}
}
}
}
val logReader = new LogReader(new BufferedReader(new FileReader(logFile)))
val log = logReader.readLog()
logReader.printLog(log)
// for (line <- logFile.getLines) {
// if (line.startsWith("|Move")) {
// println(line)
// }
// }