ویکیپدیا:ویرایشگر خودکار/ترجمه پارامتر
ظاهر
- نکته: برای اجرای کد پایتون حتما باید پایتون ۳ بر روی سیستمتان نصب باشد و با پایتون ۲ کار نمیکند (البته با کمی تغییر در کد میتوانید آن را با پایتون ۲ هم سازگار کنید)
کدها
[ویرایش]- ماژول
public string ProcessArticle(string ArticleText, string ArticleTitle, int wikiNamespace, out string Summary, out bool Skip)
{
string OrigText = ArticleText;
Skip = false;
Summary = "+ ترجمه پارامتر";
try
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.FileName = "python";
psi.Arguments = "qet_interwiki.py";
using (System.IO.StreamWriter writer = new System.IO.StreamWriter("input.txt"))
writer.Write(ArticleText);
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
p.WaitForExit();
if (System.IO.File.Exists("output.txt"))
{
using (System.IO.StreamReader reader = System.IO.File.OpenText("output.txt"))
ArticleText = reader.ReadToEnd();
if (ArticleText == OrigText)
Skip = true;
}
else
Skip = true;
return ArticleText;
}
catch
{
Skip = true;
return OrigText;
}
}
- qet_interwiki.py.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# BY: رضا (User:reza1615 on fa.wikipedia)
# Distributed under the terms of the CC-BY-SA 3.0.
import requests,codecs,re
URL='https://en.wikipedia.org/w/api.php?action=query&prop=langlinks&redirects=1&lllimit=500&format=json&titles='
def get_fs(link):
link=link.replace(' ','_')
result = requests.get(URL+link).json()
try:
results=result['query']['pages'][list(result['query']['pages'])[0]]['langlinks']
fa_result=[x['*'] for x in results if x['lang']=='fa'][0]
if fa_result:
return fa_result.replace('_',' ')
else:
return link
except:
return link.replace('_',' ')
text = codecs.open( u'input.txt','r' ,'utf8' )
text = text.read().replace('\r','')
if not 'require' in text:
link=re.findall(r'name* \=* \'([^\n]+)\',\n',text)[0]
text=re.sub(r"name* \=* '"+link+"',\n","name = '"+get_fs(link)+"',\n",text)
with codecs.open(u'output.txt' ,mode = 'w',encoding = 'utf8' ) as f:
f.write(text)