Exemple #1
0
 def query_article(self, article):
     escaped = self.escape_sparql(article)
     ret = []
     query = """
     SELECT DISTINCT ?author ?author_fullname ?author_email
      ?date ?label ?type ?body_s ?body_p ?body_o ?body_l
      ?target_start ?target_startoffset ?target_endoffset
     WHERE {
         ?annotation rdf:type oa:Annotation ;
             oa:annotatedAt ?date ;
             oa:annotatedBy ?author .
         OPTIONAL { ?author foaf:name ?author_fullname }
         OPTIONAL { ?author schema:email ?author_email }
         OPTIONAL { ?annotation rdfs:label ?label }
         OPTIONAL { ?annotation ao:type ?type }
         OPTIONAL { ?annotation oa:hasBody ?body }
         OPTIONAL { ?body rdf:subject ?body_s }
         OPTIONAL { ?body rdf:predicate ?body_p }
         OPTIONAL { ?body rdf:object ?body_o }
         OPTIONAL { ?body rdfs:label ?body_l }
         { ?annotation oa:hasTarget ao:""" + escaped + """ }
          UNION
         { ?annotation oa:hasTarget ?bnode .
           ?bnode rdf:type oa:SpecificResource ;
                 oa:hasSource ao:""" + escaped + """ ;
                 oa:hasSelector ?selector .
           ?selector rdf:type oa:FragmentSelector ;
                 rdf:value ?target_start ;
                 oa:start ?target_startoffset ;
                 oa:end ?target_endoffset }
     }
     """
     for row in self.sparql.query(query, initNs=initNS):
         annotation = Annotation()
         annotation.parse_rdf({
             'target': article,
             'author': row[0].encode('utf-8'),
             'author_fullname': row[1].encode('utf-8') if row[1] is not None else None,
             'author_email': row[2].encode('utf-8') if row[2] is not None else None,
             'created': row[3].encode('utf-8') if row[3] is not None else None,
             'label': row[4].encode('utf-8') if row[4] is not None else None,
             'type': row[5].encode('utf-8') if row[5] is not None else None,
             'subject': row[6].encode('utf-8') if row[6] is not None else None,
             'predicate': row[7].encode('utf-8') if row[7] is not None else None,
             'object': row[8].encode('utf-8') if row[8] is not None else None,
             'obj_label': row[9].encode('utf-8') if row[9] is not None else None,
             'target_start': row[10].encode('utf-8') if row[10] is not None else None,
             'target_startoff': int(row[11]) if row[11] is not None else None,
             'target_endoff': int(row[12]) if row[12] is not None else None
         })
         ret.append(annotation.to_dict())
     return ret
Exemple #2
0
 def query_article(self, article):
     escaped = self.escape_sparql(article)
     ret = []
     query = """
     SELECT DISTINCT ?author ?author_fullname ?author_email
      ?date ?label ?type ?body_s ?body_p ?body_o ?body_l
      ?target_start ?target_startoffset ?target_endoffset
     WHERE {
         ?annotation rdf:type oa:Annotation ;
             oa:annotatedAt ?date ;
             oa:annotatedBy ?author .
         OPTIONAL { ?author foaf:name ?author_fullname }
         OPTIONAL { ?author schema:email ?author_email }
         OPTIONAL { ?annotation rdfs:label ?label }
         OPTIONAL { ?annotation ao:type ?type }
         OPTIONAL { ?annotation oa:hasBody ?body }
         OPTIONAL { ?body rdf:subject ?body_s }
         OPTIONAL { ?body rdf:predicate ?body_p }
         OPTIONAL { ?body rdf:object ?body_o }
         OPTIONAL { ?body rdfs:label ?body_l }
         { ?annotation oa:hasTarget ao:""" + escaped + """ }
          UNION
         { ?annotation oa:hasTarget ?bnode .
           ?bnode rdf:type oa:SpecificResource ;
                 oa:hasSource ao:""" + escaped + """ ;
                 oa:hasSelector ?selector .
           ?selector rdf:type oa:FragmentSelector ;
                 rdf:value ?target_start ;
                 oa:start ?target_startoffset ;
                 oa:end ?target_endoffset }
     }
     """
     for row in self.sparql.query(query, initNs=initNS):
         annotation = Annotation()
         annotation.parse_rdf({
             'target':
             article,
             'author':
             row[0].encode('utf-8'),
             'author_fullname':
             row[1].encode('utf-8') if row[1] is not None else None,
             'author_email':
             row[2].encode('utf-8') if row[2] is not None else None,
             'created':
             row[3].encode('utf-8') if row[3] is not None else None,
             'label':
             row[4].encode('utf-8') if row[4] is not None else None,
             'type':
             row[5].encode('utf-8') if row[5] is not None else None,
             'subject':
             row[6].encode('utf-8') if row[6] is not None else None,
             'predicate':
             row[7].encode('utf-8') if row[7] is not None else None,
             'object':
             row[8].encode('utf-8') if row[8] is not None else None,
             'obj_label':
             row[9].encode('utf-8') if row[9] is not None else None,
             'target_start':
             row[10].encode('utf-8') if row[10] is not None else None,
             'target_startoff':
             int(row[11]) if row[11] is not None else None,
             'target_endoff':
             int(row[12]) if row[12] is not None else None
         })
         ret.append(annotation.to_dict())
     return ret