使用owlready2从数据框中向现有本体添加新的个体。

huangapple go评论117阅读模式
英文:

Add new Individuals to an existing ontology from a dataframe using owlready 2

问题

我有以下的dataframe

import pandas as pd

data = [['onto.modify', 'onto.ModificationAction1']]
df = pd.DataFrame(data, columns=['queries', 'corpus'])

       queries                corpus
0      onto.modify            onto.ModificationAction1

corpus列的元素是一个名为ontology2的本体中某个特定ClassIndividuals

我想将queries列的元素作为Individuals添加到与corpus列的元素所属的相同Class中。

例如,如果onto.ModificationAction1属于ontology2.Thing类,那么ontology2.modify也应该属于该类。

最初,我循环遍历corpus列以找出每个Individual所属的Class

for element in df["corpus"]:
    print(element.is_a)

然而,我得到了以下错误信息:

AttributeError: 'str' object has no attribute 'is_a'

那么,我该如何解决这个错误,并最终执行上述示例中描述的操作呢?

英文:

I have the following 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 ontology2.

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.

For example, if the onto.ModificationAction1 belong to the ontology2.Thing class the same must be for the ontology2.modify.

Initially, I loop over the corpus columns to find in which Class each Individual belongs to:

for elements in df["corpus"]:
    print (element.is_a)

However, I get back:

> AttributeError: 'str' object has no attribute 'is_a'

So how do I solve this error and eventually perform what I am describing above in the example?

答案1

得分: 2

错误发生是因为您的数据框中的值是字符串,而不是个体。您需要使用search()search_one()根据名称查找个体(请参阅文档)。以下代码可以完成这个任务:

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]
    # 丢弃本体名称,只获取个体名称
    corpus_individual_name = row_series['corpus'].split('.', 1)[1]
    # 查找个体
    corpus_individual = onto.search_one(iri=onto.base_iri + corpus_individual_name)
    # 获取个体的类
    corpus_classes = corpus_individual.is_a
    # 丢弃本体名称,只获取个体名称
    query_individual_name = row_series['queries'].split('.', 1)[1]
    # 使用旧个体的第一个类创建新个体
    new_individual = corpus_classes[0](name=query_individual_name)
    # 如果旧个体是多类的,将剩余的类添加到新个体中
    for corpus_class in corpus_classes[1:]:
        new_individual.is_a.append(corpus_class)

for i in onto.individuals():
    print(i, i.is_a)

您可以在输出中看到新的个体以及它的类。

onto.ModificationAction1 [onto.Action]
onto.modify [onto.Action]

附注:我在这里假设您只有一个本体--onto。如果不是这样,您应该要么1)有一个本体的查找字典,将"onto"映射到实际的本体对象/IRI,要么2)在您的数据框中指定完整的IRI,并通过IRI查找本体。

英文:

Error happens because values in your dataframe are strings, not individuals. You have to look up an individual based on its name using search() or search_one() (see documentation). 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.

onto.ModificationAction1 [onto.Action]
onto.modify [onto.Action]

P.S.: I'm assuming here that you have only one ontology -- 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.” prefix specify full IRI in your dataframe and lookup ontology by IRI.

huangapple
  • 本文由 发表于 2023年1月8日 22:47:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定