Exemplo n.º 1
0
def entity():
    
    global graph
    
    uri = request.args.get('uri')

    form = MainForm()
    
    

    facetQuery = "SELECT ?p ?o WHERE { <" + uri + "> ?p ?o . }"
    facetResults = graph.query(facetQuery)

    if not facetResults: 
        facetQuery = "SELECT ?p ?o WHERE { <" + url_fix(uri) + "> ?p ?o . }"
        facetResults = graph.query(facetQuery)
   
   
    facetsentitylist = []
    for fr in facetResults : 
        
        facetproperty = fr[0]
        prefixed_facetproperty = u.get_prefixed(facetproperty)
        facetobject = fr[1]
        prefixed_facetobject = ""
        if facetobject.startswith("http://"):
            prefixed_facetobject = u.get_prefixed(facetobject)


        
        subjects = graph.subjects(facetproperty, facetobject)
        occurrences = sum(1 for _ in subjects)
#         current_app.logger.debug(occurrences)
        
        
        facetsentitylist.append({'facetproperty': facetproperty, 'prefixed_facetproperty': prefixed_facetproperty, 'facetobject': facetobject, 'prefixed_facetobject': prefixed_facetobject, 'occurrences': occurrences})       



    sortedlist = sorted(facetsentitylist, key=lambda k: k['occurrences']) 
    return render_template('base2.html', form=form, uri=uri, facetsentity=sortedlist)
Exemplo n.º 2
0
def work():    
    
    global facetsdict
    global resultsdict
    global graph
    
    form = MainForm()
    dataset = form.endpoints.data
    
    my_var = request.form.get("btn", "None")
    my_var2 = request.form.get("btn2", "None")
    if ((my_var == 'None') and (my_var2 == 'None')): 
        querydoc = ("app/queries/" + dataset + ".q")
        with open ((querydoc), "r") as queryfile:
            q = queryfile.read()
            form.query.data = q
        return render_template('base.html', form=form)
    
    
    if (my_var != 'None'):
        rankingfile = "app/rankings/ranking_" + dataset + ".txt"
        resultsfile = "app/results/results_" + dataset + ".rdf"
    
        if (True): 
#             if (form.datafromendpoint.data): 
           
            
            try:
                sparql = SPARQLWrapper(app.config['ENDPOINTS'].get(dataset))
                sparql.setReturnFormat(JSON)
                sparql.setQuery(form.query.data)
                sparql.setReturnFormat(XML)
                resultfile = open(resultsfile, "wb")
                r = sparql.query().convert()
                r.serialize(destination=resultfile, format="xml")
                resultfile.flush() 
                resultfile.close()
                

            except (urllib2.HTTPError, QueryBadFormed, TypeError, AttributeError), err:
                current_app.logger.debug(str(err))
                error = str(err)
                return render_template('base.html', results=[], url="", form=form, error=error, facets=[])
             

            try:  
                
                global graph
                graph = rdflib.Graph()
                graph.parse(resultsfile)
                resultsdict = u.compute_rankings(graph, rankingfile)
                url = "" 
                current_app.logger.debug(url)

                try: 
                    with open("app/static/similarities_" + dataset + ".json"):
                        url = url_for('.static', filename='similarities_' + dataset + '.json')
                except IOError:
                    url = "" 
                    
                current_app.logger.debug(url)
       
                with open ("app/queries/facets.q", "r") as myresultfile:
                    localQuery = myresultfile.read()
                entityresults = graph.query(localQuery)
                
                
                facetsdict = []
                for row in entityresults:
                    facet = row[0]
                    count = int(row[1])
                    label = u.get_prefixed(facet)
                    facetsdict.append({'facet': facet, 'label': label, 'count': count})            


            except (TypeError, xml.sax.SAXParseException), err:
                current_app.logger.debug(str(err))
                error = str(err)
                return render_template('base.html', results=resultsdict, url=url, form=form, error=error, facets=facetsdict)