123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #!/usr/bin/env/python
- # -*- coding:utf-8 -*-
- import re
- def regroup(res_list, item_groups, ans_groups):
- """
- 将多个题共用一个题干的情况进行slave重组,如地理
- :param res_list:
- :param item_groups:
- :param ans_groups:
- :return:
- """
- new_res_dict = []
- one_group = {}
- groups_data = item_groups["groups_data"]
- start_no = list(groups_data.keys())
- start_no.sort() # 排序
- def takefirst(elem):
- return int(elem.split("-")[0])
- ans_start_no = []
- if ans_groups:
- ans_start_no = list(ans_groups.keys())
- ans_start_no.sort(key=takefirst) # 排序
- added_nos = [] # 已经slave了的题号
- for n, group_no in enumerate(start_no):
- one_group["com_stem"] = res_list[group_no]["com_stem"]
- del res_list[group_no]["com_stem"]
- if groups_data[group_no]:
- st, end = groups_data[group_no].split("-")
- if not added_nos: # 开始
- if start_no[n + 1] <= int(end): # 公共题文中的结束序号信息有误
- one_group["slave"] = res_list[int(st) - 1: start_no[n + 1]]
- added_nos.append(start_no[n + 1])
- else:
- added_nos.append(int(end))
- one_group["slave"] = res_list[int(st) - 1:int(end)]
- elif int(st) <= added_nos[-1]: # 公共题文中的初始序号信息有误
- if n + 1 < len(start_no): # 不是最后一组
- if int(end) < start_no[n + 1]:
- one_group["slave"] = res_list[added_nos[-1]:int(end)]
- added_nos.append(int(end))
- else: # 结束序号有误,以pos为主
- one_group["slave"] = res_list[added_nos[-1]: start_no[n + 1]]
- added_nos.append(start_no[n + 1])
- else:
- if int(end) >= added_nos[-1]:
- one_group["slave"] = res_list[added_nos[-1]:int(end)]
- added_nos.append(int(end))
- else: # end值出错
- if str(group_no+1) + "-" in "#".join(ans_groups.keys()):
- end = re.search("[^#]{}-(\d+)[$#]".format(group_no), "#".join(ans_groups.keys())).group(1)
- one_group["slave"] = res_list[group_no: int(end) + 1]
- else:
- endp = [m for m, j in enumerate(res_list[added_nos[-1]:])
- if j["type"] != res_list[added_nos[-1]]["type"]]
- if endp:
- one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
- added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
- else:
- one_group["slave"] = res_list[group_no:]
- else:
- added_nos.append(int(end))
- one_group["slave"] = res_list[int(st) - 1:int(end)]
- if int(st) > added_nos[-1] + 1:
- new_res_dict.extend(res_list[added_nos[-1]:int(st) - 1])
- else:
- if group_no != start_no[-1]: # 不是最后一个
- one_group["slave"] = res_list[group_no: start_no[n+1]]
- added_nos.append(start_no[n+1])
- elif str(group_no+1) + "-" in "#".join(ans_groups.keys()):
- aa = ("#" + "#".join(ans_groups.keys())).split("#{}-".format(group_no+1))
- end = aa[-1].split("#", maxsplit=1)[-1]
- one_group["slave"] = res_list[group_no: int(end)]
- added_nos.append(int(end))
- # new_res_dict.extend(res_list[int(end)+1:])
- else:
- endp = [m for m, j in enumerate(res_list[added_nos[-1]:])
- if j["type"] != res_list[added_nos[-1]]["type"]]
- if endp:
- one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
- added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
- # new_res_dict.extend(res_list[added_nos[-1]:])
- else:
- one_group["slave"] = res_list[group_no:]
- one_group["type"] = one_group["slave"][0]["type"]
- one_group["que_num"] = len(one_group["slave"])
- one_group["topic_num"] = "{}-{}".format(one_group["slave"][0]["topic_num"], one_group["slave"][-1]["topic_num"])
- if ans_start_no:
- for k in ans_start_no:
- if k == one_group["topic_num"]:
- st1, end1 = k.split("-") # 真实题号组
- if len(re.findall("【详解】", ans_groups[k]["parse"])) > 1:
- parse_list = re.split("【详解】", ans_groups[k]["parse"])[1:]
- ans_list = re.split("(?<=[】\s])\d{1,2}\s*[、..、]|^\d{1,2}\s*[、..、]", ans_groups[k]["key"])[1:]
- if len(parse_list) == int(end1)+1 - int(st1):
- for i in range(len(parse_list)):
- one_group["slave"][i]["parse"] = parse_list[i].strip()
- else:
- # 就将各题解析合在一起
- one_group["parse"] = ans_groups[k]["parse"]
- if len(ans_list) == int(end1)+1 - int(st1):
- for j in range(len(ans_list)):
- one_group["slave"][j]["key"] = ans_list[j].strip()
- else:
- one_group["key"] = ans_groups[k]["key"]
- ans_start_no.remove(k)
- break
- new_res_dict.append(one_group)
- one_group = {}
- if added_nos[-1] < len(res_list):
- new_res_dict.extend(res_list[added_nos[-1]:])
- return new_res_dict
- def regroup_old(res_list, item_groups):
- """
- 将多个题共用一个题干的情况进行slave重组,如地理
- :param res_list: 拆分为小题后的结果
- :return:
- """
- new_res_dict = []
- start_no = [i for i in item_groups.keys() if i != "pos"]
- if not start_no:
- return res_list
- def takefirst(elem):
- return int(elem.split("-")[0])
- start_no.sort(key=takefirst) # 排序
- print(start_no)
- one_group = {}
- added_nos = [] # 已经slave了的题号
- for n, group_no in enumerate(start_no):
- one_group["common_stem"] = item_groups[group_no]
- st, end = group_no.split("-") # 真实题号组
- if not added_nos: # 开始
- if item_groups["pos"][n + 1] <= int(end): # 公共题文中的结束序号信息有误
- one_group["slave"] = res_list[int(st) - 1:item_groups["pos"][n + 1] - 1]
- added_nos.append(item_groups["pos"][n + 1] - 1)
- else:
- added_nos.append(int(end))
- one_group["slave"] = res_list[int(st) - 1:int(end)]
- elif int(st) <= added_nos[-1]: # 公共题文中的初始序号信息有误
- if n + 1 < len(item_groups["pos"]): # 不是最后一组
- if int(end) < item_groups["pos"][n + 1]:
- one_group["slave"] = res_list[added_nos[-1]:int(end)]
- added_nos.append(int(end))
- else: # 结束序号有误,以pos为主
- one_group["slave"] = res_list[added_nos[-1]:item_groups["pos"][n + 1] - 1]
- added_nos.append(item_groups["pos"][n + 1] - 1)
- else:
- if int(end) >= added_nos[-1]:
- one_group["slave"] = res_list[added_nos[-1]:int(end)]
- added_nos.append(int(end))
- else: # end值出错
- endp = [m for m, j in enumerate(res_list[added_nos[-1]:])
- if j["type"] != res_list[added_nos[-1]]["type"]]
- if endp:
- one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
- added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
- else:
- added_nos.append(int(end))
- one_group["slave"] = res_list[int(st) - 1:int(end)]
- if int(st) > added_nos[-1] + 1:
- new_res_dict.extend(res_list[added_nos[-1]:int(st) - 1])
- one_group["type"] = one_group["slave"][0]["type"]
- one_group["que_num"] = len(one_group["slave"])
- new_res_dict.append(one_group)
- one_group = {}
- if added_nos[-1] < len(res_list):
- new_res_dict.extend(res_list[added_nos[-1]:])
- return new_res_dict
|