forked from AdityaVardhanGara/PythonCodeGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBTPcur.py
More file actions
389 lines (363 loc) · 13.7 KB
/
Copy pathBTPcur.py
File metadata and controls
389 lines (363 loc) · 13.7 KB
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
#Libraries used: Python Turtle, Speech recognition, word2number, Tkinter
import tkinter as tk
from tkinter import *
import speech_recognition as sr
import turtle
from word2number import w2n
#----------------------example input -------------------
"""
Key words implemneted and example input:
forward backward: move forward by 100 (default 100 units)
right left: take/trun left by 80 degrees (default 90 degrees)
penup pendown: lift/put the pen up/down
colour: change colour to red (default black)
move to: move to 10 comma 20
dot: keep red dot of size 60 (default size 60 clour black)
stamp:
import: import library_name
if: if CONDITIONS then STATEMENTS end indentation
while: while CONDITIONS then STATEMENTS end indentation
All the key words implemented:
forward backward left right penup pendown
colour moveto stamp dot import if
while shape
"""
#----------------------example input ends------------------
#initialising speech recgnition python turtle and Tkinter GUI
root = Tk()
root.title("Python code Generator")
root.geometry("800x800")
r = sr.Recognizer()
skk = turtle.Turtle()
#Opening file and including basic libraries
file = open('output.py','w')
file.write("import turtle\n")
file.write("skk = turtle.Turtle()\n")
skk.pensize(5)
file.write("skk.pensize(5)\n")
skk.shape('turtle')
file.write("skk.shape('turtle')\n")
#useful lists
#list of colors
colors = ['red','black','blue','green']
#list of shapes:
shapes = ['turtle','arrow']
#list of key words
keywords=['if','for','while','forward','backward','dot','shape','left','right','penup','pen','up','down','go','move','comma','and','or','stamp','value','import','kinter'
,'speech','turtle','colour','greater','equal','less','then','indentation','indent','increment','decrement','range','in','per','where','go','dot','declare'
,'program','end']
#logical operators
logical_operators=['and','or','xor','not']
compare_operators=['greater','less','greaterequal','notequal','lessequal']
#list of Libraries
libraries=['tkinter','speech_recognition','word2number']
#list of variables declared
variables=['skk','y']
#function to return compare_operators
def operator(str):
if(str=='greaterequal'):
return '>='
elif(str=='lessequal'):
return '<='
elif(str=='notequal'):
return '!='
elif(str=='equal'):
return '='
elif(str=='greater'):
return '>'
elif(str=='less'):
return '<'
elif(str=='plus'):
return '+'
elif(str=='minus'):
return '-'
elif(str=='times'):
return '*'
elif(str=='upon'):
return '/'
elif(str=='power'):
return '**'
else:
return ' '
#function for getting condition
def condition(words,i):
size=len(words)
while(i<size and words[i]!='then'):
if (i+1<size and words[i]+words[i+1] in compare_operators):
T1.insert(END,' {} '.format(operator(words[i]+words[i+1])))
i+=1
elif(words[i] in variables or words[i] in logical_operators or words[i].isnumeric()):
T1.insert(END,' {} '.format(words[i]))
elif(words[i]=='equal'):
T1.insert(END,' {} '.format('=='))
elif(words[i] in compare_operators):
T1.insert(END,' {} '.format(operator(words[i])))
i+=1
return i+1
#function for button click (Mic_Button)
def funct():
#taking voice input on mouse click(mic Button
global textcode
global text_prev
with sr.Microphone() as source:
print("\nSpeak Anything...:)")
T0.insert(END , "\nSpeak Anything...:)")
text=''
audio = r.listen(source, phrase_time_limit=20)
try:
text = r.recognize_google(audio)
print("You said : {}".format(text))
T0.insert(END,"\nYou said : \n{}".format(text))
except:
print("Sorry could not recognize what you said")
T0.insert(END,"Sorry could not recognize what you said")
#adding input to string
text_prev=textcode
textcode+=text
#function for last line taken
def clear():
global text_prev
global textcode
textcode=text_prev
#function for converting the input to code: function for convert button
def functcon():
global textcode
global indent_string
global indent
#splitting into tokens
textcode=textcode.lower()
tot_words=textcode.split()
size=len(tot_words)
i=0
#checking whether the token is a keyword or lower case words
words=[]
while i<size:
if(tot_words[i] in keywords or tot_words[i].isnumeric() or tot_words[i] in colors or tot_words[i] in variables):
words.append(tot_words[i].lower())
elif(i>0 and (tot_words[i-1]=='for' or tot_words[i-1]=='variable')):
words.append(tot_words[i].lower())
i+=1
print(words)
print(variables)
size=len(words)
i=0
#loop over all the tokens to convert into code statemnts
while i<size:
if(words[i]=='forward'):
i+=1
if(i<size and words[i].isnumeric()):
number1=int(words[i])
i+=1
else:
s=''
number1=100
skk.forward(number1)
file.write("skk.forward({})\n".format(number1)+indent_string)
T1.insert(END,"skk.forward({})\n".format(number1)+indent_string)
elif(words[i]=='backward'):
i+=1
if(i<size and words[i].isnumeric()):
number1=int(words[i])
i+=1
else:
number1=100
skk.backward(number1)
file.write("skk.backward({})\n".format(number1)+indent_string)
T1.insert(END,"skk.backward({})\n".format(number1)+indent_string)
elif(words[i]=='left' or words[i]=='lift'):
i+=1
if(i<size and words[i].isnumeric()):
number1=int(words[i])
i+=1
else:
number1=90
skk.left(number1)
file.write("skk.left({})\n".format(number1)+indent_string)
T1.insert(END,"skk.left({})\n".format(number1)+indent_string)
elif(words[i]=='right' or words[i]=='write'):
i+=1
if(i<size and words[i].isnumeric()):
number1=int(words[i])
i+=1
else:
number1=90
skk.right(number1)
file.write("skk.right({})\n".format(number1)+indent_string)
T1.insert(END,"skk.right({})\n".format(number1)+indent_string)
elif(words[i]=='pen'):
i+=1
if(i<size and words[i]=='up'):
skk.penup()
file.write("skk.penup()\n"+indent_string)
T1.insert(END,"skk.penup()\n"+indent_string)
elif(i<size and words[i]=='down'):
skk.pendown()
file.write("skk.pendown()\n"+indent_string)
T1.insert(END,"skk.pendown()\n"+indent_string)
elif(words[i]=='colour' or words[i]=='color'):
i+=1
if(i<size and words[i] not in colors):
skk.color("black")
file.write('skk.color("black")\n'+indent_string)
T1.insert(END,'skk.color("black")\n'+indent_string)
i+=1
else:
skk.color(words[i])
file.write('skk.color("{}")\n'.format(words[i])+indent_string)
T1.insert(END,'skk.color("{}")\n'.format(words[i])+indent_string)
elif(words[i]=='move' or words[i]=='go'):
i+=1
number1=0
number2=0
if(words[i].isnumeric()):
number1=int(words[i])
i+=1
if(words[i]=='comma' or words[i]=='and'):
i+=1;
if(words[i].isnumeric()):
number2=int(words[i])
i+=1
skk.goto(number1,number2)
T1.insert(END,'skk.goto('+number1+','+number2+')\n'+indent_string)
elif(words[i]=='dot'):
if(i>0 and words[i-1] in colors):
cur_cul=words[i-1]
i+=1
if(i<size and words[i].isnumeric()):
number1=int(words[i])
else:
number1=60
i+=1
else:
i+=1
cur_cul="black"
number1=60
skk.dot(number1,cur_cul)
file.write('skk.dot()\n'+indent_string)
T1.insert(END,'skk.dot({},{})\n'.format(number1,cur_cul)+indent_string)
elif(words[i]=='stamp'):
file.write('skk.stamp()\n'+indent_string)
T1.insert(END,'skk.stamp()\n'+indent_string)
i+=1
elif(words[i]=='shape' or words[i]=='make' or words[i]=='change'):
i+=1
shape=words[i]
if(words[i] not in shapes):
shape='arrow'
skk.shape(shape)
file.write('skk.shape("{}")\n'.format(words[i])+indent_string)
T1.insert(END,'skk.shape("{}")\n'.format(shape)+indent_string)
elif(words[i]=='import'):
i+=1
if(words[i]=='kinter'):
T1.insert(END,'import tkinter\n'+indent_string)
elif(words[i]=='speech'):
T1.insert(END,'import speech_recognition\n'+indent_string)
elif(words[i]=='speech'):
T1.insert(END,'import turtle\n')
#if condition
elif(words[i]=='if'):
T1.insert(END,'if(')
i+=1
i=condition(words,i)
indent+=3
indent_string=' '*indent
T1.insert(END,'):\n'+indent_string)
#while condition
elif(words[i]=='while'):
T1.insert(END,'while(')
i+=1
i=condition(words,i)
indent+=3
indent_string=' '*indent
T1.insert(END,'):\n'+indent_string)
elif(words[i]=='for' or words[i] =='per' or words[i]=='where'):
T1.insert(END,'for ')
i+=1
while(words[i] not in keywords or words[i]=='in'):
T1.insert(END,' {} '.format(words[i]))
i+=1
if(words[i]=='range'):
T1.insert(END,' range(')
i+=1
while(words[i].isnumeric() or words[i]=='comma'):
T1.insert(END,'{}'.format(words[i]))
i+=1
T1.insert(END,')')
indent+=3
indent_string=' '*indent
T1.insert(END,':\n'+indent_string)
elif(words[i]=='declare'):
i+=1
if(i<size-2 and words[i+1]=='equal' and words[i+2].isnumeric()):
T1.insert(END,words[i].lower()+' = '+words[i+2]+'\n'+indent_string)
variables.append(words[i].lower())
i+=3
elif(words[i]=='increment'):
i+=1
if(i<size and words[i] in variables):
T1.insert(END,'{}+=1\n'.format(words[i])+indent_string)
i+=1
else:
i+=1
elif(words[i]=='decrement'):
i+=1
if(i<size and words[i] in variables):
T1.insert(END,'{}-=1\n'.format(words[i])+indent_string)
i+=1
else:
i+=1
elif(words[i] in variables):
T1.insert(END,'{} '.format(words[i]))
i+=1
while(i<size-1 and operator(words[i])!=' '):
T1.insert(END,'{} {}'.format(operator(words[i]),words[i+1]))
i+=2
T1.insert(END,'\n'+indent_string)
elif(words[i]=='end' or words[i]=='and'):
i+=1
if(i<size and (words[i]=='indent' or words[i]=='indentation') ):
if(indent>=3):
indent-=3
indent_string=' '*indent
T1.insert(END,'\n'+indent_string)
elif(i<size and words[i]=='program'):
break
else:
i+=1
textcode=""
#----------------------- convert function ends --------------------------------
def pressed():
print("Button Pressed!")
#------------------------------- GUI part ----------------------------------------
textcode=" "
text_prev=" "
indent=0
indent_string=''
#Designing GUI
MainLabel=Label(root,text="This application helps you to generator Python code from your voice :)",anchor=CENTER,
height=5).pack(side=TOP,fill='x')
mainframe=Frame(root,bg='white',width=100,height=50)
mainframe.pack(side=TOP)
T0= Text(mainframe,bg='gray40',width=50,height=38,fg='white')
T0.pack(side= LEFT,fill=BOTH)
T0.insert(END, "Please say something: ")
T1= Text(mainframe,bg='gray40',width=50,height=38,fg='white')
T1.pack(side= RIGHT,fill=BOTH)
T1.insert(END, "import turtle\nskk = turtle.Turtle()\nskk.pensize(5)\nskk.shape('turtle')\n")
#function declaration
Fmic=PhotoImage(file='./mic.png')
Fmic= Fmic.subsample(3,3)
funcframe=Frame(root,bg='seashell4',height=60)
funcframe.pack(side=BOTTOM,fill='x')
Bmic=Button(funcframe, image=Fmic,command=funct)
Bmic.pack(side=BOTTOM)
Bconvert = Button(funcframe, text = 'Convert',
command = functcon)
Bconvert.place(relx=0.37,rely=0.5)
Bclear = Button(funcframe, text = 'Clear',
command = clear)
Bclear.place(relx=0.53,rely=0.5)
turtle.done()
mainloop()
#--------------------------- GUI part ends -----------------------------