Python RDF Knowledge Baseクエリ

14687 ワード

このKnowledge Baseクエリでは、SPARQLを使用します.SPARQL(SPARQL Protocol and RDF Query Language)は、RDFのために開発されたクエリー言語およびデータ取得プロトコルであり、W 3 Cのために開発されたRDFデータモデルに定義されているが、RDFで表すことができる任意の情報リソースに使用することができる.その文法のフォーマットは以下の通りです:SELECT?Predicate ?Object where {http://baike.com/resource/華南師範大学?Predicate ?Object}しかし、それは三元グループの形式を返し、直接読むのに適しておらず、前処理が必要である.ここでは、「Python json形式ファイルをRDF形式ファイルに転送」に保存されているRDFファイルをテストします.コードは以下の通りです.
# -*- coding: utf-8 -*-
import rdflib
import re
g1 = rdflib.Graph()
g1.parse("data.rdf", format="xml")
while 1:
    print "please input the three words that you want,Subject,Predicate,Object;ues ',' to split them,ues '' if you don't know:"
    try:
        Subject, Predicate, Object = input()

        data = ['', '', '']
        if Subject != '' and Predicate == '' and Object == '':  # only search Subject
            q = "select ?Predicate ?Object where {  + \
                Subject + "> ?Predicate ?Object}"
            data[0] = Subject  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[1] = txt[0][9:-1]
                data[2] = txt[1][9:-1]
                print data[0], data[1], data[2]
        elif Subject == '' and Predicate != '' and Object == '':  # only search Predicate
            q = "select ?Subject ?Object where {?Subject  + \
                Predicate + "> ?Object}"
            data[1] = Predicate  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[0] = txt[0][9:-1]
                data[2] = txt[1][9:-1]
                print data[0], data[1], data[2]
        elif Subject == '' and Predicate == '' and Object != '':  # only search Object
            q = "select ?Subject ?Predicate where {?Subject ?Predicate  + Object + ">}"
            data[2] = Object  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[0] = txt[0][9:-1]
                data[1] = txt[1][9:-1]
                print data[0], data[1], data[2]
        elif Subject != '' and Predicate != '' and Object == '':  # search Subject and Predicate
            q = "select ?Object where { + Subject + ">\
         + Predicate + "> ?Object}"
            data[0] = Subject  # .encode('gbk')
            data[1] = Predicate  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[2] = txt[0][9:-1]
                print data[0], data[1], data[2]
        elif Subject != '' and Predicate == '' and Object != '':  # search Subject and Object
            q = "select ?Predicate where { + Subject + ">  ?Predicate \
         + Object + ">}"
            data[0] = Subject  # .encode('gbk')
            data[2] = Object  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[1] = txt[0][9:-1]
                print data[0], data[1], data[2]
        elif Subject == '' and Predicate != '' and Object != '':  # search Predicate and Object
            q = "select ?Subject where { ?Subject  + Predicate + ">\
         + Object + ">}"
            data[1] = Predicate  # .encode('gbk')
            data[2] = Object  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[0] = txt[0][9:-1]
                print data[0], data[1], data[2]
        elif Subject != '' and Predicate != '' and Object != '':  # search all
            q = "select ?Predicate ?Object where {  + \
                Subject + "> ?Predicate ?Object}"
            data[0] = Subject  # .encode('gbk')
            x1 = g1.query(q)
            if len(list(x1)) > 20:
                leng = 20
            else:
                leng = len(list(x1))
            for i in range(leng):
                s = str(list(x1)[i]).decode('unicode_escape')
                txt = re.findall(r'resource/.*?\'', s)
                data[1] = txt[0][9:-1]
                data[2] = txt[1][9:-1]
                print data[0], data[1], data[2]
        else:
            break
    except:
        pass

データ量が多い場合、すべてのマッチング結果を表示するのに時間がかかることを考慮すると、クエリーされた最初の20のテスト結果しか表示されません:Python RDF知识库查询_第1张图片入力は3元グループのいずれかまたは任意の2つまたはすべてで、カンマで分割され、入力フォーマットが間違っている場合pass Python RDF知识库查询_第2张图片テストデータとプログラム:ダウンロードをクリック