Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A program that opens a text file, counts the number of words and reports the top N words ordered by the number of times they appear in the file?

Hi all im a beginner at programming, i was recently given the task of creating this program and i am finding it difficult. I have previously designed a program that calculates the number of words in a sentence that are typed in by the user, is it possible to modify this program to achieve what i want?

import string
def main():
  print "This program calculates the number of words in a sentence"
  print
  p = raw_input("Enter a sentence: ")
  words = string.split(p)
  wordCount = len(words)
  print "The total word count is:", wordCount
main()
like image 884
kmz Avatar asked Jul 05 '13 16:07

kmz


2 Answers

Use collections.Counter for counting words and open() for opening the file:

from collections import Counter
def main():
    #use open() for opening file.
    #Always use `with` statement as it'll automatically close the file for you.
    with open(r'C:\Data\test.txt') as f:
        #create a list of all words fetched from the file using a list comprehension
        words = [word for line in f for word in line.split()]
        print "The total word count is:", len(words)
        #now use collections.Counter
        c = Counter(words)
        for word, count in c.most_common():
           print word, count
main()

collections.Counter example:

>>> from collections import Counter
>>> c = Counter('aaaaabbbdddeeegggg')

Counter.most_common returns words in sorted order based on their count:

>>> for word, count in c.most_common(): 
...     print word,count
...     
a 5
g 4
b 3
e 3
d 3
like image 64
Ashwini Chaudhary Avatar answered Oct 17 '22 00:10

Ashwini Chaudhary


To open files, you can use the open function

from collections import Counter
with open('input.txt', 'r') as f:
    p = f.read() # p contains contents of entire file
    # logic to compute word counts follows here...

    words = p.split()

    wordCount = len(words)
    print "The total word count is:", wordCount

    # you want the top N words, so grab it as input
    N = int(raw_input("How many words do you want?"))

    c = Counter(words)
    for w, count in c.most_common(N):
       print w, count
like image 26
jh314 Avatar answered Oct 17 '22 00:10

jh314