Last active
July 26, 2023 11:50
-
-
Save michaelHL/31b14377ebffbae5bf3e2d108a37d215 to your computer and use it in GitHub Desktop.
有道翻译api(via R), youdao dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## install.packages(c("RCurl", "rjson", "digest")) | |
library(RCurl) | |
library(rjson) | |
library(digest) | |
## 老版, 已无法get到新的key | |
user = "blog125" | |
key = "21376174" | |
trans = function(word) { | |
word = URLencode(word) | |
web = getURL( | |
paste( | |
"http://fanyi.youdao.com/openapi.do?keyfrom=", user, | |
"&key=", key, | |
"&type=data&doctype=json&version=1.1&q=", word, | |
sep = "" | |
) | |
) | |
return(fromJSON(web)$translation) | |
} | |
trans("广州") | |
trans("西伯利亚") | |
trans("松江") | |
trans("fuck") | |
## 新版 | |
## api 见 http://ai.youdao.com/docs/api.s#java-demo | |
## 注: 下面代码在英文 Windows 系统、Linux (LC_CTYPE = en_US.UTF-8) 下正常输出, 但中文 Windows 系统下对中文的翻译会返回 NULL, | |
## 两种解决方案: | |
## - 改 R 的 locale 为英文环境: Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252") | |
## - 强制将 word 转为 UTF-8 编码: word = iconv(word, from="GBK", to="UTF-8") | |
appid = "1871d0dec09e11c9" | |
appkey = "g4mgIt79XRnJy3cprq24MeCbim85VqO4" | |
transnew = function(word) { | |
salt = 2 | |
txt = paste(appid, word, salt, appkey, sep = "") | |
hash = digest(txt, algo="md5", serialize=F) | |
web = getURL( | |
paste( | |
"http://openapi.youdao.com/api?q=", word, | |
"&from=auto&to=auto&appKey=", appid, | |
"&salt=", salt, | |
"&sign=", hash, | |
sep = "" | |
) | |
) | |
if (web != "") return(fromJSON(web)$translation) | |
return(NULL) | |
} | |
transnew("广州") | |
transnew("西伯利亚") | |
transnew("松江") | |
transnew("great") | |
## Unit Test | |
first <- c("Fear", "Frontier", "Nanny", "Job", "Yard", "Airport", "Half Pint", | |
"Commando", "Fast Food", "Basketball", "Bachelorette", "Diva", | |
"Baggage", "College", "Octane", "Clean", "Sister", "Army", | |
"Drama", "Backyard", "Pirate", "Shark", "Project", "Model", | |
"Survival", "Justice", "Mom", "New York", "Jersey", "Ax", | |
"Warrior", "Ancient", "Pawn", "Throttle", "The Great American", "Knight", | |
"American", "Outback", "Celebrity", "Air", "Restaurant", | |
"Bachelor", "Family", "Royal", "Surf", "Ulitmate", | |
"Date", "Operation", "Fish Tank", "Logging", "Hollywood", "Amateur", | |
"Craft", "Mystery", "Intervention", "Dog", "Human", "Rock", "IceRoad", | |
"Shipping", "Modern", "Crocodile", "Farm", "Amish", "Single", "Tool", "Boot Camp", | |
"Pioneer", "Kid", "Action", "Bounty", "Paradise", "Mega", "Love", | |
"Style", "Teen", "Pop", "Wedding", "An American", "Treasure", | |
"Myth", "Empire", "Motorway", "Room", "Casino", "Comedy", "Undercover", "Millionaire", | |
"Chopper", "Space", "Cajun", "Hot Rod", "The", "Colonial", "Dance", "Flying", | |
"Sorority", "Mountain", "Auction", "Extreme", "Whale", "Storage", "Cake", "Turf", | |
"UFO", "The Real", "Wild", "Duck", "Queer", "Voice", "Fame", | |
"Music", "Rock Star", "BBQ", "Spouse", "Wife", "Road", "Star", "Renovation", "Comic", "Chef", | |
"Band", "House", "Sweet") | |
mul = rep(first, 100) | |
for (i in 1:length(mul)) { | |
cat(mul[i], ": ", transnew(mul[i]), "\t\t", sep = "") | |
if (i %% 3 == 0) cat("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment