I am writing a script to accept (optional) two arguments in command line: --top to return top words by count e.g. --top 5, returns top 5; --lower to lower a list of words before counting the unique values.
I got to this stage and I am getting no output:
import collections
import argparse
def counts(text, top = 10, case = None):
""" returns counts. Default is top 10 frequent words without change of case"""
# split on whitespace
word_list = text.split()
if case is None:
c = collections.Counter(word_list)
return c.most_common(top)
else:
c = collections.Counter([w.lower() for w in word_list])
return c.most_common(top)
# declare parser
parser = argparse.ArgumentParser()
# add argument --top
parser.add_argument("--top", help="returns top N words. If not specified it returns top 10", type=int)
# add argument --lower
parser.add_argument("--lower", help = "lowercase all the words.('StackOverFlow' and 'stackoverflow' are counted equally.")
# add argument filename
parser.add_argument("filename", help = "accepts txt file")
args = parser.parse_args()
# read text file
file = open(args.filename, 'r').read()
counts(text = file, top = args.top, case = args.lower)
When I run the script with
$python script.py text.txt --top 5 --lower
I get no output. Any clue where I am going wrong?
If the file were to output something, I would expect:
(word1 count1)
(word2 count2)
(word3 count3)
(word4 count4)
(word5 count5)