用有道保存了一些查詢過(guò)的單詞和詞組,想要對(duì)這些內(nèi)容進(jìn)行檢索。
數(shù)據(jù)清理
- 導(dǎo)出單詞本為文本文件,在notepad中打開。
- 合并行 #因?yàn)橐粋€(gè)entry分到了3-5行
- 替換\r\n
- 替換\n
- 去掉entry名 #導(dǎo)出的每個(gè)條目前都有排序編號(hào)
- 替換\d{1,},
- 替換連續(xù)的多個(gè)空格為單個(gè)空格
- 去掉音標(biāo) #在R中會(huì)以亂碼顯示
- 替換[.*]
導(dǎo)入R
library(tidyverse)
vocab <- readLines("input/vocabulary_youdao.txt", encoding = "UTF-8") %>% as.tibble
# extrac word according a pattern
d_ex_vocab <- function(patt) {
library(magrittr)
extrt <- stringr::str_extract(vocab$value, patt)
dong_word_extract <- vocab[!is.na(extrt), ]
if (dim(dong_word_extract)[1] == 0)
stop("No word extracted, plz check the spell!")
write.csv(dong_word_extract, paste0("output/", patt, ".csv"), quote = F, row.names = F)
return(dong_word_extract)
}
ex_word <- d_ex_vocab("取決于")