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::exactMatchowlxml::sameAs放置引用。skos.Concepts容器 类是一个可通过setadddiscard方法,以及响应键上的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.synonymsConcept.broaderConcept.narrowerConcept.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)'

访问ConceptSchemeCollection对象 concept belongs也可以通过Concept.schemesConcept.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/')>]

##要求

###下载

软件的当前和以前版本可在 <;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>;

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JPA。Eclipselink没有为mySQL提供密码,但它应该提供   我的Servlet和@FormDataParam存在java问题   java将什么作为上下文参数传递到文件I/O方法中?   如果两个值相同,java无法找到其中一个单选按钮   java在变量和方法名中使用下划线   JavaSpringMVC单线程安全?   klazz类的java Arraylist(反射Api)   java如何在数字字符串中查找最频繁的数字?   JavaAPI设计:使数据更易于阅读与强制更多API调用   JavaHadoopMapReduceforGoogleWebGraph   java无法启动gauge API:Runner意外退出   java如何在bluemix上使用ibm工作负载调度器?   拉取一年中某一周特定日期的所有日期   java为什么是我的角节点。js应用程序将图像上传到S3� 邮递员正确上传时的符号?   在不使用任何第三方jar的情况下将文件从本地传输到linux系统(java代码)   java将现有文件夹复制到Eclipse工作区中新创建的项目中   Java中的regex RegExp帮助   当使用“系统”外观时,Java组合框setSelectedItem会出现故障   JavaASM:在类的方法中获取局部变量名和值