如何使用owlready2读取关于本体的信息

2024-05-29 11:10:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我在flask应用程序中工作,希望加载本体并打印该本体中有多少类和多少个人

这是我的代码,它不工作

import flask
from owlready2 import *

app = flask.Flask(__name__)
app.config["DEBUG"] = True


@app.route('/', methods=['GET'])
def start():
    onto_path.append("pizza.owl")
    onto = get_ontology("pizza.owl")
    onto.load()
    print(onto.classes())
    print(list(onto.individuals()))

    html = ''
    html += '<h2>clases: ' + onto.classes() + '</br>'
    html += '<h3>individuals: ' + onto.individuals()    

    return html

    #return "<h1>Distant Reading Archive</h1><p>This site is a prototype API for distant reading of science fiction novels.</p>"

app.run()

Tags: 代码importapp应用程序flaskreturnhtml本体
1条回答
网友
1楼 · 发布于 2024-05-29 11:10:12

方法classes()individuals()返回一个生成器,因此您应该将生成器强制转换为一个列表,并询问该对象的长度

n_classes = len(list(onto.classes()))
n_individuals = len(list(onto.individuals()))

在那之后,你应该在你的变量上有数字,你可以把它们和你的HTML连接起来

相关问题 更多 >

    热门问题