psysky
psysky

Reputation: 3195

Create dictionary and replace by it latin words in R

I have dataset with latin words

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

I want to set transliteration from latin-cyrillic

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

But I want to manually create the translit dictionary. For example:

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

when dictionary is created, in mydat$text,all latin words must be replaced by cyrillic words, which i set. something like this

d=dictionary(mydat$text)

How perform such replacing?

input

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

file with translit

dict=path.csv

it containt

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("ТЕСС",
"МАГ")

that's all

Upvotes: 0

Views: 114

Answers (1)

moodymudskipper
moodymudskipper

Reputation: 47320

There you go!

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] "тесс" "маг"

Upvotes: 1

Related Questions