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. :('
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!')
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})
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
# 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. :(')
# encoding=utf-8 """ @file: query_main.py @time: 2020/03/29 """ import jena_sparql_endpoint import question2sparql if __name__ == '__main__': # TODO 连接Fuseki服务器。 fuseki = jena_sparql_endpoint.JenaFuseki() # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 q2s = question2sparql.Question2Sparql( ['./external_dict/entities_list.txt']) print("\n\n爱好美食的您好啊,小食在此为您提供问答服务") print("可以提问的菜品大类包括:1.红烧肉类,2.红烧排骨类,3.可乐鸡翅类,4.糖醋排骨类,5.水煮鱼类") print(" 6.红烧鱼类,7.凉拌黑木耳类,8.鱼香肉丝类,9.水煮肉片类,10.意大利面类") print("您可以提问的问题类型包括:制作方法,所有食材,主料,辅料,配料,特色,大类包含的菜品等") print("提示:您可以提问的问题例如:") print("如何制作水煮鱼?") print("水煮鱼的制作步骤是什么?") print("红烧肉类包含哪些菜?") print("麻辣水煮肉片的食材有哪些?") print("水煮肉片的主料是什么?") while True: print("\n\n") print('-' * 150)
""" import jena_sparql_endpoint import question2sparql if __name__ == '__main__': # TODO 连接Fuseki服务器。 fuseki = jena_sparql_endpoint.JenaFuseki() # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 myDirectory = [ './myData/zone.txt', './myData/campus.txt', './myData/faculty.txt', './myData/specialty.txt', './myData/building.txt', ] q2s = question2sparql.Question2Sparql(myDirectory) while True: question = raw_input() my_query = q2s.get_sparql(question) print my_query #test 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: print 'Yes' else: print 'I don\'t know. :('
# encoding=utf-8 """ @desc:main函数,整合整个处理流程。 """ import jena_sparql_endpoint import question2sparql if __name__ == '__main__': # TODO 连接Fuseki服务器。 fuseki = jena_sparql_endpoint.JenaFuseki() # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 q2s = question2sparql.Question2Sparql(['./external_dict/word.txt']) while True: question = input() my_query = q2s.get_sparql(question.encode("utf-8").decode('utf-8')) if my_query is not None: result = fuseki.get_sparql_result(my_query) value = fuseki.get_sparql_result_value(result) # TODO 查询结果为空,根据OWA,回答“不知道” if len(value) == 0: print('I don\'t know. :(') elif len(value) == 1: print(value) else: output = ''
self.unknown_responses) elif len(values) == 1: return 'success', my_query, values[0].encode('utf-8') else: output = '' for v in values: output += v + u'、' return 'success', my_query, output[0:-1].encode('utf-8') else: return 'failure', my_query, random.choice( self.cant_understand_responses) if __name__ == '__main__': q2s = question2sparql.Question2Sparql([ './external_dict/office_title.txt', './external_dict/person_name.txt', './external_dict/place_name.txt' ]) fuseki = jena_sparql_endpoint.JenaFuseki() unknown_responses = [u'我现在还不知道,等我多学一点再告诉你。 ;-)', u'渊博如我竟也不知道,你该不是刁难我吧。。。'] cant_understand_responses = [ u'不是我不知道,只是我现在还没办法理解你的意思。:-(', u'不好意思,我还没办法理解你的问题。:-(' ] while True: question = raw_input() my_query = q2s.get_sparql(question.decode('utf-8')) if my_query is not None: print(my_query) # exit()
# encoding=utf-8 """ @author: SimmerChan @contact: [email protected] @file: query_main.py @time: 2017/12/20 15:29 @desc:main函数,整合整个处理流程。 """ import SPARQLWrapper import question2sparql if __name__ == '__main__': # # TODO 连接Fuseki服务器。 # fuseki = jena_sparql_endpoint.JenaFuseki() # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 q2s = question2sparql.Question2Sparql() question = "成龙演过的电影" my_query = q2s.get_sparql(question) print(my_query) # 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: # print('Yes') # else: # print('I don\'t know. :(') # else:
# encoding=utf-8 ###第一步 把自然语言划分成一个个word对象 列表 ###第二部 运用rule 的 apply 看哪条rule 正则表达 符合 ###第三步 把符合的rule按符合的关键词多少排序 选最大的 ###第四部 按照rule产生sparql查询语句 import jena_sparql_endpoint import question2sparql if __name__ == '__main__': #连接Fuseki服务器。 fuseki = jena_sparql_endpoint.JenaFuseki() #初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 q2s = question2sparql.Question2Sparql(['./external_dict/company.txt','./external_dict/servicetype.txt','./external_dict/service.txt']) i =1 # while True: while i <2 : i = i + 1 question = input() my_query = q2s.get_sparql(question) print('查询语句:',my_query,'\n') if my_query is not None: result = fuseki.get_sparql_result(my_query) # value = fuseki.get_sparql_result_value(result) query_head, query_result = fuseki.parse_result(result) print('结果',query_head,query_result) if query_head is None: value = query_result else: #查询单个属性 单个值
# encoding=utf-8 import jena_sparql_endpoint import question2sparql if __name__ == '__main__': # TODO 连接Fuseki服务器。 fuseki = jena_sparql_endpoint.JenaFuseki() # TODO 初始化自然语言到SPARQL查询的模块,参数是外部词典列表。 # q2s = question2sparql.Question2Sparql(['./external_dict/war.txt', './external_dict/combatant.txt', './external_dict/location.txt']) q2s = question2sparql.Question2Sparql([ 'external_dict/war.txt', 'external_dict/combatant.txt', 'external_dict/location.txt' ]) while True: question = raw_input() my_query = q2s.get_sparql(question.decode('utf-8')) # print my_query 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: print 'Yes' else: print 'I don\'t know. :(' else: # TODO 查询结果为空,根据OWA,回答“不知道” if len(value) == 0:
@time: 2017/12/20 15:29 @desc:main函数,整合整个处理流程。 """ import jena_sparql_endpoint import question2sparql 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', './external_dict/vivre_zhpname.txt', './external_dict/onepiece_place_terminology.txt' ]) while True: question = input() my_query = q2s.get_sparql(question) print('最终的查询语句:\n{}'.format(my_query)) print('查询结果') 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: