Ejemplo n.º 1
0
def definizioneSkos(risultato, oggetto):
    flag = "false"
    if (flag == "false"):
	sparql = SPARQLWrapper("http://localhost:3030/data/update")
        query = prefix
        oggetto = oggetto.replace("_", " ")
        query += """
        INSERT DATA{
          """ + risultato + """ a skos:concept;
          rdfs:label \"""" + oggetto + """\"^^xsd:string.
        }"""
        sparql.setQuery(query)
        sparql.method = 'POST'
        sparql.query()
Ejemplo n.º 2
0
def getLuogo():
    sparql = SPARQLWrapper("http://localhost:3030/data/query")	#il mio endpoint
    sparql.setQuery("""
	PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
	PREFIX  ao: <http://vitali.web.cs.unibo.it/AnnOtaria/>
    	PREFIX  oa: <http://www.w3.org/ns/oa#>
	PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
	    
	    SELECT DISTINCT (COALESCE(?label,?object) AS ?risultato)
	    WHERE{
                ?annotation a oa:Annotation;
                ao:type ?type;
                oa:hasBody ?body.
                FILTER REGEX(?type, "denotesPlace", "i").
                ?body a rdf:Statement;
                rdf:object ?object.
                OPTIONAL{?body rdfs:label ?label}
	    }
	    ORDER BY (?risultato)
    """)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results = results['results']['bindings']
    results = json.dumps(results)

    return results
Ejemplo n.º 3
0
def info(autore):

    sparql = SPARQLWrapper("http://localhost:3030/data/query")
    sparql.setQuery("""

    PREFIX  oa: <http://www.w3.org/ns/oa#>
    PREFIX  ao: <http://vitali.web.cs.unibo.it/AnnOtaria/>
    PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX  aon: <http://vitali.web.cs.unibo.it/AnnOtaria/annotation/>
    PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
    PREFIX  schema: <http://schema.org/>

    SELECT ?author (COALESCE(?name, "Nome non disponibile") AS ?annotator) (COALESCE(?email, "Email non disponibile") AS ?mail)
    WHERE{
    OPTIONAL{ 
              ?author foaf:name ?name;
              schema:email ?email.
              FILTER regex(str(?author),  \"""" + autore + """\", "i")
            }
    }
    LIMIT 1
    """)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results = results['results']['bindings']
    results = json.dumps(results)
    

    return results
Ejemplo n.º 4
0
def getAnnotation(url):
    
    sparql = SPARQLWrapper("http://localhost:3030/data/query")
    sparql.setQuery("""

    PREFIX  oa: <http://www.w3.org/ns/oa#>
    PREFIX  ao: <http://vitali.web.cs.unibo.it/AnnOtaria/>
    PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX  aon: <http://vitali.web.cs.unibo.it/AnnOtaria/annotation/>
    PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
    PREFIX  schema: <http://schema.org/>

    SELECT DISTINCT ?type ?timestamp ?author ?target ?object (COALESCE(?label1, "Label non disponibile") AS ?label) (COALESCE(?name, "Name non disponibile") AS ?annotator) (COALESCE(?email, "Email non disponibile") AS ?mail)
    WHERE{
    ?ann a oa:Annotation;
    ao:type ?type;
    oa:annotatedAt ?timestamp;
    oa:annotatedBy ?author;
    oa:hasTarget ?target.
    FILTER regex(str(?target), \"""" + url + """\", "i").
    ?ann oa:hasBody ?body.
    ?body a rdf:Statement;
    rdf:object ?object.
    OPTIONAL {?body rdfs:label ?label1}.
    }
    ORDER BY DESC(?timestamp)

    """)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results = results['results']['bindings']
    results = json.dumps(results)

    return results
Ejemplo n.º 5
0
def registraAnnotatore(author, mail):
    sparql = SPARQLWrapper("http://localhost:3030/data/update")  
    query = prefix

    #Name dev'essere parsato con 1 solo space.
    #Caso in cui il dato provenga da riempiOggetto.
    autoreSingleSpace = author.replace("_", " ")
    words = autoreSingleSpace.split()
    autoreSingleSpace = ""
    counter = 0
    for word in words:
        if(counter > 0):
            autoreSingleSpace += " "
        autoreSingleSpace += word
        counter += 1

    #Caso in cui si tratta di un inserimento di un annotazione autore
    if(mail  == "false"):
	aop = 'aop:' + author
        query += """
            INSERT DATA{
              """ + aop + """ a foaf:Person;
              foaf:name \"""" + autoreSingleSpace + """\"^^xsd:string.
            }"""
    else:
       #Caso Normale, i dati vengono da JS
       autoreParsed = author.replace(" ", "_")
       aop = 'aop:' + autoreParsed

       query += """
            INSERT DATA{
              """ + aop + """ a foaf:Person;
              foaf:name \"""" + autoreSingleSpace + """\"^^xsd:string;
              schema:email  \"""" + mail + """\"^^xsd:string.
            }"""
    sparql.setQuery(query)
    sparql.method = 'POST'
    sparql.query()
    return query
Ejemplo n.º 6
0
def getDpPedia(tipo):
    sparql = SPARQLWrapper("http://it.dbpedia.org/sparql/")
    sparql.setQuery("""
	PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>

	SELECT ?uri ?label WHERE {
 		?uri rdfs:label ?label .
 		FILTER REGEX(?label, \"""" + tipo + """\", \"i")
	} ORDER BY  ?label
	LIMIT 20""")
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results = results['results']['bindings']
    results = json.dumps(results)

    return results
Ejemplo n.º 7
0
def getAutore():
	sparql = SPARQLWrapper("http://localhost:3030/data/query")
	sparql.setQuery("""
        PREFIX  oa: <http://www.w3.org/ns/oa#>

        SELECT DISTINCT ?person
        WHERE{
        ?ann a oa:Annotation;
        oa:annotatedBy ?person.
        }
        ORDER BY DESC(?person)
	""")
	sparql.setReturnFormat(JSON)
	results = sparql.query().convert()
	results = results['results']['bindings']
	results = json.dumps(results)

	return results
Ejemplo n.º 8
0
def salvatriple2():
    annotazione = request.json
    sparql = SPARQLWrapper("http://localhost:3030/data/update")
    query = ""
    insert = "INSERT DATA {"
    for annRow in annotazione:
        # flag = annRow["discriminant"]
        if (annRow["label"] == "persona"): #  or annRow["label"] == "luogo" or annRow["label"] == "malattia" or annRow["label"] == "argomento" or annRow["label"] == "DBPedia" or annRow["label"] == "chiarezza" or annRow["label"] == "Originalita" or annRow["label"] == "Presentazione" or annRow["label"] == "Citazione" or annRow["label"] == "Commento" and flag == "fragment"):
            if(annRow["label"] == "Citazione"):
                myObject = annRow["body"]["urlCitazione"]
                myLabel = annRow["body"]["titleCitazione"]
            else:
                myObject = annRow["body"]["oggetto"]
                myLabel = annRow["body"]["label"]  
            intestazione = """
            [  a oa:Annotation;
               rdfs:label "{label}";
               ao:type "{tipo}";    
               oa:annotatedAt "{provenance}";
               oa:annotatedBy  aop:{autore};
            """.format(label = annRow["label"], tipo = annRow["type"], provenance = annRow["provenance"]["time"], autore = annRow["provenance"]["author"].encode('utf-8').replace(" ", "_"))
            body = """
               oa:hasBody [ a   rdf:Statement;
                                rdf:object      {oggetto};
                                rdf:predicate   {predicate};
                                rdf:subject     <ao:{subjectBody}>;
                                rdfs:label  "{reslabel}"
                           ];
            """.format(oggetto = riempiOggetto(annRow["type"], myObject).encode('utf-8'), predicate = annRow["body"]["predicate"], subjectBody = annRow["body"]["subjectBody"], reslabel = myLabel.encode('utf-8'))
            target = """  oa:hasTarget  [ a  oa:SpecificResource ;
                                          oa:hasSelector  [ a   oa:FragmentSelector;
                                                            rdf:value  "{value}";
                                                            oa:end    "{end}";
                                                            oa:start   "{start}"
                                                          ];
                                          oa:hasSource    ao:{source}
                                        ]
                          ]. """.format(value = annRow["target"]["start_id"], end = annRow["target"]["end_off"], start = annRow["target"]["start_off"], source = annRow["target"]["source"].encode('utf-8'))
            query = prefix+insert+intestazione+body+target+"}"
            sparql.setQuery(query)
            sparql.method = 'POST'
            sparql.query()
        else:
            intestazione = """
              [ a  oa:Annotation ;
                rdfs:label "{label}";
                ao:type "{tipo}";
                oa:annotatedAt "{time}";
                oa:annotatedBy aop:{autore};
            """.format(label = annRow["label"], tipo = annRow["type"], time = annRow["provenance"]["time"], autore = annRow["provenance"]["author"].encode('utf-8').replace(" ", "-"))
            body = """   oa:hasBody [ a rdf:Statement ;
                                    rdf:object     {oggetto};
                                    rdf:predicate  {predicate};
                                    rdf:subject    ao:{soggetto};
                                    rdfs:label     "{label}"
                                  ] ;
            """.format(oggetto = riempiOggetto(annRow["type"], annRow["body"]["label"]).encode('utf-8') , predicate = annRow["body"]["predicate"], soggetto = annRow["body"]["subjectBody"], label = annRow["body"]["label"].encode('utf-8'))
            target = """
                        oa:hasTarget  ao:{source}
                    ].
                    """.format(source = annRow["target"].encode('utf-8'))
            query = prefix+insert+intestazione+body+target+"}"
            sparql.setQuery(query)
            sparql.method = 'POST'
            sparql.query()
            
    return query
Ejemplo n.º 9
0
def salvatriple():
    annotazionejson = request.json
    sparql = SPARQLWrapper("http://localhost:3030/data/update")
    insert = """INSERT DATA {"""
    for annotazione in annotazionejson:
        #annotazioni sul documento
        if (annotazione["docOrFrag"] == "documento"): 
            subject,end = annotazione["target"].split('.') #prende solo la parte prima di .html
            inizio = """[a  oa:Annotation ;
                        rdfs:label "{label}";
                        ao:type "{tipo}";    
                        oa:annotatedAt "{data}";
                        oa:annotatedBy  aop:{autore};
                        """.format(label = creaLabel(annotazione["type"]), tipo = annotazione["type"], data = annotazione["provenance"]["time"], autore = annotazione["provenance"]["author"].encode('utf-8').replace(" ", "-"))
            corpo = """ 
                    oa:hasBody [ a rdf:Statement ;
                                    rdf:object     {oggetto};
                                    rdf:predicate  {predicate};
                                    rdf:subject    ao:{soggetto};
                                    rdfs:label     "{label}"
                                  ] ;
                                  """.format(oggetto = riempiOggetto(annotazione["type"], annotazione["body"]["object"]), predicate = annotazione["body"]["predicate"], soggetto = subject, label = annotazione["body"]["object"].encode('utf-8'))  
            target = """ oa:hasTarget ao:{urldoc} 
                        ]. """.format(urldoc = annotazione["target"])
            
        #annotazioni sul frammento
        else:
            subject,end = annotazione["target"]["source"].split('.') #prende solo la parte prima di .html
            inizio = """[a  oa:Annotation ;
                        rdfs:label "{label}";
                        ao:type "{tipo}";    
                        oa:annotatedAt "{data}";
                        oa:annotatedBy  aop:{autore};
                        """.format(label = creaLabel(annotazione["type"]), tipo = annotazione["type"], data = annotazione["provenance"]["time"], autore = annotazione["provenance"]["author"].encode('utf-8').replace(" ", "-"))
            if(annotazione["type"] == "cites"): 
                myLabel = annotazione["labelcitazione"].encode('utf-8') 
            else:
                myLabel = annotazione["body"]["object"].encode('utf-8')
            corpo = """ 
                    oa:hasBody [ a rdf:Statement ;
                                    rdf:object     {oggetto};
                                    rdf:predicate  {predicate};
                                    rdf:subject    ao:{soggetto};
                                    rdfs:label     "{label}"
                                  ] ;
                                  """.format(oggetto = riempiOggetto(annotazione["type"], annotazione["body"]["object"]), predicate = annotazione["body"]["predicate"], soggetto = subject, label = myLabel)  
            target = """  oa:hasTarget  [ a  oa:SpecificResource ;
                                          oa:hasSelector  [ a   oa:FragmentSelector;
                                                            rdf:value  "{idnodo}";
                                                            oa:end    {end};
                                                            oa:start   {start}
                                                          ];
                                          oa:hasSource    ao:{source}
                                        ]
                          ]. """.format(idnodo = annotazione["target"]["node"], end = annotazione["target"]["end_off"], start = annotazione["target"]["start_off"], source = annotazione["target"]["source"])     
            
            
        query = prefix + insert + inizio + corpo + target + "}"
        print query
        sparql.setQuery(query)
        sparql.method = 'POST'
        sparql.query()
    
    
    return query