-
Notifications
You must be signed in to change notification settings - Fork 149
/
aimlvalidate.py
60 lines (53 loc) · 1.84 KB
/
aimlvalidate.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
"""
Python AIML Validator, v1.1
Author: Cort Stratton ([email protected])
Usage:
aimlvalidate.py file1.aiml [file2.aiml ...]
"""
# Revision history:
#
# 1.0.1: Redirected stderr to stdout
# 1.0: Initial release
import aiml
import glob
import sys
import xml.sax
if __name__ == "__main__":
# Need input file(s)!
if len(sys.argv) < 2:
print __doc__
sys.exit(2)
# AimlParser prints its errors to stderr; we redirect stderr to stdout.
sys.stderr = sys.stdout
# Iterate over input files
validCount = 0
docCount = 0
for arg in sys.argv[1:]:
# Input files can contain wildcards; iterate over matches
for f in glob.glob(arg):
parser = xml.sax.make_parser(["aiml.AimlParser"])
handler = parser.getContentHandler()
docCount += 1
print "%s:" % f
try:
# Attempt to parse the file.
parser.parse(f)
# Check the number of parse errors.
if handler.getNumErrors() == 0:
validCount += 1
print "PASSED\n"
else:
print "FAILED\n"
except xml.sax.SAXParseException, msg:
# These errors occur if the document does not contain
# well-formed XML (e.g. open or unbalanced tags). If
# they occur, parsing the whole document is aborted
# immediately.
print "FATAL ERROR: %s\n" % msg
# Print final results
print "%d out of %d documents are AIML 1.0.1 compliant." % (validCount, docCount)
if docCount == validCount:
print "Congratulations!"
else:
print """For help resolving syntax errors, refer to the AIML 1.0.1 specification
available on the web at: http://alicebot.org/TR/2001/WD-aiml"""