R本身当然不具备这样的功能,需要自备词典文件。本人转concise-bing.mobi为concise-bing.txt,提取词条及对应翻译,然后才可以实现。出发点不是单个单词检索,而是批量,这是一般词典如有到和欧陆不具备的。
# mk dict db --------------------------------------------------------------
## import dict
dict <- readLines("./concise-bing.txt", encoding = "UTF-8") %>%
str_replace_all("'", "")
## rm empty lines
dict <- dict[nchar(dict)>0]
## detect word entries
quot <- dict %>% str_detect("^[[:alpha:]]")
## detect entries start with chinese
quot2 <- dict[quot] %>% str_detect("(\\.)|([\u4e00-\u9fa5])")
## clean the word entries
word <- dict[quot][!quot2]
## get word indexes
word_indx <- which(dict %in% word)
## integrate multiple translate into 1 character
temp <- list()
j=1
for (j in j:length(word_indx)) {
trans <- paste(dict[(word_indx[j]+1):(word_indx[j+1]-1)], collapse = "; ")
words <- dict[word_indx[j]]
temp[[j]] <- c(words, trans)
cat(j, "-", round(j/length(word_indx), 2), "\n")
}
dict <- temp
## name the dict
names(dict) <- dict %>% lapply("[[", 1) %>% unlist
## save
save(dict, file = "dictionary_concise-bing.rdata")
# search ------------------------------------------------------------------
query <- c("suppress")
dict[which(names(dict) %in% query)][[1]][2]
## the translate would be shown in console, or you can save the hunts into plain txt by
query <- c("your query vector")
dict[which(names(dict) %in% query)][[1]][2] %>% write.csv(file = "your path", row.names = F)
网友评论