three_parse_structure.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. # paper3_process: 第三类word试卷模式, 题目和答案分开的情况
  4. # split2one_item:将所有行文本 按题型分大类,再在每个大类中切分每个题目
  5. # split2one_item_by_topicno:将所有行文本 按题型分大类,再在每个大类中按题号切分每个题目
  6. """
  7. 总共3种方案:1、教师用卷;2、按题号切分;3、划分试题和答案,再按题号切分
  8. """
  9. from structure.ans_structure import *
  10. from utils.insert_keywords import get_con
  11. from utils.item_resplit import resplit
  12. from utils.washutil import table_label_cleal
  13. from structure.stems_structure import stems_structure_byno
  14. from utils.item_type_line import get_item_head_info
  15. from utils.topic_no import judge_item_no_type, get_right_no
  16. from utils.stem_ans_split import stem_ans_split
  17. from collections import Counter
  18. from pprint import pprint
  19. def items_ans_reform(items_list, ans_list):
  20. """
  21. 第三种word试卷格式, 题目和答案分开的情况
  22. 答案也有几种类型:带题型?
  23. :param sent_list:
  24. :param split_point:
  25. :return:
  26. """
  27. con1 = list(filter(lambda x: x.strip() != "", items_list)) # 题目
  28. anss1 = list(filter(lambda x: x.strip() != "", ans_list)) # 答案,list中的每个元素为一行
  29. if re.match(".+?省.+?试[卷题]", con1[-1]):
  30. con1 = con1[:-1]
  31. if re.match(".+?省.+?试[卷题]|.*?答题?[卷卡页]", anss1[0]):
  32. anss1 = anss1[1:]
  33. print("-------答案页----------") # 答案页可能全是图片
  34. # pprint(anss1)
  35. print("-------答案页--end--------")
  36. #--------------答案页也包含题目的情况----------但可能题目不存在-----------------------
  37. ans_n = re.findall("【答案】", "\n".join(anss1))
  38. if ans_n and len(ans_n) == len(re.findall("【解析】", "\n".join(anss1)))>2: # 带相同个数的答案和解析
  39. print("答案页中有相同个数的答案和解析")
  40. item_res = split_by_keywords(anss1)
  41. if type(item_res) != str and item_res[0]:
  42. # 还要判断题目是否为空
  43. if len([i["item_id"] for i in item_res[0] if len(i["stem"].strip()) < 5]) < 2:
  44. return item_res
  45. # ----------------- 【解析 题目】----------------------------
  46. print('---------------解析 题目-------------------')
  47. ress = stems_structure_byno(con1)
  48. if type(ress) == str:
  49. return ress
  50. else:
  51. item_res, all_type, item_type_classify, item_no_type, item_type_num, new_item_no = ress # 全题目(不含解析)的结构化
  52. # 将空题目去掉
  53. new_res = []
  54. for k, sub_res in enumerate(item_res):
  55. if sub_res['stem'].strip():
  56. sub_res['stem'] = del_no(sub_res['stem'])
  57. new_res.append(sub_res)
  58. item_res = new_res
  59. # pprint(item_res)
  60. # 先对题目的切分结果进行纠正!!!!!
  61. item_res = resplit(item_res)
  62. print("item_type_classify:", item_type_classify)
  63. print("item_type_num:", item_type_num)
  64. print('----------解析 答案---------------')
  65. # -------------解析 答案---------------------------
  66. # 分两种情况:1>>答案中又按题型排列, 如一、选择题 1.答案 2.答案
  67. # 2>>答案中不含题型关键字,只按序号排列
  68. # 3>>答案中不含题型关键字,且题目中也没有,all_type, item_type_classify为空
  69. # print(anss1)
  70. new_ans_no1 = []
  71. rd1_is_fail = 0
  72. have_type_line = re.search(r"[一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等((\[]{2,5}题", "\n".join(anss1))
  73. if have_type_line:
  74. # 这里的anss1的清洗不应该影响rd2_is_fail中的原始文本!!先不修改看看再说
  75. anss1_cy = anss1.copy() # 复制一份,保证不能影响后面
  76. while re.search(r"<td><p>[A-F]</p></td>|</td><td>[A-F]</td><td>|([A-F]\s*){3,}", anss1_cy[0]) is None and \
  77. (re.search(r"[\u4e00-\u9fa5]", anss1_cy[0]) is None
  78. or re.search(r"[一二三四五六七八九十]\s*[、..、]\s*(<imgsrc.*?/>)?\s*.{2,5}题", anss1_cy[0]) is None):
  79. del anss1_cy[0]
  80. # 答案中的题型
  81. all_type2 = re.findall(r"\n\s*[一二三四五六七八九十]\s*[、..、::]\s*([^必考基础综合中共等::((\[]{2,5}题)|"
  82. r"\n\s*[、..、::]?\s*(单选题|非?选择题|不定选择题|多选题|填空题|计算题|[解简]答题|实验题|作图题|论述题|探究题)",
  83. "\n" + "\n".join(anss1_cy))
  84. all_type2 = ["".join(a) for a in all_type2]
  85. # '本大题' 后面处理
  86. print("答案中的题型:", all_type2)
  87. ans_str = "\n" + "\n".join(anss1_cy)
  88. # try:
  89. item_res, rd1_is_fail = anss_structure_with_type(item_res, ans_str, all_type, all_type2, item_type_num, item_type_classify)
  90. # except:
  91. # rd1_is_fail = 1
  92. # 没有题型行或第一次解析失败
  93. rd2_is_fail = 0
  94. if not have_type_line or rd1_is_fail: # 答案中没有题型行 或题型行名称不规范
  95. print('没有题型行或题目和答案的题型个数不一致或第一次解析失败')
  96. anss1 = list(
  97. map(lambda x: re.sub(r"(\n|^)\s*[一二三四五六七八九十]\s*[、..、::]?\s*(<p>)?"
  98. r"(\s*.{2,5}题.+?分\s*[.。]?\s*$|.*?[((].+?[得共]\d+分.*?[))].*?$"
  99. r"|\s*.{2,5}题\s*([((].+?[))])?).*?$|(\n|^)\s*[^\d]{2,5}题(.+?分\s*[))])?\s*$", "", x), anss1))
  100. # print("anss1:", anss1)
  101. raw_item_res = item_res
  102. # try:
  103. item_res = ans_structure_step1(anss1, item_type_classify, item_res) # 答案整体结构化
  104. if str(raw_item_res) != str(item_res):
  105. rd2_is_fail = 1
  106. # except:
  107. # rd2_is_fail = 1
  108. # for i, one_item in enumerate(item_res):
  109. # item_res[i].update({'key': "", 'parse': ""})
  110. # return item_res, item_no_type, rd2_is_fail
  111. for i, one_item in enumerate(item_res):
  112. if 'key' not in one_item:
  113. item_res[i]['key'] = ""
  114. if 'parse' not in one_item:
  115. item_res[i]['parse'] = ""
  116. return item_res, item_no_type, rd2_is_fail
  117. def ans_block_split(ans_list, item_res):
  118. anss1 = list(filter(lambda x: x.strip() != "", ans_list))
  119. if re.match(".+?省.+?试[卷题]|.*?答题?[卷卡页]", anss1[0]):
  120. anss1 = anss1[1:]
  121. rd1_is_fail = 0
  122. have_type_line = re.search(r"[一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等((\[]{2,5}题", "\n".join(anss1))
  123. if have_type_line:
  124. # 这里的anss1的清洗不应该影响rd2_is_fail中的原始文本!!先不修改看看再说
  125. anss1_cy = anss1.copy() # 复制一份,保证不能影响后面
  126. while re.search(r"<td><p>[A-F]</p></td>|</td><td>[A-F]</td><td>|([A-F]\s*){3,}", anss1_cy[0]) is None and \
  127. (re.search(r"[\u4e00-\u9fa5]", anss1_cy[0]) is None
  128. or re.search(r"[一二三四五六七八九十]\s*[、..、]\s*(<imgsrc.*?/>)?\s*.{2,5}题", anss1_cy[0]) is None):
  129. del anss1_cy[0]
  130. # 答案中的题型
  131. all_type2 = re.findall(r"\n\s*[一二三四五六七八九十]\s*[、..、::]\s*([^必考基础综合中共等::((\[]{2,5}题)|"
  132. r"\n\s*[、..、::]?\s*(单选题|非?选择题|不定选择题|多选题|填空题|计算题|[解简]答题|实验题|作图题|论述题|探究题)",
  133. "\n" + "\n".join(anss1_cy))
  134. all_type2 = ["".join(a) for a in all_type2]
  135. # '本大题' 后面处理
  136. print("答案中的题型:", all_type2)
  137. ans_str = "\n" + "\n".join(anss1_cy)
  138. item_res, rd1_is_fail = anss_structure_with_type(item_res, ans_str, [], all_type2, [], {})
  139. # 没有题型行或第一次解析失败
  140. rd2_is_fail = 0
  141. if not have_type_line or rd1_is_fail: # 答案中没有题型行 或题型行名称不规范
  142. print('没有题型行或题目和答案的题型个数不一致或第一次解析失败')
  143. anss1 = list(
  144. map(lambda x: re.sub(r"(\n|^)\s*[一二三四五六七八九十]\s*[、..、::]?\s*(<p>)?"
  145. r"(\s*.{2,5}题.+?分\s*[.。]?\s*$|.*?[((].+?[得共]\d+分.*?[))].*?$"
  146. r"|\s*.{2,5}题\s*([((].+?[))])?).*?$|(\n|^)\s*[^\d]{2,5}题(.+?分\s*[))])?\s*$", "", x),
  147. anss1))
  148. # print("anss1:", anss1)
  149. raw_item_res = item_res
  150. # try:
  151. item_res = ans_structure_step1(anss1, {}, item_res) # 答案整体结构化
  152. if str(raw_item_res) != str(item_res):
  153. rd2_is_fail = 1
  154. return item_res
  155. def split_by_keywords(con_list):
  156. """
  157. 第一种试卷格式:教师用卷,含答案和解析关键字
  158. 切分思路:
  159. 1.根据大题型分,再按【答案|解析】初步拆分题目,再在‘解析’和‘答案’间细分‘题干’和‘解析’
  160. :param con_list:
  161. :return: 每个切分后的题目组成的dict
  162. """
  163. # items_con = "\n" + "\n".join(con_list)
  164. # judge_item_no_type(items_con)
  165. # item_no_type = 1
  166. # all_con = table_label_cleal()
  167. # item_no = [int(no) for no in re.findall(r'\n+\s*([1-9][0-9]?)\s*[..、、]', all_con)]
  168. # if len(item_no) <= 2:
  169. # item_no_type = 2
  170. # item_no = [int(no) for no in re.findall(r'\n+\s*[((]\s*([1-9][0-9]?)\s*[))]\s*[..、、]?', all_con)]
  171. # if len(item_no) > 3:
  172. # 去掉多余空格,作用不大
  173. con2 = ["【delete】" if (k < len(con_list) - 1 and v.strip() == "" and (
  174. re.match(r"【(答案|解析)】|(答案|解析)\s*[::]|<imgsrc\d+|\s+", con_list[k + 1].strip()) or
  175. re.match(r"(([1-9]|[1-9][0-9])\s*[..、、]|[一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等]{2,4}题)",
  176. con_list[k + 1].strip()) is None))
  177. or (k > 0 and v.strip() == "" and (
  178. re.match(r"【(答案|解析)】$|(答案|解析)\s*[::]", con_list[k - 1].strip()) or
  179. re.match(r"[a-z<>/\s]*?[一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等]{2,4}题",
  180. con_list[k - 1].strip())))
  181. else v for k, v in enumerate(con_list)]
  182. con3 = list(filter(lambda x: x != "【delete】", con2))
  183. while con3 and con3[-1].strip() == "":
  184. del con3[-1]
  185. while con3 and con3[0].strip() == "":
  186. del con3[0]
  187. con3.append("") # 不然最后一个题就漏掉了
  188. # 开头没用信息处理
  189. con3[0] = re.sub(r"([一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等]{2,4}题)", r"\n\1", con3[0])
  190. while con3 and (re.search(r"[\u4e00-\u9fa5]", con3[0]) is None
  191. or (re.search(r"[一二三四五六七八九十]\s*[、..、]\s*[^必考基础综合中等]{2,4}题", con3[0]) is None
  192. and re.match("\s*[1-9]\s*[、..、].+?", con3[0]) is None)):
  193. del con3[0]
  194. # ----------------------------------开始结构化---------------------------------------------
  195. items_con = "\n" + "\n".join(con3)
  196. # 初步获取题号,题号类型
  197. items_con, item_no_info, item_no_type = judge_item_no_type(items_con)
  198. # 1、获取题型行信息、按题型行切分
  199. con4, title_info_dict, choice_class = get_item_head_info(items_con)
  200. all_type = title_info_dict["all_type"]
  201. select_type_id = title_info_dict["select_type_id"]
  202. each_item_score, each_item_score2 = title_info_dict["each_item_score"], title_info_dict["each_item_score2"]
  203. # 2、据是否有题型行分两步进行
  204. # 没有做拆图处理
  205. res = []
  206. if not all_type:
  207. print("不存在大题题型行或题型行格式有问题")
  208. if len(re.findall(r"\n\s*【答案】", items_con)) != len(re.findall(r"\n\s*【解析】", items_con)):
  209. return "不存在大题题型行或题型行格式有问题"
  210. else:
  211. item_no = []
  212. subcon = re.split(r"((?<=\n)\s*【答案】|(?<=\n)\s*【解析】)\n?", items_con.strip())
  213. pattern1 = re.compile(r"([1-9]|[1-9][0-9])\s*[..、、].+?")
  214. if re.match(pattern1, subcon[0].strip()):
  215. st_id = re.match(pattern1, subcon[0].strip()).group(1)
  216. if int(st_id) > 1:
  217. item_no.append(int(st_id))
  218. else:
  219. item_no.append(1)
  220. else:
  221. item_no.append(1)
  222. if len(subcon) == 5: # 只有1道题
  223. dd = dict(zip(["stem", "key", "parse"],
  224. re.split(r"(?<=\n)\s*【答案】|(?<=\n)\s*【解析】", table_label_cleal(items_con))))
  225. dd["type"] = ""
  226. dd["stem"] = re.sub(r"^\d+\s*[..、、]", "", dd["stem"][:5]) + dd["stem"][5:]
  227. dd["score"] = 0
  228. dd["errmsgs"] = []
  229. dd["item_id"] = item_no[0] # 要用实际id 不是索引序号
  230. res.append(dd)
  231. else:
  232. # ------在下一题【解析】在本题【答案】之间找到下一题【stem】的位置--------
  233. all_item, item_no, errmsg_dict, count = get_con(subcon, item_no_type, item_no, index=0)
  234. # item_no.extend(local_item_no)
  235. for idk, one_item in enumerate(all_item):
  236. if one_item:
  237. dd = dict(zip(["stem", "key", "parse"],
  238. re.split(r"(?<=\n)\s*【答案】\n?|(?<=\n)\s*【解析】\n?",
  239. table_label_cleal(one_item))))
  240. dd["type"] = ""
  241. dd["stem"] = re.sub(r"\d+\s*[..、、]", "", dd["stem"][:5]) + dd["stem"][5:]
  242. dd["score"] = 0
  243. dd["errmsgs"] = [errmsg_dict[idk]] if idk in errmsg_dict else []
  244. dd["item_id"] = item_no[idk]
  245. res.append(dd)
  246. else:
  247. if len(all_type) != len(con4):
  248. print("存在题型行没有换行")
  249. return "存在题型行末尾没有换行,请在所有题型行末尾重新换行" # 放第【2】种方案中进行处理
  250. else:
  251. # if "非选择题" in all_type:
  252. # return "第" + str(all_type.index("非选择题")+1) + "大题的题型不明确"
  253. index = 0 # 每个大题的第一题的题号索引位置
  254. for num, one_type in enumerate(con4):
  255. count = 1
  256. if len(re.findall(r"\n\s*【答案】", one_type)) == len(re.findall(r"\n\s*【解析】", one_type)):
  257. subcon = re.split(r"((?<=\n)\s*【答案】|(?<=\n)\s*【解析】)\n?", one_type.strip())
  258. # index根据第一道题的题号进行纠正
  259. item_no = []
  260. pattern1 = re.compile(r"([1-9]|[1-9][0-9])\s*[..、、].+?")
  261. if re.match(pattern1, subcon[0].strip()):
  262. st_id = re.match(pattern1, subcon[0].strip()).group(1)
  263. if num == 0 and int(st_id) != 1:
  264. index = int(st_id) - 1
  265. item_no.append(int(st_id))
  266. else:
  267. item_no.append(index+1)
  268. if len(subcon) == 5: # 只有1道题
  269. dd = dict(zip(["stem", "key", "parse"],
  270. re.split(r"(?<=\n)\s*【答案】|(?<=\n)\s*【解析】", table_label_cleal(one_type))))
  271. dd["type"] = all_type[num]
  272. dd["stem"] = re.sub(r"^\d+\s*[..、、]", "", dd["stem"][:5]) + dd["stem"][5:]
  273. dd["score"] = each_item_score[num]
  274. dd["errmsgs"] = []
  275. dd["item_id"] = item_no[0] # 要用实际id 不是索引序号
  276. if not dd["score"] and each_item_score2 and str(dd["item_id"]) in each_item_score2.keys():
  277. dd["score"] = each_item_score2[str(dd["item_id"])]
  278. if select_type_id and dd["item_id"] in select_type_id:
  279. dd['is_optional'] = 'true'
  280. if dd["score"] == 0.0 and title_info_dict["total_score"][num] > 0.0:
  281. dd["score"] = title_info_dict["total_score"][num]
  282. res.append(dd)
  283. else:
  284. # ------在下一题【解析】在本题【答案】之间找到下一题【stem】的位置,再按此3个关键字进行 切分--------
  285. all_item, item_no, errmsg_dict, count = get_con(subcon, item_no_type, item_no,
  286. all_type=all_type, num=num, index=index)
  287. # item_no.extend(local_item_no)
  288. for idk, one_item in enumerate(all_item):
  289. dd = dict(zip(["stem", "key", "parse"],
  290. re.split(r"(?<=\n)\s*【答案】\n?|(?<=\n)\s*【解析】\n?",
  291. table_label_cleal(one_item))))
  292. dd["type"] = all_type[num]
  293. dd["stem"] = re.sub(r"\d+\s*[..、、]", "", dd["stem"][:5]) + dd["stem"][5:]
  294. dd["score"] = each_item_score[num]
  295. dd["errmsgs"] = [errmsg_dict[idk]] if idk in errmsg_dict else []
  296. dd["item_id"] = item_no[idk] # idk+1+index 为序号
  297. if choice_class:
  298. for k, v in choice_class.items():
  299. if dd["item_id"] in v:
  300. dd["type"] = k + "选题"
  301. # elif len(choice_class) == 1:
  302. # dd["type"] = "多选题" if k == "单" else "单选题"
  303. if not dd["score"] and each_item_score2 and str(dd["item_id"]) in each_item_score2.keys():
  304. dd["score"] = each_item_score2[str(dd["item_id"])]
  305. if select_type_id and dd["item_id"] in select_type_id:
  306. dd['is_optional'] = 'true'
  307. res.append(dd)
  308. # pprint(res)
  309. else:
  310. return "第" + str(num + 1) + "大题《" + all_type[num] + "》中【答案】或【解析】格式有误或其中某道题中出现多个相同关键字或漏关键字"
  311. index += count
  312. for i, one_item in enumerate(res):
  313. if 'key' not in one_item:
  314. res[i]['key'] = ""
  315. if 'parse' not in one_item:
  316. res[i]['parse'] = ""
  317. return res, item_no_type
  318. def split_by_topicno(con_list):
  319. """
  320. 第二种试卷格式: 不同时或都不含有{答案}和{解析}关键字
  321. 按题号切分每个题目
  322. 将所有行文本 按题型分大类,再在每个大类中切分每个题目
  323. :param con_list: 所有行文本组成的list
  324. :return: [{},{}]
  325. """
  326. con1 = list(filter(lambda x: x.strip() != "", con_list))
  327. ress = stems_structure_byno(con1) # 按题号切分后的初步结构化
  328. if type(ress) == str:
  329. return ress
  330. else:
  331. res, all_type, item_type_classify, item_no_type, item_type_num, new_item_no = ress
  332. # res, all_type, item_type_classify = stems_structure_byno(con1)
  333. print("item_type_num:", item_type_num)
  334. # pprint(res)
  335. # 可能存在有的题目有解析,有的没有
  336. for k, one_res in enumerate(res):
  337. if re.search('\n【(答案|[解分][析答]|详解|点[评睛]|考点|专题)】', one_res["stem"]):
  338. case = "case1" # 默认有“答案”关键字
  339. if re.search(r'\n【答案】|[\n】]\s*答案\s*[::]', one_res["stem"]) is None:
  340. # 没“答案”关键字
  341. case = "case0"
  342. dd1 = stem_ans_split(one_res, case) # 对切分后的每道题再细分
  343. one_res["stem"] = dd1["stem"]
  344. del dd1["stem"]
  345. one_res.update(dd1)
  346. else: # 没有解析的情况
  347. one_res.update({"key": "", "parse": ""})
  348. one_res["stem"] = del_no(one_res["stem"], item_no_type)
  349. if 'pic' in one_res:
  350. one_res["stem"] += "\n" + "\n".join(one_res["pic"])
  351. del one_res["pic"]
  352. # 先对题目的切分结果进行纠正!!!!!
  353. res = resplit(res)
  354. # 对最后一个题后面带个别答案(无答案页)
  355. if res:
  356. pattern1 = re.search('\n\s*([1-9]|[1-9][0-9])\s*[..、、]\s*(解\s*[::]|【解析|【答案)', res[-1]["stem"])
  357. if pattern1:
  358. breakp = pattern1.start()
  359. ans_str = res[-1]["stem"][breakp:]
  360. ans_no_info = pre_get_item_no(ans_str, item_no_type)
  361. ans_no, ans_no_idx = get_right_no(ans_no_info)
  362. all_ans = [del_no(ans_str[i:j]) for i, j in zip(ans_no_idx, ans_no_idx[1:] + [None])]
  363. res[-1]["stem"] = res[-1]["stem"][:breakp]
  364. res = get_ans_match(res, all_ans, ans_no)
  365. else:
  366. ans_str = res[-1]["stem"] + res[-1]["parse"]
  367. ans_no_info = pre_get_item_no(ans_str, item_no_type)
  368. ans_no, ans_no_idx = get_right_no(ans_no_info)
  369. if len(ans_no) == len(res):
  370. all_ans = [del_no(ans_str[i:j]) for i, j in zip(ans_no_idx, ans_no_idx[1:] + [None])]
  371. res[-1]["stem"] = res[-1]["stem"][:ans_no_idx[0]]
  372. res = get_ans_match(res, all_ans, ans_no)
  373. elif ans_no_idx:
  374. try:
  375. ans_no1, table_ans, st = get_table_ans(res[-1]["stem"][:ans_no_idx[0]], [], flag=1)
  376. if table_ans and 0 < ans_no[0] - ans_no1[-1] < 3:
  377. all_ans = table_ans
  378. all_ans.extend([del_no(ans_str[i:j]) for i, j in zip(ans_no_idx, ans_no_idx[1:] + [None])])
  379. new_ans_no = ans_no1
  380. new_ans_no.extend(ans_no)
  381. if st >= 0:
  382. res[-1]["stem"] = res[-1]["stem"][:st]
  383. else:
  384. res[-1]["stem"] = res[-1]["stem"][:ans_no_idx[0]]
  385. res = get_ans_match(res, all_ans, new_ans_no)
  386. except:
  387. if len(ans_no)>4 and all([True if not one_res["key"] and not one_res["parse"]
  388. else False for one_res in res[:-1]]):
  389. all_ans = [del_no(ans_str[i:j]) for i, j in zip(ans_no_idx, ans_no_idx[1:] + [None])]
  390. res[-1]["stem"] = res[-1]["stem"][:ans_no_idx[0]]
  391. res = get_ans_match(res, all_ans, ans_no)
  392. # 没有识别出答案切分点的情况,很可能答案里的部分也当成题文进行拆分,所以先判断下是否有相同的id
  393. all_no = [one_res['item_id'] for one_res in res]
  394. if len(list(set(all_no))) - len(all_no) < -2:
  395. Count_no = sorted(dict(Counter(all_no)).items(), key=lambda d: d[1], reverse=True)
  396. if Count_no[0][1] > 1:
  397. split_idx = [i for i, no in enumerate(all_no) if no == Count_no[0][0]][1]
  398. for one_res in res[split_idx:]:
  399. if re.search("[((]\s+[))]|(等于|存在|[是有为])多少|求.*?[??]",
  400. one_res["stem"] + "\n" + one_res["parse"]) is None:
  401. bef_no = [k for k, j in enumerate(res[:split_idx]) if j["item_id"]==one_res["item_id"]]
  402. if bef_no and not res[:split_idx][bef_no[0]]["parse"]:
  403. res[:split_idx][bef_no[0]]["parse"] = one_res["stem"] + "\n" + one_res["parse"]
  404. return res[:split_idx],item_no_type
  405. return res, item_no_type