-
Notifications
You must be signed in to change notification settings - Fork 16
/
twss.py
55 lines (48 loc) · 1.68 KB
/
twss.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
import sys
import getopt
from processSentence import *
from svmutil import *
import pickle
import random
def twss(sentence,vocabList,model):
#print "you said: '"+sentence+"'\n"
# these should be moved to file
responses = ['Whatever ...','Okay','Yawn','What makes you think I care?','Yada yada','Uhuh','Yeah, yeah','figures',"I'm hungry",'give me a break','so ...']
x = processSentence(sentence, vocabList)
#print [x]
p_label, p_acc, p_val = svm_predict([1], [x], model, '-b 1 -q')
print p_label, p_acc, p_val
if p_label[0] == 1:
return "That's what she said!\n"
else:
return random.choice(responses) +'\n'
def twss_lite(sentence,vocabList,model):
x = processSentence(sentence, vocabList)
p_label, p_acc, p_val = svm_predict([1], [x], model, '-b 1 -q')
return p_label[0]
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
# based on Guido's post: http://www.artima.com/weblogs/viewpost.jsp?thread=4829
# might be out of date for 2.7 ...
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)
input = open('data/vocab.pk')
vocabList = pickle.load(input)
input.close()
model = svm_load_model("data/svm_model.pk")
if not args:
raise Usage('python twss.py "<your sentence>"')
print twss(args[0],vocabList,model)
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())