Skip to content

Instantly share code, notes, and snippets.

@alexeilutay
Last active December 20, 2021 00:06
Show Gist options
  • Save alexeilutay/3c784476f47a61c74752609e2f9b564f to your computer and use it in GitHub Desktop.
Save alexeilutay/3c784476f47a61c74752609e2f9b564f to your computer and use it in GitHub Desktop.
Transliterator from Russian to English
# A function for transliteration inspired by https://gist.github.com/tukachev/7550665 but way too faster
translit2 <- function(text){
transliterations <- data.frame(
lat = c("A","B","V","G","D","E","YO","ZH","Z","I","J","K","L","M","N",
"O","P","R","S","T","U","F","KH","C","CH","SH","SHCH","''","Y","'","E'","YU","YA",
"a","b","v","g","d","e","yo","zh","z","i","j","k","l","m","n","o","p","r",
"s","t","u","f","kh","c","ch","sh","shch","''","y","'","e'","yu","ya"),
rus = c("А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О","П",
"Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я",
"а","б","в","г","д","е","ё","ж","з","и","й","к","л","м","н","о",
"п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я")
)
if(is.na(text)){return(NA)}
strsplit(text, "|") %>%
unlist() %>% as.data.frame() %>% setNames("rus") %>%
left_join(transliterations, by = "rus") %>%
mutate(lat = ifelse(is.na(lat), rus, lat)) %>%
select(lat) %>% unlist() %>% paste(collapse = "")
}
# examples:
# text <- c("огого","бубубу","ляля")
# (1) supply a string vector via map_chr()
# text %>% map_chr(~translit2(.x))
# (2) with mutate also via map_chr()
# text %>% enframe() %>% mutate(value2 = map_chr(value, translit2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment