「利用者:Hatukanezumi/仮リンクの整理/aggregateTentativeLinks.py」の版間の差分

削除された内容 追加された内容
Hatukanezumi (会話 | 投稿記録)
m rel.
Hatukanezumi (会話 | 投稿記録)
bug fixes etc.
52行目:
aggregateTentativeLinks.pyは、ウィキペディアの記事と同じライセンスにしたがって配布、利用、変更、再配布、二次著作物の作成等を行えます。
 
オリジナルの版は、このページの[{{fullurl:-|oldid=35351333}} この版]です。
 
== コード ==
82行目:
import os
import sys
import re
from wikipedia import Site, Page, handleArgs, inputChoice, output, stopme
#from catlib import Category
119 ⟶ 120行目:
WP:JAREQから、報告ずみの項目名を取得
"""
listedPages = set(){}
for ns, pfx in LISTPAGES:
for page in SITE.prefixindex(unicode(pfx, 'utf-8'), ns, False):
126 ⟶ 127行目:
if tname.lower() <> 'jareq':
continue
 
alt = [x[4:] for x in args if x.startswith('alt=')]
args = [x for x in args if x.find('=') < 0]
 
interwikireqPage = set()None
try:
listedPages.update([reqPage = Page(SITE, args[1]).title()])
except:
passcontinue
p = listedPages.get(reqPage.title(), set())
p.update([reqPage.title()])
listedPages[reqPage.title()] = p
try for al in alt:
for a in re.split(r'(?<=\]\])/|/(?=\[\[)', al):
if re.match(r'\[\[.+\]\]', a) and \
not re.match(r'.+\]\].+', a):
try:
a = Page(SITE, a.replace('[[','').replace(']]',''))
except:
continue
p = listedPages.get(a.title(), set())
p.update([reqPage.title()])
listedPages[a.title()] = p
output('listed: %d' % len(listedPages))
return listedPages
217 ⟶ 237行目:
g = 'unknown'
synonyms = []
if listedPages.has_key(page.title() in listedPages):
# WP:JAREQに報告ずみのもの。別名があればそれも追加
g = 'listed'
synonyms = [Page(SITE, x) for x in listedPages[page.title()]
if x <> page.title()]
elif page.exists():
# ページが存在する場合。リダイレクト、曖昧さ回避、白紙は分ける
if page.isRedirectPage():
g = 'redirect'
229 ⟶ 253行目:
g = 'exists'
else:
# 参考リンクのページから言語間リンクを抽出
interwiki = set()
for hint in proposedArticles.hint.get(proposed, set()):
try:
for hint in proposedArticles.hint[proposed]try:
hintPage = Page(SITE, hint)
if hintPage.isRedirectPage():
hintPage = hintPage.getRedirectTarget()
interwiki.update([x.aslink().replace('[[','').replace(']]','') for x in hintPage.interwiki()])
interwiki.update([hintPage.aslink().replace('[[','').replace(']]','')])
except:
output('Failed to get interwiki: %r' % proposedhint)
# 言語間リンクにホームウィキの項目があればシノニムとして抽出
interwiki = set()
synonyms = [Page(SITE, p) passfor p in interwiki
synonyms = [p for p in interwiki if Page(SITE, p).site().language() == LANG]
# シノニムがないものは言語間リンク数で分類
if len(synonyms):
g = 'synonym'
266 ⟶ 292行目:
refs.encode('utf-8'),
page.aslink().replace('[[', '[[special:whatLinksHere/').replace(']]', '|...]]').encode('utf-8'))
if g == 'synonym' or len(synonyms):
f += (syns.encode('utf-8'),)
out.append('* %s<small>(%s)</small> ←%s%s<br/>&#x2248;%s' % f)
288 ⟶ 314行目:
fp.close()
 
def put(pagename, commentcommentText, data, always):
count = 0
comment = commentText
text = "__TOC__\n"
for filename, title in data:
path = os.path.join(OUTPUTDIR, filename)
if os.path.exists(path):
textlines += "==[l %sfor ==\n%s\n\n"l %in (title, ''.join(file(path)))]
if commentText is None:
if not comment:
comment = ''
else:
comment += '; '
comment += unicode('%s%d件' % (title, len(lines)), 'utf-8')
count += len(lines)
text += "== %s ==\n%s\n\n" % (title, ''.join(lines))
comment = unicode('%d件: %s', 'utf-8') % (count, comment)
if 200 < len(comment) or 250 <= len(comment.encode('utf-8')):
comment = unicode(comment[:197].encode('utf-8')[:246], 'utf-8', 'ignore') + u'...'
page = Page(SITE, unicode(pagename, 'utf-8'))
 
if always:
choice = 'y'
else:
output(comment)
choice = inputChoice(
'Do you update %s' % page.aslink(),
313 ⟶ 352行目:
toDo = {}
maxCount = 0
commentText = 'updated by aggregateTentativeLinks.py'None
always = False
for arg in handleArgs(*argv):