示例#1
0
    def handle(self, *args, **options):
        if not args:
            raise CommandError("No file or resource specified.")

        info = options.get('verbosity') >= 2
        store_id = options.get('store')
        context_id = options.get('context')
        source = args[0]

        if info:
            print("Parsing {}".format(source))

        intermediate = Graph()
        try:
            intermediate.parse(source, format=options.get('format'))
        except Exception as e:
            raise CommandError(e)

        if info:
            print("Parsed {} triples".format(len(intermediate)))

        identifier = URIRef(context_id) if context_id else BNode()
        graph = utils.get_named_graph(identifier, store_id=store_id)

        if info:
            print("Storing {} triples".format(len(intermediate)))
        for triple in intermediate:
            graph.add(triple)
        if info:
            print("Done")
    def handle(self, *args, **options):
        if not args:
            raise CommandError("No file or resource specified.")

        info = options.get('verbosity') >= 2
        store_id = options.get('store')
        context_id = options.get('context')
        source = args[0]

        if info:
            print "Parsing {0}".format(source)

        intermediate = Graph()
        try:
            intermediate.parse(source, format=options.get('format'))
        except Exception as e:
            raise CommandError(e)

        if info:
            print "Parsed {0} triples".format(len(intermediate))

        identifier = URIRef(context_id) if context_id else BNode()
        graph = utils.get_named_graph(identifier, store_id=store_id)

        if info:
            print "Storing {0} triples".format(len(intermediate))
        for triple in intermediate:
            graph.add(triple)
        if info:
            print "Done"
示例#3
0
    def lineages(self, request, graph_identifier=None):
        if graph_identifier:
            graph = get_named_graph(graph_identifier)
        else:
            graph = get_conjunctive_graph()
        object = self.node()
        assert object
        MAX_DISTANCE = 5
        MAX_ITERATIONS = 5
        history = get_history(request)
        n_nodes = len(history)
        out_paths = []
        paths = [[[self, None]]] # start with only 1 path containing only a pseudo-edge
        while paths:
            path = paths[0] # 1st path
            object = path[0][0].node() # left element (subject) of first edge becomes target object of previous triples
            triples = list(graph.triples((None, None, object)))
            triples += get_reified_triples(object, graph)
            n_triples = len(triples)
            i = 0
            while n_triples and node_id(object) not in settings.EU_COUNTRY_KEYS:
                i += 1
                if i > MAX_ITERATIONS: # endless loops could result from bugs or wrong network
                    exit
                best_k = 0
                if n_triples > 1:
                    if history:
                        min_distance = MAX_DISTANCE
                        k_range = range(n_triples)
                        for k in k_range:
                            s, p, o = triples[k]
                            if node_id(s) in history:
                                distance = n_nodes - history.index(node_id(s))
                                if distance < min_distance:
                                    best_k = k
                s, p, o = triples[best_k]
                best_edge = [make_item(s), Predicate(uriref=p)]
                del triples[best_k]
                if not history:
                    for triple in triples:
                        edge = [make_item(triple[0]), Predicate(uriref=triple[1])]
                        # paths.append(deepcopy([edge]) + deepcopy(path))
                        paths.append([edge] + path)
                # path = deepcopy([best_edge]) + deepcopy(path)
                path = [best_edge] + path

                object = s
                triples = list(graph.triples((None, None, object)))
                triples += get_reified_triples(object, graph)
                n_triples = len(triples)
            out_paths.append(path[:-1])
            paths = paths[1:]
            # print_paths(out_paths)
        return out_paths
示例#4
0
def test_get_single_tag():
    from rdflib_django import utils
    graph = utils.get_named_graph('http://dbpedia.org/resource/Scotland')
    graph.add((URIRef('http://dbpedia.org/resource/Scotland'), TOPICA.tag, URIRef('http://dbpedia.org/resource/Robert_Burns')))
    graph.add((URIRef('http://dbpedia.org/resource/Robert_Burns'), RDFS.label, Literal('Robert Burns')))

    item = Item(iri='http://dbpedia.org/resource/Scotland')
    item.save()

    assert len(item.get_tags()) == 1
    assert Tag('http://dbpedia.org/resource/Robert_Burns', 'Robert Burns') in item.get_tags()
示例#5
0
    def handle(self, *args, **options):
        store_id = options.get('store')
        context_id = options.get('context')
        target = args[0] if args else sys.stdout

        if context_id:
            graph = utils.get_named_graph(URIRef(context_id), store_id=store_id)
        else:
            graph = utils.get_conjunctive_graph(store_id)

        #noinspection PyUnresolvedReferences
        graph.serialize(target, format=options.get('format'))
示例#6
0
def test_distance_between_overlapping_items():
    from rdflib_django import utils
    g1 = utils.get_named_graph('http://dbpedia.org/resource/Scotland')
    g1.add((URIRef('http://dbpedia.org/resource/Scotland'), TOPICA.tag, URIRef('http://dbpedia.org/resource/Robert_Burns')))
    g1.add((URIRef('http://dbpedia.org/resource/Robert_Burns'), RDFS.label, Literal('Robert Burns')))
    g1.add((URIRef('http://dbpedia.org/resource/Scotland'), TOPICA.tag, URIRef('http://dbpedia.org/resource/United_Kingdom')))
    g1.add((URIRef('http://dbpedia.org/resource/United_Kingdom'), RDFS.label, Literal('United Kingdom')))

    g2 = utils.get_named_graph('http://dbpedia.org/resource/England')
    g2.add((URIRef('http://dbpedia.org/resource/England'), TOPICA.tag, URIRef('http://dbpedia.org/resource/United_Kingdom')))
    g2.add((URIRef('http://dbpedia.org/resource/United_Kingdom'), RDFS.label, Literal('United Kingdom')))
    g2.add((URIRef('http://dbpedia.org/resource/England'), TOPICA.tag, URIRef('http://dbpedia.org/resource/William_Shakespeare')))
    g2.add((URIRef('http://dbpedia.org/resource/William_Shakespeare'), RDFS.label, Literal('William Shakespeare')))

    item1 = Item(iri='http://dbpedia.org/resource/Scotland')
    item1.save()
    item2 = Item(iri='http://dbpedia.org/resource/England')
    item2.save()

    assert item1.get_distance(item2) == 2.0/3.0
    assert item2.get_distance(item1) == 2.0/3.0
示例#7
0
def test_distance_between_identical_items_is_zero():
    from rdflib_django import utils
    g1 = utils.get_named_graph('http://dbpedia.org/resource/Scotland')
    g1.add((URIRef('http://dbpedia.org/resource/Scotland'), TOPICA.tag, URIRef('http://dbpedia.org/resource/Robert_Burns')))
    g1.add((URIRef('http://dbpedia.org/resource/Robert_Burns'), RDFS.label, Literal('Robert Burns')))
    g1.add((URIRef('http://dbpedia.org/resource/Scotland'), TOPICA.tag, URIRef('http://dbpedia.org/resource/William_Wallace')))
    g1.add((URIRef('http://dbpedia.org/resource/William_Wallace'), RDFS.label, Literal('William Wallace')))

    g2 = utils.get_named_graph('http://dbpedia.org/resource/Alba')
    g2.add((URIRef('http://dbpedia.org/resource/Alba'), TOPICA.tag, URIRef('http://dbpedia.org/resource/Robert_Burns')))
    g2.add((URIRef('http://dbpedia.org/resource/Robert_Burns'), RDFS.label, Literal('Robert Burns')))
    g2.add((URIRef('http://dbpedia.org/resource/Alba'), TOPICA.tag, URIRef('http://dbpedia.org/resource/William_Wallace')))
    g2.add((URIRef('http://dbpedia.org/resource/William_Wallace'), RDFS.label, Literal('William Wallace')))

    item1 = Item(iri='http://dbpedia.org/resource/Scotland')
    item1.save()
    item2 = Item(iri='http://dbpedia.org/resource/Alba')
    item2.save()

    assert item1.get_distance(item2) == 0.0
    assert item2.get_distance(item1) == 0.0
示例#8
0
def find_holes():
    graph = get_named_graph(make_uriref('http://www.wikidata.org'))
    for country_code in settings.EU_COUNTRY_KEYS:
        country = Country(id=country_code)
        holes = []
        for prop_code in settings.EU_COUNTRY_PROPERTIES:
            predicate = Predicate(id=prop_code)
            objects = graph.objects(subject=country.uriref,
                                    predicate=predicate.uriref)
            if not list(objects):
                holes.append(predicate.label())
        print('- {}: {}'.format(country.label(), ', '.join(holes)))
示例#9
0
    def handle(self, *args, **options):
        store_id = options.get('store')
        context_id = options.get('context')
        target = args[0] if args else sys.stdout

        if context_id:
            graph = utils.get_named_graph(URIRef(context_id),
                                          store_id=store_id)
        else:
            graph = utils.get_conjunctive_graph(store_id)

        # noinspection PyUnresolvedReferences
        graph.serialize(target, format=options.get('format'))
示例#10
0
def test_can_access_graph_via_item():
    # Given
    iri = 'http://dbpedia.org/resource/Kevin_Bacon'
    from rdflib_django import utils
    graph = utils.get_named_graph(iri)
    graph.add((URIRef('http://dbpedia.org/resource/Kevin_Bacon'), RDF.type, Literal('Person')))

    # When
    item = Item(iri=iri)
    item.save()

    # Then
    assert (URIRef('http://dbpedia.org/resource/Kevin_Bacon'), RDF.type, Literal('Person')) in item.graph
示例#11
0
def list_statements(request, graph_identifier=None):
    lang = get_language()[:2]
    if graph_identifier:
        graph = get_named_graph(graph_identifier)
    else:
        graph = get_conjunctive_graph()
    statement_dicts = [{
        'graph': 'wikidata',
        'subject': friend_uri(s, lang=lang),
        'predicate': friend_uri(p, lang=lang),
        'object': friend_uri(o, lang=lang)
    } for s, p, o in graph]
    return render(request, 'list_statements.html',
                  {'statement_dicts': statement_dicts})
示例#12
0
def removeProperty(request, item_code, graph_identifier=None):
    if graph_identifier:
        graph = get_named_graph(graph_identifier)
    else:
        graph = get_conjunctive_graph()
    subject = make_node(item_code)
    country_id = request.GET.get('c')
    property = make_node(request.GET.get('p'))
    datatype_id = request.GET.get('dt')
    object_value_or_id = request.GET.get('o')
    language = request.GET.get('lang', None)
    triples = graph.triples((subject, property, None))
    for triple in triples:
        object = triple[2]
        if isinstance(object, Literal):
            if datatype_id:
                if datatype_id == object.datatype_id(
                ) and object_value_or_id == str(object.value):
                    graph.remove(triple)
            elif language and object.language and (
                    language == object.language) and (object_value_or_id
                                                      == object.value):
                graph.remove(triple)
            elif language and not object.language and (object_value_or_id
                                                       == object.value):
                graph.remove(triple)
            elif not language and not object.language and (object_value_or_id
                                                           == object.value):
                graph.remove(triple)
            elif object_value_or_id == str(object.value):
                graph.remove(triple)
            """ MMR 200723
            elif language and language == object.language:
                graph.remove(triple)
            elif not language and not object.language:
                graph.remove(triple)
            """
        else:
            if isinstance(object, BNode):
                if object_value_or_id == str(object):
                    remove_node(object, graph)
                    graph.remove(triple)
            elif isinstance(object, URIRef):
                if id_from_uriref(object) == object_value_or_id:
                    remove_node(object, graph)
                    graph.remove(triple)
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
示例#13
0
def remove_item(request, item_code, graph_identifier=None):
    if graph_identifier:
        graph = get_named_graph(graph_identifier)
    else:
        graph = get_conjunctive_graph()
    item = item_from_id(item_code)
    lineages = item.lineages(request)
    parent, predicate = lineages[0][-1]
    parent_code = parent.id
    node = item.uriref
    in_triple = next(graph.triples((parent.uriref, predicate.uriref, node)))
    graph.remove(in_triple)
    remove_node(node, graph)
    if parent_code in settings.EU_COUNTRY_KEYS:
        return HttpResponseRedirect('/country/{}/'.format(parent_code))
    else:
        return HttpResponseRedirect('/item/{}/'.format(parent_code))
示例#14
0
 def __init__(self, uriref=None, id=None, bnode=None, graph=None, graph_identifier=None, in_predicate=None):
     assert uriref or id or bnode
     self.uriref = self.id = self.bnode = None
     if uriref:
         self.uriref = uriref
         self.id = id_from_uriref(uriref)
     elif id:
         self.id = id
         self.uriref = make_uriref(id)
     else:
         self.bnode = bnode
         self.id = str(bnode) # added 200708
     if graph_identifier:
         graph = get_named_graph(graph_identifier)
     elif not graph:
         graph = get_conjunctive_graph()
     self.graph = graph
     self.in_predicate = in_predicate
示例#15
0
 def __init__(self, *args, **kwargs):
     super(Item, self).__init__(*args, **kwargs)
     self.graph = utils.get_named_graph(self.iri)
示例#16
0
def export_countries(format="rdf"):
    target = "/tmp/countries.{}".format(format)
    graph = get_named_graph(make_uriref('http://www.wikidata.org'))
    graph.serialize(target, format=format)