final_structure.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # 各题型结构化
  2. import re
  3. from structure.option import option_structure, new_options_rank, table_option_struc
  4. from structure.dati2slave import get_slave
  5. def one_item_structure(xyz):
  6. """
  7. 判断解析类型,解析类型为:
  8. if:
  9. 1.stem不需要再做其他处理<-- 答案没有[;;],且答案不是ABCDEFG
  10. 2.选择题类,需要把stem中的ABCD各选项内容提取出来<--答案是ABCDEFG
  11. else:
  12. 都要看是否含有小题,如果含有小题,需要把小题提取出来,slave
  13. 3.填空题类,(1)需要提取stem中下划线的个数
  14. 选择题结构化:单选或者多选<--要把各选项是什么提取出来放在slave中
  15. one_item:{"stem":xxxx,"key":xxx,"parse":xxx}
  16. consumer: 分“高中数学”还是“全学科”;
  17. item_no_type:题号是否以(\d)的形式
  18. :return:
  19. """
  20. one_item, consumer, item_no_type = xyz
  21. if "【章节】" in one_item["parse"]: # 属于后一个题的,后面须调整
  22. one_item["chapter"] = one_item["parse"].split("【章节】")[1].split("\n")[0]
  23. one_item["parse"] = one_item["parse"].replace("【章节】" + one_item["chapter"], "")
  24. if "【章节】" in one_item["stem"]: # 属于后一个题的,后面须调整
  25. one_item["chapter"] = one_item["stem"].split("【章节】")[1].split("\n")[0]
  26. one_item["stem"] = one_item["stem"].replace("【章节】" + one_item["chapter"], "")
  27. if "【选做题】" in one_item["stem"] + one_item["key"] + one_item["parse"]:
  28. opt_str = re.search(r"【选做题】:'(\d+)分'", one_item["stem"] + one_item["key"] + one_item["parse"])
  29. one_item["option_st"] = "选做题,"+opt_str.group(1) if opt_str else "选做题" # 选做题开始的位置,后面的题开始是选做题
  30. one_item["stem"] = re.sub("【选做题】(:'(\d+)分')?", "", one_item["stem"])
  31. one_item["key"] = re.sub("【选做题】(:'(\d+)分')?", "", one_item["key"])
  32. one_item["parse"] = re.sub("【选做题】(:'(\d+)分')?", "", one_item["parse"])
  33. ans = one_item["key"]
  34. con = one_item["stem"]
  35. parse = re.sub(r"((?<=[\n】])|^)\s*解\s*[::]", "", one_item["parse"])
  36. if not one_item["type"]:
  37. # one_item["errmsgs"].append("本题没有给出明确题型!")
  38. # return one_item
  39. if re.match(r"[A-Z][A-Z;;和与、、\s]*?$", ans.strip()):
  40. one_item["type"] = "单选题" if len(ans.strip()) == 1 else "多选题"
  41. elif re.search(r"[((]\s*[))]", one_item["stem"]) or \
  42. len(re.findall(r"[\n\s]\s*[A-D]\s*[..、、]", one_item["stem"])) >= 4:
  43. one_item["type"] = "选择题"
  44. elif re.findall(r"_{2,}", one_item["stem"]):
  45. one_item["type"] = "填空题"
  46. else:
  47. one_item["type"] = "简答题"
  48. elif one_item["type"].replace("题", "") in ["单选", "多选", "选择", "不定选择"]:
  49. # # 存在试卷所给题型不规范或本身就是错的情况
  50. if len(re.findall("\n\s*[((]\s*[1-9]\s*[))]", "\n" + con)) > 1 \
  51. or (re.search(r"[((]\s*[))][\s\n]*(<img src=|A\s*[..、、])", con) is None
  52. and len(re.findall(r"[\n\s]\s*[A-D]\s*[..、、]", con)) < 2):
  53. if re.findall(r"_{2,}", con):
  54. one_item["type"] = "填空题"
  55. else:
  56. one_item["type"] = "解答题"
  57. topic_type = one_item["type"]
  58. # print(topic_type)
  59. if topic_type.replace("题", "") in ["单选", "多选", "选择", "不定选择"]:
  60. one_item = option_structure(one_item, con, ans, item_no_type)
  61. one_item["stem"] = re.sub("(?<![/\"]>)(<br\s*/?>|\n)+\s*(?!<img)", "", one_item["stem"])
  62. if 'options' not in one_item:
  63. one_item["options"] = []
  64. # 表格类型的选项再解析,
  65. if "<table>" in one_item["stem"]:
  66. may_options = table_option_struc(one_item["stem"])
  67. if may_options:
  68. one_item["options"] = may_options
  69. one_item["options_rank"] = 2
  70. one_item["errmsgs"] = [emg for emg in one_item["errmsgs"] if "选项格式不正确" not in emg]
  71. else: # 选择题结构化成功时,对选项排列方式再换思路算
  72. options_rank_2 = new_options_rank(one_item["options"])
  73. if options_rank_2:
  74. one_item["options_rank"] = options_rank_2
  75. one_item["blank_num"] = len(re.findall(r"_{2,}", one_item["stem"]))
  76. one_item["answer_type"] = "选择题"
  77. elif consumer == 'toslave': # 拆小题
  78. one_item = get_slave(one_item, con, parse, ans)
  79. # if ('slave' not in one_item or not one_item['slave']) and 'analy' in one_item:
  80. # del one_item['analy']
  81. # if one_item["type"] == "多选题":
  82. # one_item = option_structure(one_item, con, ans, item_no_type)
  83. # if 'options' not in one_item:
  84. # one_item["options"] = []
  85. else: # 不拆小题,非选择题
  86. if "#" in ans:
  87. one_item["key"] = one_item["key"].replace("#", "; ")
  88. pattern1 = re.compile(r"([是为点]|等于|=|=|有|存在)\s*_+((<img src=((?!/>).)*?/>|[^_;;。?!\n])+?)_+"
  89. r"([cdkm上]?m?\s*.?[。.?]?\s*($|<br/>|<img src|……))")
  90. pattern2 = re.compile(r"((有|存在|[是为])[\u4e00-\u9fa5]{0,2})\s*_+(\d+)_+\s*([\u4e00-\u9fa5,,;;。..])")
  91. if re.findall(r"_{2,}", one_item["stem"]): # re.search("_+([^_]*?)_+", one_item['stem']):
  92. one_item["blank_num"] = len(re.findall(r"_{2,}", one_item["stem"]))
  93. else:
  94. # 是否只需将所有标点符号去除即可,这里容易判断错误!!!!
  95. if re.search("^[A-Z]{2,}$",
  96. re.sub(r"[^\w><≤≥≡≦≧+-≈≠﹢﹣±㏒㏑∑∏π><==×÷/()()﹙﹚\[\]﹛﹜{\}∧∨∠▰▱△∆⊙⌒"
  97. r"⊆⊂⊇⊃∈∩∉∪⊕∥∣≌∽∞∝⊥∫∬∮∯Φ∅≮≯∁∴∵∷←↑→↓↖↗↘↙‖〒¤○′″¢°℃℉"
  98. r"αβγδεζηθικλμνξορστυφχψωϕ%‰℅㎎㎏㎜㎝㎞㎡㎥㏄㏎㏕$£¥º¹²³⁴ⁿ₁₂₃₄·∶½⅓⅔¼¾⅛⅜⅝⅞"
  99. r"ΑΒΓΔΕΖΗΘΙΚΜ]", "", ans)):
  100. one_item["type"] = "多选题"
  101. one_item = option_structure(one_item, con, ans, item_no_type)
  102. if 'options' not in one_item:
  103. one_item["options"] = []
  104. if one_item["type"] == "填空题" and re.search("_{2,}|填正确答案", one_item['stem']) is None:
  105. # -----放在huanhang_wash_after中调整--------------
  106. # blank_ans =[]
  107. # while re.search(pattern1, one_item["stem"]): # 答案直接填在____上的情况
  108. # blank_con1 = re.search(pattern1, one_item["stem"])
  109. # one_item["stem"] = one_item["stem"].replace(blank_con1.group(0),
  110. # blank_con1.group(1) + "____" + blank_con1.group(5))
  111. # blank_ans.append(blank_con1.group(2))
  112. # while re.search(pattern2, one_item["stem"]): # 答案直接填在____上的情况
  113. # blank_con1 = re.search(pattern2, one_item["stem"])
  114. # one_item["stem"] = one_item["stem"].replace(blank_con1.group(0),
  115. # blank_con1.group(1) + "____" + blank_con1.group(3))
  116. # blank_ans.append(blank_con1.group(2))
  117. # if not ans:
  118. # one_item["key"] = ";".join(blank_ans)
  119. # one_item["blank_num"] = len(blank_ans)
  120. # ----------------------------------------------
  121. if re.match(r"[A-Z][A-Z;;和与、、\s]*?$", ans.strip()):
  122. one_item["type"] = "单选题" if len(ans.strip()) == 1 else "多选题"
  123. one_item = option_structure(one_item, con, ans, item_no_type)
  124. if 'options' not in one_item:
  125. one_item["options"] = []
  126. elif re.search(r"[((]\s*[))]", one_item["stem"]) or ('步骤' not in one_item["stem"] and
  127. len(re.findall(r"[\n\s]\s*[A-D]\s*[..、、]", one_item["stem"])) >= 4):
  128. one_item["type"] = "选择题"
  129. one_item = option_structure(one_item, con, ans, item_no_type)
  130. if 'options' not in one_item:
  131. one_item["options"] = []
  132. elif re.findall('(有|存在|[是为==])[ \s]{3,}[a-zA-Z]', one_item["stem"]):
  133. one_item["blank_num"] = len(re.findall('(有|存在|[是为==])[ \s]{3,}[a-zA-Z]', one_item["stem"]))
  134. elif re.findall('[ \s]{3,}[a-zA-Z]\s*[,;.。;,]', one_item["stem"]):
  135. one_item["blank_num"] = len(re.findall('\s{3,}\n*\s*[a-zA-Z]\s*[,;.。;,.]', one_item["stem"]))
  136. elif re.search(pattern1, one_item["stem"]) is None and re.search(pattern2, one_item["stem"]) is None:
  137. stem = re.sub("<img src=.*?/>|[,,.。.、、]", "", one_item["stem"])
  138. if len(stem) > 2:
  139. one_item["type"] = "解答题"
  140. # print('------------------------------------------------')
  141. if one_item:
  142. # if re.match(r"(\[.*?\])?\s*\(.*?(\d+)分\)", one_item["stem"].strip()):
  143. # # print(one_item["stem"])
  144. # score_info = re.match(r"(\[.*?\])?\s*\(.*?(\d+)分\)", one_item["stem"].strip())
  145. # one_item["score"] = float(score_info.group(2))
  146. one_item["stem"] = re.sub(r"(\[.*?\])?\(.*?\d+分\)", "", one_item["stem"][:20]) + one_item["stem"][20:]
  147. return one_item