summarylogtreecommitdiffstats
path: root/cws
blob: a7a97db4b6fa8ed8e96a8f8adee2c8f36513e6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
import csv
import string
import sys

if len(sys.argv) != 3:
    print("Please enter the input and output files as arguments. For more details see https://kazmal.tech/cws.")

else:
    translate = str.maketrans('', '', string.punctuation)

    word_count = {}
    input_file = sys.argv[1]
    text = open(input_file).read()

    words = text.split()
    for word in words:
        word = word.translate(translate).lower()
        count = word_count.get(word, 0)
        count += 1
        word_count[word] = count
    rank = 1
    out_file = sys.argv[2]
    word_count_list = sorted(word_count, key=word_count.get, reverse=True)
    file_out = open(out_file, 'w')
    writer = csv.writer(file_out)
    for word in word_count_list:
        writer.writerow([word, word_count[word], rank])
        rank += 1