Ejemplo n.º 1
0
    def run_main(self):
        # TODO 连接Fuseki服务器。
        fuseki = jena_sparql_endpoint.JenaFuseki()
        # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。
        q2s = question2sparql.Question2Sparql(['./util/shop_title.txt'])

        my_query = q2s.get_sparql(self.question)
        if my_query is not None:
            result = fuseki.get_sparql_result(my_query)
            value = fuseki.get_sparql_result_value(result)

            # TODO 判断结果是否是布尔值,是布尔值则提问类型是"ASK",回答“是”或者“不知道”。
            if isinstance(value, bool):
                if value is True:
                    return 'Yes'
                else:
                    return 'I don\'t know. :('
            else:
                # TODO 查询结果为空,根据OWA,回答“不知道”
                if len(value) == 0:
                    return 'I don\'t know. :('
                elif len(value) == 1:
                    return value[0]
                else:
                    output = ''
                    for v in value:
                        output += v + u'、'
                    return output[0:-1]

        else:
            # TODO 自然语言问题无法匹配到已有的正则模板上,回答“无法理解”
            return 'I can\'t understand. :('
Ejemplo n.º 2
0
    def __init__(self, dict_path):
        self.q2s = question2sparql.Question2Sparql(dict_path)
        self.fuseki = jena_sparql_endpoint.JenaFuseki()

        self.unknown_responses = [
            '我现在还不知道,等我多学一点再告诉你。  ;-)', '渊博如我竟也不知道,你该不是刁难我吧。。。'
        ]

        self.cant_understand_responses = [
            '不是我不知道,只是我现在还没办法理解你的意思。:-(', '不好意思,我还没办法理解你的问题。:-('
        ]
        print('Sparql endpoint starts successfully!')
Ejemplo n.º 3
0
def query():
    #连接Fuseki服务器。
    fuseki = jena_sparql_endpoint.JenaFuseki()
    #初始化自然语言到SPARQL查询的模块,参数是外部词典列表。
    q2s = question2sparql.Question2Sparql([
        './external_dict/company.txt', './external_dict/servicetype.txt',
        './external_dict/service.txt'
    ])
    question = request.args.get('q')
    my_query = q2s.get_sparql(question)
    print('查询语句:', my_query, '\n')
    qtype = 1
    if my_query is not None:
        result = fuseki.get_sparql_result(my_query)
        #        value = fuseki.get_sparql_result_value(result)
        #query_result为列表 元素是字典
        query_head, query_result = fuseki.parse_result(result)
        print('结果', query_head, query_result)
        if query_head is None:
            #ASK类型问题
            if query_result is True:
                answer = 'Yes'
            else:
                answer = 'I don\'t know. :('
        else:
            if query_result == []:
                answer = 'I don\'t know. :('
                #查询单个属性 单个值
            else:
                if len(query_head) == 1 and len(query_result) == 1:
                    head = query_head[0]
                    answer = query_result[0][head]
                #查询多个属性
                else:
                    temp = list()
                    qtype = 2
                    for qr in query_result:
                        temp.append(json.dumps(qr))
                    return json.dumps({
                        'type': qtype,
                        'head': query_head,
                        'answer': temp
                    })
    else:
        # TODO 自然语言问题无法匹配到已有的正则模板上,回答“无法理解”
        qtype = 0
        answer = 'I don\'t understand. :('
    return json.dumps({'type': qtype, 'answer': answer})
Ejemplo n.º 4
0
def searchMain(question):
    # 连接Fuseki服务器。
    fuseki = jena_sparql_endpoint.JenaFuseki()
    # 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。
    q2s = question2sparql.Question2Sparql([
        './cmdb/query/external_dict/drug_name.txt',
        './cmdb/query/external_dict/person_name.txt',
        './cmdb/query/external_dict/case_name.txt',
        './cmdb/query/external_dict/drug_to_person.txt'
    ])
    my_query = q2s.get_sparql(question.decode('utf-8'))  #sparql查询语句
    # print my_query
    if my_query is not None:
        result = fuseki.get_sparql_result(my_query)
        value = fuseki.get_sparql_result_value(result)
        # print value
        # 判断结果是否是布尔值,是布尔值则提问类型是"ASK",回答“是”或者“不知道”。
        if isinstance(value, bool):
            if value is True:
                # print 'Yes'
                re = 'Yes:)'
            else:
                # print 'I don\'t know. :('
                re = 'I don\'t know. :('
        else:
            # 查询结果为空,根据OWA,回答“不知道”
            if len(value) == 0:
                # print 'I don\'t know. :('
                re = 'I don\'t know. :('
            elif len(value) == 1:
                # print value[0]
                re = value[0]
            else:
                output = ''
                for v in value:
                    output += v + u'、'
                # print output[0:-1]
                re = output[0:-1]
    else:
        # 自然语言问题无法匹配到已有的正则模板上,回答“无法理解”
        # print 'I can\'t understand. :('
        re = 'I can\'t understand. :('
    return re
Ejemplo n.º 5
0
# encoding=utf-8
"""

输入一个问题, 通过RDF模型,查询答案

"""
import jena_sparql_endpoint
import question2sparql
import datetime

if __name__ == '__main__':
    # TODO 连接Fuseki服务器。
    fuseki = jena_sparql_endpoint.JenaFuseki()
    # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。
    q2s = question2sparql.Question2Sparql(
        ['./external_dict/movie_title.txt', './external_dict/person_name.txt'])

    while True:
        question = input()  # 输入问题

        my_query = q2s.get_sparql(question)
        if my_query is not None:
            result = fuseki.get_sparql_result(my_query[0])
            value = fuseki.get_sparql_result_value(result)

            # TODO 判断结果是否是布尔值,是布尔值则提问类型是"ASK",回答“是”或者“不知道”。
            if isinstance(value, bool):
                if value is True:
                    print('Yes')
                else:
                    print('I don\'t know. :(')