def process_game(path): with open(path) as f: sgf_contents = f.read() root_node = sgf_wrapper.get_sgf_root_node(sgf_contents) assert root_node.properties['FF'] == ['4'], ("Bad game record", path) result = root_node.properties['RE'][0] assert result.lower()[0] in 'bw', result assert result.lower()[1] == '+', result black_won = result.lower()[0] == 'b' length = 0 node = root_node.next while node: props = node.properties length += 1 if props.get('B') or props.get('W') else 0 node = node.next sgf_path = canonical_name(path) return ( (b"black", root_node.properties['PB'][0]), (b"white", root_node.properties['PW'][0]), # All values are strings, "1" for true and "0" for false here (b"black_won", '1' if black_won else '0'), (b"white_won", '0' if black_won else '1'), (b"result", result), (b"length", str(length)), (b"sgf", sgf_path), (b"tag", ""), (b"tool", "eval_sgf_to_cbt"), )
def extract_data(filename): with gfile.GFile(filename) as f: contents = f.read() root_node = sgf_wrapper.get_sgf_root_node(contents) game_data = extract_game_data(filename, root_node) move_data = extract_move_data(root_node, game_data['worker_id'], game_data['completed_time'], game_data['board_size']) return game_data, move_data
def find_and_filter_sgf_files(base_dir, min_year=None, komi=None): sgf_files = [] print("Finding all sgf files in {} with year >= {} and komi = {}".format( base_dir, min_year, komi)) count = 0 for dirpath, dirnames, filenames in tqdm(os.walk(base_dir)): for filename in filenames: count += 1 if count % 5000 == 0: print("Parsed {}, Found {}".format(count, len(sgf_files))) if filename.endswith('.sgf'): path = os.path.join(dirpath, filename) with open(path) as f: sgf_contents = f.read() props = sgf_wrapper.get_sgf_root_node(sgf_contents).properties if check_year(props, min_year) and check_komi(props, komi): sgf_files.append(path) print("Found {} sgf files matching filters".format(len(sgf_files))) return sgf_files
def process_game(path): """ Get CBT metadata from a SGF file. Calling function should probably overwrite 'tool'. """ with open(path) as f: sgf_contents = f.read() root_node = sgf_wrapper.get_sgf_root_node(sgf_contents) assert root_node.properties['FF'] == ['4'], ("Bad game record", path) result = root_node.properties['RE'][0] assert result.lower()[0] in 'bw', result assert result.lower()[1] == '+', result black_won = result.lower()[0] == 'b' length = 0 node = root_node.next while node: props = node.properties length += 1 if props.get('B') or props.get('W') else 0 node = node.next return { "black": root_node.properties['PB'][0], "white": root_node.properties['PW'][0], # All values are strings, "1" for true and "0" for false here "black_won": '1' if black_won else '0', "white_won": '0' if black_won else '1', "result": result, "length": str(length), "sgf": path, "tag": "", "tool": "bigtable_output", }
def validate(path): with open(path) as f: sgf_contents = f.read() props = sgf_wrapper.get_sgf_root_node(sgf_contents).properties return check_komi(props, komi) and check_year(props, min_year)