پرش به محتوا

ویکی‌پدیا:درخواست‌های ربات/ربات مترجم ایتم‌های جعبه/ویرایش 0

از ویکی‌پدیا، دانشنامهٔ آزاد

این کد برای ترجمه آیتم های درون جعبه کاربرد دارد.موارد مورد نیاز به شرج زیر هست:

  1. اسم رده مورد نظر
  2. آینمی که متغییرش نیاز به تغییر هست item

این کد قابلیت ساپورت بیش از یک متغییر در جعبه را با کمی تغییرات دارد نمونه کار رضا ۱۶۱۵ / ب ‏۷ مارس ۲۰۱۱، ساعت ۱۴:۵۰ (UTC)[پاسخ]

#!/usr/bin/python
# -*- coding: utf-8  -*-
#
# Reza(User:reza1615), 2011
#
# Distributed under the terms of the CC-BY-SA 3.0 .
import catlib
import pagegenerators
import wikipedia,add_text2,pprint,codecs
from xgoogle.translate import Translator
self=''
def main():
    def __init__( self,generator,always,summary = None ):
        self.generator = generator
        self.always = always
        self.dry = False
        self.summary = summary
    site = wikipedia.getSite('fa')
    cat = catlib.Category(site,u'رده:الگو نقشه‌های جغرافیایی')#---category
    gen = pagegenerators.CategorizedPageGenerator(cat)
 
    for pagework in gen:
#---------------------------------------------گرفتن متن از صفحه ویکی
        try:
            text = pagework.get(force = False, get_redirect=False, throttle = True, sysop = False,change_edit_time = True) # text = page.get() <-- is the same
        except wikipedia.NoPage: # First except, prevent empty pages
            text = ''
        except wikipedia.IsRedirectPage: # second except, prevent redirect
            wikipedia.output(u'%s is a redirect!' % pagework)
            exit()# wikipedia.stopme() is in the finally, we don't need to use it twice, exit() will only close the script
        except wikipedia.Error: # third exception, take the problem and print
            wikipedia.output(u"Some Error, skipping..")
            exit()
#----------------------------------------------------------------------
        item='name'#--------مورد ترجمه
        newpagefa=boxtrans(text,item)
#--------------------------------------------------------------مقاله فارسی نویس        
        pagework=str(pagework)
        pagework=pagework.replace('[[','')
        pagework=pagework.replace(']]','')
        wikipedia.output( u"Loading %s..." % pagework )
        sitefa = wikipedia.getSite( 'fa' )
        pageinput = wikipedia.Page( sitefa,pagework )
        pageinput.put( newpagefa,comment = u'ترجمه با ربات',watchArticle = None,minorEdit = True )
def boxtrans(text,item):
    item=item+'='    
    textfa=' '   
    for lines in text.split('\n'):
        line=lines.strip()
        if line=='':
            continue
        else:
            for a in range(0,5):
                line2=line.replace('  ','')
                line2=line2.replace(' ','')
        if line2.find(item)!=-1:
           linebase= line.split('=')[0].strip()
           lineresult= line.split('=')[1].strip()
           linefa=translatorEn2Fa(lineresult)
           linefa=linebase+'='+linefa
           textfa=textfa+linefa+'\n'
           
        else:
           textfa=textfa+line+'\n'
    textfa=textfa.strip()
    return textfa
def translatorEn2Fa(text):
       #---------------------------------------------------------------------------مترجم گوگل
       for textkhord in getSplits(text):# خرد کننده متن به 700 کلمه
            translate = Translator().translate
            try:
                translated2fa=translate(textkhord, lang_to="fa")
            except:
                translated2fa=textkhord
       pprint.pprint(translated2fa)
       return translated2fa      
def getSplits(text,splitLength=700):
    ''' Translate Api has a limit on length of text(4500 characters)
         that can be translated at once, '''
    return (text[index:index+splitLength]
            for index in xrange(0,len(text),splitLength))
 #--------------------------------------------------------------------------------------------------
 
if __name__ == '__main__':
    try:
        main()
    finally:
        wikipedia.stopme()