Skip to content

Instantly share code, notes, and snippets.

@alexeytal
Created November 14, 2016 14:04
Show Gist options
  • Save alexeytal/a61c0bc04604cbb56365fe615a89f249 to your computer and use it in GitHub Desktop.
Save alexeytal/a61c0bc04604cbb56365fe615a89f249 to your computer and use it in GitHub Desktop.
import argparse
from operator import itemgetter
import sys
parser=argparse.ArgumentParser()
parser.add_argument('file', help='name of the file', type=str)
parser.add_argument('-n', help='number of words',type = int)
parser.add_argument('-s', help='short words',action = 'store_true')
options=parser.parse_args()
f = open(options.file, 'r')
dict = {}
text = f.read()
text = text.replace(',',' ')
text = text.replace('.', ' ')
text = text.split()
for i in range(len(text)):
dict[text[i]] = dict.get(text[i],0) + 1
a=sorted(dict.items(), key=lambda x:x[1], reverse=True)
#a=sorted(dict.items(), key=itemgetter(1), reverse=True)
if options.s:
a = [(x,y) for x,y in a if len(x)>2]
a = a[:options.n]
else:
a = a[:options.n]
for k,v in a:
if options.s:
if len(k) > 2:
print k, v, "%.4f" % (float(v) / len(text))
else:
print k, v, float(v) / len(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment