我正在从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
& Hello world
在给定的示例中。
有没有办法从耶拿的陈述中去除"赤裸裸"的价值观? 如果是这样,如何?谢谢。
编辑:
尝试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
如你所见,我没有得到想要的输出。我做错了什么?
你想要的是词汇形式。 请参阅 Literal.getLexicalForm。