Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Question Question 12 Write a function named wordsByLine that identifies t

ID: 3574661 • Letter: P

Question

Python Question

Question 12

Write a function named wordsByLine that identifies the distinct words on each line of an input file and their frequency. For each line of the input file, wordsByLine creates a corresponding line of an output file with the word:frequency information.

wordsByLine takes two input parameters:

1. inFile – the name (a string) of an existing file to read from

2. outFile – the name (a string) of a file to write to

inFile already exists when wordsByLine is called; wordsByLine must create outFile.

inFile consists only of lower case characters and white space.

Each line of the output file should consist of word:frequency pairs, with the word and its frequency separated by a colon (':'). Each word:frequency pair should be separated from the previous pair by a space. The word:frequency pairs on a line may be in any order.

For example, suppose that the file fish.txt contains the lines

one fish two fish

red fish blue fish

Then the function call

wordsByLine('fish.txt', 'fishWords.txt')

should produce an output file named fishWords.txt with this content

two:1 one:1 fish:2

red:1 blue:1 fish:2

Question 13

Write a function named lineIndex that takes a file name, fName, as a parameter and returns a dictionary, d, that indexes the words in fName by line number, with the first line in fName being numbered 0.

Each word in fName should be a key in the returned dictionary d, and the corresponding value should be a list of the line numbers on which the word occurs. A line number should occur no more than one time in a given list of line numbers.

The file fName contains only upper and lower case letters and white space. Capital and lower case letters are different. That is, 'You' is not the same word as 'you'.

For example, if the file named makeItRain.txt contains these lines

I have no pride

I have no shame

You gotta make it rain

Make it rain rain rain

then the following would be correct output

print(lineIndex('makeItRain.txt'))

{'rain': [2, 3], 'gotta': [2], 'make': [2], 'it': [2, 3],

'shame': [1], 'I': [0, 1], 'You': [2], 'have': [0, 1], 'no': [0, 1], 'Make': [3], 'pride': [0]}

Explanation / Answer

def wordsByLine(nm1,nm2):
   f1 = open(nm1,"r")
   f2 = open(nm2,"w")
   for l in f1.readlines():
       d = {}
       l = l[0:-1]
       l = l.split()
       for kk in l:
           if kk in d.keys():
               d[kk]+=1
           else:
               d[kk]=1
       s = ""
       for kk in d.keys():
           s = s+kk+":"+str(d[kk])+" "
       f2.write(s+' ')
wordsByLine("tempy.txt","one6.txt")