Пример #1
0
def load_generic_text(directory):
    '''Generator that yields text raw from the directory.'''
    files = find_files(directory)
    text = " "
    for filename in files:
        k = 0
        pgn = open(filename)
        for offset, headers in chess.pgn.scan_headers(pgn):
            pgn.seek(offset)
            game = chess.pgn.read_game(pgn)
            node = game
            while not node.is_end():
                next_node = node.variation(0)
                label_san = node.board().san(next_node.move)
                if " " + label_san + " " not in text:
                    text += label_san + " "
                node = next_node
            if k % 100 == 0 and k > 1:
                print ("Labeling file: " + filename + ", step: " + str(k))
            k += 1
        pgn.close()
    y = []
    for index, item in enumerate(text):
        y.append(text[index])
    y = np.array(y)
    np.savetxt("labels.txt", y.reshape(1, y.shape[0]), delimiter="", newline="\n", fmt="%s")
Пример #2
0
def load_generic_text(directory):
    '''Generator that yields text raw from the directory.'''
    files = find_files(directory)
    for filename in files:
        text = ""
        k = 0
        pgn = open(filename)
        for offset, headers in chess.pgn.scan_headers(pgn):
            pgn.seek(offset)
            game = chess.pgn.read_game(pgn)
            node = game
            nm = 1
            while not node.is_end():
                board_fen = replace_tags(node.board().fen())
                next_node = node.variation(0)
                label_san = node.board().san(next_node.move)
                text += board_fen + ":" + label_san + "\n"
                nm += 1
                node = next_node
            if k % 1000 == 0 and k > 1:
                print("Saving file: " + filename + "-" + str(k) + ".txt")
                y = []
                for index, item in enumerate(text):
                    y.append(text[index])
                y = np.array(y)
                np.savetxt(filename + "-" + str(k) + ".txt",
                           y.reshape(1, y.shape[0]),
                           delimiter="",
                           newline="\n",
                           fmt="%s")
                text = ""
            k += 1
        pgn.close()
        yield filename
Пример #3
0
    def test_read_game(self):
        pgn = open("data/games/kasparov-deep-blue-1997.pgn")
        first_game = chess.pgn.read_game(pgn)
        second_game = chess.pgn.read_game(pgn)
        third_game = chess.pgn.read_game(pgn)
        fourth_game = chess.pgn.read_game(pgn)
        fifth_game = chess.pgn.read_game(pgn)
        sixth_game = chess.pgn.read_game(pgn)
        self.assertTrue(chess.pgn.read_game(pgn) is None)
        pgn.close()

        self.assertEqual(first_game.headers["Event"], "IBM Man-Machine, New York USA")
        self.assertEqual(first_game.headers["Site"], "01")
        self.assertEqual(first_game.headers["Result"], "1-0")

        self.assertEqual(second_game.headers["Event"], "IBM Man-Machine, New York USA")
        self.assertEqual(second_game.headers["Site"], "02")

        self.assertEqual(third_game.headers["ECO"], "A00")

        self.assertEqual(fourth_game.headers["PlyCount"], "111")

        self.assertEqual(fifth_game.headers["Result"], "1/2-1/2")

        self.assertEqual(sixth_game.headers["White"], "Deep Blue (Computer)")
        self.assertEqual(sixth_game.headers["Result"], "1-0")
Пример #4
0
    def analyzePGN(self, path):
        self.analyze = True
        self.drawBoard()
        pgn = open(path, 'r')

        game = chess.pgn.read_game(pgn)
        pgn.close()

        w_bm, w_e, b_bm, b_e, count = (0, 0, 0, 0, 0)

        for move in game.mainline_moves():
            move, best, state = self.IA.compareMove(str(move), self.algo_board)
            if count % 2 == 0:
                if state == 'E':
                    w_e += 1
                elif state == 'P':
                    w_bm += 1
            else:
                if state == 'E':
                    b_e += 1
                elif state == 'P':
                    b_bm += 1
            count += 1

            self.moves.append((move, best, state))

            self.makeMove(str(move), sound=False)
            self.drawBoard()

        self.analyze = False
        self.finished_analyze.play()

        return (w_e, w_bm, b_e, b_bm)
Пример #5
0
    def delete_game_for_update_at(self,idx):
        start_offset = self.entries[idx].pgn_offset

        # if the last game is to be deleted, first get
        # the offset at the very end of the file
        stop_offset = None
        if(idx == len(self.entries) -1):
            stop_offset = self.get_end_offset()
            print("stop offset: "+str(stop_offset))
        else: # just take the start of the next game as end
            stop_offset = self.entries[idx+1].pgn_offset

        length = stop_offset - start_offset

        pgn = open(self.filename, 'r+')
        m=mm.mmap(pgn.fileno(),0)
        size = len(m)
        new_size = size - length
        m.move(start_offset,stop_offset,size-stop_offset)
        m.flush()
        m.close()
        pgn.truncate(new_size)
        pgn.close()

        return length
Пример #6
0
def pgn_prepare():
    pgn = open("test.pgn")
    game = chess.pgn.read_game(pgn)
    pgn.close()
    game.headers["Event"]
    board = game.board()
    main = game.main_line()
    #  print(main)
    variation = game.board().variation_san(main)
    #  print(variation)
    node = game
    moves = []
    while not node.is_end():
        next_node = node.variation(0)
        move = node.board().san(next_node.move)
        board = node.board()
        #    print(board)
        moves.append(move)
        node = next_node
    evals = re.findall(r'wv=(.*?),', str(game))
    start = len(moves) - len(evals)
    play = moves[start:]
    opening = moves[0:start]
    evals = [float(a) for a in evals]
    dictionary = [{a: float(b)} for a, b in zip(play, evals)]
    return opening, play, evals
Пример #7
0
def pgn_file_reader(file_dir):
    pgn = open(file_dir)
    line_br_detector = False
    pgn_data_frame = pd.DataFrame()
    #temp1 = pd.Series()
    temp0 = ''
    for line in pgn.read().splitlines():                                        #Forming DataFrame with Pandas
        if line == "":
            #print (0)
            line_br_detector  = not line_br_detector
        if line_br_detector:
            #print (1)
            pgn_data_frame = pgn_data_frame.append(temp0, ignore_index=True)
            temp0 = ''
            line_br_detector  = not line_br_detector
            #temp1 = pd.Series()
        else:
            #print (2)
            temp0 = temp0+'\n'+line
            temp0 = pd.Series(temp0)
            #temp1 = temp1.append(temp0,ignore_index = True)
            continue
        #temp0 = pd.Series(line)
        #temp1 = temp1.append(temp0, ignore_index=True)
    pgn.close()
    return pgn_data_frame
Пример #8
0
 def load(self, path):
     pgn = open(path)
     offsets = []
     for offset, headers in chess.pgn.scan_headers(pgn):
         offsets.append(offset)
     for offset in offsets:
         pgn.seek(offset)
         game = chess.pgn.read_game(pgn)
         exporter = chess.pgn.StringExporter()
         pgn_string = game.accept(exporter)
         game_headers = self._get_headers_columns(game.headers)
         game_id = [x for x in (Game.insert(pgn=pgn_string, **game_headers).returning(Game.id)).execute()][0][0]
         node = game
         while not node.is_end():
             next_node = node.variations[0]
             fen = node.board().fen()
             hash_key = hashlib.md5(fen.encode()).hexdigest()
             position_result = (Position.select().where(Position.hash == hash_key).execute())
             if not position_result:
                 position_id = [x for x in (Position.insert(hash=hash_key).returning(Position.id)).execute()][0][0]
             else:
                 position_id = position_result[0].id
             PositionGame.insert(position_id=position_id, game_id=game_id).execute()
             node = next_node
     pgn.close()
Пример #9
0
def get_player_games(player):
    game_files = get_files(player)
    games = []
    for game_file in game_files:
        pgn = open(join(player, game_file))
        games.append(chess.pgn.read_game(pgn))
        pgn.close()
    return games
Пример #10
0
def backup_game(move):
    global game, node, file_path
    pgn = open(file_path + "/temp.pgn", "w")
    node = game.end()
    node = node.add_variation(chess.Move.from_uci(move))
    exporter = chess.pgn.FileExporter(pgn)
    game.accept(exporter)
    pgn.close()
 def pgn_init(self):
     pgn = open(self.pgn)
     self.game = chess.pgn.read_game(pgn)
     exporter = chess.pgn.StringExporter(headers=True,
                                         variations=True,
                                         comments=True)
     pgn_string = self.game.accept(exporter)
     pgn.close()
     return
Пример #12
0
def load_generic_text(directory, type_file):
    '''Generator that yields text raw from the directory.'''
    files = find_files(directory)
    print(files)
    for filename in files:
        print("Parsing file: " + filename)
        board_text = ""
        movement_text = ""
        k = 0
        pgn = open(filename)
        for offset, headers in chess.pgn.scan_headers(pgn):
            pgn.seek(offset)
            game = chess.pgn.read_game(pgn)
            node = game
            nm = 1
            while not node.is_end():
                board_fen = replace_tags(node.board().fen())
                next_node = node.variation(0)
                label_san = node.board().san(next_node.move)
                board_text += " ".join(board_fen) + "\n"
                movement_text += " ".join(label_san) + "\n"
                nm += 1
                node = next_node
            if k % 1000 == 0 and k > 1:
                print("Saving: " + filename + " " + str(k))
                print("Saving: " + filename + " " + str(k))
                y = []
                m = []
                for index, item in enumerate(board_text):
                    y.append(board_text[index])
                for index, item in enumerate(movement_text):
                    m.append(movement_text[index])
                y = np.array(y)
                m = np.array(m)
                with open(
                        "nmt/datasets/" + type_file + "/" + type_file + ".gm",
                        'a') as f_handle:
                    np.savetxt(f_handle,
                               y.reshape(1, y.shape[0]),
                               delimiter="",
                               newline="\n",
                               fmt="%s")
                with open(
                        "nmt/datasets/" + type_file + "/" + type_file + ".mv",
                        'a') as f_handle:
                    np.savetxt(f_handle,
                               m.reshape(1, m.shape[0]),
                               delimiter="",
                               newline="\n",
                               fmt="%s")
                board_text = ""
                movement_text = ""
            k += 1
        pgn.close()
Пример #13
0
def pgn_to_fen_list(png_path):
    pgn = open(png_path)
    fen_list = []
    game = chess.pgn.read_game(pgn)
    board = game.board()

    for move in game.mainline_moves():
        board.push(move)
        fen_list.append(board.fen())
    pgn.close()
    return fen_list
Пример #14
0
    def update_game(self, idx, game_tree):
        old_length = self.delete_game_for_update_at(idx)

        dos_newlines = False
        if "\r\n".encode() in open(self.filename,"rb").read():
            dos_newlines = True

        if(dos_newlines):
            game_str = (str(game_tree.root())).replace('\n','\r\n')
            game_str = (game_str + '\r\n\r\n').encode('utf-8')
        else:
            game_str = (str(game_tree.root())+"\n\n").encode('utf-8')
        length = len(game_str)

        offset = self.entries[idx].pgn_offset

        # we can't mmap an empty file
        # the file is however empty, if the current
        # game was the only one in the database
        # just append it
        current_filesize = os.stat(self.filename).st_size
        print("current fs: "+str(current_filesize))
        if current_filesize == 0:
            print("file is empty")
            self.entries = []
            self.append_game(game_tree)
        else:
            pgn = open(self.filename, 'r+')
            m=mm.mmap(pgn.fileno(),0)
            size = len(m)
            new_size = size + length
            m.flush()
            m.close()
            pgn.seek(size)
            pgn.write('A'*length)
            pgn.flush()
            m=mm.mmap(pgn.fileno(),0)
            m.move(offset+length,offset,size-offset)
            m.seek(offset)
            m.write(game_str)
            m.flush()
            m.close()
            pgn.close()

            for i in range(idx+1, len(self.entries)):
                self.entries[i].pgn_offset += (length-old_length)

        self.checksum = crc32_from_file(self.filename)
        self.entries[idx] = Entry(offset, game_tree.root().headers)
def load_data(output, limit=math.inf):
    output = open(output, 'w')
    writer = csv.writer(output)

    # print(os.listdir("data"))

    for fn in os.listdir("data"):
        filepath = os.path.join("data", fn)
        print(filepath)
        pgn = open(filepath, encoding="ISO-8859-1")
        get_dataset(pgn, writer, limit)
        pgn.close()
        if numberGame > limit:
            break

    output.close()
Пример #16
0
    def update_game(self, idx, game_tree):
        old_length = self.delete_game_for_update_at(idx)

        dos_newlines = False
        if "\r\n".encode() in open(self.filename, "rb").read():
            dos_newlines = True

        if (dos_newlines):
            game_str = (str(game_tree.root())).replace('\n', '\r\n')
            game_str = (game_str + '\r\n\r\n').encode('utf-8')
        else:
            game_str = (str(game_tree.root()) + "\n\n").encode('utf-8')
        length = len(game_str)

        offset = self.entries[idx].pgn_offset

        # we can't mmap an empty file
        # the file is however empty, if the current
        # game was the only one in the database
        # just append it
        current_filesize = os.stat(self.filename).st_size
        if current_filesize == 0:
            self.entries = []
            self.append_game(game_tree)
        else:
            pgn = open(self.filename, 'r+')
            m = mm.mmap(pgn.fileno(), 0)
            size = len(m)
            new_size = size + length
            m.flush()
            m.close()
            pgn.seek(size)
            pgn.write('A' * length)
            pgn.flush()
            m = mm.mmap(pgn.fileno(), 0)
            m.move(offset + length, offset, size - offset)
            m.seek(offset)
            m.write(game_str)
            m.flush()
            m.close()
            pgn.close()

            for i in range(idx + 1, len(self.entries)):
                self.entries[i].pgn_offset += (length - old_length)

        self.checksum = crc32_from_file(self.filename)
        self.entries[idx] = Entry(offset, game_tree.root().headers)
Пример #17
0
def load_games():

    global games

    for filename in os.listdir('data/'):
        if filename.endswith(".pgn"):
            pgn = open('data/' + filename)
            first_game = chess.pgn.read_game(pgn)
            pgn.close()

            node = first_game
            game = []
            while not node.is_end():
                next_node = node.variation(0)
                game.append(node.board().san(next_node.move))
                node = next_node

            games.append(game)
Пример #18
0
def pgn_file_reader(file_dir):
    pgn = open(file_dir)
    line_br_detector = 0
    pgn_data_frame = pd.DataFrame()
    #temp0 = pd.Series()
    temp1 = pd.Series()
    for line in pgn.read().splitlines():                                        #Forming DataFrame with Pandas
        if line == "":
            line_br_detector += 1
        if line_br_detector%2 != 0:
            temp1 = pd.Series(line)
        if line_br_detector > 0 and line_br_detector%2 == 0:
            pgn_data_frame = pgn_data_frame.append(temp1, ignore_index=True)
            temp1 = pd.Series()
            line_br_detector = 0
            continue
        #temp0 = pd.Series(line)
        #temp1 = temp1.append(temp0, ignore_index=True)
    pgn.close()
    return pgn_data_frame
Пример #19
0
    def delete_game_at(self,idx):
        start_offset = self.entries[idx].pgn_offset

        # if the last game is to be deleted, first get
        # the offset at the very end of the file
        stop_offset = None
        if(idx == len(self.entries) -1):
            stop_offset = self.get_end_offset()
        else: # just take the start of the next game as end
            stop_offset = self.entries[idx+1].pgn_offset

        length = stop_offset - start_offset

        # we can't mmap an empty file
        # the file is however empty, the
        # game to be deleted was the only one in the database
        # just delete it
        current_filesize = os.stat(self.filename).st_size
        print("current fs: "+str(current_filesize))
        if current_filesize == 0:
            print("file is empty")
            pgn = open(self.filename, 'r+')
            pgn.close()
        else:
            pgn = open(self.filename, 'r+')
            m=mm.mmap(pgn.fileno(),0)
            size = len(m)
            new_size = size - length
            m.move(start_offset,stop_offset,size-stop_offset)
            m.flush()
            m.close()
            pgn.truncate(new_size)
            pgn.close()
            self.checksum = crc32_from_file(self.filename)

        for i in range(idx, len(self.entries)):
            self.entries[i].pgn_offset -= length
        del(self.entries[idx])
        # if idx was the current open game, set current_idx to None
        if(idx == self.index_current_game):
            self.index_current_game = None
Пример #20
0
    def delete_game_at(self, idx):
        start_offset = self.entries[idx].pgn_offset

        # if the last game is to be deleted, first get
        # the offset at the very end of the file
        stop_offset = None
        if (idx == len(self.entries) - 1):
            stop_offset = self.get_end_offset()
        else:  # just take the start of the next game as end
            stop_offset = self.entries[idx + 1].pgn_offset

        length = stop_offset - start_offset

        # we can't mmap an empty file
        # the file is however empty, the
        # game to be deleted was the only one in the database
        # just delete it
        current_filesize = os.stat(self.filename).st_size
        if current_filesize == 0:
            pgn = open(self.filename, 'r+')
            pgn.close()
        else:
            pgn = open(self.filename, 'r+')
            m = mm.mmap(pgn.fileno(), 0)
            size = len(m)
            new_size = size - length
            m.move(start_offset, stop_offset, size - stop_offset)
            m.flush()
            m.close()
            pgn.truncate(new_size)
            pgn.close()
            self.checksum = crc32_from_file(self.filename)

        for i in range(idx, len(self.entries)):
            self.entries[i].pgn_offset -= length
        del (self.entries[idx])
        # if idx was the current open game, set current_idx to None
        if (idx == self.index_current_game):
            self.index_current_game = None
Пример #21
0
    def delete_game_for_update_at(self, idx):
        start_offset = self.entries[idx].pgn_offset

        # if the last game is to be deleted, first get
        # the offset at the very end of the file
        stop_offset = None
        if (idx == len(self.entries) - 1):
            stop_offset = self.get_end_offset()
        else:  # just take the start of the next game as end
            stop_offset = self.entries[idx + 1].pgn_offset

        length = stop_offset - start_offset

        pgn = open(self.filename, 'r+')
        m = mm.mmap(pgn.fileno(), 0)
        size = len(m)
        new_size = size - length
        m.move(start_offset, stop_offset, size - stop_offset)
        m.flush()
        m.close()
        pgn.truncate(new_size)
        pgn.close()

        return length
Пример #22
0
def get_data(file, request):
    pgn = open(file, encoding="utf-8-sig")
    pgns = []

    game = chess.pgn.read_game(pgn)
    t_name = game.headers.get("Event", "?")
    t_date = game.headers.get("EventDate", "????.??.??")
    t_city = game.headers.get("Site", "?")
    request.session['t_name'] = t_name
    request.session['t_date'] = t_date
    request.session['t_city'] = t_city
    while game is not None:
        pgns.append(str(game))
        game = chess.pgn.read_game(pgn)

    meta = {'pgn': pgns}

    pgn.close()
    if os.path.exists(file):
        os.remove(file)
    files = Parse.objects.all()
    for file in files:
        file.delete()
    return meta
Пример #23
0
def get_pgn_training(names, path, save=False):
    training = []
    for name in names:
        pgn_path = datadir + name
        pgn = open(pgn_path)

        pgn_game = chess.pgn.read_game(pgn)
        while pgn_game is not None:
            res = pgn_game.headers['Result']
            placeholder = []
            board = pgn_game.board()
            game = Game(board)

            for move in pgn_game.mainline_moves():
                player = 1 if game.board.turn else -1
                uci_move = move.uci()
                probs = encode.get_prob_mask([uci_move])
                assert np.sum(probs) == 1
                normalized_encoded = game.copy_and_normalize().get_encoded()
                placeholder.append((normalized_encoded, probs, player))
                game.make_move(uci_move)

            # check if not resigned
            state = game.get_game_state()
            if (res == '1/2-1/2' and state == 0) or abs(state) == 1:
                for tup in placeholder:
                    train = (tup[0], state * tup[2], tup[1])
                    training.append(train)

            pgn_game = chess.pgn.read_game(pgn)

        pgn.close()

    if save:
        util.save_prev_training(training, path)
    return training
Пример #24
0
import subprocess
import random

engine = chess.uci.popen_engine("/Projects/chess-ai/stockfish")
engine.uci()
engine.author

# Get best moves
board = chess.Board("1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - 0 1")
engine.position(board)
engine.go(movetime=2000, depth=0)  # Gets tuple of bestmove and ponder move.

# Iterating over PGN file
pgn = open("C:\Projects\chess-ai\movestest.pgn")
first_game = chess.pgn.read_game(pgn)
pgn.close()

positions = []

node = first_game
while node.variations:
    next_node = node.variation(0)
    positions.append(node.board().fen())
    node = next_node


# Feeding input to and getting output from stockfish binary
def put(command):
    print('\nyou:\n\t' + command)
    engine.stdin.write(command + '\n')
Пример #25
0
    pass

logging.basicConfig(format="%(message)s", level=settings.loglevel, stream=sys.stdout)
logging.getLogger("requests.packages.urllib3").setLevel(logging.WARNING)
logging.getLogger("chess.uci").setLevel(logging.WARNING)

engine = chess.uci.popen_engine(stockfish_command())
engine.setoption({'Threads': settings.threads, 'Hash': settings.memory})
engine.uci()
info_handler = chess.uci.InfoHandler()
engine.info_handlers.append(info_handler)

while True:
    pgn = get_pgn(settings.token)
    game = chess.pgn.read_game(pgn)
    pgn.close()

    node = game

    game_id = game.headers["Site"].split('/')[-1:][0]
    logging.debug(bcolors.WARNING + "Game ID: " + game_id + bcolors.ENDC)

    prev_score = chess.uci.Score(None, None)
    puzzles = []

    logging.debug(bcolors.OKGREEN + "Game Length: " + str(game.end().board().fullmove_number))
    logging.debug("Analysing Game..." + bcolors.ENDC)

    engine.ucinewgame()

    while not node.is_end():
Пример #26
0
    def read_game(self, idx):
        self.env.reset()
        self.black = ChessPlayer(self.config, self.model)
        self.white = ChessPlayer(self.config, self.model)
        files = find_pgn_files(self.config.resource.play_data_dir)
        if len(files) > 0:
            random.shuffle(files)
            filename = files[0]
            pgn = open(filename, errors='ignore')
            size = os.path.getsize(filename)
            pos = random.randint(0, size)
            pgn.seek(pos)

            line = pgn.readline()
            offset = 0
            # Parse game headers.
            while line:
                if line.isspace() or line.startswith("%"):
                    line = pgn.readline()
                    continue

                # Read header tags.
                tag_match = TAG_REGEX.match(line)
                if tag_match:
                    offset = pgn.tell()
                    break

                line = pgn.readline()

            pgn.seek(offset)
            game = chess.pgn.read_game(pgn)
            node = game
            result = game.headers["Result"]
            actions = []
            while not node.is_end():
                next_node = node.variation(0)
                actions.append(node.board().uci(next_node.move))
                node = next_node
            pgn.close()

            k = 0
            observation = self.env.observation
            while not self.env.done and k < len(actions):
                if self.env.board.turn == chess.BLACK:
                    action = self.black.sl_action(observation, actions[k])
                else:
                    action = self.white.sl_action(observation, actions[k])
                board, info = self.env.step(action)
                observation = board.fen()
                k += 1

            self.env.done = True
            if not self.env.board.is_game_over() and result != '1/2-1/2':
                self.env.resigned = True
            if result == '1-0':
                self.env.winner = Winner.white
            elif result == '0-1':
                self.env.winner = Winner.black
            else:
                self.env.winner = Winner.draw

            self.finish_game()
            self.save_play_data(write=idx % self.config.play_data.nb_game_in_file == 0)
            self.remove_play_data()
        return self.env
Пример #27
0
def base_dataset():

    # TODO get path from command line
    engine = chess.engine.SimpleEngine.popen_uci('../stockfish_engine_x64')

    # TODO get numbers as args from command line
    initial_pgn_number = int(input("number of the first pgn to evaluate: "))
    final_pgn_number = int(input("number of the last pgn to evaluate: "))

    # procura arquivo com os scores salvos. Se ele existe, escreve no fim dele. Caso contrário, o arquivo é criado
    if os.path.isfile("scores.csv"):
        scores_file = open("scores.csv", "a")
    else:
        scores_file = open("scores.csv", "w")
        scores_file.write(
            "Id, Pgn Number, Event, Move Number, Move, Score, Comment, Label\n"
        )

    # checa se existe um arquivo contendo o primeiro id dos movimentos a serem inseridos no dataset. Caso contrário, ele é criado
    if os.path.isfile("next_id.txt"):
        next_id = open("next_id.txt")
        move_id = int(next_id.readline())
        next_id.close()
    else:
        move_id = 0

    # loop principal de avaliação das partidas
    for i in range(initial_pgn_number, final_pgn_number + 1):

        game_path = f'../../pgn_files/pgn_{i}.txt'
        # checa se pgn i existe
        try:
            if (os.path.isfile(game_path)):
                print(f'Avaliando pgn {i}')

                # abre e lê arquivo pgn
                pgn = open(game_path)
                title = pgn.readline()
                # titulo da partida, que será inserido no dataset
                title = re.findall(r'"(.*?)"', title)[0]

                # cria objeto para representar a partida partindo do arquivo pgn
                game = chess.pgn.read_game(pgn)
                board = game.board()
                # limite de busca da engine
                limit = chess.engine.Limit(depth=15)
                move_number = 1
                scores = []  # lista com as linhas a serem inseridos no dataset
                score_values = [
                ]  # lista com os scores numericos de cada jogada

                turn = 'W'

                # loop de avaliação de uma partida
                for node in game.mainline():
                    # insere próximo movimento da partida no tabuleiro
                    board.push(node.move)
                    info = engine.analyse(board, limit)

                    # tag para diferenciar jogada das brancas e das negras
                    if move_number == math.floor(move_number):
                        turn = 'W'
                    else:
                        turn = 'B'

                    # insere linha no dataset
                    scores.append(
                        f'{move_id}, {i}, "{title}", {math.floor(move_number)}{turn}, {node.move}, {info["score"].white()}, "{node.comment}"'
                    )

                    # insere score numerico na lista
                    # score_values.append(info["score"].white().score())
                    # para casos de jogadas proximos a um cheque mate, o score e substituido por um contador
                    if info["score"].white().score() == None:
                        # extrai sinal + (brancas) ou - (negras) do contador de jogadas ate mate
                        mate_score = f'{info["score"].white()}'
                        # score numerico para esta situacao vira +1000 ou -1000
                        score_normalized = int(f'{mate_score[1]}1000')
                        score_values.append(score_normalized)
                    else:
                        score_values.append(info["score"].white().score())

                    #print(f'Analisado movimento {math.floor(move_number)}')

                    move_number += 0.5
                    move_id += 1

                pgn.close()

                # cria lista contendo as labels de ponto de virada
                pontos_virada = generate_labels(score_values)

                # escreve no dataset todas as linhas de avaliação desta partida
                j = 0
                for score in scores:
                    scores_file.write(f'{score}, {pontos_virada[j]}')
                    scores_file.write("\n")
                    j += 1

            else:
                print(f'Arquivo pgn {i} não existe')

        except:
            print("Pgn com caracteres nao reconhecidos")
            log = open("log.txt", "a")
            log.write(f'{i}\n')
            log.close()

    scores_file.close()

    # salva o id do último movimento analisado

    next_id = open("next_id.txt", "w")
    next_id.write(f'{move_id}')
    next_id.close()