Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- # +++your code here+++
- # Define print_words(filename) and print_top(filename) functions.
- # You could write a helper utility function that reads a file
- # and builds and returns a word/count dict for it.
- # Then print_words() and print_top() can just call the utility function.
- ###
- def open_file_dict(filename):
- word_count = {}
- f = open(filename, 'r')
- for line in f:
- words = line.split()
- for to_lower in words:
- lower_words = to_lower.lower()
- #print lower_words
- if not lower_words in word_count: # if not exist yet lower_words in word_count
- word_count[lower_words] = 1 # put it in the dictionary
- else: # if it is already exist
- word_count[lower_words] = word_count[lower_words] + 1 #increment it by how many there is by 1
- return word_count
- def print_words(filename):
- word_count = open_file_dict(filename)
- words = sorted(word_count.keys())
- for word in words:
- print word, word_count[word]
- def get_count(word_count_tuple):
- """Returns the count from a dict word/count tuple -- used for custom sort."""
- return word_count_tuple[1]
- def print_top(filename):
- word_count = word_count_dict(filename)
- items = sorted(word_count.items(), key=get_count, reverse=True)
- # Print the first 20
- for item in items[:20]:
- print item[0], item[1]
- # This basic command line argument parsing code is provided and
- # calls the print_words() and print_top() functions which you must define.
- def main():
- if len(sys.argv) != 3:
- print 'usage: ./wordcount.py {--count | --topcount} file'
- sys.exit(1)
- option = sys.argv[1]
- filename = sys.argv[2]
- if option == '--count':
- print_words(filename)
- elif option == '--topcount':
- print_top(filename)
- else:
- print 'unknown option: ' + option
- sys.exit(1)
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment