pchampin / sophia_rs

Sophia: a Rust toolkit for RDF and Linked Data
Other
210 stars 23 forks source link

How to use PrefixMap? #113

Closed KonradHoeffner closed 2 years ago

KonradHoeffner commented 2 years ago

I want to shorten URIs shown to users using a prefix map, but following https://docs.rs/sophia/latest/sophia/prefix/trait.PrefixMap.html I do not understand how to construct and fill one. Can you give an example on how it is supposed to be used?

pchampin commented 2 years ago

How to build a PrefixMap

The trait PrefixMap is implemented by any slice of (prefix, IRI) pair. For example, a Vec of (PrefixBox, IriBox) would do the trick.

How to use a PrefixMap to abbreviate IRIs

The easiest way is to use the get_prefixed_pair method of PrefixMap. It takes any term, and returns a pair (prefix, suffix) if that term is an IRI and can be abbreviated using one of the prefixes in the map. Otheriwse, it returns None (meaning that you should display the IRI in full).

Hope this helps.

KonradHoeffner commented 2 years ago

Thanks for the quick reply! As a Rust beginner, I'm not quite sure what to do but I got as far as:

let mut prefixes: Vec<(PrefixBox,IriBox)> = Vec::new(); 
let pBox: PrefixBox = Prefix::new("http://hitontology.eu/ontology/").unwrap().boxed();

However that causes: thread 'main' panicked at 'calledResult::unwrap()on anErrvalue: InvalidPrefix("http://hitontology.eu/ontology/")', src/main.rs:17:74 What is the error cause there?

KonradHoeffner commented 2 years ago

Update

I think I got it now:

let mut prefixes: Vec<(PrefixBox,IriBox)> = Vec::new(); 
let hito_prefix: PrefixBox = Prefix::new("hito").unwrap().boxed();                              
let hito_iri: IriBox = Iri::new("http://hitontology.eu/ontology/").unwrap().boxed();
prefixes.push((hito_prefix,hito_iri));

Could this be added as an example to the documentation? (using ? instead of unwrap())

A convenience method to read a prefix map from a turtle file would also be nice and a method to add a prefix map entry such as add(&str prefix,&str iri).

pchampin commented 2 years ago

Yes, the lack of documentation is well recognized. My efforts are now focused on a big refactorying of the library. Any new documentation is at risk of being quickly outdated, so unfortunately, it is not my priority at the moment.

About being able to get the prefix map out of a parsed turtle file: I agree. There is an issue about it Rio, the parser library on which Sophia is relying.

KonradHoeffner commented 2 years ago

Ah OK, then I will just write a quick convenience method and adapt to the refactoring later for sophia 0.8, thanks for the help!