def add_games_to_db(inputfile, outputfile): """doc""" conn = sqlite3.connect(outputfile) c = conn.cursor() games_inserted = 0 for index, game in enumerate(pgn.GameIterator(inputfile)): try: e, t, g = parse_games(game) try: c.execute( """INSERT INTO TimeControl(time, increment) VALUES (?,?)""", t) except IntegrityError: pass try: c.execute( """INSERT INTO Games VALUES (?,?,?,?,?,?,?,?,?)""", g) except IntegrityError: pass except AttributeError: pass games_inserted = index + 1 if games_inserted % 100 == 0: print(games_inserted) print(str(games_inserted) + " games inserted") conn.commit() conn.close()
def addGamesToDB(inputfile, outputfile, clear=False): """doc""" conn = sqlite3.connect(outputfile) c = conn.cursor() if clear: c.execute("DELETE FROM Games") for index, game in enumerate(pgn.GameIterator(inputfile)): g = (game.result, game.blackelo, game.whiteelo, game.timecontrol, ", " + ", ".join(game.moves[:-2])) c.execute("INSERT INTO Games VALUES (?,?,?,?,?)", g) print(index) conn.commit() conn.close()
num_moves_arr = np.zeros(max_len) #[[0 for j in xrange(30)] for i in xrange(max_len)] #holds the top-1,3,5,10 predictions #hits = [(0,0) for i in xrange(max_len)] # board_images_lists = [] # legal_moves_lists = [] # movelists = [] min_elo = 2000 max_elo = 3255 max_len = 0 C = 1255 num_games = 0 f = "sample2.pgn" Play.load_models("play/models") stop = False for game in pgn.GameIterator(f): if num_games % 10 == 0: print "Done with %d games" % num_games if (not game) or stop: break num_games += 1 board = chess.Bitboard() moves = game.moves max_len = max(len(moves), max_len) # board_images = [] # movelist = [] # legal_moves_list = [] black_elo = int(game.blackelo) white_elo = int(game.whiteelo) #if args.elolayer: white_elo_layer = float(white_elo - min_elo) / C black_elo_layer = float(black_elo - min_elo) / C
def get_game(): """doc""" games = pgn.GameIterator(config.NEO4J_PGN_FILE) for game in games: if game.ficsgamesdbgameno == config.GAMEID: return game
NUM_GAMES = 2500 #assign the correct functions from util.py if args.multiple_layers: bitboard_to_image = convert_bitboard_to_image_2 flip_color = flip_color_2 else: bitboard_to_image = convert_bitboard_to_image_1 flip_color = flip_color_1 print "Started reading PGN files in directory %s" % PGN_DATA_DIR game_index = 0 for f in os.listdir(PGN_DATA_DIR): if ".pgn" in f: print "%s file opened...." % f for game in pgn.GameIterator(PGN_DATA_DIR + "/" + f): if not game: break #print PGN_DATA_DIR+"/"+f, game board = chess.Bitboard() moves = game.moves if game_index % NUM_GAMES == 0: if game_index != 0: end = timeit.default_timer() print "Processed %d moves from %d games in %fs" % ( len(X), NUM_GAMES, end - start) start = timeit.default_timer() print "Saving data for %d-%d games.." % ( game_index - NUM_GAMES, game_index) print "Saving X array..." output = TRAIN_DATA_DIR + '/X_%d_%d.npz' % (
if hasattr(game, 'moves'): ret += str(len(game.moves)) + ', ' ret += ' \'' + ' '.join(game.moves) + '\'' else: ret += '0, \'\'' ret += ')' return ret if len(sys.argv) != 2: print 'Usage: python pgn-to-sql.py input.pgn > out.sql' i = 0 for game in pgn.GameIterator(sys.argv[1]): if not (hasattr(game, 'result')): break if (i % 500) == 0: print 'INSERT INTO game (' + ', '.join( fields) + ', length, moves) VALUES ' print values_row(game) if (i % 500) == 499: print ';' else: print ',' i += 1