Пример #1
0
def crossover(population):
    """
        Performs crossover on a given generation of poems, swapping the first
        and second halves of each poem in a generation

        population (list): A list representing the breeding pool for the
        current generation
    """
    new_population = []
    for i in range(0, len(population), 2):
        new_poem1 = Poem()
        new_poem1.first_half = population[i].first_half
        new_poem1.second_half = population[i + 1].second_half

        new_poem2 = Poem()
        new_poem2.first_half = population[i + 1].first_half
        new_poem2.second_half = population[i].second_half

        new_population.append(new_poem1)
        new_population.append(new_poem2)

    return new_population
Пример #2
0
def seed_population(my_word, first_words):
    """
        Generates a starting population of poems, using n-grams to generate
        a word based on a previous sequence of words

        my_word ()
        first_words (string): a string representing the first n words of
        the inspiring test, which are used to start the poem generation
    """
    poem_string = first_words
    old_sequence = first_words
    population = []

    # generates 20 poems for a generation
    counter = 0
    max_poems = 20

    while counter < max_poems:
        poem = Poem()

        # each poem is 50 words
        for i in range(50):
            if len(poem_string) == 0:
                random_word = old_sequence.split()[0]
            else:
                random_word = rand.choice(poem_string.split())
            new_word = my_word.get_next_word(old_sequence, random_word) + " "
            poem_string += new_word

            line_val = i % 10
            if line_val == 0:
                poem_string += '\n'

            # for the purposes of crossbreeding, splits the poem in half
            if len(poem_string.split()) == 25:
                if i > 25:
                    poem.second_half = poem_string
                    poem_string = ""
                else:
                    poem.first_half = poem_string
                    poem_string = ""

            old_word_list = old_sequence.split()
            old_sequence = ""
            for k in range(1, len(old_word_list)):
                old_sequence = old_sequence + old_word_list[k] + " "
            old_sequence = old_sequence + new_word

        population.append(poem)
        counter += 1
    return population
Пример #3
0
def mutate(poem, word_dictionary, n_value, mutation_prob=0.05):
    """
        Handles the mutation functions of the poem by calling helper functions

        poem(Poem): the given poem to mutate
        word_dictionary (word_dictionary): the dictionary containing the
        n-grams information
        n_value (int): the user inputted n value
        mutation_prob (float): the probability that an indivudal word would be
        mutated
    """
    new_poem = Poem()
    new_poem.first_half = get_words(poem.first_half, word_dictionary, n_value,
                                    mutation_prob)
    new_poem.second_half = get_words(poem.second_half, word_dictionary,
                                     n_value, mutation_prob)
    return new_poem