-
Notifications
You must be signed in to change notification settings - Fork 2
/
check-sample.py
29 lines (26 loc) · 1.32 KB
/
check-sample.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
import sys
def check_sample(sentences, allowed_words):
found_bad_words = False
for words in map(lambda x: x.split(), sentences):
bad_words = list(filter(lambda x: x not in allowed_words, words))
if bad_words:
print("#ERROR: the following words are not allowed: {}".format(bad_words), file=sys.stderr)
found_bad_words = True
else:
print("{}".format(" ".join(words)))
if not found_bad_words:
print("#file only has allowed words", file=sys.stderr)
if __name__ == '__main__':
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument('-v', '--verbose', action='count', default=0,
help="verbose output")
argparser.add_argument("-i", "--samplefile", dest="sample_file", type=str,
default="sample.txt", help="sampled sentences")
argparser.add_argument("-a", "--allowedwords", dest="allowed_words_file", type=str,
default="allowed_words.txt",
help="use this list of words to check sampled sentences")
args = argparser.parse_args()
allowed_words = set(line.strip() for line in open(args.allowed_words_file))
sentences = set(line.strip() for line in open(args.sample_file))
check_sample(sentences, allowed_words)