digitalbazaar / pyld

JSON-LD processor written in Python
https://json-ld.org/
Other
602 stars 129 forks source link

Language tagged strings #192

Closed edsu closed 7 months ago

edsu commented 7 months ago

I think it may be out of scope for Framing and pyld, but I was wondering if there is any way to ensure that string literals either appear as language tagged strings or without them?

For example is there anything I can put in the frame here to ensure that Semantic Web is printed out instead of {'@language': 'en', '@value': 'Semantic Web'}?

import requests
from pyld import jsonld

uri = "http://id.loc.gov/authorities/subjects/sh2002000569"
doc = requests.get(uri, headers={"Accept": "application/ld+json"}).json()

context = {
    "@vocab": "http://www.w3.org/2004/02/skos/core#",
}

doc = jsonld.frame(doc, {"@context": context, "@id": uri})

print(doc['prefLabel'])

Or when using the resulting Python object do I always need to check whether prefLabel is associated with a str or a dict depending on how the JSON-LD was published?

edsu commented 7 months ago

I got pointed to https://github.com/w3c/json-ld-framing/issues/156 by @phochste which seems to confirm that this is not possible with framing?

dlongley commented 7 months ago

If you know the language you want and the properties you want, then you can specify those in the frame like it happens here:

http://tinyurl.com/5fthtbfs

dlongley commented 7 months ago

And if you wanted to presume that the default language in the incoming document was a particular language (which may/may not be acceptable), you could append a default language context to your incoming document and then frame (or potentially just compact, again, depending on your use case):

http://tinyurl.com/vz83ndyw

edsu commented 7 months ago

Thanks @dlongley that is very helpful! I guess there's no way to to do that for all strings in the document, and would need to be done on a property by property basis?

dlongley commented 7 months ago

Thanks @dlongley that is very helpful!

Sure!

I guess there's no way to to do that for all strings in the document, and would need to be done on a property by property basis?

Yeah, it seems that way -- unless there's another trick I've missed.