Assignment 5
Text Processing
Submit before 11:30 PM Saturday May 4
Overview
You will work on three scripts involving arrays and iterators. You are encouraged to use interactive shell (IDLE) to develop your statements before you write the script in a text editor.
For this assignment, you will want to review chapters on Strings, Reading Strings and Lists in the text.
IDLE Practice
- Assign a string with multiple words (e.g. "the lazy dog slept all day") to the variable word_line.
- Using the split method, create an array of words from word_line and assign it to the variable word_list.
- Use a for loop to iterate on word_list to write out (use print) every word in the array.
- Write code that opens a file and then prints out all of the lines in the file.
Note: you are not required to submit your IDLE practice, but make sure you do it in order to prepare you for writing the scripts below.
Scripts
- Text statistics: write a function called text_stat that takes a string as one parameter. The string should consist of multiple words separated by spaces. Using split, the function should then produce an array of words and then loop through the array counting the total number of letters. Divide the total number of letters by the number of words to report the average word length. Also report the longest word in the text.
Here is an example run:
>>> text_stat("cow aardvark sheep owl horse") The average word length is 4.8 The longest word is aardvark >>>
- File line count: Write a function called line_count that takes a file name (string) as a parameter. It should open the file and report the number of lines in the file.
Here is an example run for a file with 5 lines:
>>> line_count("text5lines.txt") The file "text5lines.txt" has 5 lines of text. >>>
- Movie search: write a function call msearch that takes a word
(string) as a parameter. The function then searches the movie titles in
the file movies.txt and reports any
matches with the given word. The function should also display the
number of matches.
Here is an example run:
>>> msearch("war") 18. 98% Snow White and the Seven Dwarfs (1937) 47 63. 92% Star Wars: Episode VII - The Force Awakens (2015) 364 There were 2 matches >>>
Deliverables
Create a summary file called assn5 (pdf or txt) that contains the following:
- A statement that summarizes your completion of the assignment. Discuss your experience. Note any help or collaboration.
- For each of your Python code problems:
- A listing of the code (copy the code in the py file and past it in the assn1 file)
- Running examples that demonstrate that your code works correctly (copy the text that shows your py file running and paste it in the assn5 file)
Submit your assn5.pdf (or assn5.txt) file to D2L.