-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
337 lines (310 loc) · 14.1 KB
/
translator.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
'''
Table of contents
imports
isD starting definition
'''
print "init translator"
## imports
#import translated
#import nltk
import sys, re, json, operator, string
#from pyDatalog import pyDatalog
'''
from textblob import TextBlob
blob = TextBlob("write 'hello, how are you?' into a file")
blob.tags'''
global isD;
## wont exec args as is, args always parsed to something
isD = {
'write' : { 'verb': [['print something'], 'scrawl'] },
'return' : { 'verb': [['return something'], 'scrawl'] },
'shout' : { 'verb': [['write something','write "!!!"'], 'yell']},
'write_a' : { 'verb': [['write a'], 'scrawl'] },
'write_then' : { 'verb': [["1", 'print something'], 'scrawl'] },
'verbose' : {'noun': {'def1': False} },
}
isD0 = isD
#isSD = re.sub("(\w+)", "'"+"\\1"+"'", isS); ## adds quotes around single words
# a.setdefault("somekey",[]).append("bob")
print 'isD: ',isD
## global variables
global pythonVerbs; pythonVerbsString = 'print,operator.add(something,b)'; pythonVerbs = pythonVerbsString.split(","); print pythonVerbs
global verbsL; verbsString = 'write,make,create'; verbsL = verbsString.split(","); #print verbs
global prepositions; prepositionsString = 'in ,into ,over '; prepositions = prepositionsString.split(",")
global functionD; functionD = {};
## helper functions
def addQuotes(string): return "'"+string+"'"
def sliceAt(string, slicePoint): return string[string.index(slicePoint)+len(slicePoint):]
def joins(list): return "".join(list)
def joins_(list): return " ".join(list)
def splits(string): return string.split(" ")
def ifKey(*args): ## check if dict has key and return it
if args[0] in isD.keys():
if args[1] in isD[args[0]].keys():
if args[2] in isD[args[0]][args[1]].keys(): return isD[args[0]][args[1]][args[2]]
else: return isD[args[0]][args[1]]
else: return isD[args[0]]
else: return None
#return (isD[args[0]][args[1]][args[2]] if args[2] in isD[args[0]][args[1]].keys() else (isD[args[0]][args[1]] if args[1] in isD[args[0]].keys() else (isD[args[0]] if args[0] in isD.keys() else None)))
#return (3 if 3>3 else (2 if 2>2 else (1 if 1>0 else None)))x
#except Exception as e: print type(e).__name__, e.args
#
## initialization functions
def getFile(file): f = open(file,"rb"); return f.read(); f.close()
def writeFile(file): f = open(file,"wb"); return f.read(); f.close()
def getFileLines(file): f = open(file,"rb"); return f.readlines(); f.close()
def getInstructions(): return getFileLines("pseudocode.py")
if 'arm' in sys.platform: dName = '/storage/sdcard1/isD.json'
else: dName = "isD.json"
def rememberD(dict1):
with open(dName) as json_file: dict1 = json.load(json_file)
def remindNoodle(): return getFile("noodle.txt")
def remindNoodleJ(): return json.loads("noodle2.py")
def dictionaryNoodle(noodle):
for line in noodle:
if line.startswith("to"): verbDef = line[:line.index(",")]; functionString = line[line.index(",")+1:]; functionD[verbDef] = functionString
return functionD
def getInput(): exec('print 1')
#return 1 if n <= 1 else n*f(f,n-1)
# if sys.platform = '': is[write] = (v,)
def saveD(dict1): json.dump(isD, dName)
## main
def main(line):
rememberD(isD)
#if verbose:
verbose = ifKey("verbose",'noun','def1')
if verbose == "False": verbose = False#True#
if verbose == "True": verbose = True
if verbose: print sys.platform; print isD
#if isD["verbose"] == {'noun':"True"}: verbose = True#
## remind verb/function definitions
noodle = remindNoodle()
global verbD; verbD = dictionaryNoodle(noodle); #print verbD
#global isD; #isD = remindNoodleJ();
#line = formatLine(line); ## parse line
if line.startswith("#"): return ## ignore comments
line = line.strip("\n").strip("\r").strip("\t"); # print line ## clean up line
if line == '': return #if line: continue
#line = line.strip("\n").strip("\r"); print line ## clean up line, leave tabs
#### find strings
'''global strings; strings = re.findall(r"\'(.+?)\'",line); #print strings
for index,string in enumerate(strings): ## replace strings with list item
line = line.replace(line[line.index(string)-1:line.index(string)+len(string)+1], strings[index]) #print 2,strings[index]#'''
if line and verbose: print '=line: ',line ############
if not line: return
#### check for contractions and split imperative clauses
# if 'and' or 'then' in line:
# lineSplit = line.split("and").split("then")
# line[line.index()]
# for line in lineSplit:
#lineList = line.split(" ",",")## split into list
#line = line.lower() ####
#lineList = re.split('(\W+)', line); lineList = [i for i in lineList if i != " "] #print lineList ##########
global lineList; lineList = re.split('(\W)', line); lineList = [i for i in lineList if i != ""]
if verbose: print '=lineList: ',lineList ########
##################### there is an order of operations: set varibles, check standard definitions, check conditionals (simple definitions), compute queries, compute imperatives
kw = lineList[0];
if verbose: print 'kw=',kw ### imperative, conditional or query must be indicated by first word
if not kw: print 'no keyword'
############ check for verb definitions or instructions
elif kw == "to":
verbL = lineList[lineList.index("to"):lineList.index(",")]; #if verbose: print verb; #print verbD[verb]
if 'something' in verbL: needsParams = True; verb = verbL.remove('something')
verbDefinition = [lineList[lineList.index(",")+1:]]
verbDefinitions = [[]]
[verbDefinitions.append([i]) if i == "," else verbDefinitions[-1].append(i) for i in verbDefinition]
if isD[verb] is not None:
input = raw_input('do you want to add this verb?')
if isD[verb] is None or input == 'y':
isD[verb] = verbDefinitions
############ test verb definitions or instructions
''' trying to evaluate definition before adding but would need to run through 'think' function
def addVerbDefintion(verb, verbDefinition):
verbDefinition[0] = verb2
if isD[verb2][0][0] == 'v': continue
else: print "I don't know how to: "+verb2
verbDefined = verbDefinition[0]
print eval(verbDefined)
isD[verb] = verbDefined
addVerbDefintion(verbDefinition)
if verb in line:
instructions = verbD[verb]; params = line[:line.index(verb)+1];
evaluate(instructions, params)
evaluate(lineList)'''
############ check for variable definition
## set variables
elif 'is' in lineList: var = joins(lineList[:lineList.index('is')-1]); assignment = joins(lineList[lineList.index('is')+2:]); print var,"is",assignment; isD[var] = {'noun':{'def1':assignment}}; exec('if verbose: print isD') in locals(),globals() ## print before assignment
elif '=' in lineList: var = joins(lineList[:lineList.index('=')-1]); assignment = joins(lineList[lineList.index('=')+2:]); print var,"=",assignment; isD[var] = {'noun':{'def1':assignment}}; exec('if verbose: print isD') in locals(),globals() #global var; var = assignment #continue
############ check for conditionals
#if "if" in line:
#if "but only if" in line:
elif kw == "if":
#if verbose: print line
#conditionalList = lineList[lineList.index('if'):lineList.index(', ')-1]; print conditionalList
#if verb in conditionalList:
if lineList[lineList.index('if')+1] == "I":
#conditionList = conditionalList.remove("if"); print conditionList
#if conditionList[0] == "I":
if conditionList[1] == "say":
command = joins(lineList[2:lineList.index(', ')-1])
instructionWhole = line[line.index(',')+1:]
if 'then' in instructionWhole: instructionWhole = instructionWhole.lstrip("then ")
verbD[command] = instructionWhole
############ check for query
elif kw == "what":
pass
elif kw in pythonVerbs or (kw in isD.keys() and 'verb' in isD[kw].keys()):
imperativeList = lineList
global args; #args = None
## parse args
args = string.join(imperativeList[2:],''); #print('args0: ',args) if verbose else "",
args = isD[args]['noun']['def1'] if args in isD.keys() and 'noun' in isD[args].keys() else args
if verbose: print('args0: ',args)
#if strings: something = 'strings[0]'; #print 'o',object1,eval(object1) #############
############ compute imperative statements - verb + args
#def computeImperative(verb, *args=None):
def computeImperative(imperativeList):
global args ######??
if verbose: print imperativeList ########
############ parse imperative statements - verb + args
## parse verb
## sentence structure VO verb-object
verb = imperativeList[0]
verbIndex = imperativeList.index(verb); #subject = lineList[:verbIndex]; # print subject ## find if subject
## parse args
if args: pass #print('args: ',args) if verbose else "",
else: #args = string.join(imperativeList[verbIndex+1:],''); #print 'args2: ',args
args = isD[args]['noun']['def1'] if args in isD.keys() and 'noun' in isD[args].keys() else string.join(imperativeList[verbIndex+1:],'')
if verbose: print('args1: ',args)
#if args in isD.keys(): args = isD[args]['noun'] if 'noun' in isD[args].keys()
#if 'noun' in isD[args].keys(): args = isD[args]['noun']; #print 'args3: ',args
## join verb and args
definition = string.join([verb, args],' ');
if verbose: print 'interp:',definition
## try execute definition with Python functions and args defined as string above or in definition
if verb in pythonVerbs:
## ask to execute
input = 'y'# raw_input("try: "+definition+" ?")
if input == 'y':
try:
exec(definition) in globals(), locals(); ################################### not secure
except Exception,e:
print 'error exec Python verb: '+str(e)
## try execute definition with verb definitions and args
if verb in isD.keys():
if type(isD[verb]['verb'][0]) is not list: print 'verb is not executable'
## loop through definitions and execute with args
for index,definition in enumerate(isD[verb]['verb'][0]):
if verbose: print index#,definition #############
if verbose: print '=definition:',definition ###########
try:
definitionL = definition.split(" ");
if verbose: print definitionL
if "=" in definition: pass
# var = joins(definitionL[:definitionL.index('=')]); assignment = joins(definitionL[definitionL.index('=')+1:]); print var,"=",assignment; var = assignment; isD[args]['noun'] = assignment
# print 11
# exec('global a') in globals(), locals()
# print 111
# print var; print a
# definition = definitionL + args; print definition def
else: computeImperative(definitionL)
# exec(definition) in globals(), locals() ################################### not secure
except Exception,e:
print 'error exec verb 1: '+str(e)
computeImperative(imperativeList)
elif kw == 'quit':
print 'noodle: ',noodle
print 'isD: ',isD
#print json.dumps(isD, indent=4, sort_keys=True)
input = raw_input("save?")
if input == 'y':
saveD(isD)
## store noodle
# append(noodle);
exit()
elif kw not in isD.keys(): print "I don't know how to.. "+kw
else: print "I don't know what.. "+kw+" ..is"
#else: print "???"
'''
def formatLine(line):
## parse line
if line.startswith("#"): return ## ignore comments
line = line.strip("\n").strip("\r").strip("\t"); # print line ## clean up line
#line = line.strip("\n").strip("\r"); print line ## clean up line, leave tabs
## find strings
global strings; strings = re.findall(r"\'(.+?)\'",line); #print strings
for index,string in enumerate(strings): ## replace strings with list item
line = line.replace(line[line.index(string)-1:line.index(string)+len(string)+1], strings[index]) #print 2,strings[index]
return line
'''
# def imperative(lineList):
#lineList = line.split(" ");
#lineList = re.split('(\W+)', line); #print lineList
#kw = lineList[0]
# if verb == 'write':
# for preposition in prepositions:
# if preposition in lineList:
##print 3,preposition
# prepositionIndex = lineList.index(preposition)
# object2 = lineList[prepositionIndex+1:]; print object2
# write(object1, preposition, " ".join(object2)); break
# write(" ".join(object1))
# if lineList[0] in functionD.keys():
# something = lineList[1:]
# def evaluate(instructionsL):
#instructionsL = instructions.split(", ")
# for instruction in instructionsL:
# for verb in verbsL:
# if verb in instructionsL: verb1 = verb
# for preposition in prepositions:
# if preposition in instructionsL: preposition1 = preposition;
# if preposition1:
# object1 = instructionsL[instructionsL.index(verb1):instructionsL.index(preposition1)]
# object2 = instructionsL[instructionsL.index(preposition1):]
# else: object1 = instructionsL[instructionsL.index(verb1):]
# look up verb function, if includes another verb, look that up - recursive
# def append(noodle):
# if functionD != verbD:
# for key,value in functionD.items():
# if functionD[key] not in verbD.keys():
# newInstruction = " ".join(['\n', key, value])
# with open("noodle.txt", 'ab') as file: file.write(newInstruction)
# elif verbD[key] != functionD[key]: print 'noodle line not the same'
def saveD(dict1):
# if isD != isD0:
# for key,value in isD.items():
# if isD[key] not in isD.keys():
# newInstruction = " ".join(['\n', key, value])
with open("isD.json", 'wb') as file: file.write(newInstruction)
# elif verbD[key] != functionD[key]: print 'noodle line not the same'
def write(sequence, preposition=False, object2=False):
fileName=''
globals().update(locals())
if object2:
# check for filename
if 'file named' in object2:
fileName = sliceAt(object2,'file named '); print fileName
if fileName: pass
else: fileName = 'temp.txt'
if preposition:
if preposition == 'in': fileMode = 'wb'
if preposition == 'into': fileMode = 'ab'
if preposition == 'over': fileMode = 'wb'
if isinstance(sequence,list):
with open(fileName, fileMode) as file: file.writelines(sequence)
if isinstance(sequence,str):
with open(fileName, fileMode) as file: file.write(sequence)
else: print sequence #'sequence>',sequence
# check __main__ to run functions now that defined in any order above
if __name__=="__main__":
## do input from instructions
inputList = getInstructions(); #print input
#for index,line in enumerate(inputList):
#main(line)
## do input from user
line=None
while line != 'q':
line = raw_input("?"); #print line
main(line)
'''
'''