【问题标题】:Create dictionary and replace by it latin words in R创建字典并用它替换R中的拉丁词
【发布时间】:2018-10-07 11:21:12
【问题描述】:

我有拉丁词的数据集

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

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

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

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

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

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

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))

#输出

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

就是这样

【问题讨论】:

  • 类似setNames(stri_trans_general(mydat$text, "latin-cyrillic"), mydat$text) ?
  • @Moody_Mudskipper,我的字典在哪里?
  • 假设。我在文件 dict.csv 中的字典
  • 我不清楚你想要什么,你能编辑你的帖子并明确说明你的确切输入是什么,你的确切输出是什么?
  • @Moody_Mudskipper,我编辑了帖子,请检查

标签: r string dplyr


【解决方案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] "тесс" "маг"

【讨论】:

  • +1 表示dput(),但是——尽管这适用于示例——列应该是character 而不是factor,因为在其他情况下可能会导致问题。
  • 我猜这更像是对OP的评论,我只是复制了他的数据,但我同意从字符开始会更容易。但是,我的解决方案适用于两者。
  • @Moody_Mudskipper,你的解决方案有什么问题,你能检查一下,有什么问题stackoverflow.com/questions/52777989/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多