Skip to content

Instantly share code, notes, and snippets.

@tukachev
Last active June 22, 2021 13:37
Show Gist options
  • Save tukachev/7550665 to your computer and use it in GitHub Desktop.
Save tukachev/7550665 to your computer and use it in GitHub Desktop.
Transliteration function
translit <- function(text, charupper=FALSE){
lat.up <- 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","SHH","''","Y","'","E'","YU","YA")
rus.up <- c("А","Б","В","Г","Д","Е","Ё","Ж","З","И","Й","К","Л","М","Н","О",
"П","Р","С","Т","У","Ф","Х","Ц","Ч","Ш","Щ","Ъ","Ы","Ь","Э","Ю","Я")
lat.low <- 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","shh","''","y","'","e'","yu","ya")
rus.low <- c("а","б","в","г","д","е","ё","ж","з","и","й","к","л","м","н","о",
"п","р","с","т","у","ф","х","ц","ч","ш","щ","ъ","ы","ь","э","ю","я")
n <- nchar(text)
text <- substring(text, 1:n, 1:n)
while (n > 0) {
for(i in 1:33) {
char.pos <- grep(rus.low[i], text)
text[char.pos] <- lat.low[i]
char.pos <- grep(rus.up[i], text)
text[char.pos] <- lat.up[i]
}
n <- n - 1
}
ifelse(charupper==TRUE, toupper(paste(text, collapse = "")), paste(text, collapse = ""))
}
#Пример использования
translit("Здесь фабула объять не может всех эмоций — шепелявый скороход в юбке тащит горячий мёд.", charupper=TRUE)
translit("Друг мой эльф! Яшке б свёз птиц южных чащ!")
translit("Любя, съешь щипцы, — вздохнёт мэр, — кайф жгуч.")
@VitlZ
Copy link

VitlZ commented Apr 11, 2015

Good job

@alexeilutay
Copy link

nice one, I used it many times before realized that it works slowly.
Adapted it to become much-much faster.
https://gist.github.com/alexeilutay/3c784476f47a61c74752609e2f9b564f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment