Reference no: EM132360724
Question 1:
Write a function named wordCount that counts how many words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when wordCount is called. wordCount creates the output file.
Input. The function wordCount takes two parameters:
i. inFile, a string that is the name of a text file that is to be read and analyzed. The file that inFile refers to contains only upper and lower case letters and white space (no punctuation marks or other special characters).
ii. outFile, a string that is the name of the file to which wordCount writes its output. //The Result
The input file is in the current working directory and you should create the output file to that directory.
Output. For each line of inFile, wordCount should write a corresponding line to outFile containing a single integer: the number of words on the line.
If the content of the file catInTheHat.txt is below,
The sun did not shine
It was too wet to play
So we sat in the house
All that cold cold wet day
I sat there with Sally
We sat there we two
And I said How I wish
We had something to do
the function call
wordCount('catInTheHat.txt', 'catInTheHatOut.txt') should create a file named catInTheHatOut.txt with this content:
5
6
6
6
5
5
6
5
Question 2:
Write a function named initialVowels that analyzes the contents of a text file for words that begin with a vowel. The letters a, e, i, o and u are vowels.
The function initialVowels should return a dictionary of vowel:word-list pairs. Each vowel should be a key in the dictionary if and only if it is the first letter of some word in the input file. The value of a key is a list of all the words in the input file in which that vowel is the first letter. (Hint: your job is easier if you lowercase the text in the input file.)
Input. The function initialVowels takes a single parameter:
i. inFile, a string that is the name of a text file. This file contains only upper and lower case letters and white space (no punctuation marks or other special characters). It is in the current working directory.
Output. Return a vowel:word-list dictionary
For example, if the file named catInTheHat.txt contains the same text as in Question 12, then the function call
print(initialVowels('catInTheHat.txt'))
should output
{'i': ['it', 'in', 'i', 'i', 'i'], 'a': ['all', 'and']}