-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammaticus.py
36 lines (30 loc) · 1.46 KB
/
grammaticus.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
import time
class Grammaticus:
punctuation_marks = ['.', ',', ':', ';', '\'', ')', '(', '!', '?']
def __init__(self):
self.word_count = 0
self.vocabulary = {}
def read(self, file):
initial_time = time.time()
for line in open(file, 'r'):
for word in line.split():
word = word.lower()
while word[0] in Grammaticus.punctuation_marks:
word = word[1:]
while word[-1] in Grammaticus.punctuation_marks:
word = word[:-1]
if word[-3:] == 'que' and word != 'quoque':
self.vocabulary['-que'] = self.vocabulary['-que'] + 1 if '-que' in self.vocabulary else 1
word = word[:-3]
self.word_count += 1
self.vocabulary[word] = self.vocabulary[word] + 1 if word in self.vocabulary else 1
duration = time.time() - initial_time
message = 'Sorted {0} words in {1:.2g} seconds ({2:.2g} milliseconds/word).'
print(message.format(self.word_count, duration, duration/self.word_count*1000))
def sort_words(self):
initial_time = time.time()
for vocabulary_entry in sorted(self.vocabulary.items(), key=lambda kv: kv[1]):
print('{:>2}: {}'.format(vocabulary_entry[1], vocabulary_entry[0]))
duration = time.time() - initial_time
message = 'Purged in {0:.2g} milliseconds.'
print(message.format(duration*1000))