当前位置:网站首页>基于apache-jena的知识问答
基于apache-jena的知识问答
2022-07-06 09:13:00 【zkkkkkkkkkkkkk】
前言
这篇文章主要写如何使用Python对apache-jena进行交互查询。具体三元组数据建立、转换、导入内容请看:知识问答三元组数据准备阶段 。本文在知识问答三元组数据准备阶段的基础上,接着往下写。
注:本文案例代码使用 https://github.com/zhangtao-seu/Jay_KG 中的代码
目录
一、代码目录结构
下图为Jay_KG项目的代码目录结构。其中重要的是query_main.py和question_temp.py文件,其中前者为程序主入口,后者为知识问答模板的定义。
二、知识问答实现
2.1、三元组数据获取
本文案例使用potege软件建模,最后导出owl文件,后继续根据 知识问答三元组数据准备阶段 文中的2.2小节内容一步一步做。本案例使用potege建模后如下图所示
2.1、定义实体
在Jay_KG/KB_query/external_dict中有一个sanguo.txt,这就是博主定义的实体文件,内容如下图所示
2.2、定义问题模板
打开 question_temp.py文件,定义模板,如下代码所示,QuestionSet类下的o_name、rides函数定义了关羽字(别名、小名)是什么?被、和关羽的坐骑(骑、战马)是什么?两个模板。
# encoding=utf-8
"""
@desc:
设置问题模板,为每个模板设置对应的SPARQL语句。demo提供如下模板:
"""
from refo import finditer, Predicate, Star, Any, Disjunction
import re
# # TODO SPARQL前缀和模板
# SPARQL_PREXIX = u"""
# PREFIX owl: <http://www.w3.org/2002/07/owl#>
# PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
# PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
# PREFIX : <http://www.semanticweb.org/张涛/ontologies/2019/1/untitled-ontology-32#>
# """
SPARQL_PREXIX = u"""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX : <http://www.semanticweb.org/dell/ontologies/2022/0/untitled-ontology-3#>
"""
SPARQL_SELECT_TEM = u"{prefix}\n" + \
u"SELECT {select} WHERE {
{\n" + \
u"{expression}\n" + \
u"}}\n"
class W(Predicate):
def __init__(self, token=".*", pos=".*"):
self.token = re.compile(token + "$")
self.pos = re.compile(pos + "$")
super(W, self).__init__(self.match)
def match(self, word):
m1 = self.token.match(word.token.decode("utf-8"))
m2 = self.pos.match(word.pos)
return m1 and m2
class Rule(object):
def __init__(self, condition_num, condition=None, action=None):
assert condition and action
self.condition = condition
self.action = action
self.condition_num = condition_num
# word_object : [词,词性]
def apply1(self, sentence):
matches = []
# 【person_entity】
for m in finditer(self.condition, sentence):
i, j = m.span()
matches.extend(sentence[i:j])
return self.action(matches), self.condition_num
class QuestionSet:
def __init__(self):
pass
@staticmethod
def o_name(word_object):
#关羽字什么?
select = u"?o"
sparql = None
for w in word_object:
if w.pos == pos_person:
e = u" :{person} :字 ?o.".format(person=w.token.decode('utf-8'))
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREXIX,
select=select,
expression=e)
print(sparql)
break
return sparql
@staticmethod
def rides(word_object):
#关羽战马是什么?
select = u"?o"
sparql = None
for w in word_object:
if w.pos == pos_person:
e = u" :{person} :骑 ?o.".format(person=w.token.decode('utf-8'))
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREXIX,
select=select,
expression=e)
print(sparql)
break
return sparql
# TODO 定义关键词
pos_person = "nr"
person_entity = (W(pos=pos_person))
other_name = (W("字") | W("别名") | W("小名"))
ride = (W("骑") | W("坐骑") | W("战马"))
# TODO 问题模板/匹配规则
"""
# 关羽字什么?
# 关羽的战马是什么?
"""
rules = [
# 关羽字什么?
Rule(condition_num=0, condition=person_entity + Star(Any(), greedy=False) + other_name + Star(Any(), greedy=False), action=QuestionSet.o_name),
Rule(condition_num=0, condition=person_entity + Star(Any(), greedy=False) + ride + Star(Any(), greedy=False), action=QuestionSet.rides),
]
三、运行代码进行问答
进入query_main文件运行,后在控制台输入之前定义好的模板,即可收到返回。
边栏推荐
- Mysql21 - gestion des utilisateurs et des droits
- Mysql26 use of performance analysis tools
- Mysql21 user and permission management
- Mysql23 storage engine
- API learning of OpenGL (2001) gltexgen
- CSDN markdown editor
- La table d'exportation Navicat génère un fichier PDM
- February 13, 2022-2-climbing stairs
- MySQL completely uninstalled (windows, MAC, Linux)
- [untitled]
猜你喜欢
How to find the number of daffodils with simple and rough methods in C language
【博主推荐】C# Winform定时发送邮箱(附源码)
Win10: how to modify the priority of dual network cards?
Mysql33 multi version concurrency control
Mysql23 storage engine
Mysql24 index data structure
MySQL 20 MySQL data directory
[recommended by bloggers] asp Net WebService background data API JSON (with source code)
Navicat 导出表生成PDM文件
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
随机推荐
Mysql25 index creation and design principles
Why is MySQL still slow to query when indexing is used?
API learning of OpenGL (2005) gl_ MAX_ TEXTURE_ UNITS GL_ MAX_ TEXTURE_ IMAGE_ UNITS_ ARB
Mysql21 - gestion des utilisateurs et des droits
MySQL25-索引的创建与设计原则
MySQL主从复制、读写分离
Mysql33 multi version concurrency control
Data dictionary in C #
Install mysql5.5 and mysql8.0 under windows at the same time
NPM an error NPM err code enoent NPM err syscall open
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
Mysql30 transaction Basics
CSDN问答模块标题推荐任务(二) —— 效果优化
Mysql26 use of performance analysis tools
Generate PDM file from Navicat export table
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Mysql 其他主机无法连接本地数据库
Idea import / export settings file
Navicat 导出表生成PDM文件