1、下载apache-jena-fuseki和apache-jena
https://jena.apache.org/download/index.cgi
2、将上一篇教程的nt文件转换为tdb格式
cd apache-jena-3.9.0\bat tdbloader.bat --loc="PATH_TO_TDB\tdb" "PATH_TO_NT\movies_mapping.nt"
3、切换到apache-jena-fuseki-3.9.0目录,启动一次服务,然后退出
4、将教程1里面的Movies.owl,拷贝到apache-jena-fuseki-3.9.0\run\databases路径下面,并重命名为Movies.ttl
5、创建配置文件apache-jena-fuseki-3.9.0\run\configuration\fuseki_conf.ttl
@prefix fuseki: <http://jena.apache.org/fuseki#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix tdb: <http://jena.hpl.hp.com/2008/tdb#> . @prefix ja: <http://jena.hpl.hp.com/2005/11/Assembler#> . @prefix : <http://base/#> . <#service> rdf:type fuseki:Service ; fuseki:name "movies" ; fuseki:serviceQuery "sparql" ; fuseki:dataset <#dataset> ; fuseki:serviceReadGraphStore "get" ; fuseki:serviceReadWriteGraphStore "data" ; fuseki:serviceUpdate "update" ; fuseki:serviceUpload "upload" . <#dataset> rdf:type ja:RDFDataset ; ja:defaultGraph <#modelInf> ; . <#modelInf> rdf:type ja:InfModel ; #ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/OWLFBRuleReasoner>] ja:reasoner [ ja:reasonerURL <http://jena.hpl.hp.com/2003/GenericRuleReasoner> ; ja:rulesFrom <file:///D:/ProjectsMy/KG/apache-jena-fuseki-3.9.0/run/databases/Rules.ttl> ] ; ja:baseModel <#baseModel> ; . <#baseModel> rdf:type tdb:GraphTDB ; tdb:location "D:/ProjectsMy/KG/workspace/data/tdb" ; tdb:unionDefaultGraph true ; .
6、创建规则文件apache-jena-fuseki-3.9.0\run\databases\Movies.ttl
这个规则规定了,演过喜剧的演员,叫做喜剧演员(Comedian)
@prefix xsd: <XML Schema> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix : <https://www.neohope.com/hansen/ontologies/2018/movies#> . [ruleComedian: (?aPerson :hasActedIn ?aMovie) (?aMovie :hasGenre ?aGenre) (?aGenre :genreName '喜剧') -> (?aPerson rdf:type :Comedian)] [ruleInverse: (?aPerson :hasActedIn ?aMove) -> (?aMovie :hasActor ?aPerson)]
7、启动apache-jena-fuseki-3.9.0
8、访问http://localhost:3030/
9、进行查询,上一篇的例子也都可以用
http://localhost:3030/dataset.html?tab=query&ds=/movies
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix : <https://www.neohope.com/hansen/ontologies/2018/movies#> SELECT ?name WHERE { ?aComedian rdf:type :Comedian. ?aComedian :personName ?name. } LIMIT 10
10、通过python访问
https://github.com/neohope/kg-demo-for-movie/tree/master/src/query-jena.py
参考链接:
https://zhuanlan.zhihu.com/knowledgegraph
https://github.com/SimmerChan/KG-demo-for-movie
PS:
参考教程中,原作者通过结巴分词+正则匹配+Jena,实现了一个简单的问答系统,感兴趣的话,大家可以看下。