我正在阅读InputStream
中的RDF文档,如下所示(scala语法):
def foo(rdfData: InputStream, dialect: String = null) = {
require(List("RDF/XML", "N-TRIPLE", "TURTLE", "TTL", "N3", "RDF/XML-ABBREV").contains(dialect) || dialect == null)
val model: Model = ModelFactory.createDefaultModel
model.read(rdfData, null, dialect)
doSomethingWithTriplesIterator(model.listStatements())
}
现在,有些三元组输入了文字,所以我做了类似的事情:
val it = model.listStatements()
it.next.getObject.toString
我得到(有时)类似于"13"^^<http://www.w3.org/2001/XMLSchema#int>
或"Hello world"@en_US
的内容,当我只对字符串值感兴趣时,即13
&amp;在给定的例子中Hello world
。
有没有办法让jena的陈述中的“裸”价值变得明确?如果是这样,怎么样? 感谢。
修改
尝试 AndyS 解决方案,使用从yago获取的三元组,我得到了:
scala> val model = ModelFactory.createDefaultModel
model: com.hp.hpl.jena.rdf.model.Model = <ModelCom {} | >
scala> val is = new java.io.ByteArrayInputStream("""<http://yago-knowledge.org/resource/Mount_Cramer> <http://yago-knowledge.org/resource/hasLatitude> "44.01102^^http://yago-knowledge.org/resource/degrees" .""".getBytes("UTF-8"))
is: java.io.ByteArrayInputStream = java.io.ByteArrayInputStream@2b3c4f10
scala> model.read(is, null, "N-TRIPLE")
res0: com.hp.hpl.jena.rdf.model.Model = <ModelCom {http://yago-knowledge.org/resource/Mount_Cramer @http://yago-knowledge.org/resource/hasLatitude "44.01102^^http://yago-knowledge.org/resource/degrees"} | [http://yago-knowledge.org/resource/Mount_Cramer, http://yago-knowledge.org/resource/hasLatitude, "44.01102^^http://yago-knowledge.org/resource/degrees"]>
scala> val it = model.listStatements
it: com.hp.hpl.jena.rdf.model.StmtIterator = com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl@55ef662c
scala> val stmt = it.next
stmt: com.hp.hpl.jena.rdf.model.Statement = [http://yago-knowledge.org/resource/Mount_Cramer, http://yago-knowledge.org/resource/hasLatitude, "44.01102^^http://yago-knowledge.org/resource/degrees"]
scala> val obj = stmt.getObject
obj: com.hp.hpl.jena.rdf.model.RDFNode = 44.01102^^http://yago-knowledge.org/resource/degrees
scala> val ltrl = obj.asLiteral
ltrl: com.hp.hpl.jena.rdf.model.Literal = 44.01102^^http://yago-knowledge.org/resource/degrees
scala> ltrl.getLexicalForm
res1: String = 44.01102^^http://yago-knowledge.org/resource/degrees
所以如你所见,我没有得到所需的输出。我做错了什么?