Bing Translator API返回不正确的希腊语单词

时间:2017-11-16 17:47:28

标签: javascript node.js microsoft-translator

我正在使用axios对Microsoft Text Translator API执行GET请求。呼叫成功,并正确地将希腊语翻译成英语。但是,当我尝试进行英语到希腊语的翻译时,答案是错误的。例如,这是单词“προγραμματισμός”的翻译出来的bp.hear(/μετάφρασε (.+)/i, (event, next) => { const microsoft_translator_text = event.captured[0]; const requst = axios.post("https://api.cognitive.microsoft.com/sts/v1.0/issueToken", null, { headers: {'Ocp-Apim-Subscription-Key': bing_translator_api} }) .then(function (response) { var microsoft_translator_auth = response.data; console.log(microsoft_translator_text); axios.get(`http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=Bearer%20${microsoft_translator_auth}&from=el&to=en&text=${microsoft_translator_text}`) .then(function (translation) { var translation_morph = translation.data.replace(/['"]+/g, '') event.reply('#translation', { translation: translation_morph, }) }) .catch(function (error) { console.log(error); }); }) .catch(function (error) { console.log(error); }); }) 而不是import csv f = open('C:\\User\\Desktop\\Replace_Test\\Testreplace.csv') csv_f = csv.reader(f) g = open('C:\\Users\\Desktop\\Replace_Test\\Testreplace.csv') csv_g = csv.writer(g) findlist = ['The String, that should replaced'] replacelist = ['The string that should replace the old striong'] #the function ?: def findReplace(find,replace): s = f.read() for item, replacement in zip(findlist,replacelist): s = s.replace(item,replacement) g.write(s) for row in csv_f: print(row) f.close() g.close() 。有人用希腊语或其他语言遇到过这个问题吗?

index.js

{{1}}

1 个答案:

答案 0 :(得分:0)

正如Fai所说,这是一个编码问题。在发送之前使用网址上的encodeURI解决了这个问题,如下所示:

var encoded_url = encodeURI("http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=Bearer " + `${microsoft_translator_auth}&from=el&to=en&text=${microsoft_translator_text}`)
axios.get(encoded_url, null, {
  headers: {'Content-type': 'application/json'},
})