Exemple #1
0
def test_turtleX_example_3(store):
    document = """
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:name "Bob" .
        <<:bob foaf:age 23>> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .
    """
    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            (IRI("http://example.org/bob"),
             IRI("http://xmlns.com/foaf/0.1/age"), 23),
            IRI("http://purl.org/dc/elements/1.1/creator"),
            IRI("http://example.com/crawlers#c1"),
        ),
        (
            (IRI("http://example.org/bob"),
             IRI("http://xmlns.com/foaf/0.1/age"), 23),
            IRI("http://purl.org/dc/elements/1.1/source"),
            IRI("http://example.net/homepage-listing.html"),
        ),
        (IRI("http://example.org/bob"), IRI("http://xmlns.com/foaf/0.1/age"),
         23),
        (IRI("http://example.org/bob"), IRI("http://xmlns.com/foaf/0.1/name"),
         "Bob"),
    ]
Exemple #2
0
def test_typed_literal(store):
    document = """
        <http://bnb.data.bl.uk/id/person/%C3%96zbayKaan1964-/birth> <http://purl.org/vocab/bio/0.1/date> "1964"^^<http://www.w3.org/2001/XMLSchema#gYear> .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [(
        IRI("http://bnb.data.bl.uk/id/person/%C3%96zbayKaan1964-/birth"),
        IRI("http://purl.org/vocab/bio/0.1/date"),
        TypedLiteral("1964", IRI("http://www.w3.org/2001/XMLSchema#gYear")),
    )]
Exemple #3
0
def from_file_to_tmp(from_file):

    blank_node_factory = BlankNodeFactory()
    store = InMemoryHexastore(blank_node_factory)

    with open(from_file, "r") as fo:
        parse(fo.read(), store.insert, blank_node_factory)

    fd, path = tempfile.mkstemp()
    os.close(fd)
    builder = Builder(path, store)
    builder.serialise()

    return path
Exemple #4
0
def test_turtle_example_2(store):
    document = """
        <http://example.org/#spiderman>
            <http://www.perceive.net/schemas/relationship/enemyOf>
            <http://example.org/#green-goblin> .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [(
        IRI("http://example.org/#spiderman"),
        IRI("http://www.perceive.net/schemas/relationship/enemyOf"),
        IRI("http://example.org/#green-goblin"),
    )]
Exemple #5
0
def cat(namespace, filenames, output):
    store = VersionedInMemoryHexastore()

    namespaces: Dict[str, Namespace] = {
        n: Namespace(n, IRI(i))
        for n, i in namespace
    }

    try:
        with Timer() as t:
            for filename in filenames:
                if filename.endswith("ttl") or filename.endswith("nt"):
                    triples = []
                    with open(filename) as fo:
                        new_namespaces = turtle.parse(
                            fo.read(), lambda s, p, o: triples.append(
                                (s, p, o)))

                    _update_namespaces(namespaces, new_namespaces)
                    store.bulk_insert(triples, 1)
                else:
                    raise RuntimeError(f"Unknown file {filename}")
    finally:
        logger.info(f"Parsing took {t.interval} seconds.")

    try:
        with Timer() as t:
            print(f"# Triples {len(store)}")

            with smart_open(output) as fo:
                turtle_serialiser.serialise(store, fo,
                                            [(n.name, n.prefix)
                                             for n in namespaces.values()])
    finally:
        logger.info(f"Serialisation took {t.interval} seconds.")
Exemple #6
0
def test_turtle_example_4(store):
    document = """
        <http://example.org/#spiderman> <http://xmlns.com/foaf/0.1/name> "Spiderman", "Человек-паук"@ru .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (IRI("http://example.org/#spiderman"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Spiderman"),
        (
            IRI("http://example.org/#spiderman"),
            IRI("http://xmlns.com/foaf/0.1/name"),
            LangTaggedString("Человек-паук", "ru"),
        ),
    ]
Exemple #7
0
def test_turtle_example_10(store):
    document = """
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .

        <http://example.org/#green-goblin> foaf:name "Green Goblin" .

        <http://example.org/#spiderman> foaf:name "Spiderman" .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (IRI("http://example.org/#green-goblin"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Green Goblin"),
        (IRI("http://example.org/#spiderman"),
         IRI("http://xmlns.com/foaf/0.1/name"), "Spiderman"),
    ]
Exemple #8
0
def test_turtle_example_15(store):
    document = """
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .

        # Someone knows someone else, who has the name "Bob".
        [] foaf:knows [ foaf:name "Bob" ] .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            BlankNode(0, store.blank_node_factory),
            IRI("http://xmlns.com/foaf/0.1/knows"),
            BlankNode(1, store.blank_node_factory),
        ),
        (BlankNode(1, store.blank_node_factory),
         IRI("http://xmlns.com/foaf/0.1/name"), "Bob"),
    ]
Exemple #9
0
def test_turtle_example_12(store):
    document = """
        @prefix : <http://example.org/elements#> .

        <http://en.wikipedia.org/wiki/Helium>
            :atomicNumber 2 ;
            :atomicMass 4.002602 ;
            :specificGravity 1.663E-4 .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == [
        (
            IRI("http://en.wikipedia.org/wiki/Helium"),
            IRI("http://example.org/elements#atomicMass"),
            Decimal("4.002602"),
        ),
        (IRI("http://en.wikipedia.org/wiki/Helium"),
         IRI("http://example.org/elements#atomicNumber"), 2),
        (IRI("http://en.wikipedia.org/wiki/Helium"),
         IRI("http://example.org/elements#specificGravity"), 1.663e-4),
    ]
Exemple #10
0
def test_turtleX_example_3_serialise(store):
    document = """
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:name "Bob" .
        <<:bob foaf:age 23>> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .
    """

    parse(document, store.insert, store.blank_node_factory)

    out = io.StringIO()
    serialise(
        store,
        out,
        [
            ("", IRI("http://example.org/")),
            ("foaf", IRI("http://xmlns.com/foaf/0.1/")),
            ("dct", IRI("http://purl.org/dc/elements/1.1/")),
        ],
    )
    doc_out = out.getvalue()
    print(doc_out)
    assert (doc_out == textwrap.dedent("""
        @prefix : <http://example.org/> .
        @prefix foaf: <http://xmlns.com/foaf/0.1/> .
        @prefix dct: <http://purl.org/dc/elements/1.1/> .

        :bob foaf:age 23 ;
            foaf:name "Bob" .

        << :bob foaf:age 23 >> dct:creator <http://example.com/crawlers#c1> ;
            dct:source <http://example.net/homepage-listing.html> .

    """).lstrip())
Exemple #11
0
def test_turtle_example_11(store):
    document = """
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
        @prefix show: <http://example.org/vocab/show/> .
        @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

        show:218 rdfs:label "That Seventies Show"^^xsd:string .
        show:218 rdfs:label "That Seventies Show"^^<http://www.w3.org/2001/XMLSchema#string> .
        show:218 rdfs:label "That Seventies Show" .
        show:218 show:localName "That Seventies Show"@en .
        show:218 show:localName 'Cette Série des Années Soixante-dix'@fr .
        show:218 show:localName "Cette Série des Années Septante"@fr-be .
        show:218 show:blurb '''This is a multi-line
literal with many quotes (\"\"\"\"\")
and up to two sequential apostrophes ('').''' .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert (
        IRI("http://example.org/vocab/show/218"),
        IRI("http://www.w3.org/2000/01/rdf-schema#label"),
        "That Seventies Show",
    ) in store
Exemple #12
0
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
root.addHandler(handler)

try:
    with Timer() as t:
        store = InMemoryHexastore()

        with Timer() as t1:
            triples = []
            with open(
                    "/Users/alex/Downloads/BNBLODBooks_sample_nt/BNBLODB_sample.nt"
            ) as fo:
                turtle.parse(fo.read(), lambda s, p, o: triples.append(
                    (s, p, o)))

        logger.info(f"library=mutant-parse time={t1.interval}")

        with Timer() as t2:
            store.bulk_insert(triples)

        logger.info(f"library=mutant-bulk-insert time={t2.interval}")
finally:
    logger.info(f"library=mutant time={t.interval}")

try:
    with Timer() as t:
        g = Graph()
        g.parse(
            "/Users/alex/Downloads/BNBLODBooks_sample_nt/BNBLODB_sample.nt",
Exemple #13
0
def test_turtle_example_9(store):
    document = """
        <http://one.example/subject1> <http://one.example/predicate1> <http://one.example/object1> .

        # @base <http://one.example/> .
        # <subject2> <predicate2> <object2> .

        # BASE <http://one.example/>
        # <subject2> <predicate2> <object2> .

        @prefix p: <http://two.example/> .
        p:subject3 p:predicate3 p:object3 .

        PREFIX p: <http://two.example/>
        p:subject3 p:predicate3 p:object3 .

        # @prefix p: <path/> .
        # p:subject4 p:predicate4 p:object4 .

        @prefix : <http://another.example/> .
        :subject5 :predicate5 :object5 .

        :subject6 a :subject7 .

        <http://伝言.example/?user=أكرم&amp;channel=R%26D> a :subject8 .
    """

    parse(document, store.insert, store.blank_node_factory)

    assert list(store.triples()) == sorted(
        [
            (
                IRI("http://one.example/subject1"),
                IRI("http://one.example/predicate1"),
                IRI("http://one.example/object1"),
            ),
            # (
            #     IRI("http://one.example/subject2"),
            #     IRI("http://one.example/predicate2"),
            #     IRI("http://one.example/object2"),
            # ),
            (
                IRI("http://two.example/subject3"),
                IRI("http://two.example/predicate3"),
                IRI("http://two.example/object3"),
            ),
            # (
            #     IRI("http://two.example/subject4"),
            #     IRI("http://two.example/predicate4"),
            #     IRI("http://two.example/object4"),
            # ),
            (
                IRI("http://another.example/subject5"),
                IRI("http://another.example/predicate5"),
                IRI("http://another.example/object5"),
            ),
            (
                IRI("http://another.example/subject6"),
                IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
                IRI("http://another.example/subject7"),
            ),
            (
                IRI("http://伝言.example/?user=أكرم&amp;channel=R%26D"),
                IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
                IRI("http://another.example/subject8"),
            ),
        ],
        key=Key,
    )