Words frequency
Please check my script and tell me, how would you simplify or improve it? This script calculates 10 (or as many as you want) most often words found in the format file `.txt.` To start the script, type in the console: python lang_frequency.py --name script_name.txt The result of the code execution will be similar: [('Šø', 1728), ('Š²', 1576), ('Š½Šµ', 1360), ('Š¾Š½', 1190), ('ŃŃŠ¾', 1100), ('Ń', 1066), ('Š½Š°', 1000), ('ŠµŠ³Š¾', 690), ('ŃŃŠ¾', 688), ('Ń', 663)] To use your `.txt` format file, you need to put it in the folder with the script from collections import Counter import re import sys import argparse def create_parser (): parser = argparse.ArgumentParser() parser.add_argument ('-n', '--name', type=argparse.FileType()) return parser parser = create_parser() namespace = parser.parse_args(sys.argv[1:]) text = namespace.name.read() number = int(input("Type a number of most often words you want to know ")) words = re.findall(r'\w+', text.lower()) ten_most_frequent_words = Counter(words).most_common(number) print(ten_most_frequent_words)