Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function generate_text(word_dict, num_words) that takes as parameters a dictionary of word transitions (generated by thecreate_dictionary function) named word_dict and a positive integer named

Write a function generate_text(word_dict, num_words) that takes as parameters a dictionary of word transitions (generated by thecreate_dictionary function) named word_dict and a positive integer named num_words. The function should use word_dict to generate and print a string of num_words words.

Guidelines:

The first word should be chosen randomly from the words associated with the sentence-start symbol, '$'. The second word should be chosen randomly from the list of words associated with the first word, etc. When the current word ends in a period ('.'), question mark ('?'), or exclamation point ('!'), the function should detect this and start a new sentence by again choosing a random word from among those associated with '$'.

Do not include '$' in the output text. It should only be used as an internal marker for your function.

You can use the random.choice function to choose from a list of possible words. Dont forget to include import random at the top of your file. Then, if wordlist is the list of possible words at a given point in the generated text, you can do something like the following:

next_word = random.choice(wordlist) 

Here again, you shouldnt try to remove or change the punctuation associated with the words, and you dont need to worry if the generated text doesnt end with appropriate punctuation. The generated text wont be perfect, but most of the time it will at least be meaningful!

If your function encounters a word that doesnt have any words associated with it in the dictionary, the function should start a new sentence. This situation can occur if the last word in the file used to create the dictionary was unique and did not end with punctuation.

Here again, we will discuss some strategies for generate_words in lecture.

Example:

Here are two examples using the same text file as above. Your output may differ because of the randomness involved in the generation.

>>> word_dict = create_dictionary('sample.txt') >>> generate_text(word_dict, 20) B C. C C C. C C C C C C C C C C C. C C C. A >>> generate_text(word_dict, 20) A B A. C C C. B A B C. A C. B A. C C C C C C. 

Try some other examples using longer documents containing English words, such as the works of William Shakespeare. In particular, we are providing a text file containing the first act of Romeo and Juliet, along with the files that we provided above in the examples for create_dictionary.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Spatial And Temporal Databases 8th International Symposium Sstd 2003 Santorini Island Greece July 2003 Proceedings Lncs 2750

Authors: Thanasis Hadzilacos ,Yannis Manolopoulos ,John F. Roddick ,Yannis Theodoridis

2003rd Edition

3540405356, 978-3540405351

More Books

Students also viewed these Databases questions

Question

What are the ways the media socialize a person?

Answered: 1 week ago

Question

What are negative messages? (Objective 1)

Answered: 1 week ago