Please write complete Python code for the question below The
Please write complete Python code for the question below
The following will go into a file called q4.py. Write an iterator which returns words from a big file, without reading the whole file into memory. Create function named find. Popular, which accepts a text file name as parameter, and print out the list of the ten most popular words (by the number of occurrences).Solution
To print 10 most popular words in a file:
import string
def compareItems((w1,c1), (w2,c2)):
     if c1 > c2:
         return - 1
     elif c1 == c2:
         return cmp(w1, w2)
     else:
         return 1
def find_popular(fname):
     data = open(fname,\'r\').read()
     data = string.lower(data)
     for ch in \'!\"#$%&()*+,-./:;<=>?@[\\\\]^_`{|}~\':
         data = string.replace(data, ch, \' \')
     words = string.split(data)
    counts = {}
     for w in words:
         counts[w] = counts.get(w,0) + 1
    items = counts.items()
     items.sort(compareItems)
     for i in range(10):
         print \"%-10s%5d\" % items[i]
fname = raw_input(\"Enter file path: \")
 find_popular(fname)

