-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathval.py
72 lines (58 loc) · 1.63 KB
/
val.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
import sys
import random
import nltk
from nltk.corpus import words as nltk_words
import re
def is_english_word(word):
"""
Check that individual word is in dictionary
param word: string from split user input
rtype: bool
"""
dictionary = set(nltk_words.words())
if word in dictionary:
return True
else:
print(word + " is not a legal english word")
return False
def is_statement_english(statement):
"""
check that user input sentence is in english
param statement: user input statement
rtype: bool
"""
words_in_statement = re.split(r'[;,\.\s]\s*', statement) #deliminates on commas, semicolon, period and any amounnt of white spaces
for word in words_in_statement[:-1]:
if is_english_word(word) == False:
print("This sentence contains illegal words")
return False
return True
def validate_length(us):
"""
Check that input string is not longer than 140 characters.
param us: a user-input string
rtype: bool
"""
if len(us) <= 140:
return True
else:
print("This sentence is more than 140 characters.")
return False
def validate_sentence(us):
"""
Check that input string ends with a period.
param us: a user-input string
rtype: bool
"""
if us[-1] == '.':
return True
else:
print("This sentence does not end in a period")
return False
def input_val(s):
"""
run all input val checks on input
param s: user input string
rtype: bool
"""
return validate_length(s) and validate_sentence(s) and is_statement_english(s)