def generate(self): super(InsuranceGeneration, self).generate() semantic = self.topic.get_semantic() if semantic.slots[KG_NAME] == '*': nodes = graph_search.get_nodes(semantic.slots[KG_TYPE]) result = [] for node in nodes: result.append(node[KG_NAME]) self._response.set_slots(semantic.slots) self._response.set_text(self.topic.get_query_info().get_text()) self._response.set_topic_intent(DOMAIN_INSURANCE, INTENT_QUERY) self._response.set_answer(', '.join(result)) else: if semantic.domain == DOMAIN_INSURANCE_COMPANY: sql = 'SELECT id, %s FROM gs8_insurance_company AS ic WHERE ic.name = "%s"' % ( semantic.slots[PROPERTY], semantic.slots[KG_NAME]) elif semantic.domain == DOMAIN_INSURANCE: sql = 'SELECT id, %s FROM gs8_insurance AS ic WHERE ic.name = "%s"' % ( semantic.slots[PROPERTY], semantic.slots[KG_NAME]) else: self.topic.end() return self._response data = db_query.get(sql) result = {} if len(data) > 0: result['id'] = dict(data[0])['id'] result['data'] = dict(data[0])[semantic.slots[PROPERTY]] result['field'] = semantic.slots[PROPERTY] self._response.set_slots(semantic.slots) self._response.set_text(self.topic.get_query_info().get_text()) self._response.set_topic_intent(semantic.domain, semantic.intent) self._response.set_answer(result) self.topic.end() return self._response
def cal_sim(self, answer, query): sql = 'SELECT image, reason, method, query FROM gs8_fault ' data = db_query.get(sql) for item in data: data = dict(item) similarity = calc_sim(data['query'], query) if similarity >= 90: result = {} self.append(result, data, similarity) answer.append(result) break
def generate(self): super(MaintenanceGeneration, self).generate() semantic = self.topic.get_semantic() sql = 'SELECT id, %s FROM gs8_maintenance AS m WHERE m.name = "%s"' % ( semantic.slots[PROPERTY], semantic.slots[KG_NAME]) data = db_query.get(sql) result = {} if len(data) > 0: if dict(data[0])[semantic.slots[PROPERTY]].strip() == '': pass else: result['data'] = dict(data[0])[semantic.slots[PROPERTY]] result['id'] = dict(data[0])['id'] result['field'] = semantic.slots[PROPERTY] self._response.set_slots(semantic.slots) self._response.set_text(self.topic.get_query_info().get_text()) self._response.set_topic_intent(DOMAIN_MAINTENANCE, INTENT_QUERY) self._response.set_answer(result) self.topic.end() return self._response
def cal_sim_0(part, phen, answer, query): for part_item in part: for phen_item in phen: sql = 'SELECT image, reason, method, query FROM gs8_fault AS t WHERE t.part="%s" AND t.phen="%s"' % ( part_item, phen_item) result = '' data = db_query.get(sql) for item in data: data = dict(item) similarity = calc_sim(data['query'], query) if similarity > 50: result = {} result['data'] = '' result['reason'] = data['reason'] result['method'] = data['method'] result['image'] = data['image'] answer.append(result) if answer: return answer[0] else: return answer
def get_field_by_parent(parent, field): sql = "select m.%s from gs8_manual_v3 m WHERE m.parent = '%s'" % ( field, parent) data = db_query.get(sql) return list( set([item[0].encode('utf-8') for item in data if item[0] != '']))
def generate(self): super(BrokenDownGeneration, self).generate() query = self.topic.get_query_info().get_text() semantic = self.topic.get_semantic() part = semantic.slots[AUTO_PART] phen = semantic.slots[AUTO_PHEN] stints = semantic.slots[STINTS] answer = [] if all([part, phen]): for part_item in part: for phen_item in phen: if not stints: sql = self.sql(part_item, phen_item) else: placeholder = [] for k, v in stints.items(): placeholder.append(' AND %s="%s"' % (k, v)) rel_property = "".join(placeholder) sql = 'SELECT image, reason, method, query FROM gs8_fault WHERE part="%s" AND phen="%s"' % ( part_item, phen_item) + rel_property data_q = db_query.get(sql) if data_q: self.answer(data_q, answer, query) else: data_q = db_query.get(self.sql(part_item, phen_item)) if data_q: self.answer(data_q, answer, query) if not answer: self.cal_sim(answer, query) elif all([len(part) == 0, len(phen) >= 1]): for phen_item in phen: sql = 'SELECT image, reason, method, query FROM gs8_fault AS t WHERE t.part="车体" AND t.phen="%s"' % phen_item data = db_query.get(sql) if len(data) > 0: for item in data: data = dict(item) result = {} similarity = calc_sim(data['query'], query) if similarity > 50: self.append(result, data, similarity) answer.append(result) if not answer: self.cal_sim(answer, query) else: self.cal_sim(answer, query) elif all([len(part) >= 1, len(phen) == 0]): for part_item in part: sql = 'SELECT image, reason, method, query FROM gs8_fault AS t WHERE t.part="%s"' % part_item data = db_query.get(sql) if len(data) > 0: for item in data: data = dict(item) result = {} similarity = calc_sim(data['query'], query) if similarity > 50: self.append(result, data, similarity) answer.append(result) break else: self.cal_sim(answer, query) answer = sorted(answer, key=operator.itemgetter('sim'), reverse=True) answer_ = answer[0] if answer else self.cal_sim_0( part, phen, answer, query) self._response.set_text(query) self._response.set_topic_intent(DOMAIN_BROKENDOWN, INTENT_QUERY) self._response.set_answer(answer_) self.topic.end() return self._response
def generate(self): super(ManualGeneration, self).generate() semantic = self.topic.get_semantic() hyponymy = semantic.hyponymy entity_name = semantic.name default = 'part_default' grp = 'part_grp' if hyponymy == PARENT: default = 'parent_default' grp = 'parent_grp' sql = 'SELECT * FROM gs8_manual_v3 ' where = ' WHERE parent="车灯" ' else: sql = 'SELECT * FROM gs8_manual_v3' where = '' for arg, val in semantic.slots.items(): if val != '': if where == '': where = ' where gs8_manual_v3.%s="%s" ' % (arg, val) else: where += ' and gs8_manual_v3.%s="%s" ' % (arg, val) sql += where data = db_query.get(sql) cnt = len(data) if cnt == 0: pass elif cnt == 1: self.response.set_answer({ METHOD: dict(data[0])[METHOD], # ATTENTION: dict(data[0])[ATTENTION], IMAGE: dict(data[0])[IMAGE] }) else: if self.topic.stateid != TopicStateID.Null: self.response.set_answer({ METHOD: dict(data[0])[METHOD], # ATTENTION: dict(data[0])[ATTENTION], IMAGE: dict(data[0])[IMAGE] }) return self.response is_break = False for row in data: if dict(row)[default] == 1: self.response.set_answer({ METHOD: dict(row)[METHOD], # ATTENTION: dict(row)[ATTENTION], IMAGE: dict(row)[IMAGE] }) is_break = True # todo end topic break if not is_break: grp_id = dict(data[0])[grp] # todo 取grpid最多的值 if grp_id is None: return self.response if grp_id == 0: return self.response inter_sql = 'select * from gs8_manual_v3_conf where gs8_manual_v3_conf.%s = %d' % ( grp, grp_id) inter = db_query.get(inter_sql) self.response.set_data({ TITLE: dict(inter[0])[INTERACTIVE], LIST: self._format_arg_list(dict(inter[0])[LIST]), UPLIST: self._format_arg_list(dict(inter[0])[UPLIST]) }) self.response.set_state_code(StateCode.Interactive) self.topic.arguments[FIELD] = dict(inter[0])[FIELD] self.topic.set_stateid(TopicStateID.SelectManualOption) return self.response