COSI 10a Introduction to Problem Solving in Python
Programming Assignment 4
Program Description
This assignment will be due at 12pm (noon) on Friday, March 15th, 2024
For this assignment you are given a file skeleton_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, prob) -> str
● compute_probabilities (prob, num_words) -> list[list]
● get_next_word_counts (words, mat, index) -> list[list]
● get_next_word(cur_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 will need 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. Your main() should be the same as the original skeleton.
4. The functions must have the same parameters and return values as in the original skeleton.
5. 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 Fall 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.
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。