# qiuwenbot, a bot to contribute to qiuwen.wiki# Copyright (C) 2022 Jinzhe Zeng## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see <https://www.gnu.org/licenses/>.#importrefromqiuwenbot.utilsimportdevide_parameters,get_cat_regex,get_template_regexfrom.commonimportget_commentfrom.filterimportFilter,register_filter
[docs]@register_filterclassReplaceROCyear(Filter):"""Filter to replace ROC flag from a string. Parameters ---------- pattern : str Pattern to replace. repl : str Replacement. """def__init__(self):self.re_bd=get_template_regex(r"[Bb]d")self.re_year=re.compile(r"(?P<year>\d+)年")self.re_found=get_cat_regex(r"(?P<year>\d+)年(.*)(设立|建立|成立|创建|創建|設立|启用|啟用)(.*)")self.re_event=get_cat_regex(r"(?P<year>\d+)年(台灣|台湾|台灣)(.*)")self.re_roc=re.compile(r"({{[\s]*ROC[\s]*}})")self.re_roc_flagicon=re.compile(r"({{[\s]*[Ff]lagicon[\s]*\|[\s]*ROC[\s]*}})")self.re_nationality=re.compile(r"\|[\s]*(國籍|国籍|[Nn]ationality)[\s]*=[\s]*({{[\s]*ROC[\s]*}})")self.re_death_place=re.compile(r"\|[\s]*(逝世地點|逝世地点|[Dd]eath_place|[Pp]lace_of_death|[Rr]esting_place)[\s]*=[\s]*({{[\s]*ROC[\s]*}})")self.re_death_place_flagicon=re.compile(r"\|[\s]*(逝世地點|逝世地点|[Dd]eath_place|[Pp]lace_of_death|[Rr]esting_place)[\s]*=[\s]*({{[\s]*flagicon[\s]*\|[\s]*ROC[\s]*}})")self.comment=get_comment("replaced_flag 0")self.replaced_chn=r"{{CHN}}"+self.commentself.replaced_chn_flagicon=r"{{flagicon|CHN}}"+self.commentself.replaced_chn_nationality=r"|\1={{PRC-TWN}}"+self.commentself.replaced_chn_no_flag=r"|\1=[[中国]]"+self.commentself.replaced_death_place=r"|\1={{CHN}}"+self.commentself.replaced_death_place_flagicon=r"|\1={{flagicon|CHN}}"+self.comment
[docs]deffilter(self,text:str)->str:"""Filter text. Parameters ---------- text : str Text to filter. Returns ------- str Filtered text. """yy=[]replace_all=Falsereplace_nationality=Falsereplace_death=Falsereplace_nationality_no_flag=Falsem_bd=self.re_bd.search(text)im_found=self.re_found.finditer(text)im_event=self.re_event.finditer(text)birth=Nonedeath=Noneifm_bdisnotNone:params=m_bd.group("params")params_dict=devide_parameters(params)birth=params_dict.get("1")death=params_dict.get("3","living")ifbirthisnotNone:m_birth=self.re_year.search(birth)ifm_birthisnotNone:y=m_birth.group("year")ifint(y)>1949:# born after 1949replace_all=Truereplace_nationality=TrueifdeathisnotNone:m_death=self.re_year.search(death)ifm_deathisnotNone:y=m_death.group("year")ifint(y)>1949:# death after 1949replace_death=Truereplace_nationality_no_flag=Trueelifdeath=="living":# living personreplace_nationality_no_flag=Trueform_foundinim_found:y=m_found.group("year")yy.append(int(y))form_eventinim_event:y=m_event.group("year")yy.append(int(y))iflen(yy)andmin(yy)>1949:replace_all=Trueifreplace_nationality:# nationalitytext=self.re_nationality.sub(self.replaced_chn_nationality,text)ifreplace_nationality_no_flag:# nationalitytext=self.re_nationality.sub(self.replaced_chn_no_flag,text)ifreplace_death:text=self.re_death_place.sub(self.replaced_death_place,text)text=self.re_death_place_flagicon.sub(self.replaced_death_place_flagicon,text)ifreplace_all:text=self.re_roc.sub(self.replaced_chn,text)text=self.re_roc_flagicon.sub(self.replaced_chn_flagicon,text)returntext