gpt4 book ai didi

java - 使用 Jena TDB 在本地加载 DBpedia?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:19:47 26 4
gpt4 key购买 nike

我需要对 DBpedia 执行查询:

SELECT DISTINCT ?poi ?lat ?long ?photos ?template ?type ?label WHERE {
?poi <http://www.w3.org/2000/01/rdf-schema#label> ?label .
?poi <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat .
?poi <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long .
?poi <http://dbpedia.org/property/hasPhotoCollection> ?photos .
OPTIONAL {?poi <http://dbpedia.org/property/wikiPageUsesTemplate> ?template } .
OPTIONAL {?poi <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type } .
FILTER ( ?lat > x && ?lat < y &&
?long > z && ?long < ω &&
langMatches( lang(?label), "EN" ))
}

我猜这些信息分散在不同的转储 (.nt) 文件中,并且 SPARQL 端点以某种方式为我们提供了一个结果集。我需要在本地下载这些不同的 .nt 文件(不是所有 DBpedia),只执行一次查询并将结果存储在本地(我不想使用 SPARQL 端点)。

  • 这次运行我应该使用耶拿的哪些部分?

我从 this post 读起来有点困惑:

So, you can load the entire DBPedia data into a single TDB location on disk (i.e. a single directory). This way, you can run SPARQL queries over it.

  • 如果我们有三个 .nt DBpedia 文件,我如何将 DBpedia 加载到单个 TDB 位置?我们如何对这些 .nt 文件应用上述查询? (任何代码都会有所帮助。)

  • 例子,这样错了吗?

 String tdbDirectory = "C:\\TDB";
String dbdump1 = "C:\\Users\\dump1_en.nt";
String dbdump2 = "C:\\Users\\dump2_en.nt";
String dbdump3 = "C:\\Users\\dump3_en.nt";
Dataset dataset = TDBFactory.createDataset(tdbDirectory);
Model tdb = dataset.getDefaultModel(); //<-- What is the default model?Should I care?
//Model tdb = TDBFactory.createModel(tdbdirectory) ;//<--is this prefered?
FileManager.get().readModel( tdb, dbdump1, "N-TRIPLES" );
FileManager.get().readModel( tdb, dbdump2, "N-TRIPLES" );
FileManager.get().readModel( tdb, dbdump3, "N-TRIPLES" );
String q = "my big fat query";
Query query = QueryFactory.create(q);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
//do something significant with it
}
qexec.close()
tdb.close() ;
dataset.close();
  • 在上面的代码中,我们使用了 "dataset.getDefaultModel"(将默认图形作为 Jena Model)。这个声明有效吗?我们是否需要创建数据集来执行查询,还是应该使用 TDBFactory.createModel(tdbdirectory)

最佳答案

让 Jena 在本地建立索引:

/** The Constant tdbDirectory. */
public static final String tdbDirectory = "C:\\TDBLoadGeoCoordinatesAndLabels";

/** The Constant dbdump0. */
public static final String dbdump0 = "C:\\Users\\Public\\Documents\\TDB\\dbpedia_3.8\\dbpedia_3.8.owl";

/** The Constant dbdump1. */
public static final String dbdump1 = "C:\\Users\\Public\\Documents\\TDB\\geo_coordinates_en\\geo_coordinates_en.nt";

...

Model tdbModel = TDBFactory.createModel(tdbDirectory);<\n>

/*Incrementally read data to the Model, once per run , RAM > 6 GB*/
FileManager.get().readModel( tdbModel, dbdump0);
FileManager.get().readModel( tdbModel, dbdump1, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump2, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump3, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump4, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump5, "N-TRIPLES");
FileManager.get().readModel( tdbModel, dbdump6, "N-TRIPLES");
tdbModel.close();

查询耶拿:

String queryStr = "dbpedia query ";

Dataset dataset = TDBFactory.createDataset(tdbDirectory);
Model tdb = dataset.getDefaultModel();

Query query = QueryFactory.create(queryStr);
QueryExecution qexec = QueryExecutionFactory.create(query, tdb);

/*Execute the Query*/
ResultSet results = qexec.execSelect();

while (results.hasNext()) {
// Do something important
}

qexec.close();
tdb.close() ;

关于java - 使用 Jena TDB 在本地加载 DBpedia?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16832862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com