1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- #!/usr/bin/env/python
- # -*- coding:utf-8 -*-
- import re
- from pprint import pprint
- def resplit(item_res):
- """
- 对题目的切分结果,判断是否还有题目切分到一起的情况,并进行继续拆分
- :param item_res:
- :return:
- """
- new_res = []
- for k, one_res in enumerate(item_res):
- p1 = re.search(r"((?<=\n)|(?<=</su[pb]>))\s*([1-9]|1[0-9])\s*[..、、].+?([是为有]|等于)[((]\s*[))]", one_res['stem'], re.S)
- # 大题出现2个相同序号如16A、16B
- p2 = re.compile(r"\n\s*([1-9]|1[0-9])\s*[ABC]\s*[..、、].+?(求|试问|[是为等于]+多少)", re.S)
- if p1 and one_res['type'].replace("题", "") in ['选择', '单选', '多选', '不定选择']: # 主要针对2个题合在一起的情况
- con1 = one_res['stem'][:p1.start()]
- con2 = one_res['stem'][p1.start():]
- upletter_num1 = re.findall("[A-E]\s*[..、、]|[A-E]\s*<imgsrc", con1)
- upletter_num1 = set([re.findall("[A-E]", i)[0] for i in upletter_num1])
- upletter_num2 = re.findall("[A-E]\s*[..、、]|[A-E]\s*<imgsrc", con2)
- upletter_num2 = set([re.findall("[A-E]", i)[0] for i in upletter_num2])
- if len(upletter_num1)>=3 and len(upletter_num2)>=3:
- new_one = one_res.copy()
- new_one_list = re.split("^\n*\s*([1-9]|1[0-9])\s*[..、、]", con2)
- if len(new_one_list) == 3:
- new_one['item_id'] = int(new_one_list[1])
- new_one['stem'] = new_one_list[2]
- one_res['stem'] = con1
- new_res.append(one_res)
- new_res.append(new_one)
- else:
- new_res.append(one_res)
- else:
- new_res.append(one_res)
- elif re.search(p2, one_res['stem']) and one_res['type'].replace("题", "") not in ['选择', '单选', '多选', '不定选择', '填空']:
- temp = one_res['stem']
- split_p = [i.start() for i in re.finditer(p2, one_res['stem'])]
- one_res['stem'] = temp[:split_p[0]]
- new_res.append(one_res)
- for i, j in zip(split_p, split_p[1:] + [None]):
- new_one = one_res.copy()
- new_one['stem'] = temp[i:j]
- new_one['item_id'] = re.search("\n\s*([1-9]\s*[ABC]|1[0-9]\s*[ABC])", new_one['stem'][:10]).group(1)
- new_res.append(new_one)
- else:
- new_res.append(one_res)
- return new_res
|