import re from pprint import pprint from util import del_outlier pat1 = re.compile(r"_*\s*[((]\s*(\d+)\s*[))]\s*_*") pat11 = re.compile(r"_*\s*[((]?\s*(\d+)\s*[))]?\s*[.、.]?\s*_+") pat111 = re.compile(r"_+\s*[((]?\s*(\d+)\s*[))]?\s*_*") pat1111 = re.compile(r"_+\s*[((]?\s*(\d+)\s*[))]?\s*[.、.]?\s*_+") pat3 = re.compile(r"_*\s*[((]\s*\d+\s*[))]\s*_*") pat33 = re.compile(r"_*\s*[((]?\s*\d+\s*[))]?\s*[.、.]?\s*_+") pat333 = re.compile(r"_+\s*[((]?\s*\d+\s*[))]?\s*_*") pat3333 = re.compile(r"_+\s*[((]?\s*\d+\s*[))]?\s*[.、.]?\s*_+") pat2 = re.compile(r'(\s*\d+)?') pat4 = re.compile( r'(_*\s*\s*_*)|(_*\s*.*?\s*_*)') pat5 = re.compile(r"()|[((]?\s*\d+\s*[))]?\s*[.、.]") pat6 = re.compile(r"[((]?\s*(\d+)\s*[))]?\s*[.、.]") pat7 = re.compile(r"[((]?\s*\d+\s*[))]?\s*[.、.]") def pics(a, b, n=False): if n: t = "\n" else: t = "" replace = t + ''''''.format( a, a, b) return replace def replace_pics1(content): if not content: return content old_content = content if "http://zsytk2.zhixinhuixue.com/static/images" in str(content): content = pat4.sub("#####", str(content)) content = re.sub(r"_+\s*[((]?\s*(\d+)\s*[))]?\s*[.、.]?\s*_+", "#####", content) res = [] for sss in range(0, 20): new_content1 = re.sub(r"#####", pics(sss + 1, sss), content, count=1, flags=re.S) content = new_content1 res.append(new_content1) if "#####" not in new_content1: break if res: if "#####" in str(res[-1]): res_final = str(res[-1]).replace("#####", " ") return res_final else: return res[-1] else: return old_content else: if len(pat2.findall(content)): content = pat4.sub("#####", content) content = re.sub(r"_+\s*[((]?\s*(\d+)\s*[))]?\s*[.、.]?\s*_+", "#####", content) res = [] for i in range(0, 20): new_content2 = re.sub(r"#####", pics(i + 1, i), content, count=1, flags=re.S) content = new_content2 res.append(new_content2) if "#####" not in new_content2: break if res: if "#####" in str(res[-1]): res_final = str(res[-1]).replace("#####", " ") return res_final else: return res[-1] else: return old_content else: if "
" in old_content: old_content = str(old_content).split("
") elif "
" in old_content: old_content = str(old_content).split("
") elif "
" in old_content: old_content = str(old_content).split("
") else: old_content = str(old_content).split("\n") try: # start_index = [1 if len(re.findall(r'[\u4e00-\u9fa5]', con.strip())) > 5 else 0 for con in old_content[:6]].index(0) start_index = [ 1 if len(re.findall(r'[\u4e00-\u9fa5]', con.strip())) / (len(con.strip()) + 0.0001) >= 0.5 else 0 for con in old_content[:3]].index(0) except: start_index = 0 old_content = "\n".join(old_content[start_index:]) replace_con = pat3.findall(old_content) + pat33.findall(old_content) + pat333.findall( old_content) + pat3333.findall(old_content) replace_con = list(set(replace_con)) if replace_con: num_list = pat1.findall(old_content) + pat11.findall(old_content) + pat111.findall( old_content) + pat1111.findall(old_content) new_num_list = del_outlier(num_list) new_replace_con_list = [] for num in replace_con: if re.search(pat1, str(num).strip()): if re.search(pat1, str(num).strip()).group(1) in new_num_list: new_replace_con_list.append(str(num).strip()) content = content.replace(str(num).strip(), "#####") if re.search(pat11, str(num).strip()): if re.search(pat11, str(num).strip()).group(1) in new_num_list: new_replace_con_list.append(str(num).strip()) content = content.replace(str(num).strip(), "#####") if re.search(pat111, str(num).strip()): if re.search(pat111, str(num).strip()).group(1) in new_num_list: new_replace_con_list.append(str(num).strip()) content = content.replace(str(num).strip(), "#####") if re.search(pat1111, str(num).strip()): if re.search(pat1111, str(num).strip()).group(1) in new_num_list: new_replace_con_list.append(str(num).strip()) content = content.replace(str(num).strip(), "#####") res = [] for i, new_replace_con in enumerate(new_replace_con_list): new_content3 = re.sub(r"#####", pics(i + 1, i), content, count=1, flags=re.S) content = new_content3 res.append(new_content3) if res: return res[-1] else: return old_content else: return old_content def replace_pics2(content): if not content: return content old_content = content if "http://zsytk2.zhixinhuixue.com/static/images" in content: content = pat5.sub("#####", content) res = [] for ii, sss in enumerate(range(len(pat5.findall(old_content)))): new_content1 = re.sub(r"#####", pics(sss + 1, sss, n=True), content, count=1, flags=re.S) content = new_content1 res.append(new_content1) if res: return res[-1] else: return old_content else: replace_con = pat7.findall(old_content) num_list = pat6.findall(old_content) new_num_list = del_outlier(num_list) new_replace_con_list = [] for num in replace_con: if pat6.search(str(num).strip()).group(1) in new_num_list: new_replace_con_list.append(str(num).strip()) content = content.replace(str(num).strip(), "#####") res = [] for i, new_replace_con in enumerate(new_replace_con_list): new_content2 = re.sub(r"#####", pics(i + 1, i), content, count=1, flags=re.S) content = new_content2 res.append(new_content2) if res: return res[-1] else: return old_content def content_replace_result(types, contents): content = contents.get("content", None) if types in ["完形填空", "任务型阅读", '七选五', "选词填空", '语法填空', '课文填空']: # if types in ["完形填空", "任务型阅读", '七选五', "选词填空", '语法填空']: return replace_pics1(content) elif types in ["句子翻译", "完成句子", "阅读表达"]: return replace_pics2(content) else: return content if __name__ == '__main__': # a = ['阅读下面短文,\xa0从短文后各题所给的四个选项(A、B、C\xa0和\xa0D)中,\xa0选出可以填人\xa0空白处的最佳选项,并在答题卡上将该项涂黑。\nThe new science of spending comes to a surprising conclusion. How we use\xa0our money may \n21 as much or more than how much we’ve got it. Money spent\xa0on experiences, \xa0\xa0\n22 \xa0\xa0\xa0material goods, brings about more\xa0happiness.\nImagine that you wake up tomorrow morning to \xa0\n23 \xa0$1 million under\xa0your\nmattress. What would you do \n24 that cash? The new-found wealth will probably\xa0make\xa0you think about one thing \n25 all else—yourself. A growing body of research\xa0reveals\xa0that money \n26 our selfish sides, focusing us on \n27 that money can do for us, and\xa0us\xa0alone. Perhaps you are imagining buying new \xa0\xa0\n28 \xa0\xa0: a faster car, or even a\xa0larger\xa0house.\nHowever, studies show that material goods often \xa0\n29 \xa0deliver lasting\xa0happiness.\nFortunately, our ongoing research \n30 various ways to get more happiness from\xa0every dollar you spend. Changing how you spend money can lead to measurable increases\xa0in your happiness. But making these \n31 requires challenging some of our\xa0fundamental\xa0assumptions about spending, since it’s\xa0\n32 not to view buying a house as a\xa0wise\xa0investment. But new research shows it has little impact on your overall happiness.\xa0A\xa0study in the United States found that homeowners, on average, were no happier\xa0than\n\n\xa0\xa0\n33 \xa0\xa0.\nThus, diligently saving up for a down payment might not be such a good idea\xa0\n34 \xa0it means skipping after-work beers with friends or your annual celebration at a\xa0favorite\xa0restaurant. Dozens of studies show that people get more happiness from buying\xa0\n35 \xa0than material things. Experiential purchases—such as trips, concerts and special\xa0meals\n—are specially \xa0\n36 \xa0to our sense of self, making us who we\xa0are.\nExperiences come with one more \n37 : They are likely to make us \n38 to\xa0other\xa0people, while more often material things are enjoyed \n39 . The importance of\xa0social\xa0contact for \xa0\xa0\n40 \xa0\xa0mental and physical health has been stressed for\xa0decades.\n', '1.\nA.present\nB.matter\nC.appear\nD.equal\n', '2.\nA.better\xa0than\nB.other\xa0than\nC.rather\xa0than\nD.more\xa0than\n', '3.\nA.keep\nB.own\nC.discover\nD.accept\n', '4.\nA.to\nB.with\nC.about\nD.for\n', '5.\nA.above\nB.beyond\nC.except\nD.after\n', '6.\nA.draws\xa0out\nB.takes\xa0in\nC.stands\xa0for\nD.makes\xa0up\n', '7.\nA.who\nB.why\nC.what\nD.how\n', '8.\nA.life\nB.equipment\nC.vehicles\nD.possessions\n', '9.\nA.manage\xa0to\nB.fail\xa0to\nC.tend\xa0to\nD.expect\xa0to\n', '10.\nA.invites\nB.offers\nC.prefers\nD.follows\n', '11.\nA.changes\nB.plans\nC.decisions\nD.mistakes\n', '12.\nA.easy\nB.hard\nC.foolish\nD.sensible\n', '13.\nA.buyers\nB.sellers\nC.builders\nD.renters\n', '14.\nA.if\nB.until\nC.unless\nD.though\n', '15.\nA.experiences\nB.goods\nC.cars\nD.houses\n', '16.\nA.exposed\nB.added\nC.applied\nD.related\n', '17.\nA.disadvantage\nB.conclusion\nC.purpose\nD.benefit\n', '18.\nA.grateful\nB.close\nC.similar\nD.equal\n', '19.\nA.together\nB.worldwide\nC.long\nD.alone\n', '20.\nA.increasing\nB.damaging\nC.improving\nD.recovering\n'] a = ['''It is always interesting and revealing (发人深省的) to observe human behavior. In the past years, we have always held a party for Christmas gift __16___ at our house with at least ten members of our __17__ . Each year, we all buy gifts rapped (包裹) in Christmas paper. When everyone is sitting around a circle at home, all the gifts are __18__ in the middle of the floor. People choose a __19__ from a bowl that tells him or her when it is their __20__ to choose a gift. The person with the number one can choose any gift and then, each person __21__ his or her turn. Before they choose a gift, they have the___22___to take the gift from the former person. __23__ when all people have chosen a gift, the person __24__ the first number can then take gift away from any other person. What is always interesting is __25__ each person as they choose a gift. What happens next usually happens more often than not. The biggest gift __26__ gets chosen first. It is never the __27__ gift, nor is it the most expensive one. What does this tell us about most human __28__ ? Is the __29__ always the best or worth the most? Do we __30__ that something small is not worth as much or cannot live up to what we expect? Human beings, in fact, __31__ all sorts of __32__ and packagings. Some are bigger and some are smaller; some are dressed __33__ and some are dressed poorly. Behind these lie __34__ characteristics, talents, desires, hurts and pains and dreams. Terry Josephson, a motivational (励志的) speaker says, “ __35__ thinking in terms of limitations and start thinking in terms of possibilities.”'''] # pprint(a) # b = replace_pics1(",".join(a)) b = replace_pics1(a[0]) print(b)