def _search(task, search, heuristic): logging.info('Search start: {0}'.format(task.name)) if heuristic: solution = search(task, heuristic) else: solution = search(task) logging.info('Search end: {0}'.format(task.name)) return solution
def sch(): start = Jealous(['A1', 'A2', 'A3', 'C1', 'C2', 'C3'], [], 0) goal = Jealous([], ['A1', 'A2', 'A3', 'C1', 'C2', 'C3'], 1) print('******** Jealous Agents - Depth First Search *******') ans = search(start, goal, 'dfs', 3000, False) print('Number of states visited: ' + str(ans)) print('\n\n******** Jealout Agents - Breadth First Search *******') ans = search(start, goal, 'bfs', 3000, False) print('Number of states visited: ' + str(ans))
def _search(task, search, heuristic, use_preferred_ops=False): logging.info('Search start: {0}'.format(task.name)) if heuristic: if use_preferred_ops: solution = search(task, heuristic, use_preferred_ops) else: solution = search(task, heuristic) else: solution = search(task) logging.info('Search end: {0}'.format(task.name)) return solution
def _search(task, search, heuristic, use_preferred_ops=False): logging.info("Search start: {}".format(task.name)) if heuristic: if use_preferred_ops: solution = search(task, heuristic, use_preferred_ops) else: solution = search(task, heuristic) else: solution = search(task) logging.info("Search end: {}".format(task.name)) return solution
def _search(task, search, heuristic, use_preferred_ops=False): logging.info('Search start: {0}'.format(task.name)) if heuristic: if use_preferred_ops: solution = search(task, heuristic, use_preferred_ops) else: solution = search(task, heuristic) else: global node_data solution, node_data = search(task) logging.info('Search end: {0}'.format(task.name)) return solution
def gameSearch(puzzle): while True: # try: choice = int(input("Enter your choice of algorithm:\n1) Uniform Cost Search\n2) A* with the Misplaced Tile heuristic\n3) A* with the Euclidean distance heuristic\n4) A* with the Manhattan Sergeant distance heuristic\n")) if choice == 1: print("1 Selected") start = time.perf_counter() results,level, maxQueue = search(puzzle, False, False, False) finish = time.perf_counter() outputGame(results) print(f"Time of completion: {finish-start:0.4f}s") print(f"Number of nodes expanded: {level}") print(f"Max number of nodes in queue: {maxQueue}") input("Press Enter to continue...") break elif choice == 2: print("2 Selected") start = time.perf_counter() results, level, maxQueue = search(puzzle, True, False, False) finish = time.perf_counter() outputGame(results) print(f"Time of completion: {finish-start:0.4f}s") print(f"Number of nodes expanded: {level}") print(f"Max number of nodes in queue: {maxQueue}") input("Press Enter to continue...") break elif choice == 3: print("3 Selected") start = time.perf_counter() results,level, maxQueue = search(puzzle, False, True, False) finish = time.perf_counter() outputGame(results) print(f"Time of completion: {finish-start:0.4f}s") print(f"Number of nodes expanded: {level}") print(f"Max number of nodes in queue: {maxQueue}") input("Press Enter to continue...") break elif choice == 4: print("4 Selected") start = time.perf_counter() results,level, maxQueue = search(puzzle, False, False, True) finish = time.perf_counter() outputGame(results) print(f"Time of completion: {finish-start:0.4f}s") print(f"Number of nodes expanded: {level}") print(f"Max number of nodes in queue: {maxQueue}") input("Press Enter to continue...") break # except: # print("Invalid choice!") return
def quick_search(bot, update): """User types his/her query directly (no command) User being rerouted to classic search function, it's just a wrapper really. Args: bot: (object) the bot instance. update: (object) the message sent by the user. """ raw_query = update.message.text # Impossible ? if not raw_query: return else: search(bot, update, raw_query)
def fun1(querry): def get_entry(): # print (ent.get()) global r r = str(ent.get()) root.destroy() root = Tk() large_font = ('Verdana',30) canvas = Canvas(root, width=1000, height=150) canvas.pack() widget = Label(canvas, text=' WEB CRAWLER', fg='black') widget.config(font=("Courier", 30)) widget.pack() canvas.create_window(300, 30, window=widget) var = StringVar(root) ent = Entry(root,textvariable = var,font=large_font,width=32) canvas.create_window(430, 100, window=ent) btn1 = Button(root, text="SEARCH", command=get_entry,height=3,width=10) canvas.create_window(900, 100, window=btn1) result = search(querry) def callback(event): webbrowser.open_new(event.widget.cget("text")) for i in range(len(result)): lbl = Label(root, text=(str(result[i])[2:-3]), fg="blue", cursor="hand2") lbl.pack(fill =X, padx=10,pady=10) lbl.bind("<Button-1>", callback) root.mainloop() return r
def search_results(): search_term = request.form['myform'] process_hash = {} the_flag = 1 if the_flag == 0: results = search_json(search_term) else: results = search(search_term) for each_item in results: for each_value in each_item: process_hash[each_value] = each_item[each_value] display_hash=[] display_hash_view = [ (v,k) for k,v in process_hash.iteritems() ] display_hash_view.sort(reverse=True) for v,k in display_hash_view: display_hash.append(str(filename_hash_list[k])+", Rank :"+str(v)) display_html = "" for each_value in display_hash: hyperlink = each_value.split(',') display_html += "<a target='_blank' href='file:///"+str(hyperlink[0])+"'>"+str(each_value)+"</a><br>" display_html += "</body>" display_html += "</html>" return display_html
def batch(batchFile, vocabulary, grammar, generator): """ Run a batch search, exit on the first error. """ searches = [] fh = open(batchFile, 'r') for line in fh: line = line.strip() if len(line) > 0: searches.append(line) fh.close() if len(searches) == 0: print "No searches to batch :(" sys.exit(1) for searchString in searches: print '-'*80 print searchString.center(80) print '-'* 80 if search(searchString, vocabulary, grammar, generator) != 0: sys.exit(1) print ''
def search_posts(request): posts = None if 'query' in request.GET: posts = search(FilmIdea, request.GET['query']) return redirect("/p/search_results") return render_to_response('search_results.html', {'posts': posts}, context_instance=RequestContext(request))
def plan_to_state(self, state1, state2, algo): ''' orientation is not considered for goal check, this is done since we need to plan only to abstract states which do not differ by orientation ''' action_dict = self.get_successor(state1) action_list, total_nodes_expanded = search(state1, state2, self, algo) return action_list, total_nodes_expanded
def execute(): if len(sys.argv) != 9: print len(sys.argv) raise SystemExit("Please use the following pattern\n") no_of_pddl_files = int(sys.argv[2]) objects_to_vary = sys.argv[4] range_of_variation = int(sys.argv[6]) search_algorithm = sys.argv[8] domain_file = "zenotravelStrips.pddl" problem_template = "pfile1zeno" folder_for_generated_files = "Simple astar_mod 50/" """ Check here if folder already exists to avoid regenerating new problems for new searches try: os.makedirs(folder) except OSError, e: if e.errno == errno.EEXIST: #Folder already exists so delete all its contents pass #cleanUpDirectory(folder) no need to wipe off gen files else: raise """ """ no_of_pddl_files = 5 range_of_variation = 20 #FIXME; if it is city; subtract 3 from this number as there are 3 initial cities; objects_to_vary = "city" """ #Generate samples gen.generator(problem_template, folder_for_generated_files, no_of_pddl_files, objects_to_vary, range_of_variation) #Get data from generated samples gen_dir = os.getcwd() + "/" + folder_for_generated_files processed_count = len([filename for filename in os.listdir(gen_dir) if not filename in [".",".."]]) data = [] counter = 0 for filename in [filename for filename in os.listdir(gen_dir) if not filename in [".",".."]]: problem_file = os.path.join(gen_dir,filename) problem = strips_engine.loadIntoProblem(domain_file, problem_file) count = problem.initial.problem.get_object_type_count(objects_to_vary) node, max_nodes_in_memory, nodes_traversed = search(search_algorithm,problem) space = max_nodes_in_memory time = nodes_traversed result = [count,space,time] data.append(result) counter = counter + 1 left = processed_count - counter print "(%s %s %s %s)" %(str(counter), "pddl files processed ",str(left), "files left") write_results(objects_to_vary,no_of_pddl_files,search_algorithm,range_of_variation,data)
def stats(h, display): # Print stats of astar_search start = time.time() solution = search(h, display) elapsed = time.time() - start print("Length:", len(solution)) print("Time:", elapsed)
def search_ajax(): query = request.args['q'] page = request.args.get('page', 0, type=int) print query, page content, total, time = search(es, query, page) #print 'content: ', content print 'total: ', total print 'time: ', time return jsonify(res=content, total=total, page=page)
def cmd_search(bot, update, args): """**_/search_** command User typed /search command. Either with some search query behind it. Or without any text, in which case we ask her/him to type his/her query. Args: bot: (object) the bot instance. update: (object) the message sent by the user. args: (list) search query typed by the user. """ raw_query = ' '.join(args) if not raw_query: # User typed /search command without any text (args empty) bot.sendMessage(chat_id=update.message.chat_id, text=info_messages['QUERY_MSG']) else: search(bot, update, raw_query)
def searchDistance(cls, start, goal, walls, search, this_problem): # Return the BFS/DFS/USC/Astar distance if (start, goal) not in this_problem.heuristicInfo: problem = cls(start, goal, walls) cost = len(search(problem)) this_problem.heuristicInfo[(start, goal)] = cost this_problem.heuristicInfo[(goal, start)] = cost return cost else: return this_problem.heuristicInfo[(start, goal)]
def searche(): q = request.args.get("q", None) if q is None: return render_template("index_template.html", results=[]) search_result, titles = search(index_db, redis_client, q) results = [{"url": url, "title": titles[url]} for url in search_result] if results == []: return render_template("index_template.html", error="Nothing found") return render_template("index_template.html", results=results)
def get_path(worldname, maze): if worldname == 'dl12World': #pass # your code here path = search(make_maze_successors(maze), maze.start, lambda x: x == maze.goal, False, True) ans = [] for p in path: ans.append(maze.indices_to_point((p[0], p[1]))) return ans else: return [util.Point(0.911250, 0.911250), util.Point(1.721250, 0.506250), util.Point(2.531250, 1.316250), util.Point(1.721250, 1.721250), util.Point(0.911250, 2.126250), util.Point(1.721250, 2.936250), util.Point(2.531250, 2.531250)]
def tweetMessage(): tweets = search(location + ' wildfire ') numTweets = len(tweets) finalMessage = '' for x in range(numTweets): tweetText = str(x + 1) + '. ' + str(tweets[x]) + '\n' finalMessage = finalMessage + tweetText finalMessage = 'Here is what Twitter has to say: \n' + finalMessage # print(finalMessage) return finalMessage
def _search(task, search, heuristic, search_name, max_nodes=INFINITY, use_preferred_ops=False): logging.info('Search start: {0}'.format(task.name)) if heuristic: if use_preferred_ops: solution = search(task, heuristic, use_preferred_ops) else: solution = search(task, heuristic) else: if search_name == "full": solution = search(task, max_nodes) else: solution = search(task) logging.info('Search end: {0}'.format(task.name)) return solution
def menu(email): global connection, cursor mail(email) select = input( "Select what you want to do.\n1.Offer a ride.\n2.Search for rides.\n3.Book members or cancel bookings.\n4.Post ride requests.\n5.Search and delete ride requests.\n6.Quit" ) if select == "1": offer(email, connection, cursor) elif select == "2": search(email, connection, cursor) elif select == "3": book(email, connection, cursor) elif select == "4": post(email, connection, cursor) elif select == "5": search_delete_requests(email, connection, cursor) elif select == "6": welcome_screen() else: print("Invalid input") menu(email) connection.commit() return
def initShape(self): #Add Value tab self.decode = decodeValues(self.heditor) self.search = search(self.heditor) self.goto = goto(self.heditor) self.options = options(self.heditor) self.insertTab(0, self.decode,"Decode") self.insertTab(1, self.search, QIcon(":hex_search.png") ,"Search") self.insertTab(2, self.goto, "Goto") self.insertTab(3, self.options, QIcon(":hex_opt.png"), "Options")
def initShape(self): #Add Value tab self.decode = decodeValues(self.heditor) self.search = search(self.heditor) self.goto = goto(self.heditor) self.options = options(self.heditor) self.insertTab(0, self.decode, "Decode") self.insertTab(1, self.search, QIcon(":hex_search.png"), "Search") self.insertTab(2, self.goto, "Goto") self.insertTab(3, self.options, QIcon(":hex_opt.png"), "Options")
def path(a, b): try: a = airport_code(a) b = airport_code(b) x1 = [] x1 = search(a, b) if x1[0] != True: raise RuntimeError('1') i = [u'延误概率:', str(x1[1])] l = ''.join(i) a = airport_ch(a) b = airport_ch(b) style = Style(page_title="基于echarts库的航班信息可视化系统", title_top="#fff", title_pos="center", width=1200, height=600, background_color="#A9A9A9") data_path = [[a, b]] style_geo = style.add( #title = "基于echarts库的航班信息可视化系统", border_color='#000000', symbol_size='50', is_label_show=True, line_curve=0.2, line_opacity=0.6, legend_text_color="#000000", legend_pos="right", geo_normal_color='#FFFFFF', geo_effect_symbol="plane", geo_effect_color="#800000", geo_effect_symbolsize=15, label_color=['#a6c84c', '#ffa022', '#46bee9'], label_pos="right", label_formatter="{b}", label_text_color="#000000", label_text_size='25', label_text_font='宋体') geolines = GeoLines(u"基于echarts库的航班信息可视化系统", **style.init_style) geolines.add(l, data_path, **style_geo) geolines.render() f = 'render.html' webbrowser.open(f, new=1) except: messagebox.showinfo(u"提示", u"没有找到数据") return 0
def run(): print("Loading the files and preparing the system...") init("./some_files") print("The system is ready.") while True: term = input("Enter your text: \n") while term[-1] != '#': res = search(term) for i in range(len(res)): print(f"{i+1}.", end=" ") res[i].print() term += input(f"\u001b[38;5;28m\x1B[3m{term}\033[0m")
def run(): print("Loading the files and preparing the system...") init("./some") print("The system is ready.", end=" ") while True: term = input("Enter your text:") while term[-1] != '#': res = search(term) for i in range(len(res)): print(f"{i + 1}.", end=" ") res[i].print() term += input(term)
def main(self): params = common.getParameters(sys.argv[2]) mode = params.get('mode') url = urllib.unquote_plus(params['url']) if 'url' in params else None external = 'main' if 'main' in params else None if not external: external = 'usearch' if 'usearch' in params else None if mode == 'play': self.play(url) if mode == 'play_episode': self.play_episode( url, urllib.unquote_plus(params['urlm']), params.get('post_id'), params.get('season_id'), params.get('episode_id'), urllib.unquote_plus(params['title']), params.get('image'), params.get('idt'), urllib.unquote_plus(params['data']) ) if mode == 'show': self.show(url) if mode == 'index': self.index(url, int(params.get('page', 1))) if mode == 'categories': self.categories() if mode == 'sub_categories': self.sub_categories(url) if mode == 'search': search(self, params.get('keyword'), external) if mode == 'history': self.history() if mode == 'collections': self.collections(int(params.get('page', 1))) elif mode is None: self.menu()
def simulate(theta, users, resolution, search_type): graph = [[[0, 1, 10 / resolution, 0, 0], [0, 2, 1 / resolution, 50, 0]], [[1, 3, 1 / resolution, 50, 0], [1, 2, 1 / resolution, theta, 0]], [[2, 3, 10 / resolution, 0, 0]], []] search = search_dict[search_type] all_results = [] mean = users * resolution p = 0.1 e = .0000001 d_list = binomial(mean / p, p, 100000) for k in range(users * resolution): if search_type == 'gm': lower_bound = k upper_bound = int(mean / p) + 1 cond_prob_dist = dist_cond(d_list, lower_bound, upper_bound, e) search_results = search(graph, cond_prob_dist) else: search_results = search(graph) all_results.append(search_results) path = search_results[1] for i in range(len(path) - 1): for j in range(len(graph[path[i]])): if graph[path[i]][j][1] == path[i + 1]: graph[path[i]][j][4] += 1 total_cost = curr_total_cost(graph, resolution) avg_cost = total_cost / users if search_type == 'g': d = min(all_results[-1][0], all_results[-2][0]) return d, avg_cost else: return search_results[0], avg_cost
def test_no_category_with_country_search(self): # get start time start = time.time() # Do Method operations result = search(keyword, 'us') # calculate duration dur = time.time() - start # See if operation works self.assertEqual(result['status'], 'ok') time_readout(inspect.stack()[0][0].f_code.co_name, dur, result['totalResults'])
def testHighlightSearch(self): fileContents = [ 'this is a sentence.\nSentences verbalize a thought.\nI like the letter a.\n', 'a a\na\n' ] fileNames = ['file1', 'file2'] hl = [[[0, 10], [20, 45]], []] output = [[ 'file1', ['a', 'this is a ', 0, 8, 8], ['a', '... ize a tho ...', 1, 8, 8] ]] assert search( fileContents, fileNames, 'a', 10, highlight=hl ) == output, 'search for [a] with highlights: incorrect output.'
def testSearchTerm(self): fileContents = [ 'this is a sentence.\nSentences verbalize a thought.\nI like the letter a.\n', 'a a\na\n' ] fileNames = ['file1', 'file2'] output = [[ 'file1', ['a', '... is a sen ...', 0, 8, 8], ['a', '... ize a tho ...', 1, 8, 8], ['a', '... letter a.', 2, 11, 11] ], [ 'file2', ['a', 'a a', 0, 0, 0], ['a', 'a a', 0, 2, 2], ['a', 'a', 1, 0, 0] ]] assert search(fileContents, fileNames, 'a', 10), 'search for [a]: incorrect output.'
def main(fileName): # make the 2-dimensional list into ndarray matrix dataMatrix = np.array(readData(fileName)) # construct kd tree root = buildKdTree(dataMatrix) # give the query query = np.array([8, 3]) # minDis: negative infinity minDis = float("inf") # initialize the result as None result = [] return search(root, query, result, minDis)
def index(results=None): if 'username' not in session.keys(): flash("You need to be logged in to access this page.") return redirect(url_for('login')) if request.method == 'POST': form_data = request.form results = search(form_data) keyphrase = request.form['keyphrase'] limit = request.form['limit'] else: keyphrase = "" limit = 10 if results: for result in results: result['id_string'] = "{:0>6d}".format(result['id']) else: results = [] return render_template('index.html', keyphrase=keyphrase, limit=limit, results=results, results_len=len(results))
def computer_move(): possible_moves = [] with chess.polyglot.open_reader("data/Performance.bin") as reader: for entry in reader.find_all(board): #print(entry.move, entry.weight, entry.learn) possible_moves.append(entry.move) # play book move # play book move if possible if len(possible_moves) > 0: ind = random.randint(0, len(possible_moves) - 1) move = possible_moves[ind] board.push(move) svg = chess.svg.board(board=board) update_svg(svg) # otherwise search the best move else: start_time = time.time() max_depth = 5 # iterative deepening #for d in range(1, depth_+1): bestval, pv_moves = search(board, -9999999, 9999999, depth=max_depth, ply=0, max_depth=max_depth, null_move=False) print(pv_moves) print("--- %s seconds ---" % (time.time() - start_time)) #print(pv.PVlist) #print(len(flatten(pv.PVlist))) board.push(pv_moves[0]) #print(bestval) svg = chess.svg.board(board=board) update_svg(svg) if (board.is_game_over()): print("GAME OVER!")
def home(): try: text = "Welcome to homepage " + session.get("username") if request.method == 'GET': searchType = ['ISBN', 'Title', 'Auther'] try: return render_template("userhome.html", text=text, searchType=searchType) except: return render_template( "registration.html", text="Please enter valid username and password") else: results, message = search(request.form["searchType"], request.form["search"]) return render_template('userhome.html', books=results, message=message) except: return render_template("registration.html", text="Please Login")
def api_search(): if request.is_json: tokens = request.get_json() searchType = tokens["type"].strip() if "type" in tokens and "search" in tokens and searchType in [ 'ISBN', 'Title', 'Author' ]: searchQuery = tokens['search'].strip() results, message = search(searchType, searchQuery) if len(message) == 0: lis = [] for result in results: b = {} b["ISBN"] = result.isbn lis.append(b) resultJson = {} resultJson['Books'] = lis return jsonify(resultJson) return (jsonify({"Error": message}), 400) return (jsonify({"Error": "Invaiid Request"}), 400) else: return (jsonify({"Error": "Invaiid Request"}), 400)
def solve(instance, algorithm, heuristic, start, goal): instance.set_goal(goal) if algorithm == 'a-star': if heuristic is None: throw_error("heuristic is mandatory for a-star") elif heuristic == 'manhattan': return search(instance, start, AStarFringe(ManhattanDistanceHeuristic(goal))) elif heuristic == 'octile': return search(instance, start, AStarFringe(OctileDistanceHeuristic(goal))) else: throw_error("invalid heuristic") elif algorithm == 'best-first': return search(instance, start, BestFirstFringe(ManhattanDistanceHeuristic(goal))) elif algorithm == 'uniform-cost': return search(instance, start, UniformCostFringe()) elif algorithm == 'limited-depth-first': return search(instance, start, LimitedDepthFirstFringe(float(heuristic))) elif algorithm == 'iterative-deepening': limit = 0 fringe = LimitedDepthFirstFringe(limit) while True: try: return search(instance, start, fringe) except SolutionNotFoundError as s: if fringe.filtered_out: fringe = s.fringe fringe.limit += 0.5 fringe.initialized = False fringe.init(list(reversed(fringe.filtered_out))) else: fringe = LimitedDepthFirstFringe(limit) continue except KeyboardInterrupt as ki: print("ids stopped at limit %d" % limit) raise ki else: throw_error("invalid algorithm")
def main(): #get 8 puzzle print("Enter an 8puzzle") r1 = input() r2 = input() r3 = input() #split input into seperate numbers r1 = r1.split() r2 = r2.split() r3 = r3.split() r1 = r1 + r2 + r3 #make all into int r1 = list(map(int, r1)) r1 = node(r1) print (""""Enter the type of search (0) Uniform cost (1) A* with misplaced tile heuristic (2) A* with manhattan distance """) s = input() if search(r1, int(s)): print("solution found") else: print("no solution")
def search_posts(request): posts = None if "query" in request.GET: posts = search(FilmIdea, request.GET["query"]) return redirect("/p/search_results") return render_to_response("search_results.html", {"posts": posts}, context_instance=RequestContext(request))
# Create your views here. from django.template import Context, loader from django.http import HttpResponse, HttpResponseNotAllowed, HttpResponseRedirect, HttpResponseBadRequest, Http404 from django.contrib.admin import site as adminsite from search import * import re i = 0 lines = open("search/MetadataDump_flattened").readlines() points = [ def search(request): t = loader.get_template('search2.html') c = Context({}) return HttpResponse(t.render(c)) def getresultsStatus(request): global i global lines #f = open("temp-" + str(i),"w") #f.write(str(request)) i += 1 parts = str(request).split(',') result = [ ] for part in parts: if "POST" not in part: continue #f.write(" Number of lines in read file : " + str(len(lines))) query = part.strip().split('\'')[1].strip()
def main(file_dir): ''' Prerequisites: You must purchase the Web of Knowledge subscription from Thomson Reuters. SID numbers must be passed in from auth.py Template filename must be in the following format. "companyname".xlsx Main directory with patent numbers is folder_dir = '/Users/James/Documents/'. Opens the excel file that contains source patent numbers and examines the backward citing patents for each source patent. The backward citing patents' derwent technological classification numbers and their respective counts will be recorded in the database. ''' conn = sqlite3.connect("patent_database.db") #creates a patent database. cursor = conn.cursor() print "Start Time: ", time.strftime('%X %x %Z') #Start a counter here so you can renew your SID when 3.5 hours have passed. comp_name = file_dir patent_dict = xl_in(comp_name, 10, "") # insert "" for a full list. # note that when importing a raw dataset, you must change patent_dict to raw_xl_in()[0] since # the output is a tuple: (patent_dict, citing_pat_dict) # patents in the xl file should not have a hyphen as the input module adds them. sid_list = authlist(3, 'up', 1, 2) SID = sid_list #You may hard-code the SID list as such: SID = ['SID1', 'SID2'] patent_list = patent_dict.keys() print 'Number of patents', len(patent_list) print 'Estimated Time: ', (len(patent_list) * 0.35), 'minutes' max_count = 500 i = 0 cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name="+'"'+comp_name+'"') # check if the database table exists. if len(cursor.fetchall()) == 0: # if the database table does not exist, then make one. create_table="CREATE TABLE"+'"' + comp_name + '"'+ "(patent_number text, derwent_code text, derwent_count float)" cursor.execute(create_table) # creates a table... cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name = 'invalid_pats'") # check if the database table exists. if len(cursor.fetchall()) == 0: # if the invalid table does not exist, then make one. invalids="CREATE TABLE"+'invalid_pats'+ "(comp_name text, patent_number text)" cursor.execute(invalids) # creates a table... for patent_no in patent_list: cursor.execute("SELECT patent_number FROM " + comp_name) rawpat_db = cursor.fetchall() patents_in_db = [] for pat in rawpat_db: patents_in_db.append(pat[0]) if patent_no not in patents_in_db: i = i + 1 #initiate the counter. print patent_no, 'Count: ', i throttle_time = 0.2 try: search_result = search('PN', str(patent_no), 3, 5, SID[0]) #SID[0] except: conn.close() break output_dict = get_ids_of_cited_patents(search_result) if output_dict == None: db_insert="insert into "+'invalid_pats'+ " ('comp_name', 'patent_number') values "+\ "('" + comp_name + "' ,'" + patent_no + "')" cursor.execute(db_insert) else: uid_source_pat = output_dict.keys()[0] cited_uid_list = output_dict[uid_source_pat] number_cited = len(cited_uid_list) print 'number cited', number_cited if number_cited > max_count: #handles the case in which there are too many cited patents. #In this case, the script sends a second request to collect the remaining counts. derwent_count_loop = {} # initializes the output for num_loops in range(1, (number_cited // max_count) + 1): SID_counter = num_loops % len(SID) #SID[(num_loops % 2)] alternates between 0 and 1 to avoid being throttled out. start_record = ((num_loops - 1) * max_count) + 1 try: cited_search_result = retrieve_by_id(cited_uid_list, 3, start_record, max_count, SID[SID_counter]) except: conn.close() break iter_derwent_counts = get_derwent_counts(cited_search_result) derwent_count_loop = aggregate(derwent_count_loop, iter_derwent_counts) start_record = int(number_cited / max_count) * max_count + 1 remainder_count = number_cited % max_count try: remainder_search_result = retrieve_by_id(cited_uid_list, 3, start_record, remainder_count, SID[0]) # handles the remaining cited patents except: conn.close() break remainder_derwent_counts = get_derwent_counts(remainder_search_result) derwent_counts = aggregate(derwent_count_loop, remainder_derwent_counts) elif number_cited != 0: try: cited_search_result = retrieve_by_id(cited_uid_list, 3, 1, number_cited, SID[0]) #SID[] except: conn.close() break derwent_counts = get_derwent_counts(cited_search_result) if number_cited < 5: time.sleep(throttle_time) if number_cited == 0: final_output = {0:0} #no cited patents elif derwent_counts == {}: final_output = {'parse error':0} else: final_output = check_for_r(derwent_counts) #creates a dict without an R class code. # sample final output: {derwentcode1: count, derwentcode2: count...} counts are floats. for derwent_code in final_output.keys(): db_insert="insert into "+'"'+ comp_name +'"'+ " ('patent_number', 'derwent_code', 'derwent_count') values "+\ "('" + patent_no + "' ,'" + derwent_code + "' ,'" + final_output[derwent_code] + "')" cursor.execute(db_insert) conn.commit() print "End Time: ", time.strftime('%X %x %Z')
def wikipedia(term, last=False): global wikiuri if not '%' in term: if isinstance(term, unicode): t = term.encode('utf-8') else: t = term q = urllib.quote(t) u = wikiuri % q bytes = web.get(u) else: bytes = web.get(wikiuri % term) bytes = r_tr.sub('', bytes) if not last: r = r_redirect.search(bytes[:4096]) if r: term = urllib.unquote(r.group(1)) return wikipedia(term, last=True) paragraphs = r_paragraph.findall(bytes) if not paragraphs: if not last: term = search(term) return wikipedia(term, last=True) return None # Pre-process paragraphs = [para for para in paragraphs if (para and 'technical limitations' not in para and 'window.showTocToggle' not in para and 'Deletion_policy' not in para and 'Template:AfD_footer' not in para and not (para.startswith('<p><i>') and para.endswith('</i></p>')) and not 'disambiguation)"' in para) and not '(images and media)' in para and not 'This article contains a' in para and not 'id="coordinates"' in para and not 'class="thumb' in para] # and not 'style="display:none"' in para] for i, para in enumerate(paragraphs): para = para.replace('<sup>', '|') para = para.replace('</sup>', '|') paragraphs[i] = text(para).strip() # Post-process paragraphs = [para for para in paragraphs if (para and not (para.endswith(':') and len(para) < 150))] para = text(paragraphs[0]) m = r_sentence.match(para) if not m: if not last: term = search(term) return wikipedia(term, last=True) return None sentence = m.group(0) maxlength = 275 if len(sentence) > maxlength: sentence = sentence[:maxlength] words = sentence[:-5].split(' ') words.pop() sentence = ' '.join(words) + ' [...]' if (('using the Article Wizard if you wish' in sentence) or ('or add a request for it' in sentence)): if not last: term = search(term) return wikipedia(term, last=True) return None sentence = '"' + sentence.replace('"', "'") + '"' sentence = sentence.decode('utf-8').encode('utf-8') wikiuri = wikiuri.decode('utf-8').encode('utf-8') term = term.decode('utf-8').encode('utf-8') return sentence + ' - ' + (wikiuri % term)
def test_search_class(self): results = search('Brachinite') # print (len(results)) assert len(results) == 2
((3,2,[5,4,3,2,1,0]), 15)] smallCases = [((3,3,[1,3,2,5,4,6,7,8,0]), 20), ((3,3,[1,2,5,4,0,8,3,6,7]), 8)] slowSearches = [("ucs",search.ucs),("ids",search.ids)] fastSearches = [("astar", search.astar), ("idastar", search.idastar)] if __name__ == '__main__': success = True for (name,search) in slowSearches: print "Testing ", name for ((w,h,initial),solLen) in smallCases: (unused, size, sol) = search(w,h,initial[:]) if (size > solLen): success = False print "Search ", name, " Path to long" print sol if (not checkMove(initial[:], sol, w)): sucess = False print "Search ", name, " solution incorrect" for (name,search) in fastSearches: print "Testing ", name for ((w,h,initial),solLen) in smallCases + bigCases: (unused, size, sol) = search(w,h,initial[:]) print sol if (size > solLen): success = False print "Search ", name, " Path to long"
def wikipedia(term, language="en", last=False): global wikiuri if not "%" in term: if isinstance(term, unicode): t = term.encode("utf-8") else: t = term q = urllib.quote(t) u = wikiuri % (language, q) bytes = web.get(u) else: bytes = web.get(wikiuri % (language, term)) if bytes.startswith("\x1f\x8b\x08\x00\x00\x00\x00\x00"): f = StringIO.StringIO(bytes) f.seek(0) gzip_file = gzip.GzipFile(fileobj=f) bytes = gzip_file.read() gzip_file.close() f.close() bytes = r_tr.sub("", bytes) if not last: r = r_redirect.search(bytes[:4096]) if r: term = urllib.unquote(r.group(1)) return wikipedia(term, language=language, last=True) paragraphs = r_paragraph.findall(bytes) if not paragraphs: if not last: term = search(term) return wikipedia(term, language=language, last=True) return None # Pre-process paragraphs = [ para for para in paragraphs if ( para and "technical limitations" not in para and "window.showTocToggle" not in para and "Deletion_policy" not in para and "Template:AfD_footer" not in para and not (para.startswith("<p><i>") and para.endswith("</i></p>")) and not 'disambiguation)"' in para ) and not "(images and media)" in para and not "This article contains a" in para and not 'id="coordinates"' in para and not 'class="thumb' in para ] # and not 'style="display:none"' in para] for i, para in enumerate(paragraphs): para = para.replace("<sup>", "|") para = para.replace("</sup>", "|") paragraphs[i] = text(para).strip() # Post-process paragraphs = [para for para in paragraphs if (para and not (para.endswith(":") and len(para) < 150))] para = text(paragraphs[0]) m = r_sentence.match(para) if not m: if not last: term = search(term) return wikipedia(term, language=language, last=True) return None sentence = m.group(0) maxlength = 275 if len(sentence) > maxlength: sentence = sentence[:maxlength] words = sentence[:-5].split(" ") words.pop() sentence = " ".join(words) + " [...]" if ( ("using the Article Wizard if you wish" in sentence) or ("or add a request for it" in sentence) or ("in existing articles" in sentence) ): if not last: term = search(term) return wikipedia(term, language=language, last=True) return None sentence = '"' + sentence.replace('"', "'") + '"' sentence = sentence.decode("utf-8").encode("utf-8") wikiuri = wikiuri.decode("utf-8").encode("utf-8") term = term.decode("utf-8").encode("utf-8") return sentence + " -- " + (wikiuri % (language, term))
usage() sys.exit() elif o in ('-b', '--batch'): batchFile = a elif o in ('-s', '--search'): searchString = a.strip() elif o in ('-l', '--lookup'): lookupString = a.strip() elif o in ('-d', '--debug'): set_debug(True) else: assert False, "unhandled option" if (searchString == None or len(searchString) == 0) and (lookupString == None or len(lookupString) == 0) \ and (batchFile == None or len(batchFile) == 0): usage() sys.exit(1) debug('Initializing...') vocabulary, grammar, generator = config.configure() if lookupString != None and len(lookupString) > 0: sys.exit(lookup(lookupString, vocabulary)) elif batchFile != None and len(batchFile) > 0: batch(batchFile, vocabulary, grammar, generator) else: sys.exit(search(searchString, vocabulary, grammar, generator)) if __name__ == '__main__': main()
def test_search_countries(self): results = search('Franch') # print (len(results)) assert len(results) == 2
# Scrape Genius.com to retrieve lyrics title=raw_data[0] artist= raw_data[1] progression = raw_data[4] # To be used to display the right portion of the screen search_terms = artist + " " + title if song_playing != search_terms: # To avoid looking for lyrics everytime we store them offline in a textfile FILENAME = "offline/"+search_terms+".txt" if os.path.isfile(FILENAME): with open(FILENAME, 'r') as file: print file.read() else: client_id, client_secret, client_access_token = load_credentials() song_url = search(search_terms, client_access_token) response = requests.get(song_url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'}) soup = BeautifulSoup(response.text, "lxml") html = soup.find(class_="lyrics").find_all('p') # Sometimes, there is something above or below the lyrics # We retrieve the longest string as we can guess it's the lyrics index_maxlen = 0 tmp = 0 for index in range(len(html)): if len(html) > tmp: tmp = len(html[index].get_text()) index_maxlen = index lyrics = html[index_maxlen].get_text() print lyrics with codecs.open(FILENAME, 'ab', encoding='utf8') as file: file.write(lyrics)
parser = argparse.ArgumentParser() parser.add_argument('-r', '--roll_number', help='Roll number', required=True) parser.add_argument('-s', '--save', help='Save data', action='store_true') parser.add_argument('-l', '--load', help='Search and load data', action='store_true') args = parser.parse_args() rollNumber = args.roll_number.upper() toSave = False if args.save: toSave = True toLoad = False if args.load: toLoad = True details = search(DATA_DETAILS_PATH, rollNumber) if details is None: print "Sorry. The content is not available offline." sys.exit() else: details = getDetails(rollNumber) imagePath = getImage(DATA_IMAGE_PATH, rollNumber) details['__image_path__'] = imagePath # Displaying student details and showing image displayData(details) if not toLoad and toSave: # Saving details to json file outFilePath = DATA_DETAILS_PATH + os.sep + rollNumber + ".json"
def wikipedia(term, language='en', last=False): global wikiuri if not '%' in term: if isinstance(term, unicode): t = term.encode('utf-8') else: t = term q = urllib.quote(t) u = wikiuri % (language, q) bytes = str(urllib2.urlopen(u).read()) else: bytes = str(urllib2.urlopen(wikiuri % (language, term))) if bytes.startswith('\x1f\x8b\x08\x00\x00\x00\x00\x00'): f = StringIO.StringIO(bytes) f.seek(0) gzip_file = gzip.GzipFile(fileobj=f) bytes = gzip_file.read() gzip_file.close() f.close() bytes = r_tr.sub('', bytes) if not last: r = r_redirect.search(bytes[:4096]) if r: term = urllib.unquote(r.group(1)) return wikipedia(term, language=language, last=True) paragraphs = r_paragraph.findall(bytes) if not paragraphs: if not last: term = search(term) return wikipedia(term, language=language, last=True) return None # Pre-process paragraphs = [para for para in paragraphs if (para and 'technical limitations' not in para and 'window.showTocToggle' not in para and 'Deletion_policy' not in para and 'Template:AfD_footer' not in para and not (para.startswith('<p><i>') and para.endswith('</i></p>')) and not 'disambiguation)"' in para) and not '(images and media)' in para and not 'This article contains a' in para and not 'id="coordinates"' in para and not 'class="thumb' in para] # and not 'style="display:none"' in para] for i, para in enumerate(paragraphs): para = para.replace('<sup>', '|') para = para.replace('</sup>', '|') paragraphs[i] = text(para).strip() # Post-process paragraphs = [para for para in paragraphs if (para and not (para.endswith(':') and len(para) < 150))] para = text(paragraphs[0]) m = r_sentence.match(para) if not m: if not last: term = search(term) return wikipedia(term, language=language, last=True) return None sentence = m.group(0) maxlength = 275 if len(sentence) > maxlength: sentence = sentence[:maxlength] words = sentence[:-5].split(' ') words.pop() sentence = ' '.join(words) + ' [...]' if (('using the Article Wizard if you wish' in sentence) or ('or add a request for it' in sentence) or ('in existing articles' in sentence)): if not last: term = search(term) return wikipedia(term, language=language, last=True) return None # Return only the sentence if the global variable is set to True # I'm certain this isn't the right way to do this... if sentenceOnly is True: return sentence sentence = '"' + sentence.replace('"', "'") + '"' sentence = sentence.decode('utf-8').encode('utf-8') wikiuri = wikiuri.decode('utf-8').encode('utf-8') term = term.decode('utf-8').encode('utf-8') return sentence + ' - ' + (wikiuri % (language, term))
def ubuntu_fr(term, last=False): global ubuntu_uri if not '%' in term: if isinstance(term, unicode): t = term.encode('utf-8') else: t = term q = urllib.quote(t) u = ubuntu_uri % (q) bytes = web.get(u) else: bytes = web.get(ubuntu_uri % (term)) if bytes.startswith('\x1f\x8b\x08\x00\x00\x00\x00\x00'): f = StringIO.StringIO(bytes) f.seek(0) gzip_file = gzip.GzipFile(fileobj=f) bytes = gzip_file.read() gzip_file.close() f.close() bytes = r_tr.sub('', bytes) if not last: r = r_redirect.search(bytes[:4096]) if r: term = urllib.unquote(r.group(1)) return ubuntu_fr(term, last=True) paragraphs = r_paragraph.findall(bytes) if not paragraphs: if not last: term = search(term) return ubuntu_fr(term, last=True) return None # Pre-process paragraphs = [para for para in paragraphs if (para and 'technical limitations' not in para and 'window.showTocToggle' not in para and 'Deletion_policy' not in para and 'Template:AfD_footer' not in para and not (para.startswith('<p><i>') and para.endswith('</i></p>')) and not 'disambiguation)"' in para) and not '(images and media)' in para and not 'This article contains a' in para and not 'id="coordinates"' in para and not 'class="thumb' in para] # and not 'style="display:none"' in para] for i, para in enumerate(paragraphs): para = para.replace('<sup>', '|') para = para.replace('</sup>', '|') paragraphs[i] = text(para).strip() # Post-process paragraphs = [para for para in paragraphs if (para and not (para.endswith(':') and len(para) < 150))] para = text(paragraphs[0]) m = r_sentence.match(para) if not m: if not last: term = search(term) return ubuntu_fr(term, last=True) return None sentence = m.group(0) maxlength = 275 if len(sentence) > maxlength: sentence = sentence[:maxlength] words = sentence[:-5].split(' ') words.pop() sentence = ' '.join(words) + ' [...]' if (('Vous avez suivi un lien vers une page qui n\'existe pas encore.' in sentence) or ('Créer cette page' in sentence)): if not last: term = search(term) return ubuntu_fr(term, last=True) return None sentence = '"' + sentence.replace('"', "'") + '"' sentence = sentence.decode('utf-8').encode('utf-8') ubuntu_uri = ubuntu_uri.decode('utf-8').encode('utf-8') term = term.decode('utf-8').encode('utf-8') return sentence + ' - ' + (ubuntu_uri % (term))
def test_search_meteor(self): results = search('Aachen') # print (len(results)) assert len(results) == 2