final_structure.py 8.8 KB

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