Example #1
0
def write_naive_sonnet():
    sonnet = ''
    for i in range(14):
        if i % 4 ==0:
            sonnet += '\n'
        sonnet += sample_sentence(hmm, word_to_int, 10) + ',\n'
    return sonnet
Example #2
0
def write_rhyming_sonnet():
    sonnet = ''

    phoneme_sentences = {}
    # generate 360 sentences of length 10
    count = 0
    while count < 360:
        sentence = sample_sentence(hmm, word_to_int, n_words=10)
        num_syllables = count_sentence_syllables(sentence)
        if num_syllables == 10:
            last_syllable = get_last_syllable(sentence)
            phoneme_sentences[last_syllable] = sentence
            count += 1

    # get the structure
    structure = get_structure()
    for i,syllable in enumerate(structure):
        if i % 4 == 0:
            sonnet += '\n'
        sonnet += random.choice(phoneme_sentences[syllable]) + ',\n'
     return sonnet
Example #3
0
    dic = open(os.path.join(os.getcwd(),
                            '../data/Syllable_dictionary.txt')).read()
    lines = [line.split() for line in dic.split('\n') if line.split()]

    syl_dic = {}

    for line in lines:
        normal_syl = []
        end_syl = []
        for i, word in enumerate(line):

            if i == 0:
                continue

            if word[0] == 'E':
                end_syl.append(int(word[1:]))
            else:
                normal_syl.append(int(word))

        if len(end_syl) == 0:
            end_syl = normal_syl
        syl_dic[line[0]] = [normal_syl, end_syl]
    #########
    #print(syl_dic)
    for j in range(20):
        for i in range(12):
            print(sample_sentence(HMM, obs_map, syl_dic, stress_dic))
        for i in range(2):
            print('  ' + sample_sentence(HMM, obs_map, syl_dic, stress_dic))
        print('')
# ## Part G: Visualization of the sparsities of A and O

# We can visualize the sparsities of the A and O matrices by treating the matrix entries as intensity values and showing them as images. What patterns do you notice?

# In[9]:

visualize_sparsities(hmm8, O_max_cols=50)

# ## Generating a sample sentence

# As you have already seen, an HMM can be used to generate sample sequences based on the given dataset. Run the cell below to show a sample sentence based on the Constitution.

# In[5]:

print('Sample Sentence:\n====================')
print(sample_sentence(hmm8, obs_map, n_words=25))

# ## Part H: Using varying numbers of hidden states

# Using different numbers of hidden states can lead to different behaviours in the HMMs. Below, we train several HMMs with 1, 2, 4, and 16 hidden states, respectively. What do you notice about their emissions? How do these emissions compare to the emission above?

# In[ ]:

hmm1 = unsupervised_HMM(obs, 1, 100)
print('\nSample Sentence:\n====================')
print(sample_sentence(hmm1, obs_map, n_words=25))

# In[ ]:

hmm2 = unsupervised_HMM(obs, 2, 100)
print('\nSample Sentence:\n====================')
Example #5
0
    states_to_wordclouds,
    parse_observations,
    sample_sentence,
    visualize_sparsities,
    animate_emission,
)

# pre-porcessing
poem_lists, uatrain_lists, volta_lists, couplet_lists, word_to_int, int_to_word = parse_data('data/shakespeare.txt')

# train HMM
hmm = unsupervised_HMM(poem_lists, n_states=20, N_iters=10)

# sample naive sentence
print('Sample Naive Sentence:\n====================')
print(sample_sentence(hmm, word_to_int, n_words=10))

def write_naive_sonnet():
    sonnet = ''
    for i in range(14):
        if i % 4 ==0:
            sonnet += '\n'
        sonnet += sample_sentence(hmm, word_to_int, 10) + ',\n'
    return sonnet

print('Naive Sonet:\n====================')
print (write_naive_sonnet() + '\n\n\n\n')

# Poetry Generation

def write_rhyming_sonnet():
Example #6
0
hmm4 = pickle.load(open('hmm4.p', 'rb'))
hmm16 = pickle.load(open('hmm16.p', 'rb'))
hmm32 = pickle.load(open('hmm32.p', 'rb'))
hmm64 = pickle.load(open('hmm64.p', 'rb'))

ids = None
ids_map = None

with open('obs.p', 'rb') as f:
    ids = pickle.load(f)
    ids_map = pickle.load(f)

text = ''

for i in range(14):
    new = sample_sentence(hmm64, ids_map, n_words=5)
    print(new)
    text += new

obs_map = []

# for key, value in ids_map.items():
#     if key in text:
#         obs_map.append(key)

obs_map = dict(filter(lambda x: x[0] in text, ids_map.items()))

wordcloud = text_to_wordcloud(text, title='sonnet')

visualize_sparsities(hmm64, O_max_cols=50)
Example #7
0
    for j in i:
        word = '\'' + j
        if word in outliers:
            line.append(new_unique.index(word))
        else:
            line.append(new_unique.index(j))
    ids.append(line)

with open('obs.p', 'wb') as f:
    pickle.dump(ids, f)
    pickle.dump(ids_map, f)

hmm8 = unsupervised_HMM(ids, 10, 100)
pickle.dump(hmm8, open('hmm8.p', 'wb'))
print('Sample Sentence:\n====================')
print(sample_sentence(hmm8, ids_map, n_words=25))

hmm1 = unsupervised_HMM(ids, 1, 100)
pickle.dump(hmm1, open('hmm1.p', 'wb'))
print('\nSample Sentence:\n====================')
print(sample_sentence(hmm1, ids_map, n_words=25))

hmm2 = unsupervised_HMM(ids, 2, 100)
pickle.dump(hmm2, open('hmm2.p', 'wb'))
print('\nSample Sentence:\n====================')
print(sample_sentence(hmm2, ids_map, n_words=25))

hmm4 = unsupervised_HMM(ids, 4, 100)
pickle.dump(hmm4, open('hmm4.p', 'wb'))
print('\nSample Sentence:\n====================')
print(sample_sentence(hmm4, ids_map, n_words=25))