0

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.

MintyAnt
  • 2,978
  • 7
  • 25
  • 37
  • 1
    These seems very close to your other question http://stackoverflow.com/questions/27408739/parsing-rdf-items. Also, it's probably going to get closed since it asks for a library... – The Archetypal Paul Dec 15 '14 at 17:13
  • 3
    In Jena, use RDFDataMgr.parse(StreamRDF sink, StringReader in, Lang.NT) You need to add a DOT tot he end of your strings to make them N-triples. Writer a StreamRDF implementation that captures the first triple sent to it. – AndyS Dec 15 '14 at 21:19
  • @Paul Thats fine. The other question was about parsing a file, this one is about singular strings, which is why I made a new one – MintyAnt Dec 16 '14 at 18:51
  • @AndyS Thanks, sent me on the right path, ill post an answer shortley – MintyAnt Dec 16 '14 at 18:51

1 Answers1

0

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.

MintyAnt
  • 2,978
  • 7
  • 25
  • 37
  • 1
    I am at a loss to see how this is different from the answer I gave you in the original question - except for the fact that you're using Jena where I was using Sesame. – Jeen Broekstra Dec 16 '14 at 20:09
  • @JeenBroekstra I did not want to load in an entire file. I am locked into looking at a file 1 line at a time and dealing with it there. Your method was to use a filestream, where I wanted to load in just a string. Turns out the two methods are very similar anyways. Since the project i'm using is already using Jena, I looked into how to pull this off with jena. Since there was little documentation online for this exact process, i'm hoping my answer will help someone else. Besides, I accepted your answer on the other question as it did answer my original question. – MintyAnt Dec 17 '14 at 16:54
  • 1
    Clear. I simply wanted to understand why my original answer didn't help you (enough). FWIW if the only difference is the file vs string thing: if you replaced the `FileInputStream` in my code example with a simple `StringReader`, everything else would work exactly the same. – Jeen Broekstra Dec 17 '14 at 23:33