在Python中加载本体
我有一个用OWL写的本体(Ontology)。有没有人知道我怎么把它加载到Python里?有没有什么包可以用,或者手动加载的方法?在其他问题中提到的rdflib对我来说不太合适,因为它主要是处理RDF的。而"Seth"这个库虽然很不错,但它不工作,因为它需要"Pellet"库,而那个网站似乎已经无法访问了,而且"Seth"也只支持JVM 1.4!
5 个回答
2
如果有人再次遇到这个问题,现在有一个可以在Python中与OWL本体进行交互的包:https://pypi.org/project/Owlready2/
根据项目描述,它有以下功能:
- 可以导入OWL 2.0本体,支持NTriples、RDF/XML或OWL/XML格式。
- 可以将OWL 2.0本体导出为NTriples或RDF/XML格式。
- 可以像使用普通Python对象一样,轻松操作本体的类、实例和属性。
- 可以为本体类添加Python方法。
- 使用HermiT推理器自动对类和实例进行分类。
- 经过测试,支持最多1亿个RDF三元组(但可能支持更多)。
- 此外,四元组存储与RDFlib Python模块兼容,可以用来执行SPARQL查询。
2
在rdfAlchemy这个项目里,有一个叫做commands.py
的文件,其中有一个命令部分,正是为了这个目的而开始的。虽然我有一段时间没用过它,但这正是你提到的内容,也就是从本体(ontology)创建Python框架的起步。
实际上,它使用了paster的“命令”。如果你进入rdfalchemy的子目录,然后输入:
paster rdfSubject -h
你应该会看到:
Usage: /home/phil/venv/some_path/bin/paster rdfSubject [options] paster rdfalchemy.commands Create an rdfSubject subclass with descriptors from an RDF Schema will set the rdf_type Descriptors will be created 1. rdfs:domain and rdfs:range are respected 2. rdfSingle is used for properties that are * owl:InverseFunctionalProperty * owl:FunctionalProperty 3. rdfList or rdfContainer is used if the proper range is set 4. rdfMultiple is used for all others The resulting .py file is ment to be a skeleton for the developers confvience. Do not expect to be able to use the raw results. Create an rdfSubject subclass with descriptors from an RDF Schema Options: -h, --help show this help message and exit -v, --verbose -n, --simulate -s SCHEMA, --schema=SCHEMA file name or url of rdfSchema for this class -o FOUT, --fout=FOUT output file name default: stdout (e.g. ../MyRdfModel.py) -l, --list list valid instances of `owl:Class` in the schema file
2
我还没有完全尝试过这个,不过你可以考虑把你的本体(ontology)加载到一个Sesame数据库里,然后用Python的工具来查询它。我玩过一点RDFAlchemy和pySesame,但我对它们的好坏还没有太多感觉。总的来说,我觉得RDF/OWL数据库还不够成熟,所以你可能会遇到一些比较大的技术难题。