-
Notifications
You must be signed in to change notification settings - Fork 4
/
mul1.py
401 lines (307 loc) · 14 KB
/
mul1.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
try:
import Tkinter as tk
import ttk
except ImportError:
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
global str
from PIL import ImageTk, Image
import os
import time
import re
import random
from os.path import abspath, join, dirname
import names
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
from random import randint
import nltk.data
import docx
root= tk.Tk()
root.geometry("1560x768")
root.title("Mathword Problem")
root.configure(background="#01b9f5")
t=[1,1]
str3=""
def generateQuestion():
item = ['Beg', 'Pen', 'Book', 'Box', 'Chocolate', 'Biscuit', 'Car', 'Press', 'Mango', 'Banana', 'Doll', 'Flower', 'Bread', 'Watch', 'apple', 'apricot', 'avocado', 'banana', 'blackcurrant', 'blackberry', 'blueberry', 'cherry', 'coconut', 'fig', 'toy', 'kiwi', 'lemon', 'lime', 'lychee', 'mango', 'nectarine', 'orange', 'papaya', 'peach', 'pear', 'pineapple', 'plum', 'quince', 'raspberry', 'strawberry', 'watermelon']
z = (random.choice(item))
multiplication = ['chop', 'cut up', 'slice', 'dice', 'cube', 'mince', 'carve', 'divide', 'hash', 'cut']
az1 = (random.choice(multiplication))
__title__ = 'names'
__version__ = '0.2'
__author__ = 'Trey Hunner'
__license__ = 'MIT'
full_path = lambda filename: abspath(join(dirname(__file__), filename))
FILES = {
'first:male': full_path('dist.male.first'),
'first:female': full_path('dist.female.first'),
'last': full_path('dist.all.last'),
}
def multiwordReplace(text, wordDic):
"""
take a text and replace words that match a key in a dictionary with
the associated value, return the changed text
"""
rc = re.compile('|'.join(map(re.escape, wordDic)))
def translate(match):
return wordDic[match.group(0)]
return rc.sub(translate, text)
p1 = random.randint(2, 30)
p2 = random.randint(30, 199)
q = str(p1)
x = str(p2)
def get_name(filename):
selected = random.random() * 90
with open(filename) as name_file:
for line in name_file:
name, _, cummulative, _ = line.split()
if float(cummulative) > selected:
return name
def get_first_name(gender=None):
if gender not in ('male', 'female'):
gender = random.choice(('male', 'female'))
return get_name(FILES['first:%s' % gender]).capitalize()
def get_last_name():
return get_name(FILES['last']).capitalize()
def get_full_name(gender=None):
return u"%s %s" % (get_first_name(gender), get_last_name())
tn = (names.get_first_name())
p = (names.get_first_name())
str1 = "John had some apple. He cut each apple into 7 slices. If total 49 apple slices John make, then how many apple John had?"
# the dictionary has target_word : replacement_word pairs
# print (str1)
wordDic = {
'John': tn,
'apple': z,
'7': q,
'49': x,
'cut' : az1,}
# call the function and get the changed text
str2 = multiwordReplace(str1, wordDic)
str3 = (str2)
#print (str2)
output = ""
# Load the pretrained neural net
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
# Tokenize the text
tokenized = tokenizer.tokenize(str3)
# Get the list of words from the entire text
words = word_tokenize(str3)
# Identify the parts of speech
tagged = nltk.pos_tag(words)
for i in range(0, len(words)):
replacements = []
# Only replace nouns with nouns, vowels with vowels etc.
for syn in wordnet.synsets(words[i]):
# Do not attempt to replace proper nouns or determiners
if tagged[i][1] == 'NNP' or tagged[i][1] == 'DT':
break
# The tokenizer returns strings like NNP, VBP etc
# but the wordnet synonyms has tags like .n.
# So we extract the first character from NNP ie n
# then we check if the dictionary word has a .n. or not
word_type = tagged[i][1][0].lower()
if syn.name().find("." + word_type + "."):
# extract the word only
r = syn.name()[0:syn.name().find(".")]
replacements.append(r)
if len(replacements) > 0:
# Choose a random replacement
replacement = replacements[randint(0, len(replacements) - 1)]
output = output + " " + replacement
else:
# If no replacement could be found, then just use the
# original word
output = output + " " + words[i]
strr = str3
tn = [int(s) for s in strr.split() if s.isdigit()]
t[0] = tn[0]
t[1] = tn[1]
return strr
def OnExit():
root.destroy()
def OnHome():
root.destroy()
os.system("python Home.py")
def OnHints():
root.destroy()
os.system("python add1_c.py")
class Base_Form(object):
"""Base class of all forms"""
def __init__(self, widget_class, master, action, hidden_input, kw):
self.action = action
if hidden_input is None:
self.hidden_input = dict()
else:
if not isinstance(hidden_input, dict):
raise ValueError("'hidden_input' should be a dict")
self.hidden_input = hidden_input
kw["class"] = "Form"
widget_class.__init__(self, master, **kw)
class Base_SubmitButton(object):
"""Base class of submit buttons"""
def submit(self):
form_widget = self
while True:
form_widget = form_widget.master
if form_widget is None:
raise Exception("No equation found")
else:
if form_widget.winfo_class() == "Form":
break
if form_widget.action is None: return
form_action = form_widget.action
form_data = {}
form_data.update(form_widget.hidden_input)
# Applying list for python 2/3 compatibility. dict_values is a view in Python 3.
list_of_widgets = list(form_widget.children.values())
while True:
try:
widget = list_of_widgets.pop()
except IndexError:
break
list_of_widgets.extend(list(widget.children.values()))
if not hasattr(widget,"fieldname"): continue
field_name = widget.fieldname
Tk_class = widget.winfo_class()
if Tk_class == "Entry" or Tk_class == "TEntry":
field_value = widget.get()
elif Tk_class == "Text":
field_value = widget.get("1.0",'end-1c')
elif Tk_class == "TCombobox":
field_value = widget.get()
elif Tk_class == "Listbox":
field_value = [widget.get(idx) for idx in widget.curselection()]
else:
continue
form_data[field_name] = field_value
form_action(form_data)
class Form_Frame(tk.Frame, Base_Form):
def __init__(self, master, action=None, hidden_input=None, **kw):
Base_Form.__init__(self, tk.Frame, master, action, hidden_input, kw)
class Form_TFrame(tk.Frame, Base_Form):
def __init__(self, master, action=None, hidden_input=None, **kw):
Base_Form.__init__(self, ttk.Frame, master, action, hidden_input, kw)
class Form_LabelFrame(tk.LabelFrame, Base_Form):
def __init__(self, master, action=None, hidden_input=None, **kw):
Base_Form.__init__(self, tk.LabelFrame, master, action, hidden_input, kw)
class Form_TLabelFrame(ttk.LabelFrame, Base_Form):
def __init__(self, master, action=None, hidden_input=None, **kw):
Base_Form.__init__(self, ttk.LabelFrame, master, action, hidden_input, kw)
Form = Form_Frame
class Submit_Button(tk.Button, Base_SubmitButton):
def __init__(self, parent, *args, **kw):
kw["command"] = self.submit
tk.Button.__init__(self, parent, *args, **kw)
class Submit_TButton(ttk.Button, Base_SubmitButton):
def __init__(self, parent, *args, **kw):
kw["command"] = self.submit
ttk.Button.__init__(self, parent, *args, **kw)
if __name__== "__main__":
try:
from Tkinter import Frame, Entry, Radiobutton, Checkbutton, Text, Listbox, Tk, Label, StringVar
import tkMessageBox as messagebox
from ttk import Combobox
from Tkconstants import *
except ImportError:
from tkinter import Frame, Entry, Radiobutton, Checkbutton, Text, Listbox, Tk, Label, messagebox, StringVar
from tkinter.ttk import Combobox
from tkinter.constants import *
import pprint
pp = pprint.PrettyPrinter(indent=4)
class MainWindow(tk.Frame):
counter = 0
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
#self.str3 = "John had some apples. He cut each apple into 7 slices. If total 49 apple slices John make, then how many apples John had?(approx)"
self.t = t
#var.set("Equation Form Of The Mathword Problem Is:\n"+str(t[0])+ "+"+ str(t[1])+"= ?")
self.k=divmod(self.t[1],self.t[0])
self.label = tk.Label( self,font=('arial', 15, 'bold'), bd=16,bg="#01b9f5", fg="#012b74", text="Equation Form Of The Mathword Problem Is:\n"+"? * " +str(self.t[0])+ " = "+ str(self.t[1]),relief=RAISED)
self.label.pack()
self.button = tk.Button(self, font=('arial', 15, 'bold'), bd=16,bg="#ff9900", fg="#012b74", text="Click here for answer",
command=self.create_window)
self.button.pack(side="top")
def create_window(self):
self.counter += 1
t = tk.Toplevel(self)
l = tk.Label(t, font=('arial', 25, 'bold'), bd=16,bg="#01b9f5", fg="#012b74", text="Correct Answer is->"+"\n Quotient is: "+str(self.k[0]) +"\n Remainder is: "+str(self.k[1]))
l.pack(side="top", fill="both", expand=True, padx=100, pady=100)
def answer(self):
self.k=divmod(self.t[1],self.t[0])
return str("\n Quotient is: "+str(self.k[0]) +"\n Remainder is: "+str(self.k[1]))
img2 = ImageTk.PhotoImage(Image.open("math1.jpg"))
panel = Label(root, image = img2, bg="#01b9f5")
panel.pack(side = "bottom", fill = "both", expand = "yes")
#top label for topic
Label(root, font=('aldhabi',30,'bold'), text=" QUESTION GENERATOR FOR MATHWORD PROBLEM", fg="white", bg="#01b9f5", bd=10, anchor='w').pack(anchor=W, padx=(0,0))
##uper part of the page
objt = MainWindow(root)
form = Form(root, action =lambda data: messagebox.showinfo("Submitted_Result_Is:",pp.pformat(data)+"\n\nCorrect answer is: "+ objt.answer()),bg="#99ccff")
form.pack(expand=True, fill="both", ipadx=10, ipady=10)
# It's possible to provide hidden data
# form.hidden_input["hidden_var1"] = "value1"
# form.hidden_input["hidden_var2"] = "value2"
Label(form, height=1, font=('arial', 12, 'bold'), text="Question:", bg="#99ccff", fg='blue4', bd=20, anchor='w').grid(row=1,column=0, sticky=E, pady=(0,0))
#function to show and print question
entry = Entry(form, font=('arial',10,'bold'), width=120, bd=13, bg="#99ccff", fg="#012b74")
entry.fieldname = "Question"
entry.grid(row=1,column=1, sticky =E+W)
entry.delete(0, END)
str3 = generateQuestion()
entry.insert(0, (str3))
Label(form, font=('arial', 12, 'bold'), text="Answer write here:", bg="#99ccff", fg="#012b74", bd=10, anchor='w').grid(row=5,column=0, sticky=E, pady=(8,2))
text1 = Text(form,font=('arial', 12, 'bold'), height=2, fg="#012b74", bg="#99ccff", bd=10)
text1.fieldname = "Submitted Answer"
text1.grid(row=5,column=1, sticky =E+W)
def nextQuestion():
str3 = generateQuestion()
entry.delete(0,1000)
text1.delete(1.0,END)
entry.insert(0, (str3))
def OnGenQuest():
doc = docx.Document()
doc.add_heading('Data set of MATHWORD PROBLEM',0)
for i in range(0,3000):
str3 = generateQuestion()
entry.delete(0,END)
entry.insert(0,(str3))
doc.add_paragraph('{\nIndex ' + str(i) + ':')
doc.add_paragraph('Question : "' + str3 + '"' )
doc.add_paragraph('Equation : ' + '" X = '+ str(t[1]) + ' / ' + str(t[0]) + '"')
doc.add_paragraph('Answer : "' + str(t[1] / t[0]) + '" \n}')
doc.save('dataSetMul1.docx')
Label(form, font=('algerian', 15, 'bold'), text=" For Equation Click On Yes", bg="#b0c4de", fg="#ff9900", bd=10, anchor='w').grid(row=26,column=0, sticky=E, pady=(15,0))
#creat button for connecting pages
bExit = Button(root, text='Exit', font=('algerian', 12, 'bold'), bd=10,
width=10, command=lambda:OnExit())
bExit.pack(side="left")
bYes = Button(root, text='Yes', font=('algerian', 12, 'bold'), bd=10,fg="#ff9900",
width=10, command=lambda:funct())
bYes.pack(side="left")
bHome = Button(root, text='Home', font=('algerian', 12, 'bold'), bd=10,
width=10, command=lambda:OnHome())
bHome.pack(side="left")
bNext = Button(root, text='Next', font=('algerian', 12, 'bold'), bd=10,
width=10, command=lambda:nextQuestion())
bNext.pack(side="left")
bHints = Button(root, text='Hints', font=('algerian', 12, 'bold'), bd=10,
width=10, command=lambda:OnHints())
bHints.pack(side="left")
bGenData = Button(root, text='Generate Data Set', font=('algerian', 12, 'bold'), bd=10,fg="#ff9900",
width=20, command=lambda:OnGenQuest())
bGenData.pack(side="left")
frame = Frame(root)
frame.pack()
def funct():
root = tk.Tk()
main = MainWindow(root)
root.geometry("1560x768")
root.title("Mathword Problem")
main.pack(side="top", fill="both", expand=True)
##creat gap
Label(form, font=('arial',12,'bold'),bg="#99ccff",bd=8, fg="#99ccff", text=" ").grid(row=24,column=0, sticky=E, pady=(8,0))
Submit_Button(form,font=('arial',15,'bold'),bg="#b0c4de",bd=10, fg="#ff9900", text="Submit").grid(row=26,column=1,sticky =E)
root.mainloop()