def _search_direct_then_feed(self, chain: TranslationChain, unpack_key_name: str) -> tuple: """ 将第一次直接查询的结果作为第二次查询的输入 """ results_1 = self._search_direct(chain) pattern_sql = next(chain.iter(1)) sqls = [] for items in results_1: if not items: sqls.append(None) continue for item in items: sqls.append(pattern_sql.format(item[unpack_key_name])) results_2 = self._search_direct(sqls) return results_1, results_2
def _search_double_direct_then_feed(self, chain: TranslationChain, unpack_key_name: str) -> tuple: """ 第一次直接查询,第二次先执行直接查询后将查询结果投递至最后的查询 """ results_1 = self._search_direct(chain) temp_res = self._search_direct(chain, 1) sqls = [] for feed in temp_res: for pattern_sql in chain.iter(2): if feed is None: sqls.append(None) continue sqls.append(pattern_sql.format(feed[0][unpack_key_name])) results_2 = self._search_direct(sqls) return results_1, results_2, temp_res
def _search_double_direct_then_feed(self, chain: TranslationChain, unpack: bool = False) -> tuple: """ 第一次直接查询,第二次先执行直接查询后将查询结果投递至最后的查询 """ results_1 = self._search_direct(chain, unpack=unpack) temp_res = self._search_direct(chain, 1) final_sqls = [] for feed in temp_res: sqls = [] for pattern_sql in chain.iter(2, unpack=unpack): if not feed: sqls.append(None) continue sqls.append(pattern_sql.format(feed.name)) final_sqls.append(sqls) results_2 = self._search_direct(final_sqls) return results_1, results_2, temp_res
def make_index_compose_ans(self, answer: Answer, builder: AnswerBuilder, chain: TranslationChain, result: Result): data = self._search_direct(chain) builder.feed_data(data) collect = [] units = [] sub_titles = [] # for overall sqls_overall = [sql for sql in chain.iter(3)] data_overall = self._search_direct(sqls_overall) # collect for total, (item, name) in zip( data_overall, builder.product_data_with_name(result['index']) ): # 为使两可迭代对象同步迭代和collect不用做判空,此处不使用if_is_none参数 if not item: answer.add_answer(f'指标“{name.name}”没有任何组成') continue indexes, areas = [], [] for n in item: n.life_check(result['year'][0]) if n: if n.label == 'Index': indexes.append(n.name) else: areas.append(n.name) if len(indexes) == 0 and len(areas) == 0: answer.add_answer(f'指标“{name.name}”没有任何组成') continue # for indexes sqls1 = [sql.format(i) for sql in chain.iter(1) for i in indexes] data1 = self._search_direct(sqls1) # for areas sqls2 = [sql.format(name.name, a) for sql in chain.iter(2) for a in areas] data2 = self._search_direct(sqls2) # make data pairs final_data = {} for k, v in zip(indexes + areas, data1 + data2): if not v: continue if v.child_id is None: continue try: final_data.setdefault(v.child_id, []).append((k, float(v.value))) except ValueError or TypeError: answer.add_answer(f'{name.name}中{k}的记录非数值类型,无法比较') return # make other for k, v in final_data.items(): op1 = sum([x[1] for x in v]) op2 = float(total.value) if op1 < int(op2): # 舍弃一些误差,避免图中出现极小的部分 final_data[k].append(('其他', round(op2 - op1, 2))) collect.append(final_data) units.append(total.unit) sub_titles.append(f'{name.subject()}为{total.val()},其构成分为:') # paint for pie in self.painter.paint_pie(collect, units, title=result.raw_question, sub_titles=sub_titles): answer.save_chart(pie) answer.add_answer(f'该问题的回答已渲染为图像,详见:{CHART_RENDER_DIR}/{result.raw_question}.html')