예제 #1
0
def line_mono_correlation(line, a, monograms):  # line is a list of words
    total = 0
    word_count = 0
    for i in range(len(line)):
        if not dictionary.is_word(line[i]):
            continue
        word_count += 1
        for j in range(len(line)):
            if i == j or not dictionary.is_word(line[j]):
                continue
            total += mono_correlation(monograms[line[i]], monograms[line[j]], a)
    if word_count <= 1:
        return 0
    return total / (word_count * (word_count - 1))
예제 #2
0
def mutate_line(my_linehaiku, monograms):
    my_line = my_linehaiku.wordarray
    new_line = my_line
    k = random.randint(0, len(my_line) - 1)
    found_a_word = False
    for i in range(1000):
        if dictionary.is_word(my_line[k]):
            found_a_word = True
            break
        k = random.randint(0, len(my_line) - 1)
    if not found_a_word:
        return my_linehaiku  # just give up

    word_list = [
        x
        for x in monograms
        if (monograms[x].wordtype == monograms[my_line[k]].wordtype)
        & (monograms[x].syllables == monograms[my_line[k]].syllables)
    ]

    word_weight = [monograms[x].occurrences for x in word_list]
    generated_word = random_weighted_occurrence(word_list, word_weight)
    new_line[k] = generated_word
    return Line_Haiku(new_line, my_linehaiku.typenum)
예제 #3
0
def train_haiku(haiku, monograms, bigrams, digrams, line_types):
    """Takes in a new Haiku as well as three dictionaries: one of 
    Monogram objects (keys are words), one of Bi-Gram objects (keys are
    phrases), and one of Line_type objects (keys are skeletons). 
    Updates the three dictionaries, returns None."""

    for line in haiku.triple:
        words = line.wordarray
        
        # updates line_types
        abstract_skeleton = (tuple((tuple(dictionary.wordtype(a)),
           dictionary.syllablecnt(a)) for a in words), line.typenum)
        if abstract_skeleton in line_types:
            line_types[abstract_skeleton].update()
        else:
            abstrlin = Line_type(abstract_skeleton[0], abstract_skeleton[1])
            abstrlin.update()
            line_types[abstrlin.skeleton] = abstrlin

        # updates individual monograms
        for i in range(len(words)):
            w = dictionary.word_filter(words[i])
            if dictionary.is_word(w):
                # print (w, "is a word")
                if w in monograms:
                    monograms[w].update(haiku, line.typenum)
                else:
                    new_mono = Monogram(w)
                    new_mono.update(haiku, line.typenum)
                    monograms[w] = new_mono
                    
        #if len(words) == 0:
        #    print("empty word")
        #    exit()   
        
        #updates bigrams and digrams      
        for i in range(len(words)):
            if i < len(words)-1:
                (w_1, w_2)=(dictionary.word_filter(words[i]),
                            dictionary.word_filter(words[i+1]))
                
                if i == 0:
                    #if w_1 == "":
                    #    print("empty filtered word")
                    #    print(words)
                    
                    if ("\n", w_1) in digrams:
                        digrams[("\n", w_1)] += 1
                    else: 
                        digrams[("\n", w_1)] = 1
                
                if (w_1, w_2) in digrams:
                    digrams[(w_1, w_2)] += 1
                else:
                    digrams[(w_1, w_2)] = 1

                if (dictionary.is_word(w_1) and dictionary.is_word(w_2)):
                    if (w_1, w_2) in bigrams:
                        bigrams[(w_1, w_2)].update()
                    else:
                        new_bi = Bi_Gram(w_1, w_2)
                        new_bi.update()
                        bigrams[(w_1, w_2)] = new_bi
            else:
                w = dictionary.word_filter(words[i])
                if (w, "/n") in digrams:
                    digrams[(w, "\n")] +=1
                else:
                    digrams[(w, "\n")] = 1