def single_from_id(author_num, unigraph, cutoff): """ Takes node id, full graph and cutoff and returns graph with all nodes reachable by source node up to a distance of cutoff """ # make full node id author_id = "http://eprints.gla.ac.uk/view/author/" + author_num + ".html" # Node not found, return error message if author_id not in graph_utils.get_node_set(unigraph): errorMessage = json.dumps({"error": "Sorry, the author was not found"}) return HttpResponse(errorMessage, content_type='application/json') # Make and return single author graph author_graph = graph_utils.single_author_graph(unigraph, author_id, cutoff) graphdata = graph_utils.json_from_graph(author_graph) newdata = json.dumps(graphdata) return HttpResponse(newdata, content_type='application/json')
def longest_from_id(source_num, unigraph): """ Takes the id for a node and a graph and returns a graph of the node's path to its furthest reachable node """ # Make full node id out of the id number source_id = "http://eprints.gla.ac.uk/view/author/" + source_num + ".html" # Check if node in graph, if not return error message if source_id not in graph_utils.get_node_set(unigraph): errorMessage = json.dumps({"error": "Sorry, the author was not found"}) return HttpResponse(errorMessage, content_type='application/json') # Otherwise get longest path and make and return path graph longest_path = graph_utils.get_longest_path(unigraph, source_id) path_graph = graph_utils.make_path_graph(longest_path, unigraph) graphdata = graph_utils.json_from_graph(path_graph) newdata = json.dumps(graphdata) return HttpResponse(newdata, content_type='application/json')
def kw_search(request): """ Deals with keyword searches """ # Get query from request if request.method == 'GET': query = request.GET.get('query') # Paths for the indices path = os.path.join(settings.INDICES_PATH + "\invindex.db") tkw_path = os.path.join(settings.INDICES_PATH + "\paperkwindex.db") # Make search object, passing index paths srch = search.Search(path, tkw_path) # If query in quote marks, do phrase search if query[0] == "\"" and query[-1] == "\"": query = query[1:-1] author_titles = srch.phrase_search(query) # If 'OR' in query, it is an OR search elif 'OR' in query: # Remove the OR so it is not used as a query term q = query.replace('OR', '') author_titles = srch.or_search(q) # Otherwise it is an AND search else: author_titles = srch.and_search(query) unigraph = get_unigraph() # Make the query term graph to display in front end and return term_graph = graph_utils.make_search_graph(query, author_titles, unigraph, 30) graphdata = graph_utils.json_from_graph(term_graph) newdata = json.dumps(graphdata) return HttpResponse(newdata, content_type='application/json')
def path_graph_from_ids(source_num, target_num, unigraph): """ Takes the node ids for source and target and the graph and returns a graph for the path between source and target """ # Use the ids provided to make urls (which are the actual node ids used in the graph) source_id = "http://eprints.gla.ac.uk/view/author/" + source_num + ".html" target_id = "http://eprints.gla.ac.uk/view/author/" + target_num + ".html" # Get the set of nodes in the graph to check for the source and target uni_node_set = graph_utils.get_node_set(unigraph) # Check if source and target in graph, if not return error message if source_id not in uni_node_set and target_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, neither author was found"}) return HttpResponse(errorMessage, content_type='application/json') elif source_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, the source author was not found"}) return HttpResponse(errorMessage, content_type='application/json') elif target_id not in uni_node_set: errorMessage = json.dumps({"error": "Sorry, the target author was not found"}) return HttpResponse(errorMessage, content_type='application/json') # If both were found, get the path betwen them s_path = graph_utils.get_path(unigraph, source_id, target_id) # If s_path is false, no path was found between the authors, return error message if not s_path: errorMessage = json.dumps({"error": "Sorry, no path was found between the authors"}) return HttpResponse(errorMessage, content_type='application/json') # Otherwise make a graph connecting the path between the authors and return path_graph = graph_utils.make_path_graph(s_path, unigraph) graphdata = graph_utils.json_from_graph(path_graph) newdata = json.dumps(graphdata) return HttpResponse(newdata, content_type='application/json')