stems_to_groups.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import re
  4. from pprint import pprint
  5. def regroup(res_list, item_groups, ans_groups):
  6. """
  7. 将多个题共用一个题干的情况进行slave重组,如地理
  8. item_groups中的groups_data,key值表示带公共题干的试题位置,从0开始计;
  9. value值:'fei'表示本题不是小题多问;
  10. '\d-\d'表示哪几个题合成slave;
  11. ''空表示带公共题干试题开始位置,没有slave范围
  12. 例:item_groups: {'is_groups': 1, 'groups_data':
  13. {0: 'fei', 5: '', 8: '', 11: 'fei', 12: '', 15: '', 19: '20-21', 23: 'fei'}}
  14. :param res_list:
  15. :param item_groups:
  16. :param ans_groups:
  17. :return:
  18. """
  19. new_res_dict = []
  20. groups_data = item_groups["groups_data"]
  21. start_no = list(groups_data.keys()) # 与真实题号差1
  22. start_no.sort() # 排序
  23. def takefirst(elem):
  24. return int(elem.split("-")[0])
  25. ans_start_no = []
  26. if ans_groups:
  27. ans_start_no = list(ans_groups.keys())
  28. ans_start_no.sort(key=takefirst) # 排序
  29. contained_no = [] # 答案参与重组的题号
  30. for ans_no in ans_start_no:
  31. st1, ed1 = ans_no.split("-")
  32. contained_no.extend(list(range(int(st1)-1, int(ed1))))
  33. not_contained_no = set(range(len(res_list))) - set(contained_no)
  34. added_nos = [] # 已经slave了的真实题号
  35. # 开始是单层题型结构时
  36. temp_no = -1
  37. while groups_data and groups_data[start_no[0]] == "fei":
  38. if start_no[0] - temp_no == 1:
  39. new_res_dict.append(res_list[start_no[0]])
  40. elif start_no[0] - temp_no > 1:
  41. new_res_dict.extend(res_list[temp_no+1: start_no[0]+1])
  42. temp_no = start_no[0]
  43. del start_no[0]
  44. if start_no[0] > 0:
  45. new_res_dict.extend(res_list[temp_no+1:start_no[0]])
  46. one_group = {}
  47. alone_item_nos = []
  48. print("start_no:", start_no)
  49. fei_no = {}
  50. for n, group_no in enumerate(start_no):
  51. # print("added_nos:", added_nos, group_no)
  52. if "com_stem" not in res_list[group_no]: # 不带"com_stem"
  53. if group_no == start_no[-1] and groups_data[group_no] == "fei": # 最后一个不管
  54. continue
  55. if groups_data[group_no] == "fei": # 可能存在多个fei
  56. if n - 1 in fei_no:
  57. new_res_dict.extend(res_list[fei_no[n - 1] + 1: group_no + 1])
  58. else:
  59. new_res_dict.append(res_list[group_no])
  60. added_nos.append(group_no+1)
  61. fei_no[n] = group_no
  62. continue
  63. # 其他情况
  64. new_res_dict.append(res_list[group_no])
  65. added_nos.append(group_no+1)
  66. # continue
  67. else:
  68. # 遇到带"com_stem"的试题
  69. one_group["com_stem"] = res_list[group_no]["com_stem"]
  70. del res_list[group_no]["com_stem"]
  71. if "-" in groups_data[group_no]: # slave范围知道时
  72. st, end = groups_data[group_no].split("-")
  73. if not added_nos: # 开始
  74. if n + 1 < len(start_no) and start_no[n + 1] <= int(end): # 公共题文中的结束序号信息有误,以下一个题的key值为主
  75. one_group["slave"] = res_list[int(st) - 1: start_no[n + 1]]
  76. added_nos.append(start_no[n + 1])
  77. else:
  78. added_nos.append(int(end))
  79. if len(res_list) < int(end):
  80. st = int(st) - int(res_list[0]["topic_num"]) + 1
  81. end = int(end) - int(res_list[0]["topic_num"]) + 1
  82. one_group["slave"] = res_list[int(st) - 1:int(end)]
  83. elif int(st) <= added_nos[-1]: # 公共题文中的初始序号信息有误
  84. if n + 1 < len(start_no): # 不是最后一组
  85. if int(end) < start_no[n + 1]:
  86. one_group["slave"] = res_list[added_nos[-1]:int(end)]
  87. added_nos.append(int(end))
  88. else: # 结束序号有误,
  89. one_group["slave"] = res_list[added_nos[-1]: start_no[n + 1]]
  90. added_nos.append(start_no[n + 1])
  91. else:
  92. if int(end) >= added_nos[-1]:
  93. one_group["slave"] = res_list[added_nos[-1]:int(end)]
  94. added_nos.append(int(end))
  95. else: # end值出错
  96. if str(group_no+1) + "-" in "#".join(ans_groups.keys()):
  97. end = re.search("[^#]{}-(\d+)[$#]".format(group_no), "#".join(ans_groups.keys())).group(1)
  98. one_group["slave"] = res_list[group_no: int(end) + 1]
  99. else:
  100. endp = [m for m, j in enumerate(res_list[added_nos[-1]:])
  101. if j["type"] != res_list[added_nos[-1]]["type"]]
  102. if endp:
  103. one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
  104. added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
  105. else:
  106. one_group["slave"] = res_list[group_no:]
  107. else:
  108. added_nos.append(int(end))
  109. one_group["slave"] = res_list[int(st) - 1:int(end)]
  110. if int(st) > added_nos[-1] + 1:
  111. new_res_dict.extend(res_list[added_nos[-1]:int(st) - 1])
  112. else: # salve范围不知道时
  113. # print("yyy:", group_no, start_no, groups_data)
  114. if group_no != start_no[-1]: # 不是最后一个
  115. if groups_data[group_no] == "fei": # 可能存在多个fei
  116. if n - 1 in fei_no:
  117. new_res_dict.append(res_list[fei_no[n-1]+1: group_no+1])
  118. else:
  119. new_res_dict.append(res_list[group_no])
  120. added_nos.append(group_no+1)
  121. fei_no[n] = group_no
  122. continue
  123. elif "#" + str(group_no + 1) + "-" in "#" + "#".join(ans_groups.keys()): # 以答案的序号为准
  124. aa = ("#" + "#".join(ans_groups.keys())).split("#{}-".format(group_no + 1))
  125. end = aa[-1].split("#", maxsplit=1)[0]
  126. one_group["slave"] = res_list[group_no: int(end)]
  127. added_nos.append(int(end))
  128. if int(end) < start_no[n+1]: # 中间单独的题目
  129. alone_item_nos.append([int(end), start_no[n + 1]])
  130. added_nos.append(start_no[n + 1])
  131. # new_res_dict.extend(res_list[int(end)+1:])
  132. else:
  133. one_group["slave"] = res_list[group_no: start_no[n+1]]
  134. added_nos.append(start_no[n+1])
  135. else:
  136. if groups_data[group_no] == "fei":
  137. continue
  138. elif "#{}-".format(group_no + 1) in "#" + "#".join(ans_groups.keys()): # 以答案的序号为准
  139. aa = ("#" + "#".join(ans_groups.keys())).split("#{}-".format(group_no + 1))
  140. end = aa[-1].split("#", maxsplit=1)[-1]
  141. one_group["slave"] = res_list[group_no: int(end)]
  142. added_nos.append(int(end))
  143. else:
  144. # 只根据题型来判断截止点,不靠谱,先按作答类型
  145. endp = []
  146. if added_nos:
  147. endp = [m for m, j in enumerate(res_list[added_nos[-1]:]) if "answer_type" in j and
  148. "作文" in j["answer_type"] and j["answer_type"] != res_list[added_nos[-1]]["answer_type"]]
  149. if not endp or (endp[0] <= 1 and len(res_list[added_nos[-1]:]) > 6) or endp[0] > 6: # 默认最多小题是6个
  150. endp = [m for m, j in enumerate(res_list[added_nos[-1]:]) if "answer_type" in j
  151. and "answer_type" in res_list[added_nos[-1]] and
  152. j["answer_type"] != res_list[added_nos[-1]]["answer_type"]]
  153. # print("endp:", endp, )
  154. if endp and endp[0] > 1:
  155. one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
  156. added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
  157. # new_res_dict.extend(res_list[added_nos[-1]:])
  158. else:
  159. one_group["slave"] = res_list[group_no:]
  160. added_nos.append(len(res_list))
  161. one_group["type"] = one_group["slave"][0]["type"] if one_group["slave"] else ""
  162. one_group["que_num"] = len(one_group["slave"])
  163. if one_group["slave"]:
  164. if one_group["slave"][-1]["topic_num"] != one_group["slave"][0]["topic_num"]:
  165. one_group["topic_num"] = "{}-{}".format(one_group["slave"][0]["topic_num"],
  166. one_group["slave"][-1]["topic_num"])
  167. else:
  168. one_group["topic_num"] = one_group["slave"][0]["topic_num"]
  169. else:
  170. one_group["topic_num"] = ""
  171. # ---------小题答案拆分----------
  172. one_group = ans_regroup(ans_start_no, one_group, ans_groups)
  173. if "key" not in one_group:
  174. one_group["key"] = ""
  175. if "parse" not in one_group:
  176. one_group["parse"] = ""
  177. new_res_dict.append(one_group)
  178. if alone_item_nos: # 把中间单独的不参与重组的题目也加上
  179. for alone_no in alone_item_nos:
  180. new_res_dict.extend(res_list[alone_no[0]: alone_no[1]])
  181. alone_item_nos = []
  182. one_group = {}
  183. # 把末尾不参与重组的题目也加上
  184. if added_nos[-1] < len(res_list):
  185. new_res_dict.extend(res_list[added_nos[-1]:])
  186. # ---------------------题目重组end----------------------------------
  187. if not_contained_no: # 答案不参与重组的题号
  188. for one_no in not_contained_no:
  189. for idx, one_res in enumerate(new_res_dict):
  190. if one_no+1 == one_res["topic_num"]:
  191. parse_split2group(one_res)
  192. # ------对重组后的每个大题更新题型结构,并对公共题文初步添加缩进-------
  193. for one_res in new_res_dict:
  194. if "com_stem" in one_res: # 公共题文中暂不考虑填空个数
  195. # 添加缩进属性<p style="text-indent: 2em">、居中属性<p style="text-align:center">
  196. # new_com_stem = suojin(one_res["com_stem"])
  197. new_com_stem = one_res["com_stem"]
  198. new_com_stem = re.sub(r"(完成|回答)下?[面列]?的?第?(\d{1,2})[-到至第~~-]+?(\d{1,2})小?题",
  199. r"\1下面小题", new_com_stem)
  200. new_com_stem = new_com_stem.replace(" ", "&nbsp;&nbsp;") # 允许手动调整的空格保留
  201. # 字体设置:带缩进的行均设置为楷体,其他是宋体
  202. new_com_stem = re.sub(r'(<p style="text-indent:.*?">\n*|<p style="text-align: center;?">\n*'
  203. r'|<p style="text-align: right;?">\n*)([^\n]+?)', r'\1<span style="font-family:楷体;">\2',
  204. new_com_stem)
  205. one_res["stem"] = new_com_stem + "\n" + one_res["stem"] if "stem" in one_res else new_com_stem
  206. del one_res["com_stem"]
  207. elif "slave" in one_res and one_res["slave"] and "stem" in one_res: # 带小问的试题题文也设置一下字体
  208. lw_com_stem = re.sub(r'(<p style="text-indent:.*?">\n*|<p style="text-align: center;?">\n*'
  209. r'|<p style="text-align: right;?">\n*)([^\n]+?)', r'\1<span style="font-family:楷体;">\2',
  210. one_res["stem"])
  211. one_res["stem"] = lw_com_stem
  212. one_res["topic_num"] = str(one_res["topic_num"])
  213. if "slave" in one_res:
  214. one_res['type'] = '小题多问类'
  215. elif "options" in one_res:
  216. one_res['type'] = '选择类'
  217. else:
  218. one_res['type'] = '解答类'
  219. if re.search("(阅读|针对|结合).{,4}[资材]料|(\n|^)\s*材料一\s", one_res['stem']):
  220. one_res["stem"] = re.sub(r'(<p style="text-indent:.*?">\n*|<p style="text-align: center;?">\n*'
  221. r'|<p style="text-align: right;?">\n*)([^\n]+?)',
  222. r'\1<span style="font-family:楷体;">\2',
  223. one_res["stem"])
  224. # ind_label = '<p style="text-indent: 2em">'
  225. # if "【范文】" in one_res['key']: # "写作"
  226. # anss = re.split("\n+", one_res['key'])
  227. # ids = [n for n, a in enumerate(anss) if "【范文】" in a][0]
  228. # may_title = anss[ids].replace("【范文】", "").strip()
  229. # if not may_title:
  230. # ids += 1
  231. # may_title = anss[ids].strip()
  232. # if 0 < len(may_title) < 5:
  233. # new_ans = "\n".join(anss[:ids]) + '<p style="text-align:center">' + anss[ids] + "</p>" \
  234. # + ind_label + ('</p>' + ind_label).join(anss) + "</p>"
  235. # else:
  236. # new_ans = ind_label + '</p><p style="text-indent: 2em">'.join(anss) + "</p>"
  237. # one_res['key'] = new_ans
  238. # elif re.search(r"(阅读|针对).{,4}[资材]料|(\n|^)\s*材料一\s", one_res['stem']) \
  239. # and "text-indent: 2em" not in one_res['stem']:
  240. # one_res['stem'] = suojin(one_res['stem'])
  241. return new_res_dict
  242. def ans_regroup(ans_start_no, one_group, ans_groups):
  243. """
  244. 答案重组
  245. ans_start_no:ans_groups中的题号组
  246. :return:
  247. """
  248. if ans_start_no:
  249. for k in ans_start_no:
  250. if k == one_group["topic_num"]:
  251. st1, end1 = k.split("-") # 真实题号组
  252. # --------------------------解析----------------------------
  253. parse_list = []
  254. if len(re.findall("【详解】", ans_groups[k]["parse"])) > 1:
  255. parse_list = re.split("【详解】", ans_groups[k]["parse"])
  256. comm_parse, parse_list = parse_list[0], parse_list[1:]
  257. else:
  258. t_seq_no = list(range(int(st1), int(end1) + 1))
  259. t_seq_no = list(map(str, t_seq_no))
  260. if any([True if len(no) > 1 else False for no in t_seq_no]):
  261. parse_list = re.split(r"(?<=[】\n])\s*(" + "|".join(t_seq_no) + r")\s*[、..、]",
  262. "\n" + ans_groups[k]["parse"])
  263. comm_parse, parse_list = parse_list[0], parse_list[1:]
  264. parse_list = [pr for idn, pr in enumerate(parse_list) if idn % 2 == 1]
  265. else:
  266. parse_list = re.split(r"(?<=[】\n])\s*[" + "".join(t_seq_no) + r"]\s*[、..、]",
  267. "\n" + ans_groups[k]["parse"])
  268. comm_parse, parse_list = parse_list[0], parse_list[1:]
  269. if len(parse_list) > 1:
  270. if len(parse_list) == int(end1) + 1 - int(st1):
  271. if comm_parse:
  272. one_group["parse"] = comm_parse
  273. for i in range(len(parse_list)):
  274. pr = parse_list[i].strip()
  275. if i == len(parse_list) - 1 and re.search("\n\s*[【参考]*?译文\s*[】::]|\n\s*【点睛】", pr):
  276. pr, hd, one_group["parse"] = re.split("\n\s*([【参考]*?译文\s*[】::]|【点睛】)",
  277. pr, maxsplit=1)
  278. one_group["parse"] = hd + one_group["parse"]
  279. one_group["slave"][i]["parse"] = pr
  280. if "slave" in one_group["slave"][i]: # 解析再拆-->小问解析
  281. slave_parse_list = re.split("(?<=[\s\n])[((]\s*\d{1,2}[))]", "\n" + pr)
  282. if len(slave_parse_list) - 1 == len(one_group["slave"][i]["slave"]):
  283. for pi in range(len(slave_parse_list) - 1):
  284. one_group["slave"][i]["slave"][pi]["parse"] = slave_parse_list[pi + 1].strip()
  285. one_group["slave"][i]["parse"] = slave_parse_list[0].strip()
  286. else:
  287. # 就将各题解析合在一起
  288. one_group["parse"] = ans_groups[k]["parse"]
  289. else:
  290. one_group['parse'] = ans_groups[k]["parse"]
  291. # --------------------------答案----------------------------
  292. ans_list = re.split("(?<=[】\n])\d{1,2}\s*[、..、]|\s{2,}\d{1,2}\s*[、..、]",
  293. "\n" + ans_groups[k]["key"])[1:]
  294. if len(ans_list) > 1:
  295. if len(ans_list) == int(end1) + 1 - int(st1):
  296. for j in range(len(ans_list)):
  297. one_group["slave"][j]["key"] = ans_list[j].strip()
  298. if "slave" in one_group["slave"][j]: # 答案再拆
  299. slave_ans = re.sub(r"([((]\s*\d\s*[))])\s*[、..、,,::]\s*\1", r"\1", ans_list[j])
  300. slave_ans_list = re.split("(?<=[\s\n])[((]\s*\d{1,2}[))]", "\n" + slave_ans.strip())
  301. if len(slave_ans_list) - 1 == len(one_group["slave"][j]["slave"]):
  302. for aj in range(len(slave_ans_list) - 1):
  303. one_group["slave"][j]["slave"][aj]["key"] = slave_ans_list[aj + 1].strip()
  304. one_group["slave"][j]["key"] = slave_ans_list[0].strip()
  305. else:
  306. one_group["key"] = ans_groups[k]["key"]
  307. # ans_start_no.remove(k)
  308. # break
  309. else:
  310. one_group['key'] = ans_groups[k]["key"]
  311. # 先暂时不去掉
  312. # for si, s in enumerate(one_group["slave"]):
  313. # if "errmsgs" in s:
  314. # del one_group["slave"][si]["errmsgs"]
  315. else: # ans_groups为空时
  316. # 针对答案在后面且【答案】1.xx 2.xx \n【解析】1.xx 2.xx \n【答案】3.xx 4.xx \n【解析】3.xx 4.xx
  317. # 或1.xx 2.xx \n【解析】1.xx 2.xx \n 3.xx 4.xx \n【解析】3.xx 4.xx
  318. if (one_group["slave"][0]["parse"] in ["略", ""] or one_group["slave"][0]["key"] in ["略", "", "见解析"]) \
  319. and ("-"in str(one_group["topic_num"]) and len(one_group["slave"]) > 1
  320. and one_group["slave"][-1]["parse"].strip()):
  321. st1, end1 = one_group["topic_num"].split("-") # 真实题号组
  322. t_seq_no = list(range(int(st1), int(end1) + 1))
  323. t_seq_no = list(map(str, t_seq_no))
  324. parse_list = re.split(r"(?<=[】\n])\s*(" + "|".join(t_seq_no) + r")\s*[、..、]",
  325. "\n" + one_group["slave"][-1]["parse"])
  326. comm_parse, parse_list = parse_list[0], parse_list[1:]
  327. parse_list = [pr.strip() for idn, pr in enumerate(parse_list) if idn % 2 == 1]
  328. if len(parse_list) in [int(end1) + 1 - int(st1), int(end1) - int(st1)]:
  329. if comm_parse:
  330. one_group["parse"] = comm_parse
  331. for ni, pr in enumerate(parse_list):
  332. if ni == int(end1) - int(st1): # 最后一个
  333. pr = re.sub("\n\s*【答案】$", "", pr)
  334. if re.search("\n\s*[【参考]*?译文\s*[】::]|\n\s*【点睛】", pr):
  335. pr, hd, one_group["parse"] = re.split("\n\s*([【参考]*?译文\s*[】::]|【点睛】)",
  336. pr, maxsplit=1)
  337. one_group["parse"] = hd + one_group["parse"]
  338. if one_group["slave"][ni]["key"] in ["略", "", "见解析"]:
  339. one_group["slave"][ni]["key"] = one_group["slave"][ni]["parse"]
  340. one_group["slave"][ni]["parse"] = pr
  341. return one_group
  342. def suojin(item_str):
  343. """
  344. 文本缩进处理
  345. :param item_str:
  346. :return:
  347. """
  348. ind_label = '<p style="text-indent: 2em">'
  349. con_list = re.split("\n+", item_str.strip())
  350. if len(con_list) > 1 and re.search("(阅读|针对).{,4}[资材]料", con_list[0]):
  351. new_con = con_list[0] + ind_label + ('</p>' + ind_label).join(con_list[1:]) + "</p>"
  352. else:
  353. new_con = ind_label + ('</p>' + ind_label).join(con_list) + "</p>"
  354. new_con = re.sub(r'<p style="text-indent: 2em">(\s*<img .+?)</p>($|<p style="text-indent: 2em">)',
  355. r'\1\n\2', new_con, flags=re.S).strip()
  356. return new_con
  357. def parse_split2group(item_list):
  358. """
  359. 有slave的题目将外层的解析拆入salve中
  360. :return:
  361. """
  362. # print(item_list)
  363. raw_item_list = item_list.copy()
  364. flag = 0
  365. # print(item_list)
  366. if "com_stem" in item_list and "slave" in item_list and len(item_list["slave"]) == 1: # 嵌套
  367. item_list = item_list["slave"][0]
  368. flag = 1
  369. if "slave" in item_list and (item_list["key"] or item_list["parse"]) and \
  370. any([True if not (s["key"] + s["parse"]).strip() else False for s in item_list["slave"]]):
  371. # 解析
  372. parse_list = re.split(r"(?<=[\s\n】])[((]\s*[\dl]{1,2}\s*[))]", "\n" + item_list["parse"].strip())
  373. if len(parse_list) - 1 == len(item_list["slave"]):
  374. for pi in range(len(parse_list) - 1):
  375. item_list["slave"][pi]["parse"] = parse_list[pi + 1].strip()
  376. item_list["parse"] = parse_list[0].strip()
  377. # 答案
  378. ans = re.sub(r"([((]\s*\d\s*[))])\s*[、..、,,::]\s*(\1)", r"\2", item_list["key"])
  379. ans_list = re.split("(?<=[\s\n】])[((]\s*[\dl]{1,2}\s*[))]", "\n" + ans.strip())
  380. if len(ans_list) - 1 == len(item_list["slave"]):
  381. for aj in range(len(ans_list) - 1):
  382. item_list["slave"][aj]["key"] = ans_list[aj + 1].strip()
  383. item_list["key"] = ans_list[0].strip()
  384. # 2021-12-21
  385. if "com_stem" in item_list:
  386. item_list["stem"] = item_list["com_stem"].strip() + "<br/>" + item_list["stem"] \
  387. if "stem" in item_list else item_list["com_stem"]
  388. del item_list["com_stem"]
  389. if flag:
  390. raw_item_list["slave"] = [item_list]
  391. item_list = raw_item_list
  392. return item_list
  393. def regroup_old(res_list, item_groups):
  394. """
  395. 将多个题共用一个题干的情况进行slave重组,如地理
  396. :param res_list: 拆分为小题后的结果
  397. :return:
  398. """
  399. new_res_dict = []
  400. start_no = [i for i in item_groups.keys() if i != "pos"]
  401. if not start_no:
  402. return res_list
  403. def takefirst(elem):
  404. return int(elem.split("-")[0])
  405. start_no.sort(key=takefirst) # 排序
  406. print(start_no)
  407. one_group = {}
  408. added_nos = [] # 已经slave了的题号
  409. for n, group_no in enumerate(start_no):
  410. one_group["common_stem"] = item_groups[group_no]
  411. st, end = group_no.split("-") # 真实题号组
  412. if not added_nos: # 开始
  413. if item_groups["pos"][n + 1] <= int(end): # 公共题文中的结束序号信息有误
  414. one_group["slave"] = res_list[int(st) - 1:item_groups["pos"][n + 1] - 1]
  415. added_nos.append(item_groups["pos"][n + 1] - 1)
  416. else:
  417. added_nos.append(int(end))
  418. one_group["slave"] = res_list[int(st) - 1:int(end)]
  419. elif int(st) <= added_nos[-1]: # 公共题文中的初始序号信息有误
  420. if n + 1 < len(item_groups["pos"]): # 不是最后一组
  421. if int(end) < item_groups["pos"][n + 1]:
  422. one_group["slave"] = res_list[added_nos[-1]:int(end)]
  423. added_nos.append(int(end))
  424. else: # 结束序号有误,以pos为主
  425. one_group["slave"] = res_list[added_nos[-1]:item_groups["pos"][n + 1] - 1]
  426. added_nos.append(item_groups["pos"][n + 1] - 1)
  427. else:
  428. if int(end) >= added_nos[-1]:
  429. one_group["slave"] = res_list[added_nos[-1]:int(end)]
  430. added_nos.append(int(end))
  431. else: # end值出错
  432. endp = [m for m, j in enumerate(res_list[added_nos[-1]:])
  433. if j["type"] != res_list[added_nos[-1]]["type"]]
  434. if endp:
  435. one_group["slave"] = res_list[added_nos[-1]:endp[0] + len(res_list[:added_nos[-1]])]
  436. added_nos.append(endp[0] + len(res_list[:added_nos[-1]]))
  437. else:
  438. added_nos.append(int(end))
  439. one_group["slave"] = res_list[int(st) - 1:int(end)]
  440. if int(st) > added_nos[-1] + 1:
  441. new_res_dict.extend(res_list[added_nos[-1]:int(st) - 1])
  442. one_group["type"] = one_group["slave"][0]["type"]
  443. one_group["que_num"] = len(one_group["slave"])
  444. new_res_dict.append(one_group)
  445. one_group = {}
  446. if added_nos[-1] < len(res_list):
  447. new_res_dict.extend(res_list[added_nos[-1]:])
  448. return new_res_dict