[英]Add new Individuals to an existing ontology from a dataframe using owlready 2
I have the following dataframe
我有以下
dataframe
import pandas as pd
data = [['onto.modify', 'onto.ModificationAction1']]
df = pd.DataFrame(data, columns=['queries', 'corpus'])
queries corpus
0 onto.modify onto.ModificationAction1
The elements of corpus column
are Individuals
of a particular Class
of an ontology
called onto
corpus column
的元素是称为ontology
的onto
Class
的Individuals
I want to add the elements of the queries column
as individuals
to the same Class
that the elements of the corpus column
belong to.我想将
queries column
的元素作为individuals
添加到corpus column
的元素所属的同一个Class
。
For example if the onto.ModificationAction1
belong to the onto.Thing class
the same must be for the onto.modify
例如,如果
onto.ModificationAction1
属于onto.Thing class
则onto.modify
必须相同
Initially, I loop over the corpus columns
to find in which Class
each Individual
belongs to:最初,我遍历
corpus columns
以查找每个Individual
属于哪个Class
:
for elements in df["corpus"]:
print (element.is_a)
However, I get back:但是,我回来了:
AttributeError: 'str' object has no attribute 'is_a'
So how to solve this error and eventually perform what I am describing above in the example?那么如何解决这个错误并最终执行我在上面的示例中描述的内容呢?
Error happens because values in your dataframe are strings, not individuals.发生错误是因为您的 dataframe 中的值是字符串,而不是个人。 You have to look up an individual based on its name using
search()
or search_one()
(see documentation ).您必须使用
search()
或search_one()
根据姓名查找个人(请参阅文档)。 This code should do the job:这段代码应该完成这项工作:
import pandas as pd
import owlready2
data = [['onto.modify', 'onto.ModificationAction1']]
df = pd.DataFrame(data, columns=['queries', 'corpus'])
onto = owlready2.get_ontology("http://test.org/onto.owl")
class Action(owlready2.Thing):
namespace = onto
modification_action = Action(name="ModificationAction1")
for row in df.iterrows():
row_series = row[1]
# Trow away ontology name, get only individual name
corpus_individual_name = row_series['corpus'].split('.', 1)[1]
# Find individual
corpus_individual = onto.search_one(iri=onto.base_iri + corpus_individual_name)
# Get individual classes
corpus_classes = corpus_individual.is_a
# Trow away ontology name, get only individual name
query_individual_name = row_series['queries'].split('.', 1)[1]
# Create new individual with first class of old individual
new_individual = corpus_classes[0](name=query_individual_name)
# In case old individual is multiclass, add remaining classes to new individual
for corpus_class in corpus_classes[1:]:
new_individual.is_a.append(corpus_class)
for i in onto.individuals():
print(i, i.is_a)
You can see the new individual in the output along with its class.您可以在 output 及其 class 中看到新个人。
onto.ModificationAction1 [onto.Action]
onto.modify [onto.Action]
PS: I'm assuming here that you have only one ontology -- onto
. PS:我在这里假设你只有一个本体——
onto
。 If not, you should either 1) Have a lookup dictionary for ontologies to map "onto"
into actual ontology object/IRI, or 2) instead of “onto.”
如果不是,你应该 1) 有一个本体的查找字典 map
"onto"
到实际的本体对象/IRI,或者 2) 而不是“onto.”
prefix specify full IRI in your dataframe and lookup ontology by IRI.前缀在您的 dataframe 中指定完整的 IRI,并通过 IRI 查找本体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.