Parse a single RDF string

828 views Asked by At

I have two strings of RDF Turtle data

val a: String = "<http://www.test.com/meta#0001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class>"
val b: String = "<http://www.test.com/meta#0002> <http://www.test.com/meta#CONCEPT_hasType> \"BEAR\"^^<http://www.w3.org/2001/XMLSchema#string>"

Each line has 3 items in it. I want to run one line through an RDF parse and get:

val items : Array[String] = magicallyParse(a)
items(0) == "http://www.test.com/meta#0001"

Bonus if I can also extract the Local items from each parsed items

0001, type, Class
0002, CONCEPT_hasType, (BEAR, string)

Is there a library out there (java or scala) that would do this split for me? I have looked at Jena and OpenRDF but could not find a way to do this single-line split up.

1

There are 1 answers

3
MintyAnt On

Thanks to @AndyS 's suggestion I came out with this for triples

val line1: String = "<http://www.test.com/meta#0001> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#Class> ."

val reader: Reader = new StringReader(line1) ;
val tokenizer = TokenizerFactory.makeTokenizer(reader)
val graph: Graph = GraphFactory.createDefaultGraph()
val sink: StreamRDF = StreamRDFLib.graph(graph)
val turtle: LangTurtle = new LangTurtle(tokenizer, new ParserProfileBase(new Prologue(), null), sink)
turtle.parse()

println("item is this: " + graph)
println(graph.size())
println(graph.find(null, null, null).next())
val trip = graph.find(null, null, null).next()
val sub = trip.getSubject
val pred = trip.getPredicate
val obj = trip.getObject
println(s"subject[$sub] predicate[$pred] object[$obj]")

val subLoc = sub.getLocalName
val predLoc = pred.getLocalName
val objLoc = obj.getLocalName
println(s"subject[$subLoc] predicate[$predLoc] object[$objLoc]")

and then for quads I referenced this code to get this

  def extractRdfLineAsQuad(line: String): Option[Quad] = {
    val reader: Reader = new StringReader(line)
    val tokenizer = TokenizerFactory.makeTokenizer(reader)
    val parser: LangNQuads = new LangNQuads(tokenizer, RiotLib.profile(Lang.NQUADS, null), null)

    if (parser.hasNext) Some(parser.next())
    else None
  }

Far from pretty, it does what I require.