/
verify.py
38 lines (30 loc) · 1.21 KB
/
verify.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
__author__ = 'spencertank'
from errors import raiseError
import re
frequenciesFormattingError = "Word frequencies formatting error on line: "
misspelledFormattingError = "Misspelled words formatting error on line: "
wordMatchRe = r"^[[a-zA-Z ]|\\]*$"
def frequenciesCheckIntegrity(frequencyList):
wordRe = re.compile(wordMatchRe)
for i, item in enumerate(frequencyList):
#check that list is formatted correctly
if not isinstance(item, list) or len(item) != 2:
raiseError(frequenciesFormattingError + str(i))
word = item[0]
frequency = item[1]
#check that first entry is a word
if not wordRe.match(word):
raiseError(frequenciesFormattingError + str(i))
#check that second entry is an int
try:
int(frequency)
except ValueError:
raiseError(frequenciesFormattingError + str(i))
def misspelledCheckIntegrity(misspelledList):
wordRe = re.compile(wordMatchRe)
for i, word in enumerate(misspelledList):
try:
if not wordRe.match(word):
raiseError(misspelledFormattingError + str(i))
except TypeError:
raiseError(misspelledFormattingError + str(i))