question

Upvotes
Accepted
1.2k 6 10 8

How can I set up a transformation that creates a new field based on a literal value of another field and a lookup map?

My original data source contains ISO country codes that I would like to expand into full country names and add as an additional property to my original data source to facilitate concordance to Organization Authority.

Example data:

CTRYISOCD

CHN

CHN

JPN

SGP

ARG

DEU

BRA

MEX

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

1 Answer

Upvote
Accepted
1.2k 6 10 8

The following SPARQL and transformation scripts would work in this case.

SPARQL query

SELECT * FROM NAMED <http://rdf.entagen.com/context/context_name> WHERE 
{
	GRAPH ?g {		?s <http://rdf.entagen.com/context/context_name/property/ISOCD> ?o .
	}
}

Transformation code

import org.openrdf.model.*
import org.openrdf.model.impl.*
import com.entagen.masterindex.api.*
import com.entagen.masterindex.search.*

/** Generates new RDF based on the Sparql query results.
*
*/

public Collection<Statement> transformAsRdf(Map<String, Value> values) {
        def countryMap = [
        ABW:"Aruba",AFG:"Afghanistan",AGO:"Angola",AIA:"Anguilla",ALA:"Åland Islands",ALB:"Albania",AND:"Andorra",ARE:"United Arab Emirates",ARG:"Argentina",ARM:"Armenia",ASM:"American Samoa",ATA:"Antarctica"
        ]
    
	def subject = values.s
        def predicate = new URIImpl("http://rdf.entagen.com/context/property/country_name")
        def object = new LiteralImpl(countryMap[values.o.stringValue()].toUpperCase()) ?: new LiteralImpl("UNKNOWN")
        def context = values.g 

        return [new ContextStatementImpl(subject, predicate, object, context)]

}

/** Generates new Annotations based on the Sparql query results.
*
*/

public Collection<AnnotationDto> transformAsAnnotations(Map<String, Value> values) {
    return null
}

Explanation

Values.o is an implementation of org.openrdf.model.Value

It won’t work directly as a key lookup in the countryMap. We need to retrieve its string value by calling the values.o.stringValue() method first.

Finally, we need to push it back into a Literal, so wrapping it in a new LiteralImpl().

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 5.0 MiB each and 10.0 MiB total.

Click below to post an Idea Post Idea