forked from capturemathan/ReviseWithMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Questiongen.py
156 lines (118 loc) · 5.78 KB
/
Questiongen.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import Flask, jsonify
from textblob import TextBlob
import nltk
from textblob import Word
import sys
app = Flask(__name__)
@app.route('/<string>')
def parse(string):
line = string
questions = ""
try:
txt = TextBlob(string)
questions = ""
for sentence in txt.sentences:
if type(sentence) is str:
sentence = TextBlob(sentence)
bucket = {}
for i, j in enumerate(sentence.tags):
if j[1] not in bucket:
bucket[j[1]] = i
question = ''
# WHAT CLAUSE QUESTIONS.
l1 = ['NNP', 'VBG', 'VBZ', 'IN']
l2 = ['NNP', 'VBG', 'VBZ']
l3 = ['PRP', 'VBG', 'VBZ', 'IN']
l4 = ['PRP', 'VBG', 'VBZ']
l5 = ['NN', 'VBG', 'VBZ']
l6 = ['NNP', 'VBZ', 'JJ']
l7 = ['NNP', 'VBZ', 'NN']
l8 = ['NNP', 'VBZ']
l9 = ['PRP', 'VBZ']
l10 = ['NN', 'VBZ']
# WHERE CLAUSE QUESTIONS.
wh1 = ['NNP', 'VBZ', 'IN']
# WHO CLAUSE QUESTIONS.
p1 = ['NNP', 'VBZ', 'DT', 'NN']
p2 = ['NNP', 'VBZ', 'DT', 'NN', 'IN']
# HOW CLAUSE QUESTIONS.
h1 = ['NN', 'VBN', 'IN']
h2 = ['NN', 'VBD', 'IN']
# NUMBER [QUANTITATIVE INFO]
v1 = ['CD', 'NN']
v2 = ['CD', 'NNS']
if all(key in bucket for key in p1):
question = 'Who' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NNP']] + '?'
elif all(key in bucket for key in p2):
question = 'Who' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NNP']] + '?'
elif all(key in bucket for key in wh1):
question = 'Where' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NNP']] + '?'
elif all(key in bucket for key in h1):
question = 'How' + ' ' + \
sentence.words[bucket['NN']] + ' ' + \
sentence.words[bucket['VBN']] + '?'
elif all(key in bucket for key in h2):
question = 'How' + ' ' + \
sentence.words[bucket['NN']] + ' ' + \
sentence.words[bucket['VBD']] + '?'
elif all(key in bucket for key in v1):
question = 'How' + ' ' + 'many ' + \
sentence.words[bucket['NN']] + '?'
elif all(key in bucket for key in v2):
question = 'How' + ' ' + 'many ' + \
sentence.words[bucket['NNS']] + '?'
elif all(key in bucket for key in l1):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + sentence.words[bucket['NNP']
] + ' ' + sentence.words[bucket['VBG']] + '?'
elif all(key in bucket for key in l2):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + sentence.words[bucket['NNP']
] + ' ' + sentence.words[bucket['VBG']] + '?'
elif all(key in bucket for key in l3):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + sentence.words[bucket['PRP']
] + ' ' + sentence.words[bucket['VBG']] + '?'
elif all(key in bucket for key in l4):
question = 'What ' + sentence.words[bucket['PRP']] + ' ' + ' does ' + \
sentence.words[bucket['VBG']] + ' ' + \
sentence.words[bucket['VBG']] + '?'
elif all(key in bucket for key in l5):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + sentence.words[bucket['NN']
] + ' ' + sentence.words[bucket['VBG']] + '?'
elif all(key in bucket for key in l6):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NNP']] + '?'
elif all(key in bucket for key in l7):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NNP']] + '?'
elif all(key in bucket for key in l9):
if sentence.words[bucket['PRP']] in ['she', 'he']:
question = 'What' + ' does ' + sentence.words[bucket['PRP']].lower(
) + ' ' + sentence.words[bucket['VBZ']].singularize() + '?'
elif all(key in bucket for key in l8):
question = 'What' + ' does ' + \
sentence.words[bucket['NNP']] + ' ' + \
sentence.words[bucket['VBZ']].singularize() + '?'
elif all(key in bucket for key in l10):
question = 'What' + ' ' + \
sentence.words[bucket['VBZ']] + ' ' + \
sentence.words[bucket['NN']] + '?'
if 'VBZ' in bucket and sentence.words[bucket['VBZ']] == "’":
question = question.replace(" ’ ", "'s ")
questions += question
return jsonify(q=questions)
except Exception as e:
raise e
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=5000)