dati2slave.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import re
  4. from structure.ans_structure import only_parse_split
  5. def get_slave(one_item, con, parse, ans):
  6. """
  7. 带小问的大题 按小问切分
  8. :return:
  9. """
  10. # if re.search(r"[;;]", ans) and len(re.findall(r"[((]\s*\d\s*[))]", con)) > 1: # 模板要求老师小题题号(1)(2)
  11. th1 = {"(Ⅰ)": "(1)", "(Ⅱ)": "(2)", "(Ⅲ)": "(3)", "(IV)": "(4)", "(Ⅳ)": "(4)", "(Ⅴ)": "(5)",
  12. "Ⅰ": "(1)", "Ⅱ": "(2)", "Ⅲ": "(3)", "IV": "(4)", "Ⅳ": "(4)", "Ⅴ": "(5)"}
  13. con = re.sub(r"([\n】])\s*[((]\s*(" + "|".join(th1.keys()) + ")\s*[))]", lambda x: x.group(1) + th1[x.group(2)], con)
  14. parse = re.sub(r"([\n】])\s*[((]\s*(" + "|".join(th1.keys()) + ")\s*[))]", lambda x: x.group(1) + th1[x.group(2)],
  15. parse)
  16. con = re.sub("(<[/a-z]+>|[((]\s*\d+\s*分\s*[))])\s*([((]\s*\d\s*[))])", r"\1" + "\n" + r"\2", con)
  17. parse = re.sub("(<[/a-z]+>)\s*([((]\s*\d\s*[))])", r"\1" + "\n" + r"\2", parse)
  18. # parse = re.sub("(答案分?别?[为是]?\s*[::])\s*[((]\s*(\d)\s*[))]", r"\1[#[\2]#]", parse)
  19. kuo_num = len(re.findall(r"[((]\d[))]", con.replace(" ", "")))
  20. circle_num = len(re.findall(r"\n[((](i{1,3}|[ⅰⅱⅲⅳ①②③④])[))]|\n[①②③④]\s*(?![+-])", con.replace(" ", "")))
  21. if len(re.findall(r"[((]\d[))]|\n[((](i{1,3}|[ⅰⅱⅲⅳ①②③④])[))]|\n[①②③④]\s*(?![+-])", con.replace(" ", ""))) > 1:
  22. by_sub_item = True # 答案是按照小题获取还是按照空的个数,答案老师有时候全部用;隔开,有时候又会分题号
  23. # 题干
  24. if kuo_num > 1:
  25. con = re.sub(r"((?<=[\n::;;。求])|^)\s*([((]\s*\d\s*[))])\s*(?!小?题?中)", "【ⅳ】", con)
  26. elif circle_num > 1:
  27. con = re.sub(r"((?<=[\n::;;。求])|^)\s*([((]\s*(\d|i{1,3}|[ⅰⅱⅲⅳ①②③④])\s*[))]|[①②③④]\s*(?![+-]))", "【ⅳ】", con)
  28. # print(con)
  29. # print('-------------------------------')
  30. con_list = re.split(r"【ⅳ】", con)
  31. # 答案 不能只用空格隔开
  32. ans_list = [] if ans != "见解析" else "见解析"
  33. if re.search(r"[;;]|\n[((](\d|i{1,3}|[ⅰⅱⅲⅳ①②③④])[))]|\n[①②③④]\s*(?![+-])", ans.replace(" ", "")):
  34. if len(re.findall(r"[((]\d[))]", ans.replace(" ", ""))) > 1: # 优先按(\d)拆分
  35. # and len(re.findall(r"\n[((](i{1,3}|[ⅰⅱⅲⅳ①②③④])[))]|\n[①②③④]\s*(?![+-])", ans.replace(" ", ""))) > 0:
  36. ans = re.sub(r"((?<=[\n::;;。])|^)\s*([((]\s*\d\s*[))])", "【ⅳ】", ans)
  37. elif len(re.findall(r"\n[((](i{1,3}|[ⅰⅱⅲⅳ①②③④])[))]|\n[①②③④]\s*(?![+-])", ans.replace(" ", ""))) > 1:
  38. ans = re.sub(r"((?<=[\n::;;。])|^)\s*([((]\s*(\d|i{1,3}|[ⅰⅱⅲⅳ①②③④])\s*[))]|[①②③④]\s*(?![+-]))", "【ⅳ】",
  39. ans)
  40. ans_list.extend(re.split(r"【ⅳ】", ans))
  41. while not ans_list[0]:
  42. ans_list = ans_list[1:]
  43. if len(ans_list) < len(con_list) - 1:
  44. ans_list = re.split(r"[;;](?! height)", ans)
  45. by_sub_item = False
  46. # 解析
  47. parse_list = []
  48. syn_list = []
  49. analy_comment = []
  50. parse_common = ""
  51. ans_summarize = ""
  52. if parse:
  53. if re.search('【(详解|解析|解答)】', parse): # 2020-6-10
  54. temp_parse = re.split('【详解】|【解析】|【解答】', parse)
  55. parse = temp_parse[1]
  56. # parse_list.append(temp_parse[0])
  57. # 若分析也分小问来,则单独拆分
  58. if len(re.findall(r"[((]\d[))]", temp_parse[0].replace(" ", ""))) > 1:
  59. syn = re.sub(r"((?<=[\n::;;。】])|^)\s*([((]\s*\d\s*[))])", "【ⅳ】", temp_parse[0])
  60. syn_list.extend(re.split(r"【ⅳ】", syn))
  61. syn_list.append(temp_parse[0]) # 【详解】|【解析】|【解答】 前面的部分
  62. if re.search("【(点评|点睛)】", parse):
  63. comment = re.split('(【点评】|【点睛】)', parse)
  64. analy_comment.append(comment[-2] + comment[-1])
  65. parse = comment[0]
  66. # 解析拆分小问
  67. if len(re.findall(r"[((]\d[))]", parse.replace(" ", ""))) > 1:
  68. parse = re.sub(r"((?<=[\n::;;。])|^)\s*([((]\s*\d\s*[))])", "【ⅳ】", parse)
  69. parse = re.sub(r"(/>)\s*([((]\s*\d\s*[))])", r"\1【ⅳ】", parse)
  70. else:
  71. parse = re.sub(r"((?<=[\n::;;。])|^)\s*([((]\s*(\d|i{1,3}|[ⅰⅱⅲⅳ①②③④])\s*[))]|[①②③④]\s*(?![+-]))", "【ⅳ】", parse)
  72. parse = re.sub(r"(/>)\s*([((]\s*(\d|i{1,3}|[ⅰⅱⅲⅳ①②③④])\s*[))]|[①②③④]\s*(?![+-]))", r"\1【ⅳ】", parse)
  73. # 将解析末尾出现的‘故答案为’在成功slave后删掉
  74. if re.search('(故|因[而此]|所以)\s*[::]?\s*答案分?别?([为是]|填)?\s*[::]\s*(.+?)(\n|$)', parse):
  75. ans_s = re.search('(\n.*?|^.*?|<p>)((故|因[而此]|所以)\s*[::]?\s*答案分?别?([为是]|填)?\s*[::]\s*(.+?))(\n|$)', parse)
  76. # print("ans_s:",ans_s)
  77. # print(parse)
  78. if ans_s.group(5) and ans_s.group(5).count("【ⅳ】") > 1:
  79. ans_summarize = ans_s.group(2)
  80. ans_s_index = parse.index(ans_summarize) if ans_s.group(1) == '<p>' or not ans_s.group(1).strip() \
  81. or ans_s.group(1).strip() is None else parse.index(ans_s.group(1))
  82. ans_summarize = [ans_s.group(2), ans_s_index]
  83. parse = parse.replace(ans_summarize[0], "")
  84. elif ans_s.group(5) and "【ⅳ】" in ans_s.group(5):
  85. aa5 = ans_s.group(5).replace("【ⅳ】", "")
  86. parse = parse.replace(ans_s.group(5), aa5)
  87. parse = re.sub("(【ⅳ】\s*解答?\s[::])\s*【ⅳ】", r"\1", parse)
  88. little_parse = re.split(r"【ⅳ】", parse)
  89. if len(syn_list) - 1 == len(little_parse) and len(little_parse) > 2: # 不拼接;syn_list长有4,little_parse长为3
  90. parse_list = ["分析:{}\n解答:{}".format(syn_list[k + 1], p) for k, p in
  91. enumerate(little_parse[1:])]
  92. parse_common = syn_list[0] + '\n' + little_parse[0] # 分小问解析的共同部分
  93. else:
  94. if syn_list and len(re.sub("[^\u4e00-\u9fa5]", "", syn_list[-1])) > 4: # 有4个汉字以上
  95. analy = syn_list[-1]
  96. analy_comment.insert(0, analy)
  97. parse_list.extend(little_parse)
  98. if len(parse_list) > 1:
  99. # if parse_list[0].strip():
  100. # common = parse_list[0]
  101. # parse_list = ["{} {}".format(common, p) for p in parse_list]
  102. parse_common = parse_list[0]
  103. parse_list = parse_list[1:]
  104. one_item = split2little_con(con_list, ans_list, parse_list, one_item, by_sub_item, ans_summarize)
  105. if "slave" in one_item and one_item["slave"]:
  106. one_item['parse'] = parse_common
  107. if analy_comment:
  108. one_item['analy'] = "\n".join(analy_comment)
  109. else:
  110. if re.findall(r"_{2,}", one_item["content"]):
  111. one_item["blank_num"] = len(re.findall(r"_{2,}", one_item["content"]))
  112. if re.search("^[A-Z]{2,}$", re.sub("\W", "", ans)):
  113. one_item["item_topic_name"] = "多选题"
  114. return one_item
  115. def split2little_con(con_list, ans_list, parse_list, one_item, is_sub_item, ans_summarize):
  116. """
  117. 将按小问切分开的题干、答案、解析 进行 【结构化组合】
  118. :param con_list:切开了小问的题干
  119. :param ans_list:切开了小问的答案
  120. :param parse_list:切开了小问的解析
  121. :param one_item: 初步切开的一道题目
  122. :param is_sub_item: 答案是否按小题号获取(还是按照空的个数获取)的标志
  123. :param ans_summarize: 解析中的综述 [内容,索引]
  124. :return:
  125. """
  126. # print(con_list)
  127. # print(ans_list)
  128. # print(parse_list)
  129. # print('***********************')
  130. old_con = one_item["content"]
  131. if len(con_list) > 1:
  132. if con_list[0] == "": # 说明全是小题,没有总题文
  133. one_item["content"] = ""
  134. else:
  135. one_item["content"] = con_list[0]
  136. slave = []
  137. for index, s in enumerate(con_list[1:]):
  138. blank_num = len(re.findall(r"_{2,}", s))
  139. s = re.sub(r"[((]\d+分[))]", "", s[:9]) + s[9:]
  140. one_slave = {}
  141. if len(con_list) - len(parse_list) == 1:
  142. one_slave = {"topic_no": index + 1,
  143. "content": s,
  144. # "answer": ans_list[index],
  145. "parse": parse_list[index]} # 按索引取解析
  146. if isinstance(ans_list, list) and len(con_list) - len(ans_list) == 1:
  147. one_slave["answer"] = ans_list[index]
  148. elif not parse_list and isinstance(ans_list, list) and len(con_list) - len(ans_list) == 1:
  149. one_slave = {"topic_no": index + 1,
  150. "content": s,
  151. "answer": ans_list[index],
  152. }
  153. if one_slave:
  154. if is_sub_item is False: # 有具体答案的情况,可能要按照空的个数来拿,但至少一个小题一个答案
  155. if blank_num >= 1: # 填空题
  156. one_ans = re.sub(r"(\n|^)\s*[((]\s*" + str(index + 1) + r"\s*[))](.+)", r"\2",
  157. ";".join(ans_list[:blank_num]))
  158. one_ans = re.sub(r"((?<=[\n;;。])|^)\s*([((]\s*(i{1,3}|[ⅰⅱⅲⅳ①②③④])\s*[))]|[①②③④]\s*(?![+-]))(.+)",
  159. r"【ⅳ】\4", one_ans)
  160. one_slave["answer"] = one_ans.replace("【ⅳ】", "")
  161. ans_list = ans_list[blank_num:]
  162. else: # 没有空的时候,这个题也应该至少有一个答案
  163. one_slave["answer"] = ans_list[0] if len(ans_list) > 0 else ""
  164. ans_list = ans_list[1:]
  165. elif type(ans_list) == str: # 无具体答案的情况:答案为‘见解析’
  166. one_slave["answer"] = ans_list
  167. if one_item["item_topic_name"] == "填空题":
  168. one_item["item_topic_name"] = "解答题"
  169. if blank_num > 0:
  170. one_slave["blank_num"] = blank_num
  171. else:
  172. if one_item["item_topic_name"] == "填空题":
  173. one_item["item_topic_name"] = "解答题"
  174. # 对带小题的大题,对每个小题的答案重新再提取一次
  175. if not ans_list or "answer" not in one_slave or not one_slave["answer"] or one_slave["answer"] == '见解析':
  176. new_ans = only_parse_split(one_slave["parse"], one_item["item_topic_name"], reparse_n = 2) # 再解析
  177. if new_ans["answer"]:
  178. one_slave["answer"] = new_ans["answer"]
  179. if not new_ans["parse"]:
  180. one_slave["parse"] = ""
  181. slave.append(one_slave)
  182. one_item["slave"] = slave
  183. one_item["topic_no"] = "1-{}".format(len(slave)) if len(slave) > 1 else "1"
  184. # 有slave的话,就把外面的ans和parse字段给删除掉
  185. if slave:
  186. del one_item["answer"]
  187. if parse_list:
  188. del one_item["parse"]
  189. else:
  190. one_item["content"] = old_con
  191. if ans_summarize:
  192. one_item["parse"] = one_item["parse"][:ans_summarize[1]] + '\n' + ans_summarize[0] + '\n' + one_item["parse"][ans_summarize[1]:]
  193. return one_item