当前位置:网站首页>Knowledge Q & A based on Apache Jena
Knowledge Q & A based on Apache Jena
2022-07-06 11:06:00 【zkkkkkkkkkkkkk】
Preface
This article is mainly about how to use Python Yes apache-jena Interactive query . Specific triple data establishment 、 transformation 、 Please see : Knowledge Q & a triplet data preparation stage . This paper is based on the data preparation stage of Knowledge Q & a triplet , Then write down .
notes : The case code of this article uses https://github.com/zhangtao-seu/Jay_KG The code in
Catalog
One 、 Code directory structure
Two 、 Knowledge Q & A implementation
3、 ... and 、 Run the code for question and answer
One 、 Code directory structure
The following figure for Jay_KG The code directory structure of the project . The important thing is query_main.py and question_temp.py file , The former is the main entry of the program , The latter is the definition of knowledge question and answer template .

Two 、 Knowledge Q & A implementation
2.1、 Triple data acquisition
This case uses potege Software modeling , Finally, export owl file , Then continue according to Knowledge Q & a triplet data preparation stage In this paper, the 2.2 Section content is done step by step . Use in this case potege After modeling, see the following figure

2.1、 Define the entity
stay Jay_KG/KB_query/external_dict There is one of them. sanguo.txt, This is the entity file defined by the blogger , The content is shown in the figure below

2.2、 Define problem templates
open question_temp.py file , Define templates , As shown in the following code ,QuestionSet Under class o_name、rides Function defines Guan Yu word ( Alias 、 nickname ) What is it? ? By 、 And Guan Yu's Mount ( Riding a 、 War horse ) What is it? ? Two templates .
# encoding=utf-8
"""
@desc:
Set the problem template , Set the corresponding... For each template SPARQL sentence .demo Provide the following template :
"""
from refo import finditer, Predicate, Star, Any, Disjunction
import re
# # TODO SPARQL Prefix and template
# 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/ zhang /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 : [ word , The part of speech ]
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):
# What is Guan Yu's word ?
select = u"?o"
sparql = None
for w in word_object:
if w.pos == pos_person:
e = u" :{person} : word ?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):
# What is Guan Yu's war horse ?
select = u"?o"
sparql = None
for w in word_object:
if w.pos == pos_person:
e = u" :{person} : Riding a ?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 Define keywords
pos_person = "nr"
person_entity = (W(pos=pos_person))
other_name = (W(" word ") | W(" Alias ") | W(" nickname "))
ride = (W(" Riding a ") | W(" mount ") | W(" War horse "))
# TODO Question template / Matching rules
"""
# What is Guan Yu's word ?
# What is Guan Yu's war horse ?
"""
rules = [
# What is Guan Yu's word ?
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),
]
3、 ... and 、 Run the code for question and answer
Get into query_main File run , The template defined before console input , You can receive the return .
边栏推荐
- Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
- API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
- Solution: log4j:warn please initialize the log4j system properly
- csdn-Markdown编辑器
- [recommended by bloggers] background management system of SSM framework (with source code)
- Idea import / export settings file
- Navicat 導出錶生成PDM文件
- C语言标准的发展
- Yum prompt another app is currently holding the yum lock; waiting for it to exit...
- 【博主推荐】SSM框架的后台管理系统(附源码)
猜你喜欢

一键提取pdf中的表格

Swagger、Yapi接口管理服务_SE

MySQL20-MySQL的数据目录

【博主推荐】C#生成好看的二维码(附源码)

Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis

02-项目实战之后台员工信息管理

安装numpy问题总结

MySQL主從複制、讀寫分離
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)

Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
随机推荐
【博主推荐】C#生成好看的二维码(附源码)
frp内网穿透那些事
Mysql 其他主机无法连接本地数据库
Invalid global search in idea/pychar, etc. (win10)
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
图片上色项目 —— Deoldify
Mysql22 logical architecture
01项目需求分析 (点餐系统)
Ubuntu 20.04 安装 MySQL
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
安全测试涉及的测试对象
Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
SSM整合笔记通俗易懂版
Why is MySQL still slow to query when indexing is used?
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
Windows下安装MongDB教程、Redis教程
Ansible practical Series II_ Getting started with Playbook
一键提取pdf中的表格