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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Fill-in-BlanksGenerator.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1tR2E8s8uQUxL6wslauPM_p20imUOhnxD
|
8 |
+
"""
|
9 |
+
|
10 |
+
from nltk import pos_tag, word_tokenize, sent_tokenize
|
11 |
+
import nltk
|
12 |
+
# nltk.download('punkt')
|
13 |
+
# nltk.download('averaged_perceptron_tagger')
|
14 |
+
import re
|
15 |
+
import random
|
16 |
+
from collections import defaultdict
|
17 |
+
|
18 |
+
'''
|
19 |
+
function to clean the input paragraph or paragraphs and returning sentense_wise tagged words
|
20 |
+
cleaning has to be done as '[' , ']' , '@'.. are tagged as nouns. and this program is to build blanks with nouns only
|
21 |
+
'''
|
22 |
+
def clean_and_tag(paragraph):
|
23 |
+
# cleaning
|
24 |
+
para = re.sub(r'[^a-zA-Z0-9 .-]','',paragraph)
|
25 |
+
#dictionary for all sentences
|
26 |
+
tags_dict_sentences = defaultdict(list)
|
27 |
+
for sentence in sent_tokenize(para):
|
28 |
+
# tagging each word in the current sentence and creating a dictionary out of em
|
29 |
+
# actually it's enough just to care about 'NNP' and 'NN'
|
30 |
+
temp_tags = pos_tag(word_tokenize(sentence))
|
31 |
+
tags_dict = defaultdict(list)
|
32 |
+
tags_dict_sentences[sentence] = tags_dict
|
33 |
+
for i in ((temp_tags)):
|
34 |
+
tags_dict[i[1]].append(i[0])
|
35 |
+
return tags_dict_sentences
|
36 |
+
|
37 |
+
'''
|
38 |
+
function to replace a word with a blank
|
39 |
+
'''
|
40 |
+
def replaceblank(word, sentence):
|
41 |
+
#uses regex to replace a word with a blank
|
42 |
+
temp = re.compile(re.escape(word), re.IGNORECASE)
|
43 |
+
return temp.sub('________________', sentence)
|
44 |
+
|
45 |
+
'''
|
46 |
+
function to basically remove a word and replace it with a blank if possible
|
47 |
+
As most of the geography, history, science books revolve around nouns, most fill in
|
48 |
+
the blanks are nouns.
|
49 |
+
if you want to teach children english, you can pick adjectives or verbs as well
|
50 |
+
'''
|
51 |
+
def removeWord(sentence, tags_dict):
|
52 |
+
#select only nouns
|
53 |
+
words = None
|
54 |
+
if 'NNP' in tags_dict:
|
55 |
+
words = tags_dict['NNP']
|
56 |
+
elif 'NN' in tags_dict:
|
57 |
+
words = tags_dict['NN']
|
58 |
+
else:
|
59 |
+
print("NN and NNP not found")
|
60 |
+
return (None, sentence, None)
|
61 |
+
if len(words) > 0:
|
62 |
+
# randomly choose a word, if there is a score of NNP, we can choose the
|
63 |
+
# best word with that
|
64 |
+
word = random.choice(words)
|
65 |
+
replaced = replaceblank(word, sentence)
|
66 |
+
return (word, sentence, replaced)
|
67 |
+
else:
|
68 |
+
print("words are empty")
|
69 |
+
return (None, sentence, None)
|
70 |
+
|
71 |
+
'''
|
72 |
+
function which combines everything and generates blanks
|
73 |
+
|
74 |
+
inputs are paragraph = paragraph(s) you want to generate blanks from
|
75 |
+
num_of_blanks = number of blanks you want to create
|
76 |
+
|
77 |
+
right now, the function works only to create blanks upto a limit. when
|
78 |
+
number of blanks to be created are more than total sentences, we have to create
|
79 |
+
multiple blanks from same sentence, so answers can be deduced easily. where as
|
80 |
+
if you don't care about that, we can extend the code a little and get that too
|
81 |
+
|
82 |
+
output is an array
|
83 |
+
each entry in the array is an array again, whose 1st element is the blank and
|
84 |
+
2nd element is the missing word
|
85 |
+
'''
|
86 |
+
def create_blanks(paragraph,num_of_blanks):
|
87 |
+
# retrieve sentences whose words are tagged
|
88 |
+
tags_dict_sentences = clean_and_tag(paragraph) # a dictionary
|
89 |
+
# these many blanks are possible, as they can't be more than number of sentences
|
90 |
+
possib_blanks = len(list(tags_dict_sentences.keys()))
|
91 |
+
if possib_blanks> num_of_blanks:
|
92 |
+
# randomly shuffle the sentences
|
93 |
+
rand_sents = (list(tags_dict_sentences.keys()))
|
94 |
+
random.shuffle(rand_sents)
|
95 |
+
|
96 |
+
#store the blanks in an array
|
97 |
+
blanks_arr = []
|
98 |
+
|
99 |
+
# number of prepared blanks and dummy variable to iterate through rand_sents
|
100 |
+
prepared_blanks = 0
|
101 |
+
i = 0
|
102 |
+
while prepared_blanks<num_of_blanks and (i<len(rand_sents)):
|
103 |
+
curr_sent_tags = tags_dict_sentences[rand_sents[i]]
|
104 |
+
(word, sentence, replaced) = removeWord(rand_sents[i], tags_dict_sentences[rand_sents[i]])
|
105 |
+
if replaced is not None:
|
106 |
+
blanks_arr.append([replaced,word])
|
107 |
+
i+=1
|
108 |
+
prepared_blanks+=1
|
109 |
+
else:
|
110 |
+
i+=1
|
111 |
+
if prepared_blanks<num_of_blanks:
|
112 |
+
print("sorry, couldn't form more than {} blanks".format(prepared_blanks))
|
113 |
+
return blanks_arr
|
114 |
+
# the same as above but asking for input
|
115 |
+
else:
|
116 |
+
try:
|
117 |
+
num_of_blanks = int(input('''number of blanks you want to create are more
|
118 |
+
than number of sentences, please give a number less than {}.
|
119 |
+
if you want to quit, just press any key other than integer and enter\n'''.format(
|
120 |
+
possib_blanks)))
|
121 |
+
if num_of_blanks<possib_blanks:
|
122 |
+
flag = 0
|
123 |
+
rand_sents = (list(tags_dict_sentences.keys()))
|
124 |
+
random.shuffle(rand_sents)
|
125 |
+
blanks_arr = []
|
126 |
+
prepared_blanks = 0
|
127 |
+
i = 0
|
128 |
+
while prepared_blanks<num_of_blanks and (i<len(rand_sents)):
|
129 |
+
curr_sent_tags = tags_dict_sentences[rand_sents[i]]
|
130 |
+
(word, sentence, replaced) = removeWord(rand_sents[i], tags_dict_sentences[rand_sents[i]])
|
131 |
+
if replaced is not None:
|
132 |
+
blanks_arr.append([replaced,word])
|
133 |
+
i+=1
|
134 |
+
prepared_blanks+=1
|
135 |
+
else:
|
136 |
+
i+=1
|
137 |
+
if prepared_blanks<num_of_blanks:
|
138 |
+
print("sorry, couldn't form more than {} blanks".format(prepared_blanks))
|
139 |
+
return blanks_arr
|
140 |
+
except:
|
141 |
+
print('quittin :(')
|
142 |
+
return None
|
143 |
+
|
144 |
+
text = """A Lion lay asleep in the forest, his great head resting on his paws. A timid little Mouse came upon him unexpectedly, and in her fright and haste to
|
145 |
+
get away, ran across the Lion's nose. Roused from his nap, the Lion laid his huge paw angrily on the tiny creature to kill her. "Spare me!" begged
|
146 |
+
the poor Mouse. "Please let me go and some day I will surely repay you." The Lion was much amused to think that a Mouse could ever help him. But he
|
147 |
+
was generous and finally let the Mouse go. Some days later, while stalking his prey in the forest, the Lion was caught in the toils of a hunter's
|
148 |
+
net. Unable to free himself, he filled the forest with his angry roaring. The Mouse knew the voice and quickly found the Lion struggling in the net.
|
149 |
+
Running to one of the great ropes that bound him, she gnawed it until it parted, and soon the Lion was free. "You laughed when I said I would repay
|
150 |
+
you," said the Mouse. "Now you see that even a Mouse can help a Lion." """
|
151 |
+
file =None
|
152 |
+
|
153 |
+
import mysql.connector
|
154 |
+
import datetime;
|
155 |
+
|
156 |
+
mydb = mysql.connector.connect(
|
157 |
+
host="qtechdb-1.cexugk1h8rui.ap-northeast-1.rds.amazonaws.com",
|
158 |
+
user="admin",
|
159 |
+
password="F3v2vGWzb8vaniE3nqzi",
|
160 |
+
database="spring_social"
|
161 |
+
)
|
162 |
+
|
163 |
+
import gradio as gr
|
164 |
+
|
165 |
+
total = 5
|
166 |
+
def createdblanks_text(text,subject,total):
|
167 |
+
|
168 |
+
words_text = len(re.findall(r'\w+', text))
|
169 |
+
words_subject = len(re.findall(r'\w+', subject))
|
170 |
+
|
171 |
+
|
172 |
+
if (words_text < 150):
|
173 |
+
raise gr.Error("Invalid Input (Words limit must be more than 150 words).")
|
174 |
+
# print("Number of words:", words)
|
175 |
+
|
176 |
+
elif (words_subject < 1):
|
177 |
+
raise gr.Error("Invalid Input (Title must be one or more than one word).")
|
178 |
+
|
179 |
+
else:
|
180 |
+
output="<b>Read the question carefully and fill in the blanks.</b><br/>"
|
181 |
+
blanks = create_blanks(text,total)
|
182 |
+
|
183 |
+
i=1
|
184 |
+
for blank in blanks:
|
185 |
+
output = output + "<b> Q"+ str(i) + ") " + blank[0] + "</b><br/>"
|
186 |
+
output = output + "<br/>"
|
187 |
+
i += 1
|
188 |
+
output = output + "<b>Correct Answer Key</b><br/>"
|
189 |
+
i=1
|
190 |
+
for blank in blanks:
|
191 |
+
output = output + "<b style='color:green;'>" + "Ans"+ str(i) + ": " +blank[1]+ "</b>"
|
192 |
+
output = output + "<br/>"
|
193 |
+
i += 1
|
194 |
+
|
195 |
+
# mycursor = mydb.cursor()
|
196 |
+
# timedate = datetime.datetime.now()
|
197 |
+
|
198 |
+
# sql = "INSERT INTO filltexts (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
|
199 |
+
# val = (subject, text, output, timedate)
|
200 |
+
# mycursor.execute(sql, val)
|
201 |
+
|
202 |
+
# mydb.commit()
|
203 |
+
|
204 |
+
# print(mycursor.rowcount, "record inserted.")
|
205 |
+
|
206 |
+
return output
|
207 |
+
|
208 |
+
import gradio as gr
|
209 |
+
|
210 |
+
|
211 |
+
context = gr.Textbox(lines=10, placeholder="Enter paragraph/content here...", label="Enter your content (words input must be more than 150 words).")
|
212 |
+
total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
|
213 |
+
subject = gr.Textbox(placeholder="Enter subject/title here...", label="Enter your title (title must contain 1 word)")
|
214 |
+
|
215 |
+
output = gr.HTML( label="Question and Answers")
|
216 |
+
|
217 |
+
iface = gr.Interface(
|
218 |
+
fn=createdblanks_text,
|
219 |
+
inputs=[context,subject, total],
|
220 |
+
outputs=output,
|
221 |
+
allow_flagging="never",flagging_options=["Save Data"])
|
222 |
+
|
223 |
+
# iface.launch(debug=True)
|
224 |
+
|
225 |
+
import glob
|
226 |
+
import os.path
|
227 |
+
import pandas as pd
|
228 |
+
|
229 |
+
total=5
|
230 |
+
|
231 |
+
def createdblanks(text,subject,total):
|
232 |
+
output="<b>Read the question carefully and fill in the blanks.</b><br/>"
|
233 |
+
blanks = create_blanks(text,total)
|
234 |
+
|
235 |
+
i=1
|
236 |
+
for blank in blanks:
|
237 |
+
output = output + "<b> Q"+ str(i) + ") " + blank[0] + "</b><br/>"
|
238 |
+
output = output + "<br/>"
|
239 |
+
i += 1
|
240 |
+
output = output + "<b>Correct Answer Key</b><br/>"
|
241 |
+
i=1
|
242 |
+
for blank in blanks:
|
243 |
+
output = output + "<b style='color:green;'>" + "Ans"+ str(i) + ": " +blank[1]+ "</b>"
|
244 |
+
output = output + "<br/>"
|
245 |
+
i += 1
|
246 |
+
|
247 |
+
return output
|
248 |
+
|
249 |
+
def filecreate(x,subject,total):
|
250 |
+
|
251 |
+
# Using Latest temp file from folder
|
252 |
+
|
253 |
+
# folder_path = r'C:\Users\HP\AppData\Local\Temp'
|
254 |
+
# file_type = r'\*txt'
|
255 |
+
# files = glob.glob(folder_path + file_type)
|
256 |
+
# max_file = max(files, key=os.path.getctime)
|
257 |
+
|
258 |
+
# fp = open(max_file, 'r')
|
259 |
+
# text = fp.read()
|
260 |
+
# print(text)
|
261 |
+
# print(fp)
|
262 |
+
|
263 |
+
|
264 |
+
with open(x.name) as fo:
|
265 |
+
text = fo.read()
|
266 |
+
# print(text)
|
267 |
+
|
268 |
+
words_text = len(re.findall(r'\w+', text))
|
269 |
+
words_subject = len(re.findall(r'\w+', subject))
|
270 |
+
|
271 |
+
|
272 |
+
if (words_text < 150):
|
273 |
+
raise gr.Error("Invalid Input (Words limit must be more than 150 words).")
|
274 |
+
# print("Number of words:", words)
|
275 |
+
|
276 |
+
elif (words_subject < 1):
|
277 |
+
raise gr.Error("Invalid Input (Title must be one or more than one word).")
|
278 |
+
|
279 |
+
else:
|
280 |
+
|
281 |
+
generated = createdblanks(text,subject, total)
|
282 |
+
# mycursor = mydb.cursor()
|
283 |
+
|
284 |
+
# timedate= datetime.datetime.now()
|
285 |
+
|
286 |
+
# sql = "INSERT INTO fillfiles (subject, input, output, timedate) VALUES (%s,%s, %s,%s)"
|
287 |
+
# val = (subject, text, generated, timedate)
|
288 |
+
# mycursor.execute(sql, val)
|
289 |
+
|
290 |
+
# mydb.commit()
|
291 |
+
|
292 |
+
# print(mycursor.rowcount, "record inserted.")
|
293 |
+
|
294 |
+
return generated
|
295 |
+
|
296 |
+
# filecreate(file,2)
|
297 |
+
|
298 |
+
import gradio as gr
|
299 |
+
|
300 |
+
context = gr.HTML(label="Text")
|
301 |
+
file = gr.File(label="Upload your file (File must contain more than 150 words).")
|
302 |
+
total = gr.Slider(1,10, value=1,step=1, label="Total Number Of Questions")
|
303 |
+
subject = gr.Textbox(placeholder="Enter subject/title here...", label="Enter your title (title must contain 1 word).")
|
304 |
+
|
305 |
+
|
306 |
+
fface = gr.Interface(
|
307 |
+
fn=filecreate,
|
308 |
+
inputs=[file,subject,total],
|
309 |
+
outputs=context,
|
310 |
+
# css=".gradio-container {background-color: white}",
|
311 |
+
# theme="huggingface",
|
312 |
+
allow_flagging="never",flagging_options=["Save Data"])
|
313 |
+
|
314 |
+
# fface.launch(debug=True, show_api=False)
|
315 |
+
|
316 |
+
demo = gr.TabbedInterface([iface, fface], ["Text", "Upload File"], theme="huggingface")
|
317 |
+
demo.launch(debug=True, show_api=False)
|