Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COSI 10a Introduction to Problem Solving in Python (Spring 2023)

Programming Assignment 4

Program Description

This assignment will be due at 11am (the end of class) on Monday, March 27.

For this assignment you are given a file pa4.py in which you will write your code. The file contains a pre-written main () function which is already complete as well as the following functions which you are tasked with filling out:

●   make_text (unique_words, word_index, prob) -> str

●    compute_probabilities (prob, num_words) -> None

●   get_next_word_counts (words, mat, index) -> None

●   get_next_word (unique_words, dist) -> str

●   get_word_list (filename) -> list

Note that the arrow after the function denotes the return type of the function. If a function’s return type is None then it does not return anything.

The goal of this program is to create a text generator based on a very simplified version of a       markov chain. The program should read in text from a file (you are encouraged to use the sonnets.txt file provided on LATTE) and compute the probability of any word w being followed by any other word x. As a simple example, consider a situation where our text input is “The quick    brown fox jumps over the lazy dog” . In this example, the word “the” is followed once by “quick”   and once by lazy” . The total number of times a word appears after “the” is 2 (1 “quick” + 1 “ lazy” ). For any unique word in the text, the probability that it will appear after the word “the” is    the number of times it appears after the” in the text divided by the total number of times a word  appears after “the” . Therefore, the probability of “ lazy” or “quick” appearing after “the” is ½ or 50%, while the probability of fox” appearing after the” is 0/2 or 0%.

For this assignment I would also encourage you to make use of the tqdm library which you will   need to install using your terminal (pip install tqdm on mac, for example). You will also need to make use of the random.choices(population, weights)function which takes in a list of items to choose from and a list of the same length with weights (a probability distribution) and returns an item from the population list chosen randomly according to the given weights.

You may talk to other students about the assignment and consult the slides and online resources for syntax, but all code you submit must be your own. Do not copy code directly from other students or from any online resources. The best way to not get caught plagiarizing is to    not do it in the first place.

Requirements

1.   Your program must run without error and must correctly perform the task of text generation using probabilities.

2.   There should be no code outside of a function except for a call to main (), constants, or import statements.

3.   Include a comment at the top of the file with your name, the assignment number, and a brief description of what the program does. For example:

#Full Name

#COSI 10a Spring 2023

#Programming Assignment 3

#Description: …

Submission and Grading

Submit via Latte a zip file named yourfirstname_yourlastnamePA4.zip which contains your program file. To create a zip file on Mac, right click the document/folder and select the “Compress [document/folder name]” option. On Windows, right click the document/folder and    select the Send To..” and then “zip/compressed file” options. See the top of this document for the due date and time.

There are 3 possible grades you may receive for this assignment: fail, pass, and pass+. Your     program will be graded pass/fail on each of the requirements listed above. If your code does not meet all of the requirements listed in the section above you will not pass. For this assignment,   grades of pass+ will be given to students who the grader feels did an exemplary job with regards to code readability and design.