解析单个RDF字符串

时间:2014-12-15 16:46:37

标签: scala rdf jena openrdf

我有两个RDF Turtle数据字符串

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>"

每行有3个项目。我想通过RDF解析运行一行并得到:

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

如果我还可以从每个已解析的项目中提取本地项目

,则获得奖励
0001, type, Class
0002, CONCEPT_hasType, (BEAR, string)

那里有一个库(java或scala)可以为我分割吗?我看过Jena和OpenRDF,但找不到这种单线分割的方法。

1 个答案:

答案 0 :(得分:0)

感谢@AndyS的建议,我为三元组出了这个

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]")

然后为四元组referenced this code获取此

  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
  }

远非漂亮,它符合我的要求。