stems_to_groups.py 26 KB

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