-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtrial-faq.py
85 lines (75 loc) · 2.38 KB
/
trial-faq.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import getopt
import pdb
CSCI3651_PREREQ = "CSCI 2911, CSCI 2912"
CSCI3651_TEXT = "Engineering Long Lasting Software"
COURSE_CREATOR = lambda ident: Course(ident)
class Course:
def __init__(self, ident):
self.ident = ident
# would be nice if constants above could be reflected in lettuce features
# this hashtable approach is weird
# issues will include
# 1. punctuation
# 2. missed spaces
# 3. close synonyms ...
# 4. ultimately large nested hashtable difficult to maintain?
# 5. variations on sentence structure a pain to handle? will we get any generalization?
# e.g. "what is the textbook for the course?", "does this course have a textbook?"
# these examples make it look like a bag of words appraach is sensible
def ask(question):
hashtable = {"what":
{"are":
{"the":
{"course":
{"requirements?":CSCI3651_PREREQ}
}
},
"is":
{"the":
{"course":
{"textbook?":CSCI3651_TEXT}
}
}
},
"there":
{"is":
{"a":
{"course":
{"called":
{"ANYTHING":COURSE_CREATOR}
}
}
}
}
}
for word in question.lower().split():
try:
hashtable = hashtable[word]
except KeyError as e:
# issue now is that all errors will trigger KeyError here
# should every key be a function call? don't think that will work - maybe in js?
# maybe should handle KeyError based on context ...?
#pdb.set_trace()
hashtable = hashtable["ANYTHING"]
# and then we pickle this object? store it in a db?
return hashtable(e[0].upper()).ident
return hashtable
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "h", ["help"])
except getopt.error, msg:
raise Usage(msg)
print ask(args[0])
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())