def run(self, dispatcher, tracker, domain): graph_database = GraphDatabase() # first need to know the entity type we are looking for entity_type = get_entity_type(tracker) if entity_type is None: dispatcher.utter_template("utter_rephrase", tracker) return [] # check what attributes the NER found for entity type attributes = get_attributes_of_entity(entity_type, tracker) # query knowledge base entities = graph_database.get_entities(entity_type, attributes) # filter out transactions that do not belong the set account (if any) if entity_type == "transaction": account_number = tracker.get_slot("account") entities = self._filter_transaction_entities( entities, account_number) if not entities: dispatcher.utter_template( "I could not find any entities for '{}'.".format(entity_type), tracker) return [] # utter a response that contains all found entities # use the 'representation' attributes to print an entity entity_representation = schema[entity_type]["representation"] dispatcher.utter_message( "Found the following '{}' entities:".format(entity_type)) sorted_entities = sorted( [to_str(e, entity_representation) for e in entities]) for i, e in enumerate(sorted_entities): dispatcher.utter_message(f"{i + 1}: {e}") # set slots # set the entities slot in order to resolve references to one of the found # entites later on entity_key = schema[entity_type]["key"] slots = [ SlotSet("entity_type", entity_type), SlotSet("listed_items", list(map(lambda x: to_str(x, entity_key), entities))), ] # if only one entity was found, that the slot of that entity type to the # found entity if len(entities) == 1: slots.append(SlotSet(entity_type, to_str(entities[0], entity_key))) reset_attribute_slots(slots, entity_type, tracker) return slots
def run(self, dispatcher, tracker, domain): graph_database = GraphDatabase() car_series_list = graph_database.get_entities(entity_type='车系') dispatcher.utter_message(template="utter_answer", answer="小通找到了下列车系:") car_series_list_slot = ['车系'] for i, e in enumerate(car_series_list): answer = str(i + 1) + ": " + e['name'] dispatcher.utter_message(template="utter_answer", answer=answer) car_series_list_slot.append(e['name']) slots = [SlotSet("listed_items", car_series_list_slot)] return slots
from graph_database import GraphDatabase from py2neo import Graph, Node, data, Path, Relationship graphDatabase = GraphDatabase() attributes = [{'name': '2021款 精英型'}] result = graphDatabase.get_entities(entity_type='车型', attributes=attributes) print(result) #[{'id': 1, 'label': '车型', 'name': '2021款 精英型'}] attributes = [{'id': 1}] result = graphDatabase.get_entities(entity_type='车型', attributes=attributes) print(result) #[{'id': 1, 'label': '车型', 'name': '2021款 精英型'}] result = graphDatabase.get_entities(entity_type='车系') print(result) #[{'id': 1, 'label': '车型', 'name': '2021款 精英型'}] for i, e in enumerate(result): print(f"{i + 1}: {e['name']}") # # result = graphDatabase.get_entities(entity_type='车型') # print(result) #[{'id': 955, 'label': '车型', 'name': '2021款 精英型'}, {'id': 963, 'label': '车型', 'name': '2019款 2.4L汽油手动四驱高底盘先锋版长厢'}......] # # result = graphDatabase.get_entities(entity_type='车身',limit=2) # print(result) #[{'id': 957, 'label': '车身', '车身结构': '皮卡', '车门数(个)': 4.0, '高度(mm)': 1809.0, '座位数(个)': '5', '后排车门开启方式': '平开门', 'name': '车身节点', '轴距(mm)': 3155.0, '货箱尺寸(mm)': '1485x1510x530'}, {'id': 965, 'label': '车身', '车身结构': '皮卡', '车门数(个)': 4.0, '高度(mm)': 1809.0, '座位数(个)': '5', '后排车门开启方式': '平开门', 'name': '车身节点', '轴距(mm)': 3470.0, '货箱尺寸(mm)': '1800x1510x530'}] # # result = graphDatabase.get_entities(entity_type='发动机',limit=2) # print(result) #[{'id': 961, 'label': '发动机', '最大扭矩(N·m)': '310', '最大功率(kW)': 130.0, 'name': '发动机节点'}, {'id': 968, 'label': '发动机', '每缸气门数(个)': '4', '供油方式': '多点电喷', '排量(L)': '2.4', '进气形式': '自然吸气', '最大扭矩(N·m)': '200', '环保标准': '国V', '最大功率(kW)': 105.0, '最大功率转速(rpm)': '5250', '最大马力(Ps)': 143.0, '最大扭矩转速(rpm)': '2500-3000', '缸体材料': '未知', '气缸数(个)': '4', '气缸排列形式': 'L', '缸盖材料': '铝合金', 'name': '发动机节点', '燃料形式': '汽油', '燃油标号': '92号', '配气机构': '未知'}] # # attributes = [{'供油方式':'直喷','排量(L)':'2.0'}] # result = graphDatabase.get_entities(entity_type='发动机',attributes=attributes,limit=2) # print(result) #[{'id': 1029, 'label': '发动机', '进气形式': '涡轮增压', '每缸气门数(个)': '4', '供油方式': '直喷', '排量(L)': '2.0', '最大扭矩(N·m)': '375', '环保标准': '国VI', '最大功率(kW)': 120.0, '最大马力(Ps)': 163.0, '缸体材料': '未知', '气缸排列形式': 'L', '气缸数(个)': '4', '缸盖材料': '未知', 'name': '发动机节点', '燃料形式': '柴油', '燃油标号': '0号', '配气机构': '未知'}, {'id': 1039, 'label': '发动机', '进气形式': '涡轮增压', '每缸气门数(个)': '4', '供油方式': '直喷', '排量(L)': '2.0', '最大扭矩(N·m)': '350', '环保标准': '国VI', '最大功率(kW)': 160.0, '最大马力(Ps)': 218.0, '缸体材料': '未知', '气缸排列形式': 'L', '气缸数(个)': '4', '缸盖材料': '铝合金', 'name': '发动机节点', '燃料形式': '汽油', '燃油标号': '92号', '配气机构': '未知'}] # # entity_id = 1029 # aimed_attribute = '供油方式'