def extract_game_data(gcs_path, root_node): props = root_node.properties komi = float(sgf_wrapper.sgf_prop(props.get('KM'))) result = sgf_wrapper.sgf_prop(props.get('RE', '')) board_size = int(sgf_wrapper.sgf_prop(props.get('SZ'))) value = utils.parse_game_result(result) was_resign = '+R' in result filename = os.path.basename(gcs_path) filename_no_ext, _ = os.path.splitext(filename) # BigQuery's TIMESTAMP() takes in unix millis. completion_millis = 1000 * int(filename_no_ext.split('-')[0]) worker_id = filename_no_ext.split('-')[-1] model_num = shipname.detect_model_num(props.get('PW')[0]) sgf_url = gcs_path first_comment_node_lines = root_node.next.properties['C'][0].split('\n') # in-place edit to comment node so that first move's comment looks # the same as all the other moves. root_node.next.properties['C'][0] = '\n'.join(first_comment_node_lines[1:]) resign_threshold = float(first_comment_node_lines[0].split()[-1]) return { 'worker_id': worker_id, 'completed_time': completion_millis, 'board_size': board_size, 'model_num': model_num, 'result_str': result, 'value': value, 'was_resign': was_resign, 'sgf_url': sgf_url, 'resign_threshold': resign_threshold, }
def parse_sgf(sgf_path): with open(sgf_path) as f: sgf_contents = f.read() collection = sgf.parse(sgf_contents) game = collection.children[0] props = game.root.properties assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!" result = utils.parse_game_result(sgf_prop(props.get('RE'))) positions, moves = zip(*[(p.position, p.next_move) for p in sgf_wrapper.replay_sgf(sgf_contents)]) return positions, moves, result, props