def update(): global time, config, pedes, G, num_out, count time += 1 p = GammaRandom(generatorSet) count = count + len(p) num_out = ped_update(pedes, Map, num_out, time, statictics) if count < 30000: for ped in p: ped.set_start(time) pedes = pedes + p #print "statistics: ",len(statictics) del p[:] if len(pedes) == 0 and count > 29000: #print "######complete#######" heat = [] for i in range(length): row = [] for j in range(width): row.append(Map[i][j].get_sum()) heat.append(row) write(heat, 'heat.txt', 'list') write(statictics, "Pedestrian.json", "dict") #print "people in road: ",len(pedes) for i in range(0, length): for j in range(0, width): config[i, j] = Map[i][j].getstate()
def genRet(tree): global regs # If the function returns a negative variable if (type(tree[1]) is list and tree[1][0] == "-"): tree[1] = genExpr(tree[1], "rax") negate(tree[1], "rax") # If the function returns not value elif (type(tree[1]) is list and tree[1][0] == "~"): reg = regs.pop() writeMov(reg, getRegisterInScope(tree[1][1])) write("\tnot " + reg + "\n") writeMov("rax", reg) regs.append(reg) else: tree = cleanList(tree) # If the function returns if (len(tree) > 1): to_return = getScopePlusVar(tree[1]) if (to_return != ""): to_return = allocationTable[to_return] to_return = to_return.__getitem__(0) if (to_return[0] == "["): aux_reg = regs.pop() writeMov(aux_reg,to_return) to_return = aux_reg regs.append(aux_reg) else: to_return = tree[1] writeMov("rax", to_return)
def genIncDec(tree): global allocationTable if(tree[0] == "DEC"): op = "dec" else: op = "inc" write("\t", op, " ", allocationTable[getScopePlusVar(tree[1])].__getitem__(0), "\n")
def thank(request): if 'mturk-id' in request.GET: global respondent ID = request.GET['mturk-id'] respondent.append(ID) write(ID) return render(request, "end.html")
def genMain(tree): global PART PART = 0 block = tree[4][1] stack.append("__main_") write("main:\n") genBlock(block) PART = 0 write("\tret\n") stack.pop()
def genFunctionCall(tree): global BYTE_SIZE args = tree[2] if (len(args) == 1): args = [args] if (len(args) == 0): write("\tcall _" + tree[0] + "\n") else: for arg in args: var = getScopePlusVar(arg) # If argument is a variable if (var != ""): var = allocationTable[var].__getitem__(0) # If it's a value else: arg = cleanToFirstValue(arg) declTree = ["DECL", cleanToFirstValue(arg)] if (isString(arg)): declTree.append('STRING') elif (isChar(arg)): declTree.append('CHAR') else: declTree.append('INT') var = genDecl(declTree) write("\tpush " + var + "\n") write("\tcall _" + tree[0] + "\n") write("\tadd rsp, " + str(BYTE_SIZE*len(args)) + "\n")
def genRead(tree): global STRING_COUNT var = getScopePlusVar(tree[1]) scanType = allocationTable[var].__getitem__(1) reg = allocationTable[var].__getitem__(0) write("\txor rax, rax\n") writeMov("rdi", getPrintType(scanType)) pointer = "s" + str(STRING_COUNT) writeToDataSection("\t" + pointer + " db 0\n") writeMov("rsi", pointer) write("\tcall scanf\n") writeMov("rbx", "[" + pointer + "]") writeMov(reg, "rbx") STRING_COUNT += 1
def getFunctionArgs(tree): args = tree[3] if (not type(args[0]) is list): args = [(args)] write("\tpush rbp\n") write("\tmov rbp, rsp\n") rbp_count = len(args)*BYTE_SIZE + BYTE_SIZE prefix = "__" + tree[1] + "_" for arg in args: aux_reg = regs.pop() var = prefix + arg[1] allocationTable.update({var:("[rbp+"+ str(rbp_count)+"]", arg[2])}) rbp_count -= BYTE_SIZE
def negate(tree, startVar): #Check tree is well formed if (len(tree) == 2 and tree[0] == '-'): sign = tree[0] variable = tree[1][0] #If the value is already in a register reg = getScopePlusVar(tree[1]) if (reg != ""): writeMov(startVar, reg) write("\tneg ", startVar, "\n") #Else put the value in the register and negate it else: writeMov(startVar, tree[1][0]) write("\tneg ", startVar, "\n")
def genPrint(tree): global allocationTable global regs global stack global STRING_COUNT value = tree[1] varStart = "" printType = "" # If we want to print a function call if (type(tree[1]) is list and len(tree[1]) == 4 and tree[1][1] == "LPAREN" and tree[1][3] == "RPAREN"): genFunctionCall(tree[1]) reg = regs.pop() writeMov(reg, "rax") varStart = reg printType = "writeInt" regs.append(reg) # If we want to print a value directly elif (getRegisterInScope(cleanToFirstValue(value)) == "" and isValue(value)): # Special case for printing a string if (isString(cleanToFirstValue(value))): value = cleanToFirstValue(tree[1])[1:-1] writeToDataSection("\ts"+str(STRING_COUNT)+" dd `"+ str(value)+"`, 0\n") allocationTable.update({"s"+str(STRING_COUNT):(value, "STRING")}) varStart = "s"+str(STRING_COUNT) printType = getPrintType("STRING") STRING_COUNT += 1 else: varStart = cleanToFirstValue(value) printType = getPrintType(value) # If we want to print a variable content elif (len(value) == 1 and getRegisterInScope(cleanToFirstValue(value)) != ""): value = getScopePlusVar(value) printType = allocationTable[value].__getitem__(1) printType = getPrintType(printType) varStart = getRegisterInScope(value) elif (len(value) > 1): # Multiple cases, should use genExpr varStart = genExpr(value, varStart) printType = "writeInt" writeMov("rax", "0") writeMov("rsi", varStart) writeMov("rdi", printType) write("\tcall printf\n")
def genExpr(tree, startVar): global regs if (startVar == ""): startVar = regs.pop() if (len(tree) == 1): if (isValue(tree)): return cleanToFirstValue(tree) elif (len(tree) == 2): reg = getRegisterInScope(cleanToFirstValue(tree[1])) if (reg == ""): reg = cleanToFirstValue(tree[1]) if (tree[0] == "~"): writeMov(startVar, reg) write("\tnot ", startVar, "\n") return startVar elif(tree[0] == "-"): writeMov(startVar, reg) write("\tneg ", startVar, "\n") return startVar #solve subExpressions of current expression #and replace the variables with their values for i in range(len(tree)): if (len(tree[i]) > 1): newStartVar = regs.pop() tree[i] = genExpr(tree[i], newStartVar) else: #!!This if below might cause trouble, it should actually #check that tree[i] is not an operand if (i%2 == 0): #If I have a value in my expression, e.g. x + 2 if (isValue(tree[i])): tree[i] = cleanToFirstValue(tree[i]) else: tree[i] = getRegisterInScope(tree[i]) #solve this expression writeMov(startVar, tree[0]) while (len(tree) >= 3): writeOp(startVar, tree[1], tree[2]) tree = tree[2:] return startVar
def genVoid(tree): global PART PART = 1 if (tree[0] == "MAINF"): block = tree[4][1] args = tree[2] name = "hatta" else: block = tree[5][1] args = tree[3] name = tree[1] stack.append("__" + name + "_") write("\n_" + name + ":\n") write("\tpush rbp\n") write("\tmov rbp, rsp\n") if (len(args) > 0): getFunctionArgs(tree) genBlock(block) stack.pop() write("\tpop rbp\n") write("\tret\n") PART = 0
def genIf(tree): global regs global IF_NUMBER IF_NUMBER += 1 ifNumber = str(IF_NUMBER) #IF 1 write("\nif", ifNumber, ":\n") genBoolExpr(tree[2]) #If the condition doesn't hold, JUMP if(tree[6] == "ENDIF"): write("endIf", ifNumber, "\n") else: innerIf = (IF_NUMBER + 1) y = str(innerIf) write("if", y , "\n") genBlock(tree[5]) #JUMP TO ENDIF 1 write("\n\tjmp endIf", ifNumber) #Else and else_if if(tree[6] != "ENDIF"): genIf2(tree[6:], ifNumber) #ENDIF write("\nendIf", ifNumber,":\n" )
def genWhile(tree): global WHILENUMBER WHILENUMBER += 1 whileNumber = str(WHILENUMBER) write("while",whileNumber, ":\n") #Genetars the condition. genBoolExpr(tree[2]) #Jumps at the end of while is it doesn't write("endWhile",whileNumber,"\n") #Generates the body of the while loop. genBlock(tree[4]) #Writes the label for the end of the loop. write("\nendWhile",whileNumber,":\n")
def genIf2(tree, ifNumber): global regs global IF_NUMBER IF_NUMBER += 1 y = str(IF_NUMBER) write("\nif", y, ":\n") if(tree[0] == "ELSE"): genBlock(tree[1]) elif(tree[0] == "ELSE_IF"): genBoolExpr(tree[2]) if(tree[6] != "ENDIF"): #IF NOT TRUE. JUMP NEXT IF y = str(IF_NUMBER + 1) write("if", y , "\n") else: #If not true, jump endIf write("endIf", ifNumber) genBlock(tree[5]) #IF TRUE, JUMP ENDIF write("\n\tjmp endIf", ifNumber, "\n") if(tree[6] != "ENDIF"): genIf2(tree[7:], ifNumber)
def genType(tree): global PART global BYTE_SIZE global regs PART = 1 stack.append("__" + tree[1] + "_") write("_"+tree[1]+":\n") args = tree[3] if (len(args) > 0): getFunctionArgs(tree) block = tree[7][1] genBlock(block) write("\tpop rbp\n") write("\tret\n") stack.pop() PART = 0
def run(self, type, k_vec = [2], l_vec = [2]): if True == gol.get_value('DEBUG'): print ('CosineAlshTester Run:') try: if 'cosine' == type: pass else: raise ValueError except ValueError: print ('CosineAlshTester: type error: ' + str(type)) return validate_metric = dot compute_metric = L2Lsh.distance#??? #top num_neighbor for each point exact_hits = [[ix for ix, dist in self.linear(q, validate_metric, self.num_neighbours)] for q in self.origin_queries] #top_N for each point N = 10 top_N = [[ix for ix, dist in self.linear(q, validate_metric, N)] for q in self.origin_queries] #print ('==============================') write('RE', '==============================\n') #print ('CosineAlshTester ' + type + ' TEST:') write('RE', 'CosineAlshTester ' + type + ' TEST:\n') #print ('L\tk\tacc\ttouch') #precision and recall K = 6 R = [] d = len((self.ext_queries[0])) for i in range(K): R.append([random.gauss(0, 1) for i in range(d)]) #print(R) print(self.origin_datas) print(self.origin_queries) print(self.datas) print(self.queries) print(self.ext_datas) print(self.ext_queries) #hash(data) """ data_code = [] for i in range(len(self.ext_datas)): data_code.append([np.sign(dot(self.ext_datas[i], R[j])) for j in range(K)]) """ data_code = [[np.sign(dot(self.ext_datas[i], R[j])) for j in range(K)] for i in range(len(self.ext_datas))] print(data_code) #hash(query) """ query_code = [] for i in range(len(self.ext_queries)): query_code.append([np.sign(dot(self.ext_queries[i], R[j])) for j in range(K)]) """ query_code = [[np.sign(dot(self.ext_queries[i], R[j])) for j in range(K)] for i in range(len(self.ext_queries))] print(query_code) #compute matches matches = [[0 for i in range(len(self.ext_datas))] for j in range(len(self.ext_queries))] print(matches) #print("Query code") #print(query_code) #print("Data code") #print(data_code) temp_matches = 0 for i in range(len(self.ext_queries)): for j in range(len(self.ext_datas)): for m in range(K): if query_code[i][m] == data_code[j][m]: temp_matches += 1 matches[i][j] = temp_matches temp_matches = 0 print(matches) """ #sort all the points in the set //(index, maches) for each query point query_index = 0 points = [(ix, p) for ix, p in enumerate(matches[query_index])] #print("points") #print(points) sorted_points = sorted(points, key=itemgetter(1), reverse=True) #print("sorted_points") #print(sorted_points) #cumpute precision and recall relevant_seen = 0 k = 0 for pair in sorted_points: if pair[0] in set(top_N[query_index]): relevant_seen += 1 k += 1 precision = float(relevant_seen) / float(k) recall = float(relevant_seen) / float(N) print('precision:' + str(precision) + ':recall:' + str(recall)) """ print(len(self.ext_queries)) print("top"+str(N)) print(top_N) print("\n") for query_index in range(len(self.ext_queries)): # sort all the points in the set //(index, maches) for each query point points = [(ix, p) for ix, p in enumerate(matches[query_index])] print("points-start") print(points) sorted_points = sorted(points, key=itemgetter(1), reverse=True) print(sorted_points) print("points-end") # cumpute precision and recall relevant_seen = 0 k = 0 for pair in sorted_points: if pair[0] in set(top_N[query_index]): relevant_seen += 1 k += 1 precision = float(relevant_seen) / float(k) recall = float(relevant_seen) / float(N) print('precision:' + str(precision) + ':recall:' + str(recall)) #write('RE', "precision:" + str(precision) + ":recall:" + str(recall)+'\n') if 1 == recall: break print("\n") #write('RE', '\n\n') return # concatenating more hash functions increases selectivity for k in k_vec: lsh = LshWrapper(type, self.d, self.rand_range, k, 0) # using more hash tables increases recall for L in l_vec: lsh.resize(L) lsh.index(self.ext_datas) correct = 0 index_q = 0 for q, hits in zip(self.ext_queries, exact_hits): lsh_hits = [ix for ix, dist in lsh.query(q, compute_metric, self.num_neighbours)] #test #print('test') #print(lsh_hits) #index in order???与hits长度有时候不相等 #print(hits) #index in order, exact hits if lsh_hits == hits: correct += 1 #mine copy_lsh_hits = lsh_hits copy_hits = hits #copy_lsh_hits.sort() #copy_hits.sort() print("hits") #write('RE', "hits\n") print(copy_hits) #write('RE', str(copy_hits)+'\n') #print("lsh_hits") #write('RE', "lsh_hits") #print(copy_lsh_hits) #write('RE', str(copy_lsh_hits)) #print(lsh_hits) #precision and recall return_lsh_hits = sorted(copy_lsh_hits, key = lambda x:dot(self.origin_datas[x],self.origin_queries[index_q]), reverse=True) print("return_lsh_hits") #write('RE', "return_lsh_hits\n") print(return_lsh_hits) #write('RE', str(return_lsh_hits)+'\n') """ relevant_seen = 0 for i in range(len(return_lsh_hits)): if return_lsh_hits[i] in set(top_10[index_q]): relevant_seen += 1 precision = float(100 * relevant_seen / (i + 1)) recall = float(100 * relevant_seen / 10) print("precision:" + str(precision) + ":recall:" + str(recall)) #write('RE', "precision:" + str(precision) + ":recall:" + str(recall)+'\n') #print(precision) #print(recall) if 10 == (i+1): break """ """ ip = [] for i in range(self.num_neighbours): ip.append(dot(self.origin_queries, self.origin_datas[copy_hits[i]])) #ip.sort(reverse=True) print("ip") print(ip) lsh_ip = [] for i in range(len(copy_lsh_hits)): lsh_ip.append(dot(self.origin_queries, self.origin_datas[copy_lsh_hits[i]])) #lsh_ip.sort(reverse=True) print("lsh_ip") print(lsh_ip) """ #assert (len(copy_lsh_hits) == len(copy_hits)) #assert (len(copy_hits) == self.num_neighbours) common_set = set(copy_lsh_hits) & set(copy_hits) print('L\tk') #write('RE', 'L\tk\n') print("{0}\t{1}".format(L, k)) #write('RE', "{0}\t{1}".format(L, k)+'\n') #print('precision\t') #write('RE', 'precision') #print("{0}\t".format(len(common_set) / self.num_neighbours)) #print("{0}\t".format(len(common_set) / len(hits))) #write('RE', "{0}\t".format(len(common_set) / len(hits))) print('\n') #write('RE', '\n\n') """ if (len(common_set) / len(hits)) > 0.6: print('L\tk') print("{0}\t{1}".format(L, k)) print('precision\t') # print("{0}\t".format(len(common_set) / self.num_neighbours)) print("{0}\t".format(len(common_set) / len(hits))) else: print('precision\t') # print("{0}\t".format(len(common_set) / self.num_neighbours)) print("{0}\t".format(len(common_set) / len(hits))) if True == gol.get_value('DEBUG'): print ('Queried Sorted Validation:') for j, vd in enumerate(lsh_hits): print (str(q) + '\t->\t' + str(vd)) """ index_q += 1
def selectLanguage(title, languages, select_language, language): if language == "english": write(title[0] + "\n" + languages[0] + "\n" + languages[1] + "\n" + languages[2]) elif language == "spanish": write(title[1] + "\n" + languages[3] + "\n" + languages[4] + "\n" + languages[5]) elif language == "french": write(title[2] + "\n" + languages[6] + "\n" + languages[7] + "\n" + languages[8]) while True: if language == "english": answer = input(write(select_language[0])) elif language == "spanish": answer = input(write(select_language[1])) elif language == "french": answer = input(write(select_language[2])) if answer == "1": write("\nEnglish is set!") database.set_lang("en") main.clear_alt() language = "english" # Set fallback language as English main.menu(main_menu, main_menu_title, language) break elif answer == "2": write("\nSpanish is set!") database.set_lang("es") main.clear_alt() spanish = main_menu_spanish spanish_title = mainMenu_spanish language = "spanish" # Set fallback language as Spanish main.menu(spanish, spanish_title, language) break elif answer == "3": write("\nFrench is set!") database.set_lang("fr") main.clear_alt() french = main_menu_french language = "french" # Set fallback language as French main.menu(french, mainMenu_french, language) break else: if language == "english": write(option_error[0]) main.clear_alt() elif language == "spanish": write(option_error[1]) main.clear_alt() elif language == "french": write(option_error[2]) main.clear_alt()
def askUser(query_question, user_options, language): while True: if language == "english": answer = input( write(user_options[0] + "\n" + user_options[1] + "\n" + user_options[2] + "\n" + "\n" + user_options[3])) elif language == "spanish": answer = input( write(user_options[4] + "\n" + user_options[5] + "\n" + user_options[6] + "\n" + "\n" + user_options[7])) elif language == "french": answer = input( write(user_options[8] + "\n" + user_options[9] + "\n" + user_options[10] + "\n" + "\n" + user_options[11])) if answer == "1": if language == "english": # Fallback language English main.clear_alt() query.search(query_question, language) break elif language == "spanish": # Fallback language Spanish main.clear_alt() query.search(spanish_q, language) break elif language == "french": # Fallback language Spanish main.clear_alt() query.search(french_q, language) break elif answer == "2": if language == "english": main.clear_alt() main.menu(main_menu, main_menu_title, language) break elif language == "spanish": main.clear_alt() main.menu(main_menu_spanish, mainMenu_spanish, language) break elif language == "french": main.clear_alt() main.menu(main_menu_french, mainMenu_french, language) break else: if language == "english": # Fallback language English write(option_error[0]) main.clear_alt() elif language == "spanish": # Fallback language Spanish write(option_error[1]) main.clear_alt() elif language == "french": # Fallback language Spanish write(option_error[2]) main.clear_alt()
def menu(main_menu, main_menu_title, language): write(main_menu[0] + "\n" + main_menu[1] + "\n" + main_menu[2] + "\n" + main_menu[3] + "\n") # If user input something wrong, don't stop the program. while True: answer = input(write("\n" + main_menu_title)) if answer == "1": # Open search function # Check for language fallback. if language == "english": # Fallback language English. main.clear_alt() query.search(query_question[0], language) break elif language == "spanish": # Fallback language Spanish. main.clear_alt() query.search(query_question[1], language) break elif language == "french": # Fallback language French. main.clear_alt() query.search(query_question[2], language) break # Check for language fallback. elif answer == "2": # Open select language menu if language == "english": # Fallback language English main.clear_alt() main.selectLanguage(language_menu_title, languages_list, language_select, language) break elif language == "spanish": # Fallback language Spanish main.clear_alt() main.selectLanguage(language_menu_title, languages_list, language_select, language) break elif language == "french": # Fallback language French main.clear_alt() main.selectLanguage(language_menu_title, languages_list, language_select, language) break elif answer == "3": # Close program if language == "english": write("Goodbye! :)") break elif language == "spanish": write("Adiós! :)") break elif language == "french": write("Au Revoir! :)") break else: if language == "english": write(option_error[0]) main.clear_alt() elif language == "spanish": write(option_error[1]) main.clear_alt() elif language == "french": write(option_error[2]) main.clear_atl()
def search(query_question, language): search = input(write(query_question)) main.clear_alt() try: summary = database.summary(search, sentences=2) + "\n" write(summary) if language == "english": # Fallback language English # Show if and only if language is English question = input( write( "\nWant to hear me dictating this text? 'Y' for yes, 'N' for no.\n" "Note: This is an experimental function and just work in English" "\n? ")) while True: if question.lower() in ("yes", "y"): write( "Dictation is playing, make sure to turn the speakers on!" ) os.system("say '" + summary + "'") write("\nDictation ends.\n") break elif question.lower() in ("no", "n"): break else: write(option_error) elif language == "spanish": pass # Just ignore this statement (NULL) elif language == "french": pass # Just ignore this statement (NULL) # Check for any possible errors. except database.exceptions.DisambiguationError: if language == "english": write("Too much results for this term, please be specific!") elif language == "spanish": write( "Demasiado resultados para este término, ¡por favor sea específico!" ) elif language == "french": write("Trop de résultats pour ce terme, soyez précis!") except database.exceptions.PageError: if language == "english": write("'" + search + "'" + " does not match any page, try again!\n") elif language == "spanish": write( "'" + search + "'" + " no coincide con ninguna página, vuelve a intentarlo!\n") elif language == "french": write( "'" + search + "'" + " no coincide con ninguna página, vuelve a intentarlo!\n") main.clear_alt() query.askUser(query_question, user_options, language)
elif language == "french": # Fallback language Spanish write(option_error[2]) main.clear_alt() ###################################################################### # Entry point to the program if __name__ == "__main__": # Don't allow the user to use the program without internet. while True: if is_connected() == True: write("Welcome to wikipedia console.\n") write( "Here you can search for anything in three languages: English, Spanish, French.\n" ) write("Enjoy :)\n\n") main.menu(main_menu, main_menu_title, language) break else: write( "\nPlease connect to the internet before loading this program") ask_again = input( write( "\nWant to try again? Type 'y' for yes or 'n' for no: \n")) if ask_again.lower() in ("yes", "y"):
def WriteNotePad(self): write(self.filename)
to be careful with the 'w' mode as it will overwrite into the file if it already exists. All previous data are erased. Writing a string or sequence of bytes( for binary files) is done using write() method. This method returns the number of characters written to the file. with open("test.txt", 'w', encoding='utf-8') as f: f.write("my first file\n") f.write("This file\n\n")
def genBoolExpr(tree): global relational_ops global IF_NUMBER global regs #If the three has length 3 and the second element is a relational operator if(len(tree) == 3 and tree[1] in relational_ops): varStart1 = regs.pop() genExpr(tree[0], varStart1) varStart2 = regs.pop() genExpr(tree[2], varStart2) write("\n\tcmp ",varStart1, ", ", varStart2,"\n") write("\t", getJump(tree[1]), " ") #Restore the registers used regs.append(varStart1) regs.append(varStart2) #If the expression is of type !=, etc elif(len(tree)==2 and tree[0] == "!"): genBoolExpr2(tree[1]) register = regs.pop() write("\n\tmovzx ",register, ", al\n") write("not ", register) write("cmp ", register, ", 1") write("\n\tjne ") #Restore the register used regs.append(register) else: genBoolExpr2(tree) register = regs.pop() write("\n\tmovzx ",register, ", al\n") write("cmp ", register, ", 1") write("\n\tjne ") #Restore the registers used regs.append(register)
def main(): t, j = read_test() remove(cat) write("e", j) wrapUp()
def genBoolExpr2(tree): global relational_ops global IF_NUMBER global regs ifNumber = str(IF_NUMBER) IF_NUMBER += IF_NUMBER write("\ncond", ifNumber, ":\n") #If the three has length 3 and the second element is a relational operator if(len(tree) == 3 and tree[1] in relational_ops): varStart1 = regs.pop() genExpr(tree[0], getAllStack(), varStart1) varStart2 = regs.pop() genExpr(tree[2], getAllStack(), varStart2) write("\n\tcmp ",varStart1, ", ", varStart2,"\n") write("\tset",getSet(tree[1]), " al") #Restore the registers used regs.append(varStart1) regs.append(varStart2) elif(len(tree) > 1): if(tree[1] == "&&"): genBoolExpr2(tree[0]) register = regs.pop() write("\n\tmovzx ",register ,", al\n") write("\tcmp ", register, ", 1\n") #If the condition is false, then jump at the end of #the condition with al = 0. write("\n\tjne endCond", ifNumber , "\n") #Restore the register used regs.append(register) genBoolExpr2(tree[2:]) elif(tree[1] == "||"): genBoolExpr2(tree[0]) register = regs.pop() write("\n\tmovzx ",register, ", al\n") write("\tcmp ", register, ", 1\n") #If the condition is true, then jump at the end of #the condition with al = 1. write("\n\tje endCond", ifNumber, "\n") #Restore the register used regs.append(register) genBoolExpr2(tree[2:]) elif(tree[0] == "!"): genBoolExpr2(tree[1:]) register = regs.pop() write("\n\tmovzx ",register, ", al\n") write("not ", register) write("cmp ", register, ", 1") write("\n\tjne endCond", ifNumber , "\n") #Restore the register used regs.append(register) write("\nendCond",ifNumber, ":\n")
def run(self, type, k_vec = [2], l_vec = [2]): if True == gol.get_value('DEBUG'): print ('SimpleAlshTester Run:') try: if 'simple' == type: pass else: raise ValueError except ValueError: print ('SimpleAlshTester: type error: ' + str(type)) return validate_metric = dot compute_metric = L2Lsh.distance #top num_neighbor for each point exact_hits = [[ix for ix, dist in self.linear(q, validate_metric, self.num_neighbours)] for q in self.origin_queries] # top10 for each point top_10 = [[ix for ix, dist in self.linear(q, validate_metric, 10)] for q in self.origin_queries] print ('==============================') write('RE', '==============================\n') print ('SimpleAlshTester ' + type + ' TEST:') write('RE', 'SimpleAlshTester ' + type + ' TEST:\n') #print ('L\tk\tacc\ttouch') # concatenating more hash functions increases selectivity for k in k_vec: lsh = LshWrapper('cosine', self.d, self.rand_range, k, 0) # using more hash tables increases recall for L in l_vec: lsh.resize(L) lsh.index(self.ext_datas) correct = 0 index_q = 0 for q, hits in zip(self.ext_queries, exact_hits): lsh_hits = [ix for ix, dist in lsh.query(q, compute_metric, self.num_neighbours)] if lsh_hits == hits: correct += 1 if True == gol.get_value('DEBUG'): print ('Queried Sorted Validation:') for j, vd in enumerate(lsh_hits): print (str(q) + '\t->\t' + str(vd)) #mine copy_lsh_hits = lsh_hits copy_hits = hits #copy_lsh_hits.sort() #copy_hits.sort() #print("hits") write('RE', "hits\n") #print(copy_hits) write('RE', str(copy_hits)+'\n') #print("lsh_hits") #write('RE', "lsh_hits") #print(copy_lsh_hits) #write('RE', str(copy_lsh_hits)) #print(lsh_hits) #precision and recall return_lsh_hits = sorted(copy_lsh_hits, key = lambda x:dot(self.origin_datas[x],self.origin_queries[index_q]), reverse=True) #print("return_lsh_hits") write('RE', "return_lsh_hits\n") #print(return_lsh_hits) write('RE', str(return_lsh_hits)+'\n') """ relevant_seen = 0 for i in range(len(return_lsh_hits)): if return_lsh_hits[i] in set(top_10[index_q]): relevant_seen += 1 precision = float(100 * relevant_seen / (i + 1)) recall = float(100 * relevant_seen / 10) #print("precision:" + str(precision) + ":recall:" + str(recall)) write('RE', "precision:" + str(precision) + ":recall:" + str(recall)+'\n') #print(precision) #print(recall) if 10 == (i+1): break """ """ ip = [] for i in range(self.num_neighbours): ip.append(dot(self.origin_queries, self.origin_datas[copy_hits[i]])) #ip.sort(reverse=True) print("ip") print(ip) lsh_ip = [] for i in range(len(copy_lsh_hits)): lsh_ip.append(dot(self.origin_queries, self.origin_datas[copy_lsh_hits[i]])) #lsh_ip.sort(reverse=True) print("lsh_ip") print(lsh_ip) """ #assert (len(copy_lsh_hits) == len(copy_hits)) #assert (len(copy_hits) == self.num_neighbours) common_set = set(copy_lsh_hits) & set(copy_hits) #print('L\tk') write('RE', 'L\tk\n') #print("{0}\t{1}".format(L, k)) write('RE', "{0}\t{1}".format(L, k)+'\n') #print('precision\t') #write('RE', 'precision') #print("{0}\t".format(len(common_set) / self.num_neighbours)) #print("{0}\t".format(len(common_set) / len(hits))) #write('RE', "{0}\t".format(len(common_set) / len(hits))) #print('\n') write('RE', '\n\n') """ if (len(common_set) / len(hits)) > 0.6: print('L\tk') print("{0}\t{1}".format(L, k)) print('precision\t') # print("{0}\t".format(len(common_set) / self.num_neighbours)) print("{0}\t".format(len(common_set) / len(hits))) else: print('precision\t') # print("{0}\t".format(len(common_set) / self.num_neighbours)) print("{0}\t".format(len(common_set) / len(hits))) if True == gol.get_value('DEBUG'): print ('Queried Sorted Validation:') for j, vd in enumerate(lsh_hits): print (str(q) + '\t->\t' + str(vd)) """ index_q += 1