-
Notifications
You must be signed in to change notification settings - Fork 551
/
tf-06.py
executable file
·75 lines (66 loc) · 1.93 KB
/
tf-06.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
import sys, re, operator, string
#
# The functions
#
def read_file(path_to_file):
"""
Takes a path to a file and returns the entire
contents of the file as a string
"""
with open(path_to_file) as f:
data = f.read()
return data
def filter_chars_and_normalize(str_data):
"""
Takes a string and returns a copy with all nonalphanumeric
chars replaced by white space
"""
pattern = re.compile('[\W_]+')
return pattern.sub(' ', str_data).lower()
def scan(str_data):
"""
Takes a string and scans for words, returning
a list of words.
"""
return str_data.split()
def remove_stop_words(word_list):
"""
Takes a list of words and returns a copy with all stop
words removed
"""
with open('../stop_words.txt') as f:
stop_words = f.read().split(',')
# add single-letter words
stop_words.extend(list(string.ascii_lowercase))
return [w for w in word_list if not w in stop_words]
def frequencies(word_list):
"""
Takes a list of words and returns a dictionary associating
words with frequencies of occurrence
"""
word_freqs = {}
for w in word_list:
if w in word_freqs:
word_freqs[w] += 1
else:
word_freqs[w] = 1
return word_freqs
def sort(word_freq):
"""
Takes a dictionary of words and their frequencies
and returns a list of pairs where the entries are
sorted by frequency
"""
return sorted(word_freq.items(), key=operator.itemgetter(1), reverse=True)
def print_all(word_freqs):
"""
Takes a list of pairs where the entries are sorted by frequency and print them recursively.
"""
if(len(word_freqs) > 0):
print(word_freqs[0][0], ' - ', word_freqs[0][1])
print_all(word_freqs[1:]);
#
# The main function
#
print_all(sort(frequencies(remove_stop_words(scan(filter_chars_and_normalize(read_file(sys.argv[1]))))))[0:25])