1
$\begingroup$

I want to translate a group of words into simplified Chinese more quickly:

    wordlist1 = 
      ToString /@ {abate, aberrant, abound, abrogate, abstain, abstruse, 
        accede, accessory, accommodate, accredit, acquaint, acquisitive, 
        adduce, adept, admonish, adore, adorn, affiliate, affluent, 
        agitate, agony, agreeable, album, allegiance, allegorical};
    WordTranslation[#, "Chinese"] & /@ wordlist1

But the result of the above code is traditional Chinese. If I specify "SimplifiedChinese" as the parameter, I still can't get the desired result. How can I get the result of simplified Chinese?

    {\[LongDash],\[LongDash],\[LongDash],\[LongDash],{戒},\[LongDash],\[LongDash],{附件},{提供住宿},\[LongDash],{使認識},\[LongDash],\[LongDash],\[LongDash],\[LongDash],{崇拜},{裝飾},\[LongDash],{豐富的},{煽動},{極度痛苦},{同意的},{簿},{忠誠},\[LongDash]}

BaiduTranslate[str_, from_ : "en", to_ : "zh"] := 
 Block[{q, appid, key, salt, sign, data},
    q = str;
  
  appid = "20190314000277077";(*你的appid,如何申请请自行百度*)
  key = "sjRd6QGeuLAR1jTr0d3G";
  
    salt = ToString@RandomInteger[2^32];
    sign = Hash[StringJoin[appid, q, salt, key], "MD5", "HexString"];
  data = Import[
    StringJoin[
     "http://api.fanyi.baidu.com/api/trans/vip/translate?q=", 
     URLEncode@q, "&from=", from, "&to=", to, "&appid=", appid, 
     "&salt=", salt, "&sign=", sign], "RawJSON"];
    data["trans_result"][[1]]["dst"]]
BaiduTranslate /@ wordlist1

If I use the above code, I will get a lot of word results as "KeyAbsent"("dst"), how can I modify it to better translate this group of words?

$\endgroup$

1 Answer 1

3
$\begingroup$

Mathematica and the Wolfram Language have built-in multilingual dictionaries to translate between languages, with functions including WordTranslation and TextTranslation.

WordTranslation and TextTranslation both accept a list of words for translation. Using lists works more quickly than translating one word at a time with WordTranslation[#, "Chinese"] & /@ wordlist1.

WordTranslation understands a set of standard names of languages or their Wolfram Language entities. It does not support simplified Chinese.

TextTranslation requires internet connectivity and uses external services. It accepts "ChineseSimplified" and "ChineseTraditional" variations for translating to Chinese.

Here are examples of translating a list of English words to Chinese. The examples use TextTranslation for simplified and traditional Chinese, and WordTranslation for comparison.

wordlist = {"abate", "aberrant", "abound", "abrogate", "abstain", "abstruse", "accede",
  "accessory", "accommodate", "accredit", "acquaint", "acquisitive", "adduce", "adept",
  "admonish", "adore", "adorn", "affiliate", "affluent", "agitate", "agony", "agreeable", 
  "album", "allegiance", "allegorical"};

TableForm[Transpose[{wordlist,
  TextTranslation[wordlist, "ChineseSimplified"],
  TextTranslation[wordlist, "ChineseTraditional"],
  WordTranslation[wordlist, "Chinese"]}],
TableHeadings -> {None,
  {"English", "Chinese simplified", "Chinese traditional", "WordTranslation"}}]
English Chinese simplified Chinese traditional WordTranslation
abate 方丈 方丈 Missing[NotAvailable]
aberrant 异常 異常 Missing[NotAvailable]
abound 比比皆是 比比皆是 Missing[NotAvailable]
abrogate 废除 廢除 Missing[NotAvailable]
abstain 投弃权票 投棄權票
abstruse 深奥 深奧 Missing[NotAvailable]
accede 访问 訪問 Missing[NotAvailable]
accessory 配件 配件 附件
accommodate 容纳 容納 提供住宿
accredit 授权 授權 Missing[NotAvailable]
[...] [...] [...] [...]
allegorical 寓言 寓言 Missing[NotAvailable]
$\endgroup$
3
  • $\begingroup$ The function TextTranslation call Google translation, which requires payment, I would like to know whether Baidu translation or other ways can replace the default Google translation options. $\endgroup$ Commented Feb 1, 2021 at 6:06
  • 1
    $\begingroup$ @Alittlemouseonthepampas – I don't know enough Chinese to check Baidu's policy or documentation for the API you're using, so I can't help with your BaiduTranslate function (I used a bunch of my Wolfram service credits while testing my answer). Are the KeyAbsent results caused by words that Baidu can't translate? Google translate includes a free $10 usage credit per month. Does Baidu have a similar policy or usage limits? I've asked about Wolfram's service credit policy; free service credits were included with my license. $\endgroup$
    – creidhne
    Commented Feb 1, 2021 at 21:12
  • $\begingroup$ Youdao translation provides some free Chinese-English API translation services:YoudaoTranslate[str_, from_ : "en", to_ : "zh"] := Block[{q, appid, key, salt, sign, data}, q = str; appid = "neverland";(*你的appid,如何申请请自行百度*) key = "969918857";(*你的key*) data = Import[ StringJoin["http://fanyi.youdao.com/openapi.do?keyfrom=", appid, "&key=", key, "&type=data&doctype=json&version=1.1&q=", URLEncode@q], "RawJSON"]; data["translation"][[1]]]; YoudaoTranslate["an apple a day"] $\endgroup$ Commented Feb 4, 2021 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.