0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【owlready2】(7)Pythonでオントロジーを作成

0
Posted at

3.Pythonでオントロジーを作成

3.1 空のオントロジーを作る

get_ontology関数を利用する。

>>> from owlready2 import *
>>> onto = get_ontology('http://test.org/onto.owl#')
>>> type(get_ontology)
<class 'method'>
>>> type(onto)
<class 'owlready2.namespace.Ontology'>
>>> onto.search(iri='*')
[.anonymous, test.org.onto.owl]

get_ontologyの返り値はオントロジーオブジェクトである。このオントロジーにおいて、エンティティやRDFトリプル(SVOの組)を定義、設定するには、Pythonのwith構文を用いる。

with onto:
  <Python code>

3.2 クラスの作成

オントロジーのクラスはPythonのクラスと同じであった。よってclass クラス名(親クラス): としてクラス定義できる。

>>> with onto:
...     class Bacterium(Thing): pass
...     class Shape(Thing): pass
...     class Grouping(Thing): pass
...
>>> onto.search(iri='*')
[.anonymous, test.org.onto.owl, onto.Bacterium, onto.Shape, onto.Grouping]

owlready2にはログレベルという内部処理状態を表示する範囲がある。レベルは0~9である。デフォルトはレベル0である。次のように機能する。

>>> set_log_level(9)
>>> with onto:
...     class TestClass(Thing): pass
...
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#TestClass http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#TestClass http://www.w3.org/2000/01/rdf-schema#subClassOf http://www.w3.org/2002/07/owl#Thing

これは、

$:TestClass\ \ \ rdf:type\ \ \ owl:Class$
$:TestClass\ \ \ rdfs:subClassOf\ \ \ owl:Thing$

のトリプルができたことを表している。次の例は継承関係がよくわかるだろう。

>>> with onto:
...     class Rod(Shape): pass
...     class Isolated(Grouping): pass
...     class InPair(Grouping): pass
...
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#Rod http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#Rod http://www.w3.org/2000/01/rdf-schema#subClassOf http://test.org/onto.owl#Shape
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#Isolated http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#Isolated http://www.w3.org/2000/01/rdf-schema#subClassOf http://test.org/onto.owl#Grouping
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#InPair http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.w3.org/2002/07/owl#Class
* Owlready2 * ADD TRIPLE http://test.org/onto.owl#InPair http://www.w3.org/2000/01/rdf-schema#subClassOf http://test.org/onto.owl#Grouping

他のオントロジーを利用した継承もできる。

>>> set_log_level(0)
>>> onto_chap3 = get_ontology('bacteria.owl').load()
>>> with onto:
...     class MyBacteriumClass(onto_chap3.Bacterium): pass
...
>>> onto.MyBacteriumClass.iri
'http://test.org/onto.owl#MyBacteriumClass'
>>> list(onto.MyBacteriumClass.ancestors())
[bacteria.Bacterium, owl.Thing, onto.MyBacteriumClass]
>>> onto_chap3
get_ontology("http://lesfleursdunormal.fr/static/_downloads/bacteria.owl#")
>>> default_world.search(iri='*')
[.anonymous, test.org.onto.owl, onto.Bacterium, onto.Shape, onto.Grouping, onto.TestClass, onto.Rod, onto.Isolated, onto.InPair, _downloads.bacteria.owl, bacteria.has_shape, bacteria.Bacterium, bacteria.Shape, bacteria.is_shape_of, bacteria.has_grouping, bacteria.Grouping, bacteria.is_grouping_of, bacteria.gram_positive, bacteria.nb_colonies, bacteria.Round, bacteria.Rod, bacteria.Isolated, bacteria.InPair, bacteria.InCluster, bacteria.InChain, bacteria.InSmallChain, bacteria.InLongChain, bacteria.Pseudomonas, bacteria.Coccus, bacteria.Bacillus, bacteria.Staphylococcus, bacteria.Streptococcus, bacteria.round1, bacteria.in_cluster1, bacteria.unknown_bacterium, onto.MyBacteriumClass]
>>> onto_chap3.Bacterium
bacteria.Bacterium

bacteria.*がonto_chap3オントロジーにあるクラス、インディビデュアルだとわかる。
多重継承もできる。

>>> with onto:
...     class RNA(Thing): pass
...     class Enzyme(Thing): pass
...     class Ribozyme(RNA, Enzyme): pass

RDF/XML書式では次のとおりである。

<owl:Class rdf:about="#RNA">
  <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>

<owl:Class rdf:about="#Enzyme">
  <rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
</owl:Class>

<owl:Class rdf:about="#Ribozyme">
  <rdfs:subClassOf rdf:resource="#RNA"/>
  <rdfs:subClassOf rdf:resource="#Enzyme"/>
</owl:Class>

Pythonではクラスを動的に作れる。ここでの「動的」の意味は、プログラム処理中にクラス名や親クラスをデータとして与えてクラスを生成する、ということである。データとしては文字列型のデータでよい。この機能を利用すれば、オントロジーでクラスを動的に作ることができる。動的にクラスを作るためにはtypesモジュールをインポートする。

>>> import types
>>> class_name = 'MyClass'
>>> SuperClasses = [owl['Thing']]
>>> with onto:
...     NewClass = types.new_class(class_name, tuple(SuperClasses))
...
>>> NewClass
onto.MyClass
>>> list(NewClass.ancestors())
[onto.MyClass, owl.Thing]

この例ではクラスを生成するのに"MyClass"と"Thing"という文字列しか使っていないことが分かる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?