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
失败!