1

我正在从 中读取 RDF 文档InputStream,如下(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在给定的例子中。

有没有办法从耶拿的陈述中得到“赤裸裸的”价值观?如果是这样,如何?谢谢。

编辑:

尝试使用从 yago 获取的三元组的AndyS解决方案,我得到:

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

如您所见,我没有得到所需的输出。我究竟做错了什么?

4

1 回答 1

4

你想要的是词法形式。请参阅Literal.getLexicalForm

于 2013-11-11T11:50:31.950 回答