Материал из Википедии — свободной энциклопедии
# -*- coding: UTF-8 -*-
__version__='$Id:'
import wikipedia
import re, sys
import codecs
import xmlreader
from zbib_tekst import *
def vivod(b):
wikipedia.output(b)
otch.write(b)
otch.flush()
def ubrkoment1 (text):
# Ignore tekst within HTML comments
nowikiOrHtmlCommentR = re.compile(r'<!--.*?-->', re.IGNORECASE | re.DOTALL)
match = nowikiOrHtmlCommentR.search(text)
while match:
text = text[:match.start()] + text[match.end():]
match = nowikiOrHtmlCommentR.search(text)
return text
def provrimcif(t):
simv=[u'I',u'V',u'X',u'L',u'C',u'D','M']
if len(t)>10:
return 0
for c in t:
if (not c in simv):
return 0
return 1
diakr={
u'ƒ': u'f',
u'„': u',,',
u'…': u'...',
u'Š': u'S',
u'Œ': u'OE',
u'™': u'(tm)',
u'š': u's',
u'œ': u'oe',
u'Ÿ': u'Y',
u'¢': u'c',
u'¤': u'¤',
u'¥': u'Y',
u'©': u'(c)',
u'': u'-',
u'®': u'(r)',
u'°': u'°',
u'±': u'±',
u'²': u'2',
u'³': u'3',
u'µ': u'µ',
u'¹': u'1',
u'¼': u'1/4',
u'½': u'1/2',
u'¾': u'3/4',
u'¿': u'?',
u'À': u'A',
u'Á': u'A',
u'Â': u'A',
u'Ã': u'A',
u'Ä': u'A',
u'Å': u'A',
u'Æ': u'AE',
u'Ç': u'C',
u'È': u'E',
u'É': u'E',
u'Ê': u'E',
u'Ë': u'E',
u'Ì': u'I',
u'Í': u'I',
u'Î': u'I',
u'Ï': u'I',
u'Ð': u'D',
u'Ñ': u'N',
u'Ò': u'O',
u'Ó': u'O',
u'Ô': u'O',
u'Õ': u'O',
u'Ö': u'O',
u'×': u'x',
u'Ø': u'O',
u'Ù': u'U',
u'Ú': u'U',
u'Û': u'U',
u'Ü': u'U',
u'Ý': u'Y',
u'ß': u'ss',
u'à': u'a',
u'á': u'a',
u'â': u'a',
u'ã': u'a',
u'ä': u'a',
u'å': u'a',
u'æ': u'ae',
u'ç': u'c',
u'è': u'e',
u'é': u'e',
u'ê': u'e',
u'ë': u'e',
u'ì': u'i',
u'í': u'i',
u'î': u'i',
u'ï': u'i',
u'ñ': u'n',
u'ò': u'o',
u'ó': u'o',
u'ô': u'o',
u'õ': u'o',
u'ö': u'o',
u'ø': u'o',
u'ù': u'u',
u'ú': u'u',
u'û': u'u',
u'ü': u'u',
u'ý': u'y',
u'ÿ': u'y',
u'Ā': u'A',
u'ā': u'a',
u'Ă': u'A',
u'ă': u'a',
u'Ą': u'A',
u'ą': u'a',
u'Ć': u'C',
u'ć': u'c',
# u'Ĉ': u'C',
# u'ĉ': u'c',
u'Ċ': u'C',
u'ċ': u'c',
u'Č': u'C',
u'č': u'c',
u'Ď': u'D',
u'ď': u'd',
u'Đ': u'D',
u'đ': u'd',
u'Ē': u'E',
u'ē': u'e',
u'Ĕ': u'E',
u'ĕ': u'e',
u'Ė': u'E',
u'ė': u'e',
u'Ę': u'E',
u'ę': u'e',
u'Ě': u'E',
u'ě': u'e',
# u'Ĝ': u'G',
# u'ĝ': u'g',
u'Ğ': u'G',
u'ğ': u'g',
u'Ġ': u'G',
u'ġ': u'g',
u'Ģ': u'G',
u'ģ': u'g',
# u'Ĥ': u'H',
# u'ĥ': u'h',
u'Ħ': u'H',
u'ħ': u'h',
u'Ĩ': u'I',
u'ĩ': u'i',
u'Ī': u'I',
u'ī': u'i',
u'Ĭ': u'I',
u'ĭ': u'i',
u'Į': u'I',
u'į': u'i',
u'İ': u'I',
u'IJ': u'IJ',
u'ij': u'ij',
# u'Ĵ': u'J',
# u'ĵ': u'j',
u'Ķ': u'K',
u'ķ': u'k',
u'Ĺ': u'L',
u'ĺ': u'l',
u'Ļ': u'L',
u'ļ': u'l',
u'Ľ': u'L',
u'ľ': u'l',
u'Ŀ': u'L',
u'ŀ': u'l',
u'Ł': u'L',
u'ł': u'l',
u'Ń': u'N',
u'ń': u'n',
u'Ņ': u'N',
u'ņ': u'n',
u'Ň': u'N',
u'ň': u'n',
u'ʼn': u'n',
u'Ŋ': u'NG',
u'ŋ': u'ng',
u'Ō': u'O',
u'ō': u'o',
u'Ŏ': u'O',
u'ŏ': u'o',
u'Ő': u'O',
u'ő': u'o',
u'Œ': u'OE',
u'œ': u'oe',
u'Ŕ': u'R',
u'ŕ': u'r',
u'Ŗ': u'R',
u'ŗ': u'r',
u'Ř': u'R',
u'ř': u'r',
u'Ś': u'S',
u'ś': u's',
# u'Ŝ': u'S',
# u'ŝ': u's',
u'Ş': u'S',
u'ş': u's',
u'Š': u'S',
u'š': u's',
u'Ţ': u'T',
u'ţ': u't',
u'Ť': u'T',
u'ť': u't',
u'Ŧ': u'T',
u'ŧ': u't',
u'Ũ': u'U',
u'ũ': u'u',
u'Ū': u'U',
u'ū': u'u',
# u'Ŭ': u'U',
# u'ŭ': u'u',
u'Ů': u'U',
u'ů': u'u',
u'Ű': u'U',
u'ű': u'u',
u'Ų': u'U',
u'ų': u'u',
u'Ŵ': u'W',
u'ŵ': u'w',
u'Ŷ': u'Y',
u'ŷ': u'y',
u'Ÿ': u'Y',
u'Ź': u'Z',
u'ź': u'z',
u'Ż': u'Z',
u'ż': u'z',
u'Ž': u'Z',
u'ž': u'z',
u'Ơ': u'O',
u'ơ': u'o',
u'Ư': u'U',
u'ư': u'u',
u'Ǎ': u'A',
u'ǎ': u'a',
u'Ǐ': u'I',
u'ǐ': u'i',
u'Ǒ': u'O',
u'ǒ': u'o',
u'Ǔ': u'U',
u'ǔ': u'u',
u'Ǖ': u'U',
u'ǖ': u'u',
u'Ǘ': u'U',
u'ǘ': u'u',
u'Ǚ': u'U',
u'ǚ': u'u',
u'Ǜ': u'U',
u'ǜ': u'u',
u'Ǻ': u'A',
u'ǻ': u'a',
u'Ǽ': u'AE',
u'ǽ': u'ae',
u'Ǿ': u'O',
u'ǿ': u'o',
}
tokdl=[
(u'a',1),
(u'o',1),
(u'aj',2),
(u'oj',2),
(u'an',2),
(u'on',2),
(u'ajn',3),
(u'ojn',3),
]
razd=[u',', u' (', u' bei ', u' am ', u' an ', u' in ', u' im ', u' vor ',]
# u' of ', u' de ', u' en ',]
def annot(vhsi,n,t):
t = ubrkoment1(t)
t = t.replace(u'<br>',u'\n\n').replace(u'<BR>',u'\n\n')
t = t.replace(u'\r',u' ').replace(u'\t',u' ')
while u' ' in t:
t = t.replace(u' ',u' ')
t = t.replace(u' \n',u'\n')
tks=[u'. ',u'.\n',u'\n',u'\n\n',u'\n ',u'\n:',u'\n*',u'{|',u'{{',u'\n=',u'=\n',
u'<gallery>',u'<math>',u'---\n',]
tkzs={u'{|':u'|}',u'{{':u'}}',u'<gallery>':u'</gallery>',
u'<math>':u'</math>'}
tes=[u' estas ',u' estis ',u' estos ',u' estus ',u' estu ',]
prefi=vhsi.family.image_namespace(vhsi.lang)
tksi=[u'[[Image:', u'[[image:', u'[[%s:'%prefi, u'[[%s:'%(prefi.lower()),]
tks+=tksi
n1=n
(pn,i)=iskats_mn(n,1,razd)
if pn!=-1:
n1=n[:pn]
nes=[u"'''%s'''"%n, u"'''%s'''"%n1,]
npr=0
rt=u''
p=0
while p<len(t):
(p1,i)=iskats_mn(t,p,tks)
if p1==-1:
p1=len(t)
elif not tkzs.has_key(tks[i]):
p1+=1
kt=ubr_nk_prob(t[p:p1])
if len(kt)>20 and kt.endswith(u'.'):
(pe,ie)=iskats_mn(kt,0,tes)
if pe!=-1:
(pn,ipn)=iskats_mn(kt,0,nes)
if pn!=-1:
pn2=pn+len(nes[ipn])
rt=u'*'+kt[:pn]+u"'''[[%s]]'''"%n+kt[pn2:]
break
npr+=1
if npr>=2:
break
os=tks[i]
if os in tksi:
os=u'[['
if tkzs.has_key(os):
zs=tkzs[tks[i]]
p2=iskkonpodp(t,p1+len(os),os,zs)
if p2==-1:
p=len(t)
else:
p=p2+len(zs)
else:
p=p1
return rt
tk_de=[u'German',u'Aŭstr',u'Svis',]
def sslk(vhsi,n,t):
r={}
pl=wikipedia.Page(vhsi,n)
pl._contents=t
lip=pl.linkedPages()
for pl2 in lip:
s=pl2.title()
if s!=n:
r[s]=1
plkat = pl.categories()
k=[]
for pka in plkat:
k.append(pka.title())
return (r,k)
def main(vhjaz,fivh,fivih,tblzapr,doann):
diakr_de={}
for a, b in diakr.iteritems():
diakr_de[a]=b
diakr_de[u'ä']=u'ae'
diakr_de[u'ö']=u'oe'
diakr_de[u'ü']=u'ue'
diakr_de[u'Ä']=u'Ae'
diakr_de[u'Ö']=u'Oe'
diakr_de[u'Ü']=u'Ue'
f1=codecs.open(fivih, 'w', 'utf-8')
insite=wikipedia.getSite(vhjaz,fam = u'wikipedia')
tblp = {}
tblss = {}
tbltip = {}
tblali = {}
tblann = {}
# open xml dump and read page titles out of it
dump = xmlreader.XmlDump(fivh)
redirR = insite.redirectRegex()
readPagesCount = 0
sch_ali=0
sch_aliu=0
sch_apa=0
for entry in dump.parse():
readPagesCount += 1
# if readPagesCount>=5000:
# break
# always print status message after 1000 pages
if readPagesCount % 1000 == 0:
print '%i pages read...' % readPagesCount
m = redirR.match(entry.text)
tt=entry.title
if (u':' in tt) or (u'/Aldonaĵoj' in tt):
continue
if tblzapr.has_key(tt):
vivod(u'zapr %s\n'%tt)
continue
if m and entry.text.startswith(u'#'):
target = m.group(1)
tblss[tt]={target:1}
tbltip[tt]=1
tblali[tt]=target
# pass
else:
t=entry.text
ss={}
kat=[]
(ss,kat)=sslk(insite,tt,t)
tblss[tt]=ss
di1=diakr
f_de=0
for k in kat:
for tk in tk_de:
if tk in k:
f_de=1
if f_de:
di1=diakr_de
if (u'{{apartigilo}}' in t) or (u'{{Apartigilo}}' in t):
tbltip[tt]=2
else:
tbltip[tt]=3
(p,i)=iskats_mn(tt,1,razd)
if p==-1:
ttp=tt
for cd, cp in di1.iteritems():
ttp=ttp.replace(cd,cp)
else:
ttp=ubr_nk_prob(tt[:p])
ttp3=ttp
for cd, cp in di1.iteritems():
ttp3=ttp3.replace(cd,cp)
ttp3=ttp3.upper()
if len(ttp3)==3:
fg=1
for sec in ttp3:
if ord(sec)<ord(u'A') or ord(sec)>ord(u'Z'):
fg=0
if fg and ttp3!=ttp:
vivod(u':3LK %s %s\n'%(ttp3,tt))
ttp=ttp3
tbl={}
if ttp!=tt:
tbl[ttp]=1
if doann.has_key(tt):
ann=doann[tt]
else:
ann=annot(insite,tt,t)
if ann!=u'':
tblann[tt]=ann
vivod(u'ann %s %s\n'%(tt,ann))
for se, sez in tbl.iteritems():
if tblp.has_key(se):
tblp[se].append(tt)
else:
tblp[se]=[tt]
for tt, na in tblali.iteritems():
if tblss.has_key(na):
ssna=tblss[na]
for ss, zn in ssna.iteritems():
tblss[tt][ss]=zn
dapatxt=u' (apartigilo)'
for tt, sst in tblss.iteritems():
if tt.endswith(dapatxt):
tt1=tt[:len(tt)-len(dapatxt)]
vivod(u'dapatxt %s %d\n'%(tt,len(sst)))
if tblss.has_key(tt1):
vivod(u'+ -> %s %d\n'%(tt1,len(tblss[tt1])))
for ss, zn in sst.iteritems():
tblss[tt1][ss]=zn
else:
vivod(u'- -> %s\n'%tt1)
tbls=[]
for se, tim in tblp.iteritems():
# if len(tim)<=2:
# tim1=tim
# else:
# tim1=[]
# for tt in tim:
# if not u'(' in tt:
# tim1.append(tt)
# if len(tim1)>0 and len(tim1)<=10:
tim1=tim
tbls.append((len(tim1),se,tim1))
tbls.sort(reverse=True)
# tbls.sort()
sch_d=0
sch_u=0
sch_vhsta=0
sch_nizmapa=0
sch_nalivapa=0
sch_nsozdapa=0
sch_nsozdali=0
for ltim, se, tim in tbls:
tip=0
if tbltip.has_key(se):
tip=tbltip[se]
# vivod(u'%s %d %d\n'%(se,tip,ltim))
n=0
for tt in tim:
if tblss.has_key(se) and tblss[se].has_key(tt):
vivod(u'%du %s %s\n'%(tip,se,tt))
sch_u+=1
continue
else:
vivod(u'%d+ %s %s\n'%(tip,se,tt))
sch_d+=1
n+=1
q=[u'',u'',u'',u'',u'',u'']
q[0]=se
q[1]=se
q[2]=tt
if tblann.has_key(tt):
q[3]=tblann[tt]
else:
q[3]=(u"*'''[[%s]]'''")%tt
q[4]=tt
q[5]=u'l'
qt=spisvstr(q,u'@')+u'\n'
# if ltim<=5:
if 1:
f1.write(qt)
f1.flush()
# else:
# vivod(qt)
if n>0:
if tip==3:
sch_vhsta+=1
elif tip==2:
sch_nizmapa+=1
elif tip==1:
sch_nalivapa+=1
else:
if n>=2:
sch_nsozdapa+=1
else:
sch_nsozdali+=1
vivod((u'\n\n\n'+
u'd=%d '+
u'u=%d\n'+
u'vhsta=%d '+
u'nizmapa=%d '+
u'nalivapa=%d '+
u'nsozdapa=%d '+
u'nsozdali=%d\n')%(
sch_d,
sch_u,
sch_vhsta,
sch_nizmapa,
sch_nalivapa,
sch_nsozdapa,
sch_nsozdali))
vhjaz = u'eo'
#vhjaz = sys.argv[1]
fvh = sys.argv[1]
fvih = sys.argv[2]
fzapr = sys.argv[3]
fann = sys.argv[4]
#fotch = u'mm_otch.txt'
fi=fvih
if fi.endswith(u'.txt'):
fi=fi[:len(fi)-4]
fotch = fi+'_ot.txt'
#mysite = wikipedia.getSite()
otch = codecs.open(fotch, 'w', 'utf-8')
mysite = wikipedia.getSite()
tblzapr={}
if fzapr!=u'.':
tblzapr=chit_slov_u(fzapr)
ann={}
if fann!=u'.':
tblann=chit_spis_u(fann)
for sa in tblann:
vivod(u'sa="%s"\n'%sa)
sas=sa.split(u'@')
if len(sas)<2:
continue
tpl=[]
for sa1 in sas[1:]:
tpl+=chit_spis_u(ubr_nk_prob(sa1))
ta=chit_spisn_u(ubr_nk_prob(sas[0]),u'@',2)
vivod(u'len(ta)=%d len(tpl)=%d\n'%(len(ta),len(tpl)))
slrazr={}
for pl in tpl:
if pl.startswith(u'+ '):
pli=ubr_nk_prob(pl[2:])
slrazr[pli]=1
vivod(u'len(slrazr)=%d\n'%len(slrazr))
for a in ta:
if slrazr.has_key(a[0]):
ann[perv_upper(a[0])]=a[1]
vivod(u'len(ann)=%d\n'%len(ann))
main(vhjaz,fvh,fvih,tblzapr,ann)