def cmd(data_dir, output_path): data_dir = Path(data_dir) p = data_dir.glob('wdoor+floodgate-*.csa') year = data_dir.stem output_path = Path(output_path) if not output_path.parent.exists(): output_path.parent.mkdir(parents=True) r = re.compile(r'wdoor\+floodgate-(\d+-\d+F?)\+(.+)\.csa') with h5py.File(str(output_path), 'a') as f: group = f.create_group('/{}'.format(year)) parser = cshogi.Parser() for path in tqdm(p): m = r.search(str(path)) if m is None: print(path) parser.parse_csa_file(str(path)) group.create_dataset('/{}/{}/{}'.format(year, m.group(1), m.group(2)), shape=(len(parser.moves), ), dtype=np.int32, data=parser.moves)
def read_kifu(kifu_list): positions = [] parser = cshogi.Parser() for filepath in kifu_list: kifu = shogi.CSA.Parser.parse_file(filepath)[0] board = shogi.Board() for move in kifu['moves']: # bitboard piece_bb = copy.deepcopy(board.piece_bb) occupied = copy.deepcopy( (board.occupied[shogi.BLACK], board.occupied[shogi.WHITE])) pieces_in_hand = copy.deepcopy((board.pieces_in_hand[shogi.BLACK], board.pieces_in_hand[shogi.WHITE])) positions.append( (piece_bb, occupied, pieces_in_hand, move, kifu['win'])) board.push_usi(move) return positions
def read_kifu_cython(kifu_list): positions = [] parser = cshogi.Parser() for filepath in kifu_list: parser.parse_csa_file(filepath) board = cshogi.Board() for move, score in zip(parser.moves, parser.scores): hcpe = np.empty(1, dtype=cshogi.HuffmanCodedPosAndEval) # hcp board.to_hcp(hcpe[0]['hcp']) # eval hcpe[0]['eval'] = score # move hcpe[0]['bestMove16'] = cshogi.move16(move) # result hcpe[0]['gameResult'] = parser.win positions.append(hcpe) board.push(move) return positions