Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A new Turtle serializer #1425

Merged
merged 6 commits into from Oct 17, 2021
Merged

A new Turtle serializer #1425

merged 6 commits into from Oct 17, 2021

Conversation

nicholascar
Copy link
Member

This is a slightly improved, or at least changed, Turtle serializer that does more indenting and new lining.

In particular it:

  • uses PREFIX instead of @Prefix
  • uses BASE instead of @base
  • adds a new line at RDF.type, or 'a'
  • adds a newline and an indent for all triples with more than one object (object list)
  • adds a new line and ';' for the last triple in a set with '.'
    on the start of the next line
  • uses default encoding (encode()) is used instead of "latin-1"

This serializer is called with g.serialize(format="turtle2")

Here is a comparison of turtle v. turtle2 outputs:

@prefix ex: <https://example.org/> .
@prefix ex2: <https://example2.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:b a ex:Thing ;
    ex:name "B" ;
    ex2:name "B" .

ex:c a ex:Thing ;
    ex:bnObj [ ex:lst ( ex:one ex:two ex:three ),
                ( ex:four ex:fize ex:six ) ] ;
    ex:lst ( ex:one ex:two ex:three ),
        ( ex:four ex:fize ex:six ) ;
    ex:lst2 ( ex:one ex:two ex:three ) ;
    ex:name "C" .

turtle2:

PREFIX ex: <https://example.org/>
PREFIX ex2: <https://example2.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

ex:b
    a ex:Thing ;
    ex:name "B" ;
    ex2:name "B" ;
.

ex:c
    a ex:Thing ;
    ex:bnObj [
            ex:lst
                (
                    ex:one
                    ex:two
                    ex:three
                ) ,
                (
                    ex:four
                    ex:fize
                    ex:six
                )
        ] ;
    ex:lst
        (
            ex:one
            ex:two
            ex:three
        ) ,
        (
            ex:four
            ex:fize
            ex:six
        ) ;
    ex:lst2 (
            ex:one
            ex:two
            ex:three
        ) ;
    ex:name "C" ;
.

@nicholascar nicholascar added enhancement New feature or request turtle-1.1 labels Oct 2, 2021
@white-gecko
Copy link
Member

white-gecko commented Oct 2, 2021

How is the performance?
And why PREFIX instead of @prefix?
The many newlines might be matter of taste.
I like, that it uses the default encoding.

@nicholascar
Copy link
Member Author

nicholascar commented Oct 2, 2021

How is the performance?

I haven’t checked performance but since it’s only a few newline characters and spaces different from the current turtle serialiser, the performance will be the same.

And why PREFIX instead of @prefix?

Because this syntax matches SPARQL better and thus it’s easier to convert between this turtle and SPARQL while testing queries and data.

The turtle parser already handles both forms.

The many newlines might be matter of taste.

Yes, this is mostly a subjective thing however I will just point out that the motivation for this particular change is for better Git-style diffing. It also makes it easier to see multiple classes and list objects.

I like, that it uses the default encoding.

There are just a few places in which the current serialiser implements latin-1, probably just a hang over from Python 2.

@white-gecko
Copy link
Member

Yes the encoding should also be part of my, never finished, #1222 ... Unifying all serializers and parsers to unicode would be a good overall improvement.

@nicholascar
Copy link
Member Author

Unless there are any objects (I'll wait a few days), I'm going to merge this in.

@nicholascar nicholascar merged commit f4e1efd into master Oct 17, 2021
@nicholascar nicholascar deleted the ttl2 branch October 17, 2021 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants