پرش به محتوا

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

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

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

  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,codecs,string
#from xgoogle.translate import Translator
self = ''
wikipedia.config.put_throttle = 0
wikipedia.put_throttle.setDelay()
#------------------------------interwiki translation---------
def englishdictionry( enlink ):
    site = wikipedia.getSite( 'en' )#(3)------------change to source wiki name------
    enlink2 = enlink 
    apage = wikipedia.Page( site,enlink2 )
    NOPAGE = False
    try:
        atext = apage.get()
    except wikipedia.IsRedirectPage:
        apage = apage.getRedirectTarget()
        try:
                atext = apage.get()
        except:
                return enlink
    except:
        return enlink
    if not NOPAGE == True:
       if atext.find( '[[fa:' ) != -1: #(4)------------change to finall wiki name------
            falink = atext.split( '[[fa:' )[1]
            falink = falink.split( ']]' )[0]
            return falink
       else:
            return enlink
    else:
        return enlink
 
#--------------------------google translation
 
def main():
    def __init__( self,generator,always,summary = None ):
        self.generator = generator
        self.always = always
        self.dry = False
        self.summary = summary
    rison = ' '
    site = wikipedia.getSite( 'fa' )
    cat = catlib.Category( site,u'رده:ISO 639 name from code templates' )#---category
    gen = pagegenerators.CategorizedPageGenerator( cat )
 
    for pagework in gen:
        wikipedia.output(pagework.title())
#---------------------------------------------گرفتن متن از صفحه ویکی
        try:
            pageebham = wikipedia.Page( site,pagework.title() )
            text = pageebham.get()
        except wikipedia.IsRedirectPage:
            pageebham = pageebham.getRedirectTarget()
            try:
                    text = pageebham.get()
            except:
                    continue
        except:
            continue
#----------------------------------------------------------------------
        item = '<'#--------مورد ترجمه
        newpagefa,rison = boxtrans( text,item )
        if newpagefa == False:
            wikipedia.output( u'couldnt translat==>' + rison )
            continue
#--------------------------------------------------------------مقاله فارسی نویس
        pagework=pagework.title()
        wikipedia.output( u"Loading %s..." % pagework )
        
        try:
            pageinput = wikipedia.Page( site,pagework )
            comment = u'ربات: ترجمه نام ([[وپ:درخواست‌های ربات/ربات مترجم ایتم‌های جعبه/ویرایش ۲|کد]])'
            #comment = u' '
            pageinput.put( newpagefa,comment,watchArticle = None,minorEdit = True )
            texterror =  pagework  + '\n'
        except:
            texterror =  pagework  + '\n'
            rison = 'The page was locked'
            wikipedia.output( pagework + '==>' + rison )
def boxtrans( text,item ):
                textfa = ' '   
                onetime = True
                if text.find( item ) != -1:
                   lineresult = text.split( item )[0].strip()
                   linefa = englishdictionry( lineresult )
                   if lineresult == linefa:
                       rison = 'Result was the same'
                       return False,rison
                   else:
                       alf = 'aeouirtypsdfghjklzxcvbnmqw'
                       for alfa in alf:#---------------If english charcter was in result it will regect
                            if linefa.find( alfa ) != -1:
                                rison = 'English Alfabet found'
                                return False,rison
 
                   textfa = text.replace(lineresult,linefa)

                else:
                     textfa = text
                rison = 'done'
                return textfa,rison
def translatorEn2Fa( text ):
       #---------------------------------------------------------------------------مترجم گوگل
       for textkhord in getSplits( text ):# خرد کننده متن به 700 کلمه
            translate = Translator().translate
            try:
                translated2fa = translate( textkhord,lang_to = "fa" )
            except:
                translated2fa = textkhord
       wikipedia.output( 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__':
        main()

کد درون جعبه[ویرایش]

این کد برای استخراج آیتم‌های درون جعبه و ترجه آنها کاربرد دارد <syntaxhighlight lang="python">

  1. !/usr/bin/python
  2. -*- coding: utf-8 -*-
  3. Reza(User:reza1615), 2011
  4. Distributed under the terms of the CC-BY-SA 3.0 .

import catlib import pagegenerators import wikipedia,pprint,codecs,string from xgoogle.translate import Translator self =

  1. ------------------------------interwiki translation---------

def englishdictionry( enlink ):

   site = wikipedia.getSite( 'en' )#(3)------------change to source wiki name------
   enlink2 = enlink 
   apage = wikipedia.Page( site,enlink2 )
   NOPAGE = False
   try:
       atext = apage.get()
   except wikipedia.IsRedirectPage:
       apage = apage.getRedirectTarget()
       try:
               atext = apage.get()
       except:
               return enlink
   except:
       return enlink
   if not NOPAGE == True:
      if atext.find( '[[fa:' ) != -1: #(4)------------change to finall wiki name------
           falink = atext.split( '[[fa:' )[1]
           falink = falink.split( ']]' )[0]
           return falink
      else:
           return enlink
   else:
       return enlink
  1. --------------------------google translation

def main():

   def __init__( self,generator,always,summary = None ):
       self.generator = generator
       self.always = always
       self.dry = False
       self.summary = summary
   rison = ' '
   site = wikipedia.getSite( 'fa' )
   cat = catlib.Category( site,u'رده:الگوهای اطلاعاتی کشورها' )#---category
   gen = pagegenerators.CategorizedPageGenerator( cat )

   for pagework in gen:
  1. ---------------------------------------------گرفتن متن از صفحه ویکی
       pagework = str( pagework ).replace( '','' ).replace( '', ).strip()
       try:
           pageebham = wikipedia.Page( site,pagework )
           text = pageebham.get()
       except wikipedia.IsRedirectPage:
           pageebham = pageebham.getRedirectTarget()
           try:
                   text = pageebham.get()
           except:
                   continue
       except:
           continue
  1. ----------------------------------------------------------------------
       item = '|alias='#--------مورد ترجمه
       newpagefa,rison = boxtrans( text,item )
       if newpagefa == False:
           pprint.pprint( 'couldnt translat==>' + rison )
           texterror = str( pagework ) + '\n'
           with codecs.open( 'notranslation.txt',mode = 'a',encoding = 'utf8' ) as f:
                   f.write( texterror + '==>' + rison )
           f.close()
           rison = 
           continue
  1. --------------------------------------------------------------مقاله فارسی نویس
       pagework = str( pagework )
       pagework = pagework.replace( '[[', )
       pagework = pagework.replace( ']]', )
       wikipedia.output( u"Loading %s..." % pagework )
       try:
           sitefa = wikipedia.getSite( 'fa' )
           pageinput = wikipedia.Page( sitefa,pagework )
           comment = u'ربات: ترجمه نام (کد)'
           #comment = u' '
           pageinput.put( newpagefa,comment,watchArticle = None,minorEdit = True )
           texterror = str( pagework ) + '\n'
           with codecs.open( 'Done.txt',mode = 'a',encoding = 'utf8' ) as f:
                   f.write( '' + texterror + '' )
           f.close()
       except:
           texterror = str( pagework ) + '\n'
           rison = 'The page was locked'
           with codecs.open( 'notranslation.txt',mode = 'a',encoding = 'utf8' ) as f:
                   f.write( '===' + texterror + '==>' + rison )
           f.close()
           pprint.pprint( pagework + '==>' + rison )

def boxtrans( text,item ):

   textfa = ' '   
   countera = 0
   onetime = True
   for lines in text.split( '\n' ):
       line = lines.strip()
       if line == :
           continue
       if onetime == True:
               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 = englishdictionry( lineresult )
                  if lineresult == linefa:# function calling ========================
                      linefa = translatorEn2Fa( lineresult )# function calling ========================
                  if lineresult == linefa:
                      rison = 'Result was the same'
                      return False,rison
                  else:
                      alf = 'aeouirtypsdfghjklzxcvbnmqw'
                      for alfa in alf:#---------------If english charcter was in result it will regect
                           if linefa.find( alfa ) != -1:
                               rison = 'English Alfabet found'
                               return False,rison
                  
                  linefa = linebase + '=' + linefa
                  textfa = textfa + linefa + '\n'
                  onetime = False
               else:
                    textfa = textfa + line + '\n'
       else:
           textfa = textfa + line + '\n'
                              
   textfa = textfa.strip()
   rison = 'done'
   return textfa,rison

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()

ر