def random_word(corpus): """Return a random word and syllable count from the corpus""" syllables = 5 while syllables > 4: word = random.choice(corpus) syllables = count_syllables(word) return (word, syllables)
def random_word(corpus): """Return a random word and syllable count from the corpus""" syllables = 5 while syllables > 4: word = random.choice(corpus) syllables = count_syllables(word) logging.debug("random word & syllables = %s %s\n", word, syllables) return (word, syllables)
def word_after_one(word, dictionary, current_syllables, target_syllables): """Return all acceptable words in a corpus that follow a given word""" accepted_words = [] options = dictionary.get(word) if options != None: accepted_words = [w for w in options if current_syllables + count_syllables(w) <= target_syllables] return accepted_words
def word_after_one(word, dictionary, current_syllables, target_syllables): """Return all acceptable words in a corpus that follow a given word""" accepted_words = [] options = dictionary.get(word) if options != None: accepted_words = [w for w in options if current_syllables + count_syllables(w) <= target_syllables] logging.debug("accepted words after \"%s\" = %s\n", word, set(accepted_words)) return accepted_words
def main(): """Load the dictionary. Ask for number of words to count. Create the list of words. Print.""" dict_file = load('words.txt') num_words = int(input("\nNumber of words to check: ")) words_count_list = {} for n in range(0, num_words): word = random.choice(dict_file) if word not in words_count_list.keys(): count = sc.count_syllables(word) if count == None: words_count_list[word] = "not found" else: words_count_list[word] = count for word, count in words_count_list.items(): print("{}: {}".format(word, count))
def select_word(options): """Randomly offer 10 words from the options. User selects one. Returns that word and its syllable count""" options = list(set(options)) # Remove duplicate words selection = [] if len(options) > 10: for n in range(10): word = random.choice(options) while word in selection: word = random.choice(options) selection.append(word) else: selection = options # Print selection options for n in range(len(selection)): index = n + 1 print("{}. {}".format(index, selection[n])) choice = input("Choice: ") if choice == 'x': sys.exit() choice = int(choice) - 1 word = selection[choice] syllables = count_syllables(word) return word, syllables
def main(): """Load the corpus and the dictionaries, then build the haiku""" # Loading and preparation corpus = load_training_file('train.txt') corpus = prep_training(corpus) dict_1 = map_one_to_one(corpus) dict_2 = map_two_to_one(corpus) haiku = [[] for l in range(3)] syllables = [0 for l in range(3)] target_syllables = [5, 7, 5] # Build the first line # Select the first word next_word, syls = random_word(corpus) haiku[0].append(next_word) syllables[0] = syls logging.debug("first word & syllables = %s %s", haiku[0][0], syllables[0]) # Then get the second from the 1-to-1 corpus last_word = haiku[0][0] options = word_after_one(last_word, dict_1, syllables[0], target_syllables[0]) next_word = random.choice(options) haiku[0].append(next_word) syllables[0] += count_syllables(next_word) logging.debug("second word & syllables = %s %s", next_word, syllables[0]) # Complete the line while syllables[0] < 5: last_words = "{} {}".format(haiku[0][-2], haiku[0][-1]) options = word_after_two(last_words, dict_2, syllables[0], target_syllables[0]) while len(options) == 0: index = random.randint(0, len(corpus) - 2) last_words = "{} {}".format(corpus[index], corpus[index+1]) logging.debug("empty list, new words are %s", last_words) options = word_after_two(last_words, dict_2, syllables[0], target_syllables[0]) next_word = random.choice(options) haiku[0].append(next_word) syllables[0] += count_syllables(next_word) logging.debug("next word & syllables = %s %s", next_word, syllables[0]) # Build the next two lines in a loop running = True while running: for i in range(1, 3): last_words = "{} {}".format(haiku[i-1][-2], haiku[i-1][-1]) while syllables[i] < target_syllables[i]: options = word_after_two(last_words, dict_2, syllables[i], target_syllables[i]) while len(options) == 0: index = random.randint(0, len(corpus) - 2) last_words = "{} {}".format(corpus[index], corpus[index+1]) logging.debug("empty list, new words are %s", last_words) options = word_after_two(last_words, dict_2, syllables[i], target_syllables[i]) next_word = random.choice(options) haiku[i].append(next_word) syllables[i] += count_syllables(next_word) if len(haiku[i]) == 1: last_words = "{} {}".format(haiku[i-1][-1], haiku[i][0]) else: last_words = "{} {}".format(haiku[i][-2], haiku[i][-1]) haiku_string = "" for line in haiku: for index, word in enumerate(line): if index == len(line) - 1: haiku_string += "{}\n".format(word) else: haiku_string += "{} ".format(word) print("\nHaiku:\n") print(haiku_string) regen = '' while regen != 'y' and regen != 'n': regen = input("Regenerate last two lines? ").lower() if regen == 'n': running = False elif regen == 'y': haiku = [haiku[0], [], []] syllables = [syllables[0], 0, 0]