def main(depth, train_mode): start = time.time() model = create_network(depth, ARTIST_NAME=ARTIST_NAME, TRAIN_MODE=TRAIN_MODE) # change the lyrics file to the file with the lyrics you want to be trained on text_file = "ostr_lyrics.txt" if train_mode == True: bars = split_lyrics_file(text_file) if train_mode == False: bars = generate_lyrics(text_file) logger.debug("Generated lyrics:") logger.debug(bars) rhyme_list = prepare_list_of_all_rhymes(bars) if train_mode == True: x_data, y_data = build_dataset(bars, rhyme_list) train(x_data, y_data, model) if train_mode == False: vectors = compose_rap(bars, rhyme_list, text_file, model) rap = vectors_into_song(vectors, bars, rhyme_list) # f = open(FILE_WITH_GENERATED_LYRICS, "w") # for bar in rap: # f.write(bar) # f.write("\n") return rap stop = time.time() logger.DEBUG("Execution time in sec:{}".format(stop - start))
def find_rhyme(word): collection = prepare_connection_to_db_rhymes() logger.debug("Looking in db for {}".format(word)) check = word_is_in_db(word, collection) if check: logger.debug("Found") return check["rhymes"] logger.debug("Looking remote") time.sleep(10) result = find_rhyme_on_remote(word) record = {"word": word, "rhymes": result} logger.debug("Putting into db: {}".format(record["word"])) collection.insert_one(record) return result
def build_new_rhyme_list(lyrics): rhyme_master_list = [] logger.debug("Alright, building the list of all the rhymes") for i in lyrics: # grabs the last word in each bar word = re.sub(r"\W+", "", i.split(" ")[-1]).lower() # fixed scrapy spider for searching rhymes in web rhymeslist = find_rhyme(word) # need to convert the unicode rhyme words to UTF8 # rhymeslistends contains the last two characters for each word # that could potentially rhyme with our word rhymeslistends = [] for i in rhymeslist: rhymeslistends.append(str(i[-2:])) try: # rhymescheme gets all the unique two letter endings and then # finds the one that occurs the most rhymescheme = max(set(rhymeslistends), key=rhymeslistends.count) except Exception: rhymescheme = word[-2:] rhyme_master_list.append(rhymescheme) # rhyme_master_list is a list of the two letters endings that appear # the most in the rhyme list for the word rhyme_master_list = list(set(rhyme_master_list)) reverselist = [x[::-1] for x in rhyme_master_list] reverselist = sorted(reverselist) # rhymelist is a list of the two letter endings (reversed) # the reason the letters are reversed and sorted is so # if the network messes up a little bit and doesn't return quite # the right values, it can often lead to picking the rhyme ending next to the # expected one in the list. But now the endings will be sorted and close together # so if the network messes up, that's alright and as long as it's just close to the # correct rhymes rhymelist = [x[::-1] for x in reverselist] f = open(str(ARTIST_NAME) + ".rhymes", "w") f.write("\n".join(rhymelist)) f.close() logger.debug(rhymelist) return rhymelist
def convert_index_of_most_common_rhyme_into_float(line, rhyme_list): word = re.sub(r"\W+", "", line.split(" ")[-1]).lower() logger.debug("Looking rhyme for: {}".format(word)) rhymeslist = find_rhyme(word) rhymeslistends = [] for i in rhymeslist: rhymeslistends.append(i[-2:]) try: ordered_rhyme_ends = max(set(rhymeslistends), key=rhymeslistends.count) ordered_rhyme_ends = ordered_rhyme_ends.decode("utf-8") except AttributeError: ordered_rhyme_ends = ordered_rhyme_ends except Exception: ordered_rhyme_ends = word[-2:] try: float_rhyme = rhyme_list.index(ordered_rhyme_ends) float_rhyme = float_rhyme / float(len(rhyme_list)) logger.info("Calculated floatrhyme: {}".format(float_rhyme)) return float_rhyme except ValueError as e: logger.error("Value Error: {}".format(e.__str__())) return 0
def start_game(self): no_winner = True players = [] for i in range(self.player_amount): player = Player("Player-{}".format(i), "Color-{}".format(i)) self.game_board.register_player(player) players.append(player) # game loop logger.info("Starting new game") while no_winner: for player in players: # increment player turns player.turns += 1 logger.info("Player {}, Turn {}:".format( player.name, player.turns)) logger.debug( "Player data before turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) # grab players figures from cemetery player.grab_figures_from_cemetery(self.game_board) # check for player's figures on board if player.has_figures_on_board(self.game_board): if self.roll() == 6 and len(player.start_figures) != 0: player.place_figure(self.game_board) player.move_figure(self.game_board, self.roll()) # player has no figure on board else: # three chances to roll a 6 for i in range(3): if self.roll() == 6: # place new figure player.place_figure(self.game_board) # move figure player.move_figure(self.game_board, self.roll()) break # count finished figures to evaluate win condition finished_figures = [ figure for figure in player.finished_figures if hasattr(figure, "name") ] if len(finished_figures) == 4: no_winner = False logger.info( "Player {} won the game after {} turns!".format( player.name, player.turns)) break logger.debug( "Player data after turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) # debug output of board fields logger.debug("Board fields after turn: {}".format( self.game_board.display_board()))
def generate_texts_periodically(): logger.debug("Started text generator") collection = prepare_connection_to_db_texts() while collection.count_documents({}) < 30: logger.debug(f'Texts in base: {collection.count_documents({})}') lines = main(4, False) record = {"text": lines} logger.debug("Putting text into db") collection.insert_one(record) sleep(60)
def load_saved_rhymes_file(ARTIST_NAME): logger.debug("loading saved rhymes from " + str(ARTIST_NAME) + ".rhymes") return open(str(ARTIST_NAME) + ".rhymes", "r").read().split("\n")
def simulate_game(player_amount, figure_amount, field_amount, roll_duration, move_duration): # game init no_winner = True game_board = Board(player_amount, field_amount) players = [] for i in range(player_amount): player = Player("Player-{}".format(i), "Color-{}".format(i), figure_amount) game_board.register_player(player) players.append(player) # game loop logger.info("Starting new game simulation..") while no_winner: for player in players: # increment player turns player.turns += 1 logger.info("Player {}, Turn {}:".format(player.name, player.turns)) logger.debug( "Player data before turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) # grab players figures from cemetery player.grab_figures_from_cemetery(game_board) # check for player's figures on board if player.has_figures_on_board(game_board): if player.roll(roll_duration) == 6 and len( player.start_figures) != 0: player.place_figure(game_board) player.move_figure(game_board, player.roll(roll_duration), move_duration) # player has no figure on board else: # three chances to roll a 6 for i in range(3): if player.roll(roll_duration) == 6: # place new figure player.place_figure(game_board) # move figure player.move_figure(game_board, player.roll(roll_duration), move_duration) break # count finished figures to evaluate win condition finished_figures = [ figure for figure in player.finished_figures if hasattr(figure, "name") ] if len(finished_figures) == player.figure_amount: no_winner = False total_real_playtime = sum( [player.real_playtime for player in players]) / 60 total_real_playtime = round(total_real_playtime, 2) amount_of_six_stats = [ player.amount_of_six for player in players ] total_dice_eye_stats = [ player.total_roll for player in players ] player_names = [player.name for player in players] aos = pd.DataFrame(amount_of_six_stats, index=player_names, columns=["amount"]) tde = pd.DataFrame(total_dice_eye_stats, index=player_names, columns=["sum"]) logger.info("Player {} won the game after {} turns!".format( player.name, player.turns)) st.success( "Player {} won the game after {} turns and {} rolls!". format(player.name, player.turns, player.roll_turns)) st.info( "By using this simulator you saved {} minutes of your life avoiding playing senseless games! :)" .format(total_real_playtime)) st.subheader("Sum of dice eyes per player") st.bar_chart(tde) st.subheader("Total amount of dice eye 6 rolls per player") st.bar_chart(aos) break logger.debug( "Player data after turn: start figures: {}, finished figures: {}" .format(reveal_name(player.start_figures), reveal_name(player.finished_figures))) # debug output of board fields logger.debug("Board fields after turn: {}".format( reveal_name(game_board.fields)))