def hmm_shakespeare_sonnet_goal1(): # Load in everything sonnets, obs_map = sp.get_sonnets("data/shakespeare.txt", 3000) obs_map_r = {} for key in obs_map: obs_map_r[obs_map[key]] = key syl_map = sp.get_syllable_map("data/Syllable_dictionary.txt") # Train HMM model = HMM.unsupervised_HMM(sonnets, 5, 25) num_states = model.L # Print one blank line to make it pretty print("") # Generate sonnet last_state = np.random.choice(num_states, p=model.A_start) for n_lines in [4, 4, 2]: for l_no in range(n_lines): line = "" while (not line): curr_state = last_state l = "" no_syls = 0 while (no_syls < 10): w, curr_state = add_word(curr_state, model.A, model.O) new_word = obs_map_r[w] l += new_word l += " " no_syls += syl_map[new_word] if (count_syllables(l, syl_map) == 10): line = l last_state = curr_state print(line.capitalize()) print("")
def hmm_shakespeare_sonnet_naive(): # Load in everything sonnets, obs_map = sp.get_sonnets("data/shakespeare.txt", 900) obs_map_r = {} for key in obs_map: obs_map_r[obs_map[key]] = key # Train HMM model = HMM.unsupervised_HMM(sonnets, 10, 10) # Print one blank line to make it pretty print("") # Generate quatrain 1 for _ in range(4): line = "" while (not line): l = "" emission, states = model.generate_emission(7) for i in range(len(emission)): e = emission[i] w = obs_map_r[e] if (i == 0): w = w.capitalize() l += w l += " " line = l print(line) print("") # Generate quatrain 2 for _ in range(4): line = "" while (not line): l = "" emission, states = model.generate_emission(7) for i in range(len(emission)): e = emission[i] w = obs_map_r[e] if (i == 0): w = w.capitalize() l += w l += " " line = l print(line) # Generate couplet for _ in range(2): line = "" while (not line): l = "" emission, states = model.generate_emission(7) for i in range(len(emission)): e = emission[i] w = obs_map_r[e] if (i == 0): w = w.capitalize() l += w l += " " line = get_n_syls(10, l, syl_map) print(line)
def rnn_shakespeare_sonnet(): ''' Deal with the data and engine running the model. ''' # Load in everything sonnetsAsNums, obs_map = sp.get_sonnets("data/shakespeare.txt", 900) #print(obs_map) obs_map_r = {} sonnets = get_char_sonnets("data/shakespeare.txt", max_words=900) for key in obs_map: obs_map_r[obs_map[key]] = key syl_map = sp.get_syllable_map("data/Syllable_dictionary.txt") # Train HMM RNN(sonnets, sonnetsAsNums, obs_map)
def hmm_shakespeare_sonnet_goal2(): # Load in everything sonnets, obs_map = sp.get_sonnets("data/shakespeare.txt", 2000) obs_map_r = {} for key in obs_map: obs_map_r[obs_map[key]] = key syl_map = sp.get_syllable_map("data/Syllable_dictionary.txt") rhymes = sp.get_rhymes("data/shakespeare.txt", True) # Train HMM model = HMM.unsupervised_HMM(sonnets, 5, 25) num_states = model.L # Print one blank line to make it pretty print("") # Generate quatrains for _ in range(2): while (True): r = random.randint(0, len(rhymes) - 1) rhyme_pair_1 = rhymes[r] if (rhyme_pair_1[0] in obs_map and rhyme_pair_1[1] in obs_map): break while (True): r = random.randint(0, len(rhymes) - 1) rhyme_pair_2 = rhymes[r] if (rhyme_pair_2[0] in obs_map and rhyme_pair_2[1] in obs_map): break print( make_rhyme_line(model, obs_map[rhyme_pair_1[0]], syl_map, obs_map_r)) print( make_rhyme_line(model, obs_map[rhyme_pair_2[0]], syl_map, obs_map_r)) print( make_rhyme_line(model, obs_map[rhyme_pair_1[1]], syl_map, obs_map_r)) print( make_rhyme_line(model, obs_map[rhyme_pair_2[1]], syl_map, obs_map_r)) print("") # Generate couplet while (True): r = random.randint(0, len(rhymes) - 1) rhyme_pair = rhymes[r] if (rhyme_pair[0] in obs_map and rhyme_pair[1] in obs_map): break print(make_rhyme_line(model, obs_map[rhyme_pair[0]], syl_map, obs_map_r)) print(make_rhyme_line(model, obs_map[rhyme_pair[1]], syl_map, obs_map_r)) print("")
def hmm_shakespeare_sonnet_goal3(): # Load in everything sonnets, obs_map = sp.get_sonnets("data/shakespeare.txt", 1000) obs_counter = len(obs_map) star_wars_lines = [] f = open("data/StarWarsIV.txt") for swl in f: line = [] words = swl.split() for sww in words: w = sww.translate(str.maketrans('', '', string.punctuation)) w = w.lower() if (w not in obs_map): obs_map[w] = obs_counter obs_counter += 1 line.append(obs_map[w]) star_wars_lines.append(line) if obs_counter > 3000: break f.close() obs_map_r = {} for key in obs_map: obs_map_r[obs_map[key]] = key data = sonnets + star_wars_lines # Train HMM model = HMM.unsupervised_HMM(data, 5, 10) num_states = model.L # Print one blank line to make it pretty print("") # Generate sonnet last_state = np.random.choice(num_states, p=model.A_start) for n_lines in [4, 4, 2]: for l_no in range(n_lines): curr_state = last_state line = "" for _ in range(7): w, curr_state = add_word(curr_state, model.A, model.O) new_word = obs_map_r[w] line += new_word line += " " last_state = curr_state print(line.capitalize()) print("")
stress_map, syllable_map, A, O) return sonnet ############################################################################## ################# Model training and Sonnet Generation ################# ############################################################################## # data_path = 'data/' data_path = '../data/' rhymes = sp.get_rhymes(data_path + 'shakespeare.txt') # choose 1 from below. suggest get_stress_map stress_map = get_stress_map(data_path + 'shakespeare.txt') # stress_map = get_stress_map_nltk(data_path+'Syllable_dictionary.txt') # choose 1 from below. suggest sp.get_syllable_map syllable_map = sp.get_syllable_map(data_path + "Syllable_dictionary.txt") # syllable_map = get_full_syllable_map(data_path+'Syllable_dictionary.txt') sonnets, obs_map = sp.get_sonnets(data_path + "shakespeare.txt", 1000) model = HMM.unsupervised_HMM(sonnets, 10, 10) obs_map_r = {} for key in obs_map: obs_map_r[obs_map[key]] = key sonnet = make_rhyme_iambic_sonnet(rhymes, obs_map, obs_map_r, stress_map, syllable_map, model.A, model.O) print(sonnet)