skos对象模型若干核心元素的基本实现
python-skos的Python项目详细描述
#python skos
[![生成状态](https://secure.travis-ci.org/geo-data/python-skos.png)](http://travis-ci.org/geo-data/python-skos)
##概述
这个包提供了核心的some的基本实现 SKOS对象模型的元素,以及用于加载SKOS的API XML资源见 [SKOS Primer](http://www.w3.org/TR/skos-primer)介绍 斯科斯。
对象模型建立在[SQLAlchemy](http://sqlalchemy.org)的基础上 在sql中提供对象模型的持久性和查询 数据库。
用法
首先,包支持python的 [日志模块](http://docs.python.org/library/logging.html),其中 可以提供有关各种模块操作的有用反馈,因此让我们 激活它:
>>> import logging >>> logging.basicConfig(level=logging.INFO)
包读取由rdflib
库生成的图,因此让我们
将(相当做作的)SKOS XML文件解析为图形:
>>> import rdflib >>> xml = """<?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:owlxml="http://www.w3.org/2006/12/owl2-xml#"> <skos:Concept rdf:about="http://my.fake.domain/test1"> <skos:prefLabel>Acoustic backscatter in the water column</skos:prefLabel> <skos:definition>Includes all parameters covering the strength of acoustic signal return, including absolute measurements of returning signal strength as well as parameters expressed as backscatter (the proportion of transmitted signal returned)</skos:definition> <owlxml:sameAs rdf:resource="http://vocab.nerc.ac.uk/collection/L19/current/005/"/> <skos:broader rdf:resource="http://vocab.nerc.ac.uk/collection/P05/current/014/"/> <skos:narrower rdf:resource="http://vocab.nerc.ac.uk/collection/P01/current/ACBSADCP/"/> <skos:related rdf:resource="http://my.fake.domain/test2"/> </skos:Concept> <skos:Collection rdf:about="http://my.fake.domain/collection"> <dc:title>Test Collection</dc:title> <dc:description>A collection of concepts used as a test</dc:description> <skos:member rdf:resource="http://my.fake.domain/test1"/> <skos:member> <skos:Concept rdf:about="http://my.fake.domain/test2"> <skos:prefLabel>Another test concept</skos:prefLabel> <skos:definition>Just another concept</skos:definition> <skos:related rdf:resource="http://my.fake.domain/test1"/> </skos:Concept> </skos:member> </skos:Collection> </rdf:RDF>""" >>> graph = rdflib.Graph() >>> graph.parse(data=xml, format="application/rdf+xml")
现在我们可以使用skos.RDFLoader
对象来访问skos数据
作为python对象:
>>> import skos >>> loader = skos.RDFLoader(graph)
这实现了一个映射接口:
>>> loader.keys() ['http://my.fake.domain/test1', 'http://my.fake.domain/test2', 'http://my.fake.domain/collection'] >>> loader.values() [<Concept('http://my.fake.domain/test1')>, <Concept('http://my.fake.domain/test2')>, <Collection('http://my.fake.domain/collection')>] >>> len(loader) 3 >>> concept = loader['http://my.fake.domain/test1'] >>> concept <Concept('http://my.fake.domain/test1')>
以及返回特定映射的一些方便方法 类型:
>>> loader.getConcepts() {'http://my.fake.domain/test1': <Concept('http://my.fake.domain/test1')>, 'http://my.fake.domain/test2': <Concept('http://my.fake.domain/test2')>} >>> loader.getCollections() {'http://my.fake.domain/collection': <Collection('http://my.fake.domain/collection')>} >>> loader.getConceptSchemes() # we haven't got any `ConceptScheme`s {}
注意,您可以将python skos对象转换回它们的rdf
使用RDFBuilder
类表示:
>>> builder = RDFBuilder() >>> objects = loader.values() >>> another_graph = builder.build(objects)
RDFLoader
构造函数还接受一个max_depth
参数,该参数
默认为0
。此参数确定RDF的深度
资源得到解决,即用于限制
链接被递归地跟踪。例如,下面允许
要分析和解析的外部资源级别:
>>> loader = skos.RDFLoader(graph, max_depth=1) # you need to be online for this! INFO:skos:parsing http://vocab.nerc.ac.uk/collection/L19/current/005/ INFO:skos:parsing http://vocab.nerc.ac.uk/collection/P05/current/014/ INFO:skos:parsing http://vocab.nerc.ac.uk/collection/P01/current/ACBSADCP/
如果你想解决一个完整的(并且可能非常大的!)图表
然后使用max_depth=float('inf')
另一个构造函数参数是布尔标志flat
这个罐头
也可以在实例化后使用RDFLoader.flat
进行切换
财产当设置为False
(默认值)时,只有skos对象存在
在初始图中直接被loader:objects引用
通过分析其他资源间接创建的将只被引用
按顶层对象:
>>> loader.keys() # lists 3 objects ['http://my.fake.domain/test1', 'http://my.fake.domain/test2', 'http://my.fake.domain/collection'] >>> concept = loader['http://my.fake.domain/test1'] >>> concept.synonyms # other objects are still correctly referenced by the object model {'http://vocab.nerc.ac.uk/collection/L19/current/005/': <Concept('http://vocab.nerc.ac.uk/collection/L19/current/005/')>} >>> 'http://vocab.nerc.ac.uk/collection/L19/current/005/' in loader # but are not referenced directly False >>> loader.flat = True # flatten the structure so *all* objects are directly referenced >>> loader.keys() # lists all 6 objects ['http://vocab.nerc.ac.uk/collection/P05/current/014/', 'http://vocab.nerc.ac.uk/collection/L19/current/005/', 'http://my.fake.domain/collection', 'http://my.fake.domain/test1', 'http://my.fake.domain/test2', 'http://vocab.nerc.ac.uk/collection/P01/current/ACBSADCP/'] >>> 'http://vocab.nerc.ac.uk/collection/L19/current/005/' in loader True
上面演示的Concept.synonyms
显示了容器(一个
实例skos.Concepts
),其中skos::exactMatch
owlxml::sameAs
放置引用。skos.Concepts
容器
类是一个可通过set
类add
和
discard
方法,以及响应键上的del
:
>>> synonym = skos.Concept('test3', 'a synonym for test1', 'a definition') >>> concept.synonyms.add(synonym) >>> concept.synonyms {'http://vocab.nerc.ac.uk/collection/L19/current/005/': <Concept('http://vocab.nerc.ac.uk/collection/L19/current/005/')>, 'test3': <Concept('test3')>} >>> del concept.synonyms['test3'] # or... >>> concept.synonyms.discard(synonym)
类似于Concept.synonyms
Concept.broader
,Concept.narrower
而Concept.related
都是skos.Concepts
:
>>> assert concept in concept.broader['http://vocab.nerc.ac.uk/collection/P05/current/014/'].narrower
Concept
实例还提供了对其他SKOS数据的简单访问:
>>> concept.uri 'http://my.fake.domain/test1' >>> concept.prefLabel 'Acoustic backscatter in the water column' >>> concept.definition 'Includes all parameters covering the strength of acoustic signal return, including absolute measurements of returning signal strength as well as parameters expressed as backscatter (the proportion of transmitted signal returned)'
访问ConceptScheme
和Collection
对象
concept belongs也可以通过Concept.schemes
和
Concept.collections
属性分别:
>>> concept.collections {'http://my.fake.domain/collection': <Collection('http://my.fake.domain/collection')>} >>> collection = concept.collections['http://my.fake.domain/collection'] >>> assert concept in collection.members
以及Collection.members
属性,Collection
实例
提供对其他SKOS数据的访问:
>>> collection.uri 'http://my.fake.domain/collection' >>> collection.title collection.title >>> collection.description 'A collection of concepts used as a test'
Collection.members
是一个skos.Concepts
实例,因此新成员可以
使用skos.Concepts
界面添加和删除:
>>> collection.members.add(synonym) >>> del collection.members['test3']
###与sqlalchemy集成
python-skos
被设计为与sqlalchemy集成
必要时进行ORM。这提供了可伸缩的数据持久性和
查询功能。下面的示例使用内存中的SQLite
数据库提供什么是可能的尝试探索
[sqlalchemy orm文档](http://docs.sqlalchemy.org/en/latest/)
在此基础上,使用替代数据库和查询
技术…
>>> from sqlalchemy import create_engine >>> engine = create_engine('sqlite:///:memory:') # the in-memory database >>> from sqlalchemy.orm import sessionmaker >>> Session = sessionmaker(bind=engine) >>> session1 = Session() # get a session handle on the database >>> skos.Base.metadata.create_all(session1.connection()) # create the required database schema >>> session1.add_all(loader.values()) # add all the skos objects to the database >>> session1.commit() # commit these changes>>> session2 = Session() # a new database session, created somewhere else ;) >>> session2.query(skos.Collection).first() # obtain our one and only collection <Collection('http://my.fake.domain/collection')> >>> # get all concepts that have the string 'water' in them: >>> session2.query(skos.Concept).filter(skos.Concept.prefLabel.ilike('%water%')).all() [<Concept('http://my.fake.domain/test1')>, <Concept('http://vocab.nerc.ac.uk/collection/P01/current/ACBSADCP/')>]
##要求
- [Python](http://www.python.org)==2.{6,7}
- [SQLAlchemy](http://www.sqlalchemy.org)SQLAlchemy>;=0.7.5
- [RDFLib](http://pypi.python.org/pypi/rdflib)>;=2.4.2
- [ISO8601plus](http://pypi.python.org/pypi/iso8601plus)
- [unittest2](http://pypi.python.org/pypi/unittest2)如果使用Python<;2.7运行测试
###下载
软件的当前和以前版本可在 <;http://github.com/geo-data/python-skos/tags>;和 <;http://pypi.python.org/pypi/python-skos>
###安装
下载并解压源代码,然后从根目录运行以下命令 分发目录:
python setup.py install
建议您也运行:
python setup.py test
这将练习综合包测试套件。
##限制
- <里>最新的skos核心对象模型中只有一部分是
支持。扩展代码以支持更多的sko
然而,规范并不难
##问题和贡献
请使用 [GitHub问题跟踪程序](https://github.com/geo-data/python-skos)
代码和文档贡献是非常受欢迎的,无论是作为GitHub pull 请求或修补程序。
##许可证
[BSD 2子句](http://opensource.org/licenses/BSD-2-Clause)
##联系人
霍姆·兹瓦格斯特拉hrz@geodata.soton.ac.uk>;