import http.client, urllib.request, urllib.parse, urllib.error
def translate(IN, OUT, text):
text = urllib.parse.quote(text)
conn = http.client.HTTPConnection("translate.google.com.tr")
conn.request("GET", "/translate_a/t?client=t&text="+text+"&hl="+IN+"&tl="+OUT)
res = conn.getresponse().read().decode("cp1254",'replace')
print(res)
b1 = res.split("],[")
b2 = b1[0].strip('[]')
b3 = b2.strip('","')
b4 = b3.split('","')
return b4[0]
string = input("Turkish >>> English: ")
result = translate("tr","en",string)
print(string,">>>",result)
トルコ語を英語に翻訳できるスクリプトを書こうとしています。トルコ語の文字を入力しないと、そのスクリプトはうまく機能します。たとえば、これらのトルコ語の単語は正常に翻訳されました = (kalemlik,deneme,bilgisayar,okyanus) ですが、入力した単語に ASCII 以外の文字が含まれている場合、翻訳は失敗します。これらはトルコ語の文字 = ("ıİğĞüÜşŞöÖçÇ") であり、これらは非 ASCII 文字 = (programcı、şarkı、çalışma、örnek、İnsan、dağ、üs) を含むいくつかのトルコ語の単語です。ちなみに、cp1254 はトルコ文字の有効なエンコーディングです。この問題を解決するにはどうすればよいですか? ご存知のように、それはトルコ人だけのものではありません。
例;
Turkish >>> English: okyanus
[[["ocean","okyanus","",""]],[["isim",["ocean","brine","the deep","main","drink"],[["ocean",["okyanus","derya"]],["brine",["tuzlu su","salamura","deniz","okyanus"]],["the deep",["deniz","okyanus","enginler"]],["main",["ana boru","deniz","kuvvet","zor","okyanus","horoz dövüşü"]],["drink",["içmek","içki","içecek","içki içmek","deniz","okyanus"]]]],["sıfat",["oceanic"],[["oceanic",["okyanus","okyanusta bulunan","okyanus gibi"]]]]],"tr",,[["ocean",[5],1,0,999,0,1,0]],[["okyanus",4,,,""],["okyanus",5,[["ocean",999,1,0],["oceanic",0,1,0],["the ocean",0,1,0],["oceans",0,1,0]],[[0,7]],"okyanus"]],,,[["tr"]],2]
okyanus >>> ocean
それは成功しました。
Turkish >>> English: dağ
[[["daÄ\u0178","daÄ\u0178","",""]],,"tr",,[["daÄ\u0178",[5],1,0,1000,0,1,0]],[["daÄ\u0178",5,[["daÄ\u0178",1000,1,0]],[[0,4]],"daÄ\u0178"]],,,[["tr"]],8]
dağ >>> daÄ\u0178
失敗!