Pywikibot의 Replace.py 활용예제를 올립니다.
더 좋은 예제들 : Wikipedia:hu:Szerkesztő:Bináris/Fixes_and_functions_HOWTO
사용방법[편집 | 원본 편집]
Pywikibot의 user-fixes.py에 해당 코드를 복사합니다. 주의할 점은 파이썬의 특성상 띄어쓰기 갯수를 정확하게 지켜야 합니다!
그 다음에 이 코드를 활용하려면 Pywikibot에 로그인한 상태에서 아래와 같은 코드를 입력하시면 됩니다.
C:\(path to Pywikibot)>python pwb.py replace -fix:(패턴명) (적용범위)
틀의 텍스트 한번에 바꾸는 패턴[편집 | 원본 편집]
예시 : {{대한민국 시군구 정보}} 틀을 {{마을 정보}} 틀로 교체할 때 유용한 스크립트. 패턴명은 template_local_infobox로 명명했습니다.
def replace_local(match): #치환할 때 사용하는 스크립트
import re
text=match.string
text=re.sub('\{\{(기초자치단체 정보|기초자치단체|대한민국 시군구 정보|대한민국 시군구)', '{{마을 정보')#틀 이름 변경
text=text.replace('|총인구', '|인구') #변수명 앞에 반드시 파이프가 들어간다. 파이프 앞에 띄어쓰기가 있으면 인식 못하는 것이 결점.
text=text.replace('|총인구_조사년도', '|인구 조사년도')
text=re.sub('\|대표이미지(\s|\t)*=(\s)*(.*?\.(JPG|jpg|PNG|png))', r'|그림1=[[파일:\3|300px]]', text) #정규표현식 활용
text=text.replace('|그림설명', '|그림1설명')
text=re.sub('\|행정동(\s|\t)*=(\s)*(.*?)(\|)?', r'|하위 행정구역1 명칭=행정동\n|하위 행정구역1=\3\4', text)
text=re.sub('\|법정동(\s|\t)*=(\s)*(.*)(\|)?', r'|하위 행정구역2 명칭=법정동\n|하위 행정구역2=\3\4', text)
text=re.sub('\|읍(\s|\t)*=(\s)*(.*)(\|)?', r'|하위 행정구역3 명칭=읍\n|하위 행정구역3=\3\4', text)
text=re.sub('\|면(\s|\t)*=(\s)*(.*)(\|)?', r'|하위 행정구역4 명칭=면\n|하위 행정구역4=\3\4', text)
text=re.sub('\|(구청장|시장|군수)(\s|\t)*=(\s)*(.*)(\|)?', r'|지도자 직함=\1\n|지도자=\4\5', text)
text=re.sub('\|국회의원(\s|\t)*=(\s)*(.*)(\|)?', r'|지도자2 직함=국회의원\n|지도자2=\3\4', text)
text=re.sub('\|(구|시|군)청소재지(\s|\t)*=(\s)*(.*)(\|)?', r'|관청명={{PAGENAME}}청\n|관청소재지=\4\5', text)
text=re.sub('\|(구|시|군)목(\s|\t)*=(\s)*(.*)(\|)?', r'|상징1 종류=\1목\n|상징1=\4\5', text)
text=re.sub('\|(구|시|군)화(\s|\t)*=(\s)*(.*)(\|)?', r'|상징2 종류=\1화\n|상징2=\4\5', text)
text=re.sub('\|(구|시|군)조(\s|\t)*=(\s)*(.*)(\|)?', r'|상징3 종류=\1조\n|상징3=\4\5', text)
return text
fixes['template_local_infobox']={ #패턴명 template_local_infobox로 지정
'regex':True,
'msg':{
'ko': '봇:시군구 정보 틀 내용을 마을 정보로 대체',
},
'replacements': [ #문서 전체를 범위로 잡지 않으면 틀 바깥 부분의 내용을 복사하는 버그가 있어서 할 수 없이 이렇게 설정했습니다.
(r'^(.|\n)*\{\{대한민국 시군구( 정보)?(\s|\n)*\|((.|\n)*?)\}\}(.|\n)*$', replace_local),
(r'^(.|\n)*\{\{기초자치단체( 정보)?(\s|\n)*\|((.|\n)*?)\}\}(.|\n)*$', replace_local),
]
}
문서 내부의 중복된 링크 제거[편집 | 원본 편집]
스크립트 소스 개선 시도중입니다. 일단 이 소스의 문제점은 틀 내부의 링크가 걸려있는 것을 무시하는 옵션이 없습니다.
패턴명은 multilink입니다.
def multilink_removal(match):
pretext = match.string[:match.start()]
if '[[' + match.group(1) + ']]' in pretext or '[[' + match.group(1) + '|' in pretext:
#This text was already linked.
if match.group(2): #Is there a piped part? Return it without the pipe.
return match.group(2)[1:]
else: #Return the text among brackets
return match.group(1)
else:
# This is the first linked occurence of this text, leave untouched.
return match.group()
fixes['multilink']= { #패턴명은 multilink로 지정
#Function from https://hu.wikipedia.org/wiki/Szerkesztő:Bináris/Fixes_and_functions_HOWTO
#This fix will remove multiple links of the same text in one article
#TODO: Don't remove the link if the text has previously been linked only
# in an infobox at the beginning of the article, and this occurence
# is outside of that infobox.
#TODO: exclude image description texts from pretext, handle nested brackets
# in image links
'regex': True,
'msg': {
'en':'Bot: unlinking multiple occurences with manual edit',
'ko':'봇:문서 내 중복으로 나타난 링크 제거',
},
'replacements': [
(r'\[\[(.*?)(\|.*?)?\]\]', multilink_removal),
],
'exceptions': {
'inside-tags': [
'hyperlink',
'interwiki',
],
'inside': [
r'\[\[([Ff]ile|파일):.*?\]\]' #Images, works partially
],
}
}