-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_file_reader.py
More file actions
34 lines (29 loc) · 971 Bytes
/
Copy pathtext_file_reader.py
File metadata and controls
34 lines (29 loc) · 971 Bytes
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
class Reader:
def __init__(self, file):
self.text_file = file
self.read_file = self.text_file.read()
self.words = []
self.word_dictionary = {}
@staticmethod
def wordListToFreqDict(wordlist):
wordfreq = [wordlist.count(p) for p in wordlist]
return dict(zip(wordlist, wordfreq))
@staticmethod
def sortFreqDict(freqdict):
aux = [(freqdict[key], key) for key in freqdict]
aux.sort()
aux.reverse()
return aux
def scan_file(self):
words = self.read_file.split(' ')
for word in words:
self.words.append(word)
freqdict = self.wordListToFreqDict(self.words)
words_sorted = self.sortFreqDict(freqdict)
for word in words_sorted:
print(word)
if __name__ == '__main__':
a = input('text file to sparse (include .txt): ')
with open(a, 'r') as file:
reader = Reader(file)
reader.scan_file()