def parseData(self, data): """parses the SGF data given as its parameter. This function must be called before any other in this class is used.""" col = sgflib.SGFParser(data).parse() m = col[0].mainline() self.curse = m.cursor() self.parseRoot()
def load_file(self, path): f = open(path, 'r') data = f.read() self.props["filename"] = path self._game = sgflib.SGFParser(data).parseOneGame() self.__parseProperties()
def __init__(self, sgf_string): # FIXME: exception handling parser = sgflib.SGFParser(sgf_string) collection = parser.parse() self._cursor = collection.cursor() node = self._cursor.node data = node.data # FIXME: support for other board sizes assert (data['SZ'].data == ['19']) # FIXME: handle failures below assert (data['GM'].data == ['1']) assert (data['FF'].data == ['4']) if 'HA' in data.keys(): assert (data['HA'].data == ['0']) self._setup = {} self._setup[Color.B] =\ [(Problem._char_to_index(b[0]), Problem._char_to_index(b[1])) for b in data['AB']] self._setup[Color.W] =\ [(Problem._char_to_index(w[0]), Problem._char_to_index(w[1])) for w in data['AW']] self._wrong = False self._over = False
sys.stdout.write(str(j)) if not found_comment: sys.stdout.write("C[" + comment + "]") sys.stdout.write("TR[" + crazystone_move[i] + "]") sys.stdout.write("\n") if cursor.atEnd: sys.stdout.write(")") break i += 1 cursor.next() sgffile = open(sys.argv[1]) analysis = open(sys.argv[2]) sgfstring = sgffile.read() sgfinfo = sgflib.SGFParser(sgfstring) sgfcollection = sgfinfo.parse() sgfcursor = sgfcollection.cursor() crazystone_analysis = {} crazystone_move = {} for line in analysis: try: line = line.replace(", ", ",0") move_num = int(line.split()[0]) crazystone_analysis[move_num] = "Playout:" + line.split()[3] + " P(B wins):" + line.split()[4] + " Situation: " + line.split()[5] + " Dispersion: " + line.split()[6] + " Delta:" + line.split()[8] x_coord = chr(ord("a") + (ord(line.split()[7].split(",")[0]) - ord("A"))) if ord(x_coord) > ord("i"): x_coord = chr(ord(x_coord) - 1) y_coord = chr(19 - int(line.split()[7].split(",")[1]) + ord("a"))
import sgflib from sgflib import Property, Node _path_to_sgf = "2020-06-20_36.sgf" with open(_path_to_sgf, 'r', encoding="utf-8") as sgf_file: data = "".join([line for line in sgf_file]) sgf_data = sgflib.SGFParser(data).parse() print(str(sgf_data)) cursor = sgf_data.cursor() # 获得棋谱基本信息 # print(type(cursor)) node = cursor.node['PB'] # 获得执黑的人员名称 print(node.data[0]) print(type(node)) # move to last """获得一个落子的手数 """ def getSteps(value, cursor): cursor.reset() while not cursor.atEnd: if "B" in cursor.node: if value in cursor.node["B"]: return cursor.node_num break elif "W" in cursor.node: if value in cursor.node["W"]: return cursor.node_num break # if 'qp' in cursor.node["B"] or 'qp' in cursor.node["W"]:
def main(argv=sys.argv): if len(argv) < 2: printusage() return -1 sgffile = argv[1] test = sgffile.split('.') if test[-1] != 'sgf': print "The first argument must be a .sgf file" printusage() return #initialize variable for input parameters params = {} #read sgf file file = open(sgffile) text = file.read() parser = sgflib.SGFParser(text) coll = parser.parse() c = coll.cursor() #get board size node = c.node try: boardsize = int(node.data['SZ'].data[0]) except: boardsize = 19 #default values params = { "fileformat": "png", "filename": joinlist(sgffile.split('.')[:-1]), "imagesize": 1000 * boardsize / 19, "spk": 100, "outputdir": "" } for arg in argv[2:]: test = arg.split('=') if test[0] == "outputdir": outputdir = os.path.normpath(test[1]) if test[0] == "fileformat": if test[1] != "jpg" and test[1] != "gif" and test1 != "png": print "Invalid file format" printusage() else: params["fileformat"] = test[1] if test[0] == "filename": params["filename"] = test[1] if test[0] == "imagesize": params["imagesize"] = int(test[1]) if test[0] == "spk": params["spk"] = int(test[1]) fileformat = "." + params["fileformat"] textparams = { "fontsize": -1, "ytextspacement": 3 * params["imagesize"] / 1000, "inityspacement": 10 * params["imagesize"] / 1000, "xtextspacement": 40 * params["imagesize"] / 1000 } k = KNode(c) km = KifuMaker(k, params["imagesize"], boardsize, textparams["fontsize"], textparams["ytextspacement"], textparams["inityspacement"], textparams["xtextspacement"], params["outputdir"], params["filename"], fileformat) km.drawkifus(params["spk"])