ویکیپدیا:درخواستهای ربات/ربات مترجم ایتمهای جعبه/ویرایش ۱
ظاهر
این کد برای ترجمه آیتم های درون جعبه کاربرد دارد.موارد مورد نیاز به شرج زیر هست:
- اسم رده مورد نظر
- آینمی که متغییرش نیاز به تغییر هست item
این کد قابلیت ساپورت بیش از یک متغییر در جعبه را با کمی تغییرات دارد نمونه کار رضا ۱۶۱۵ / ب ۲۵ آوریل ۲۰۱۱، ساعت ۰۱:۵۵ (UTC) <syntaxhighlight lang="python">
- !/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:
- ---------------------------------------------گرفتن متن از صفحه ویکی
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
- ----------------------------------------------------------------------
item = '|alias='#--------مورد ترجمه newpagefa = boxtrans( text,item ) if newpagefa == False: pprint.pprint( 'couldnt find translation' ) texterror = str( pagework ) + '\n' with codecs.open( 'notranslation.txt',mode = 'a',encoding = 'utf8' ) as f: f.write( texterror ) f.close() continue
- --------------------------------------------------------------مقاله فارسی نویس
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 ) except: texterror = str( pagework ) + '\n' with codecs.open( 'texterror.txt',mode = 'a',encoding = 'utf8' ) as f: f.write( texterror ) f.close() pprint.pprint( 'could not write ' + pagework )
def boxtrans( text,item ):
textfa = ' ' for lines in text.split( '\n' ): line = lines.strip() if line == : continue 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 ) if lineresult == linefa: return False 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()