-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary-speller.py
More file actions
184 lines (149 loc) · 4.8 KB
/
Copy pathdictionary-speller.py
File metadata and controls
184 lines (149 loc) · 4.8 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import re
import sys
import time
# Words in dictionary
words = set()
# Maximum length for a word
# (e.g., pneumonoultramicroscopicsilicovolcanoconiosis)
LENGTH = 45
# Default dictionary
WORDS = "dictionaries/large"
# Check for correct number of args
if len(sys.argv) != 2 and len(sys.argv) != 3:
print("Usage: speller [dictionary] text")
sys.exit(1)
# Benchmarks
time_load, time_check, time_size, time_unload = 0.0, 0.0, 0.0, 0.0
# Determine dictionary to use
dictionary = sys.argv[1] if len(sys.argv) == 3 else WORDS
# Load dictionary
before = time.process_time()
def load(dictionary):
##########################################
##########################################
"""Load dictionary into memory, returning true if successful else false"""
file = open(dictionary, "r")
for line in file:
word = line.rstrip()
words.add(word)
file.close()
return True
##########################################
##########################################
loaded = load(dictionary)
after = time.process_time()
# Exit if dictionary not loaded
if not loaded:
print(f"Could not load {dictionary}.")
sys.exit(1)
# Calculate time to load dictionary
time_load = after - before
# Try to open text
text = sys.argv[2] if len(sys.argv) == 3 else sys.argv[1]
file = open(text, "r", encoding="latin_1")
##########################################
##########################################
def unload():
"""Unloads dictionary from memory, returning true if successful else false"""
return True
##########################################
##########################################
if not file:
print("Could not open {}.".format(text))
unload()
sys.exit(1)
# Prepare to report misspellings
print("\nMISSPELLED WORDS\n")
# Prepare to spell-check
word = ""
index, misspellings, words = 0, 0, 0
# Spell-check each word in file
while True:
c = file.read(1)
if not c:
break
# Allow alphabetical characters and apostrophes (for possessives)
if re.match(r"[A-Za-z]", c) or (c == "'" and index > 0):
# Append character to word
word += c
index += 1
# Ignore alphabetical strings too long to be words
if index > LENGTH:
# Consume remainder of alphabetical string
while True:
c = file.read(1)
if not c or not re.match(r"[A-Za-z]", c):
break
# Prepare for new word
index, word = 0, ""
# Ignore words with numbers (like MS Word can)
elif c.isdigit():
# Consume remainder of alphanumeric string
while True:
c = file.read(1)
if not c or (not c.isalpha() and not c.isdigit()):
break
# Prepare for new word
index, word = 0, ""
# We must have found a whole word
elif index > 0:
# Update counter
words += 1
# Check word's spelling
before = time.process_time()
##########################################
##########################################
def check(word):
"""Return true if word is in dictionary else false"""
if word.lower() in words:
return True
else:
return False
##########################################
##########################################
misspelled = not check(word)
after = time.process_time()
# Update benchmark
time_check += after - before
# Print word if misspelled
if misspelled:
print(word)
misspellings += 1
# Prepare for next word
index, word = 0, ""
# Close file
file.close()
# Determine dictionary's size
before = time.process_time()
##########################################
##########################################
def size():
"""Returns number of words in dictionary if loaded else 0 if not yet loaded"""
return len(words)
##########################################
##########################################
n = size()
after = time.process_time()
# Calculate time to determine dictionary's size
time_size = after - before
# Unload dictionary
before = time.process_time()
unloaded = unload()
after = time.process_time()
# Abort if dictionary not unloaded
if not unloaded:
print(f"Could not load {dictionary}.")
sys.exit(1)
# Calculate time to determine dictionary's size
time_unload = after - before
# Report benchmarks
print(f"\nWORDS MISSPELLED: {misspellings}")
print(f"WORDS IN DICTIONARY: {n}")
print(f"WORDS IN TEXT: {words}")
print(f"TIME IN load: {time_load:.2f}")
print(f"TIME IN check: {time_check:.2f}")
print(f"TIME IN size: {time_size:.2f}")
print(f"TIME IN unload: {time_unload:.2f}")
print(f"TOTAL TIME: {time_load + time_check + time_size + time_unload:.2f}\n")
# Success
sys.exit(0)