创建字典并用R替换拉丁词

时间:2018-10-07 11:21:12

标签: r string dplyr

我的数据集包含拉丁词

text<-c("TESS",
"MAG")

我要设置拉丁西里尔字母的音译

library(stringi)
d=stri_trans_general(mydat$text, "latin-cyrillic")

但是我想手动创建翻译字典。 例如:

dictionary<-c("Tess"="ТЕСС"
"MAG"="МАГ"
.......
......
)

创建字典后, 在mydat $文本中,所有拉丁词都必须替换为我设置的西里尔字母。 像这样

d=dictionary(mydat$text)

如何进行这种替换?

输入

text<-c("TESS",
"MAG")

文件已翻译

dict=path.csv

包含

dict=

structure(list(old = structure(c(2L, 1L), .Label = c("mag", "tess"
), class = "factor"), new = structure(c(2L, 1L), .Label = c("маг", 
"тесс"), class = "factor")), .Names = c("old", "new"), class = "data.frame", row.names = c(NA, 
-2L))

#output

text<-c("ТЕСС",
"МАГ")

仅此而已

1 个答案:

答案 0 :(得分:1)

你去了!

dict <- structure(list(
  old = structure(c(2L, 1L), .Label = c("mag", "tess"),class = "factor"),
  new = structure(c(2L, 1L), .Label = c("маг", "тесс"), class = "factor")),
  .Names = c("old", "new"), class = "data.frame", row.names = c(NA, -2L))

input<-c("TESS","MAG")

output <- with(lapply(dict,as.character), new[match(tolower(input),old)])
output
# [1] "тесс" "маг"