WRITE IN PYTHON Using any large text file or any literature English book in .txt format. The program will read a .txt file and process the information
Write a module called, “book_digest”. The module must have the following functions:
● digest_book(file_path: str) -> None ○ This function collects and stores the required information into global dictionaries, lists, and variables. The file (book) is read and parsed only one time then closed ○ This function should raise a FileNotFoundError exception if the file doesn’t exist ○ It would be helpful to create helper functions for this process.
● character_freq() -> dict ○ This function returns the frequency with which alphabet characters are used in the book. Alphabet characters are a-z or A-Z. ○ It returns a dictionary that uses the alphabet character as the key and the number of times it was seen as the value. ○ The alphabet characters should be counted case insensitively. This means that there is only one key for the letter ‘a’ which holds the count of both uppercase ‘a’s and lowercase ‘a’s. ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● word_lengths() -> dict ○ This function returns a dictionary that uses the word length as the key and the number of words of that length as the value. ○ Contractions like, “can’t”, “shouldn’t”, etc., are to be counted as single words and their character count should include the apostrophe. The same goes for abbreviations and their ending period. ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● avg_word_length() -> float ○ This function returns the average word length ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● line_count() -> int ○ This function returns the number of lines in the book ○ A line contains all of the characters separated by a new line character ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● sentence_count() -> int ○ This function returns the number of sentences found in the book ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● unique_word_count() -> int ○ This function returns the number of unique words found in the book ○ See unique_words description for what constitutes a unique word ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● unique_words() -> set ○ This function returns a list of all of the unique words found ○ A unique word can include contractions, abbreviations, and other forms of words. ○ In the case of abbreviations, if the abbreviation is followed by a period then the period should be considered part of the word ○ This function should raise an exception called BookNotDigestedError if a book hasn’t first been digested
● generate_password(word_qty: int, min_word_len: int,
inject_punct: bool) -> str
○ This function returns a password made up of word_qty unique words
○ Each word must be at least min_word_len characters long
○ The user can choose to have a random punctuation character
inserted between the words. ■ If this option is not chosen then the
words are separated by a space ○ The last two parameters should
have a default value set. ■ For min_word_len the default value is 4
■ For inject_punct the default value is False ○ This function can
be called providing the arguments for some or all parameters ○ The
argument for word_qty must always be provided. ○ This function
should raise an exception called BookNotDigestedError if a book
hasn’t first been digested
● A BookNotDigestedError exception which is derived from the Exception class. BookNotDigestedError must be defined in the same module.
Files is composed of three parts:
File Paths
File path is required to access file in an Operating System.It is the location of the file.
It consists of three parts:
Opening and closing a file
reader=open('Box.txt')
try:
#processing
finally:
reader.close()
Note:
Iteration in file
with open('Box.txt','r')as reader
line=reader.readline()
while line!=' ' :
print(line,end=' ')
line=reader.readline()
Get Answers For Free
Most questions answered within 1 hours.