added a simple parser for static sgf files
This commit is contained in:
parent
a8fdc2ffad
commit
5ab93abaf2
2 changed files with 79 additions and 8 deletions
|
@ -1,12 +1,6 @@
|
||||||
import os,tempfile,dircache,string,commands
|
import os,tempfile,dircache,string,commands
|
||||||
|
|
||||||
|
|
||||||
def gen_temp_dir(prefix=None):
|
|
||||||
"""generate a secure temporary directory. returns name of dir."""
|
|
||||||
dirname=tempfile.mkdtemp("",prefix)
|
|
||||||
dirname+="/"
|
|
||||||
return dirname
|
|
||||||
|
|
||||||
|
|
||||||
def read_file(filename):
|
def read_file(filename):
|
||||||
"""
|
"""
|
||||||
|
@ -82,4 +76,5 @@ def gen_temp_file(suffix="--gnugo"):
|
||||||
returns the name of a generated temporay file.
|
returns the name of a generated temporay file.
|
||||||
optionally gets a suffix for the filename.
|
optionally gets a suffix for the filename.
|
||||||
"""
|
"""
|
||||||
return tempfile.mkstemp(suffix)[1]
|
return tempfile.mkstemp(suffix)[1]
|
||||||
|
|
||||||
|
|
78
helper.py
78
helper.py
|
@ -1,4 +1,4 @@
|
||||||
import string
|
import string,re
|
||||||
|
|
||||||
DEBUG = 1
|
DEBUG = 1
|
||||||
|
|
||||||
|
@ -100,8 +100,84 @@ def dict_coords_to_gnugo_coords(coords,size):
|
||||||
digit = size+1-coords[0]
|
digit = size+1-coords[0]
|
||||||
return letter+str(digit)
|
return letter+str(digit)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_static_gnugo_sgf(s):
|
||||||
|
"""
|
||||||
|
gets a string containing the data saved by the gnugo "printsgf" order.
|
||||||
|
"""
|
||||||
|
#create a list of letters
|
||||||
|
letters = [" "]
|
||||||
|
letters.extend(list(string.letters)[0:26])
|
||||||
|
|
||||||
|
#dicitonary to return
|
||||||
|
ret = {}
|
||||||
|
|
||||||
|
#removing newlines vom given string
|
||||||
|
s = s.replace("\r\n","") #windoze
|
||||||
|
s = s.replace("\n","") #unix
|
||||||
|
|
||||||
|
#getting the board size
|
||||||
|
# looks like SZ[19]
|
||||||
|
# the pattern finds a number ([0-9]) with length (1-2) between "SZ[" and "]"
|
||||||
|
boardsize = re.search("SZ\[([0-9]{1,2})\]",s).groups()[0]
|
||||||
|
boardsize = int(boardsize)
|
||||||
|
ret["size"] = boardsize
|
||||||
|
|
||||||
|
|
||||||
|
#some regexp patterns:
|
||||||
|
#(?: ...) groups items
|
||||||
|
stones = "%s((?:\[[a-z][a-z]\])*)" #dont forget to set %s!
|
||||||
|
whitestones = stones % "AW"
|
||||||
|
blackstones = stones % "AB"
|
||||||
|
stonestolist = "\[([a-z][a-z])\]"
|
||||||
|
|
||||||
|
#getting white stones
|
||||||
|
#looks like AW[bb][cb][cc][cd][de][df]
|
||||||
|
|
||||||
|
stonestring = re.search(whitestones,s).groups()[0]
|
||||||
|
rawlist = re.findall(stonestolist,stonestring)
|
||||||
|
for item in rawlist:
|
||||||
|
#fill in specific values
|
||||||
|
ret[(letters.index(item[0]),letters.index(item[1]))] = 1
|
||||||
|
|
||||||
|
|
||||||
|
#getting black stones
|
||||||
|
stonestring = re.search(blackstones,s).groups()[0]
|
||||||
|
rawlist = re.findall(stonestolist,stonestring)
|
||||||
|
for item in rawlist:
|
||||||
|
#fill in specific values
|
||||||
|
ret[(letters.index(item[0]),letters.index(item[1]))] = 2
|
||||||
|
|
||||||
|
#who's turn is it?
|
||||||
|
if string.find(s,"PL[B]")>0:
|
||||||
|
ret["player"] = "black"
|
||||||
|
else:
|
||||||
|
ret["player"] = "white"
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
print dict_coords_to_gnugo_coords((6,5),7)
|
print dict_coords_to_gnugo_coords((6,5),7)
|
||||||
|
|
||||||
|
#sample output
|
||||||
|
s = """(;
|
||||||
|
GM[1]
|
||||||
|
FF[4]
|
||||||
|
SZ[19]
|
||||||
|
GN[GNU Go 3.7.4 load and print]
|
||||||
|
DT[2005-10-09]
|
||||||
|
KM[5.5]
|
||||||
|
RU[Japanese]
|
||||||
|
AP[GNU Go:3.7.4]
|
||||||
|
AW[bb][cb][cc][cd][de][df]
|
||||||
|
[ag][cg][ah][ch][dh][ai][bi][ci]
|
||||||
|
AB[ba][ab][ac][bc][bd][be][ce][af][cf][bg][bh]
|
||||||
|
PL[B]
|
||||||
|
)"""
|
||||||
|
print parse_static_gnugo_sgf(s)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
test()
|
test()
|
||||||
|
|
Loading…
Reference in a new issue