ویکی‌واژه:ربات/ربات برگردان‌ها

از ویکی‌واژه

<source lang=python>

  1. !/usr/bin/python
  2. -*- coding: utf-8 -*-
  3. BY: Z (User:ZxxZxxZ on fa.wikipedia)
  4. BY: رضا (User:reza1615 on fa.wikipedia)
  5. Distributed under the terms of the CC-BY-SA 3.0.

import wikipedia import pagegenerators import re, os, codecs, catlib wikipedia.config.put_throttle = 0 wikipedia.put_throttle.setDelay() faSite = wikipedia.getSite('fa') enSite = wikipedia.getSite('en') txtTmp= faChrs = u'ءاآأإئؤبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهیًٌٍَُِّْٓيك' msg = u'ربات: اصلاح بر پایهٔ شیوه‌نامه' def fixCsmtc(faTxt):

   # ZWNJ
   txt=faTxt
   txt = re.sub(u'‌{2,}', u'‌', txtTmp) # پشت‌سرهم
   txt = re.sub(u'\[\[([^\]\|]*?)‌]](%s+)' % faChrs, ur'\1‌\2', txt) # Piping
   txt = re.sub(u'‌(?![ئاآأإژزرذدوؤةبپتثجچحخسشصضطظعغفقکگلمنهیيًٌٍَُِّْٰٓٔ]|[\u0900-\u097F]|ֹ)', , txt) # در پس
   txt = re.sub(u'(?<![ئبپتثجچحخسشصضطظعغفقکگلمنهیيًٌٍَُِّْٰٓٔ]|[\u0900-\u097F]|f|ֹ)‌', , txt) # در پیش
   return txt

def setparts(txt):

   types=[u'صفت فاعلی',u'صفت شغلی',u'صفت مرکب',u'مصدر متعدی',u'مصدر لازم',u'اسم مصدر',u'صفت نسبی',u'صفت فاعلی',u'صفت مفعولی',u'اسم خاص',u'اسم مرکب',u'اسم',u'مصدر',u'صفت',u'فعل',u'ضمیر',u'آوا',u'پیشوند',u'پسوند',u'پیوندواژه',u'قید',u'شماره']
   typelist=','
   for type in types:
       if txt.find(u'==='+type+'===\n')!=-1:
                   txt=txt.replace(u'==='+type+'===\n',u'@type@==='+type+u'===\n')
   parts=txt.split(u'@type@')
   for i in range(1,len(parts)):
       for type in types:
           if parts[i].find(u'==='+type+'===\n')!=-1:
                   typelist+=type+','
   typelists=typelist.split(',')
   for x in range(0,len(typelists)-1):
       if typelists[x]==:
           continue
       for y in range(x+1,len(typelists)):
           if typelists[y]==:
               continue
           if typelists[x]==typelists[y] and parts[x]!= and parts[y]!=:
               parts[x]=parts[x]+parts[y].replace(u'==='+typelists[x]+u'===\n',u)
               parts[x]=setparts(parts[x])
               parts[y]=u
       print parts[x]
   text=u'\n'
   for part in parts:
       if part==:
           continue
       text+=part+u'\n'
   return text

def BotRun(page,text_fa):

  1. --------------------------------------------------------------action that you want to do on pages-----------------
   print page
   langs = [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'یونانی']         
   text_fa=text_fa.replace(u'\r',u).replace(u'\n=\n',u).replace(u'\n==\n',u).replace(u'\n===\n',u).replace(u'\n====\n',u).replace(u'\n=====\n',u).replace(u'\n======\n',u)
   text_fa=text_fa.replace(u'=ترجمه ها=',u'=ترجمه‌ها=').replace(u'===ترجمه‌ها===\n',u'====برگردان‌ها====\n').replace(u'=برگردان ها=',u'=برگردان‌ها=').replace(u'===برگردان‌ها===\n',u'====برگردان‌ها====\n').replace(u'====ترجمه‌ها====\n',u'====برگردان‌ها====\n').replace(u'==ترجمه‌ها==\n',u'====برگردان‌ها====\n')
   
   for lang in langs:
               text_fa=text_fa.replace(u'=='+lang+u'==\n',u';الگو:'+lang+u':').replace(lang+u':',u'الگو:'+lang+u':')
   list=text_fa.split('\n')
   passport=True
   if text_fa.find(u'====برگردان‌ها====\n')==-1:
       for line in list:
           if passport==True:
               for lang in langs:
                   if line.find(u'الگو:'+lang+u':')!=-1:                     
                           text_fa=text_fa.replace(u';الگو:'+lang+u':',u'====برگردان‌ها====\n;الگو:'+lang+u':',1)
                           passport=False
                           break
                       
   passport=True
   list=text_fa.split('\n')
   if text_fa.find(u'==فارسی==\n')==-1:
       for line in list:
             if line.find(u'====برگردان‌ها====')!=-1:                     
                   text_fa=text_fa.replace(u'====برگردان‌ها====\n',u'==فارسی==\n====برگردان‌ها====\n',1)
                   break


   text_fa=text_fa.replace(u'}}*',u'}} ').replace(u'}}#',u'}} ').replace(u':*',u': ').replace(u':#',u': ')
                   
   return text_fa


  1. ----------------------------------------------------------end of action that you want to do on pages---------------

def run(generator):

       for page in generator: 
           if str(page.title()).find('صفحهٔ اصلی')!=-1:
               continue
           try:
               if not page.canBeEdited():
                   wikipedia.output( u'Skipping locked page %s' % page.title() )
                   continue
               text_fa = page.get()#------------------------------geting pages content
           except wikipedia.NoPage:
               wikipedia.output( u'Page %s not found' % page.title() )
               continue
           except wikipedia.IsRedirectPage:#----------------------geting pages redirects contents
                pageRedirect = page.getRedirectTarget()
                text_fa = pageRedirect.get()
                wikipedia.output( u'Page %s was Redirect but edited!' %  pageRedirect )

           except:
                continue
           if str(page).find('صفحهٔ اصلی')!=-1:
               continue
           new_text=BotRun(page,text_fa)
           savepart( page,new_text )#---------------saving changes in page with new_text content-----------------------------------


def savepart( page,new_text):

           try:
               page.put( new_text,msg % page ,watchArticle = None,minorEdit = True)
           except wikipedia.EditConflict:
               wikipedia.output( u'Skipping %s because of edit conflict' % ( page.title() ) )
           except wikipedia.SpamfilterError,url:
               wikipedia.output( u'Cannot change %s because of blacklist entry %s' % ( page.title(),url ) )

def main():

   summary_commandline,template,gen = None,None,None
   exceptions,PageTitles,namespaces = [],[],[]
   autoText,autoTitle = False,False
   genFactory = pagegenerators.GeneratorFactory()
   wikipedia.setAction( msg )
   arg=False#------if you dont want to work with arguments leave it False if you want change it to True---
   if arg==False:
       for arg in wikipedia.handleArgs():
           if arg == '-autotitle':
               autoTitle = True
           elif arg == '-autotext':
               autoText = True
           elif arg.startswith( '-page:' ):
               if len(arg) == 6:
                   PageTitles.append(wikipedia.input( u'Which page do you want to chage?' ))
               else:
                   PageTitles.append(arg[6:])
           elif arg.startswith( '-cat:' ):
               if len(arg) == 5:
                   PageTitles.append(wikipedia.input( u'Which Category do you want to chage?' ))
               else:
                   PageTitles.append('Category:'+arg[5:])
           elif arg.startswith( '-template:' ):
               if len(arg) == 10:
                   PageTitles.append(wikipedia.input( u'Which Template do you want to chage?' ))
               else:
                   PageTitles.append('Template:'+arg[10:])
           elif arg.startswith('-except:'):
               exceptions.append(arg[8:])
           elif arg.startswith( '-namespace:' ):
               namespaces.append( int( arg[11:] ) )
           elif arg.startswith( '-ns:' ):
               namespaces.append( int( arg[4:] ) )
           elif arg.startswith( '-summary:' ):
               wikipedia.setAction( arg[9:] )
               summary_commandline = True
           else:
               generator = genFactory.handleArg(arg)
               if generator:
                   gen = generator
   else:
       PageTitles = [raw_input(u'Page:> ').decode('utf-8')]

   if PageTitles:
       pages = [wikipedia.Page(faSite,PageTitle) for PageTitle in PageTitles]
       gen = iter( pages )
   if not gen:
       wikipedia.stopme()
       sys.exit()
   if namespaces != []:
       gen = pagegenerators.NamespaceFilterPageGenerator( gen,namespaces )
   preloadingGen = pagegenerators.PreloadingGenerator( gen,pageNumber = 60 )#---number of pages that you want load at same time
   run(preloadingGen)

if __name__ == "__main__":

   try:
       main()
   finally:
       wikipedia.stopme()