R/translit.R

Defines functions translit

Documented in translit

#' converts text string in cyrillic into latin text string (letter by letters)
#'
#' @param "str" is string or vector of strings in cyrillic
#' @param "space" is string to replace space, "_" - by default
#' @return latin text string
#' @author Grag2015
#' @description converts text string in cyrillic into latin text string (letter by letters)
#' digits and latin symbols stay at place, space is replaced on "space", other symbols are deleted
#' @export
#' @import stringr
#' @examples
#' str <- "ìîÿ æèçíü"
#' translit(str)
#' "moya zhizn"

translit <- function(str, space="_"){
    tolat <- function(s){
        if (grepl('([a-z]|\\d{1})', s)) {
            res <- s
        } else{
            res <- switch (s,
                           à="a",
                           á="b",
                           â="v",
                           ã="g",
                           ä="d",
                           å="e",
                           ¸="e",
                           æ="zh",
                           ç="z",
                           è="i",
                           é="y",
                           ê="k",
                           ë="l",
                           ì="m",
                           í="n",
                           î="o",
                           ï="p",
                           ð="r",
                           ñ="s",
                           ò="t",
                           ó="u",
                           ô="f",
                           õ="h",
                           ö="ts",
                           ÷="ch",
                           ø="sh",
                           ù="sch",
                           û="ii",
                           ü="",
                           ú="",
                           ý="e",
                           þ="yu",
                           ÿ="ya",
                           ' '=space
            )
        }
    }
    str <- as.character(str)
    res <- character(0)
    for (j in 1:length(str)) {
        n <- nchar(str[j])
        str_t <- str_trim(str[j])
        str_t <- tolower(str[j])
        str_res <- ""
        for (i in 1:n) {
            s <- substr(str_t,i,i)
            str_res <- paste0(str_res,tolat(s))
        }
        res[j] <- str_res
    }
    res
}
Grag2015/mytools documentation built on May 6, 2019, 6:30 p.m.