-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnswer_prediction.py
91 lines (76 loc) · 2.54 KB
/
Answer_prediction.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
86
87
88
89
90
91
#imorting required libraries
import pickle
import rel
import feature_vectors
from sklearn.externals import joblib
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
# Taking input question and its body
inputQuestion = input("\nWelcome to StackOverFlow Answer Predictor! Please enter a technical question ->\n")
inputQuestionBody = input("Please enter some more details (a paragraph! as a question body)->\n")
with open('dataset', 'rb') as fp:
dataset = pickle.load(fp)
#print('dataset loaded')
for myDict in dataset: #checking if queried question already in our dataset
if myDict['qTitle'] == inputQuestion:
if myDict['acceptAnsID'] == myDict['answerId']:
print('\nAnswer found\nANSWER::')
print(myDict['answerBody'])
exit(0)
#print('not found in dataset')
#finding relevant question pool
rq = rel.RelevantQuestions()
relQIds = rq.basedOnTitle(inputQuestion)
#relQIds2 = rq.basedOnBody(inputQuestion)
relQIds.append(rq.basedOnBody(inputQuestionBody))
#print(relQIds)
#getting answers to corresponding questions
answerSet1 = []
labels_test1 = []
#answerSet2 = []
#labels_test2 = []
#print(relQIds)
if relQIds is None: #and relQIds2 is None:
print("\n \nOops! I couldn't get a answer. Please try again!. Inconvenience caused is deeply regretted.\n")
exit(0)
for qId in relQIds:
for myDict in dataset:
if myDict['qId'] == qId:
#print("Relevant Question :- ",myDict['qTitle'])
answerSet1.append(myDict)
if myDict['acceptAnsID'] == myDict['answerId']:
labels_test1.append(1)
else:
labels_test1.append(0)
#print('answers fetched')
# get all feature vectors of answers
cf = feature_vectors.featureVectors()
features_test1 = cf.cfv(answerSet1)
#features_test2 = cf.cfv(answerSet2)
#print(features_test1)
#print('feature vectors obtained')
# getting all labels
clf = joblib.load('trained_3.pkl')
result1 = clf.predict(features_test1)
#result2 = clf.predict(features_test2)
#print(labels_test1)
#print(result1)
i = 0
k = 1
for label in result1:
if label == 1:
print("\n \nAnswer %d:" %(k))
print(answerSet1[i]['answerBody'])
k = k + 1
i = i + 1
i = 0
if k == 1:
for label in labels_test1:
if label == 1:
print("\n \nAnswer %d:" %(k))
print(answerSet1[i]['answerBody'])
k = k + 1
i = i + 1
#end main