![]() ![]() Each number corresponds to asyllable # in the word and describes the stress placed on thatsyllable # when the word is pronounced. Each string in thelist # is a sequence of numbers. # This function takes a single input: # word - a string representing a word # The function returns a list of strings. ![]() def get_rhymes(word): result = pronouncing.rhymes(word) return result ![]() # This function takes a single input: # word - a string representing a word # The function returns a list of words that rhyme with # the input word. def count_syllables(word): phones = pronouncing.phones_for_word(word) count_list = if len(count_list) > 0: result = max(count_list) else: result = 0 return result # If the return value is 0, then word is not available in theCMU # dictionary. # This function takes a single input: # word - a string representing a word # The function returns the number of syllables in word as an # integer. def random_word_generator(source = None, num = 1): result = while source = None or not source.isalpha(): source = random.choice(my_corpus) word = source result.append(word) while len(result) 0: newword = random.choice(choice_list) result.append(newword) word = newword else: word = None newword = None else: newword = None while newword = None or not newword.isalpha(): newword = random.choice(my_corpus) result.append(newword) word = newword return result # The function returns a num-length list of words. # The source word is always included as the first word in theresult and is # included in the count. # If the CFD list of a word is empty, then a random word is # chosen from the entire corpus. So, the first word will be generated from the CFD # using source as the key, the second word will be generated # using the first word as the key, and so on. # This function takes two inputs: # source - a word represented as a string (defaults to None, inwhich case a # random word will be selected from the corpus) # num - an integer (how many words do you want) # The function will generate num random related words using # the CFD based on the bigrams in our corpus, starting from # source. my_corpus = for fid in (): my_corpus += (fid) bigrams = nltk.bigrams(my_corpus) cfd = nltk.ConditionalFreqDist(bigrams) # This loop constructs a corpus from all the shakespeare playsincluded in the # shakespeare corpus included in NLTK # Use: () # to see which shakespeare works are included. # This uses all the words in the entire gutenberg corpus #my_corpus = () # This uses the King James Bible as the corpus # Use: () # to see which other gutenberg works are available. Import nltk import pronouncing import random ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |