none
Как сделать несколько вариантов транслитерации для символов в PowerShell ? RRS feed

  • Вопрос

  • Нашел вот такой скрипт для транслитерации:

    function global:TranslitToLAT
    {
    param([string]$inString)
    $Translit_To_LAT = @{
    [char]'а' = "a"
    [char]'А' = "a"
    [char]'б' = "b"
    [char]'Б' = "b"
    [char]'в' = "v"
    [char]'В' = "v"
    [char]'г' = "g"
    [char]'Г' = "g"
    [char]'д' = "d"
    [char]'Д' = "d"
    [char]'е' = "e"
    [char]'Е' = "e"
    [char]'ё' = "e"
    [char]'Ё' = "e"
    [char]'ж' = "zh"
    [char]'Ж' = "zh"
    [char]'з' = "z"
    [char]'З' = "z"
    [char]'и' = "i"
    [char]'И' = "i"
    [char]'й' = "i"
    [char]'Й' = "i"
    [char]'к' = "k"
    [char]'К' = "k"
    [char]'л' = "l"
    [char]'Л' = "l"
    [char]'м' = "m"
    [char]'М' = "m"
    [char]'н' = "n"
    [char]'Н' = "n"
    [char]'о' = "o"
    [char]'О' = "o"
    [char]'п' = "p"
    [char]'П' = "p"
    [char]'р' = "r"
    [char]'Р' = "r"
    [char]'с' = "s"
    [char]'С' = "s"
    [char]'т' = "t"
    [char]'Т' = "t"
    [char]'у' = "u"
    [char]'У' = "u"
    [char]'ф' = "f"
    [char]'Ф' = "f"
    [char]'х' = "kh"
    [char]'Х' = "kh"
    [char]'ц' = "tc"
    [char]'Ц' = "tc"
    [char]'ч' = "ch"
    [char]'Ч' = "ch"
    [char]'ш' = "sh"
    [char]'Ш' = "sh"
    [char]'щ' = "shch"
    [char]'Щ' = "shch"
    [char]'ъ' = "" # "``"
    [char]'Ъ' = "" # "``"
    [char]'ы' = "y" # "y`"
    [char]'Ы' = "y" # "Y`"
    [char]'ь' = "" # "`"
    [char]'Ь' = "" # "`"
    [char]'э' = "e" # "e`"
    [char]'Э' = "e" # "E`"
    [char]'ю' = "iu"
    [char]'Ю' = "iu"
    [char]'я' = "ia"
    [char]'Я' = "ia"
    [char]' ' = "_"
    }
    $outChars=""
    foreach ($c in $inChars = $inString.ToCharArray())
    {
    if ($Translit_To_LAT[$c] -cne $Null )
    {$outChars += $Translit_To_LAT[$c]}
    else
    {$outChars += $c}
    }
    Write-Output $outChars
    }
    
    $text = Read-Host "Введите текст"
    $log = TranslitToLAT $text
    $log

    Но не могу понять как можно его доработать, добавив несколько вариантов транслитерации для одного символа.

    Как пример: если буква 'И' идёт после 'Ь' и 'Ъ' то должна прописываться "YI", а не "I". А в остальных случаях 'И' прописывалась как 'I'.

    4 октября 2019 г. 3:43

Ответы

  • День добрый
    Самое простое, и минимум переделок - обработать строчку до основного преобразования, вроде такого:

    function global:TranslitToLAT {
        param([string]$inString)
        $Translit_To_LAT = @{
            [char]'а' = "a"
            [char]'А' = "a"
            [char]'б' = "b"
            [char]'Б' = "b"
            [char]'в' = "v"
            [char]'В' = "v"
            [char]'г' = "g"
            [char]'Г' = "g"
            [char]'д' = "d"
            [char]'Д' = "d"
            [char]'е' = "e"
            [char]'Е' = "e"
            [char]'ё' = "e"
            [char]'Ё' = "e"
            [char]'ж' = "zh"
            [char]'Ж' = "zh"
            [char]'з' = "z"
            [char]'З' = "z"
            [char]'и' = "i"
            [char]'И' = "i"
            [char]'й' = "i"
            [char]'Й' = "i"
            [char]'к' = "k"
            [char]'К' = "k"
            [char]'л' = "l"
            [char]'Л' = "l"
            [char]'м' = "m"
            [char]'М' = "m"
            [char]'н' = "n"
            [char]'Н' = "n"
            [char]'о' = "o"
            [char]'О' = "o"
            [char]'п' = "p"
            [char]'П' = "p"
            [char]'р' = "r"
            [char]'Р' = "r"
            [char]'с' = "s"
            [char]'С' = "s"
            [char]'т' = "t"
            [char]'Т' = "t"
            [char]'у' = "u"
            [char]'У' = "u"
            [char]'ф' = "f"
            [char]'Ф' = "f"
            [char]'х' = "kh"
            [char]'Х' = "kh"
            [char]'ц' = "tc"
            [char]'Ц' = "tc"
            [char]'ч' = "ch"
            [char]'Ч' = "ch"
            [char]'ш' = "sh"
            [char]'Ш' = "sh"
            [char]'щ' = "shch"
            [char]'Щ' = "shch"
            [char]'ъ' = "" # "``"
            [char]'Ъ' = "" # "``"
            [char]'ы' = "y" # "y`"
            [char]'Ы' = "y" # "Y`"
            [char]'ь' = "" # "`"
            [char]'Ь' = "" # "`"
            [char]'э' = "e" # "e`"
            [char]'Э' = "e" # "E`"
            [char]'ю' = "iu"
            [char]'Ю' = "iu"
            [char]'я' = "ia"
            [char]'Я' = "ia"
            [char]' ' = "_"
        }
        $outChars = ""
        $TwoLetter_To_LAT = @{
            [string]'ьи' = 'yi'
            [string]'ъи' = 'yi'
        }
        $TwoLetter_To_LAT.GetEnumerator().name | % {
            $inString = $inString.Replace($_, $TwoLetter_To_LAT.Item($_))
        }
        foreach ($c in $inChars = $inString.ToCharArray()) {
            if ($Translit_To_LAT[$c] -cne $Null )
            { $outChars += $Translit_To_LAT[$c] }
            else
            { $outChars += $c }
        }
        Write-Output $outChars
    }
    
    $text = Read-Host "Введите текст"
    $log = TranslitToLAT $text
    $log


    Грамотная постановка вопроса - уже 50% решения.
    SCCM User Group Russia на FaceBook и в Telegram

    • Предложено в качестве ответа Vector BCOModerator 4 октября 2019 г. 15:27
    • Помечено в качестве ответа Vector BCOModerator 4 октября 2019 г. 15:27
    4 октября 2019 г. 7:42

Все ответы

  • День добрый
    Самое простое, и минимум переделок - обработать строчку до основного преобразования, вроде такого:

    function global:TranslitToLAT {
        param([string]$inString)
        $Translit_To_LAT = @{
            [char]'а' = "a"
            [char]'А' = "a"
            [char]'б' = "b"
            [char]'Б' = "b"
            [char]'в' = "v"
            [char]'В' = "v"
            [char]'г' = "g"
            [char]'Г' = "g"
            [char]'д' = "d"
            [char]'Д' = "d"
            [char]'е' = "e"
            [char]'Е' = "e"
            [char]'ё' = "e"
            [char]'Ё' = "e"
            [char]'ж' = "zh"
            [char]'Ж' = "zh"
            [char]'з' = "z"
            [char]'З' = "z"
            [char]'и' = "i"
            [char]'И' = "i"
            [char]'й' = "i"
            [char]'Й' = "i"
            [char]'к' = "k"
            [char]'К' = "k"
            [char]'л' = "l"
            [char]'Л' = "l"
            [char]'м' = "m"
            [char]'М' = "m"
            [char]'н' = "n"
            [char]'Н' = "n"
            [char]'о' = "o"
            [char]'О' = "o"
            [char]'п' = "p"
            [char]'П' = "p"
            [char]'р' = "r"
            [char]'Р' = "r"
            [char]'с' = "s"
            [char]'С' = "s"
            [char]'т' = "t"
            [char]'Т' = "t"
            [char]'у' = "u"
            [char]'У' = "u"
            [char]'ф' = "f"
            [char]'Ф' = "f"
            [char]'х' = "kh"
            [char]'Х' = "kh"
            [char]'ц' = "tc"
            [char]'Ц' = "tc"
            [char]'ч' = "ch"
            [char]'Ч' = "ch"
            [char]'ш' = "sh"
            [char]'Ш' = "sh"
            [char]'щ' = "shch"
            [char]'Щ' = "shch"
            [char]'ъ' = "" # "``"
            [char]'Ъ' = "" # "``"
            [char]'ы' = "y" # "y`"
            [char]'Ы' = "y" # "Y`"
            [char]'ь' = "" # "`"
            [char]'Ь' = "" # "`"
            [char]'э' = "e" # "e`"
            [char]'Э' = "e" # "E`"
            [char]'ю' = "iu"
            [char]'Ю' = "iu"
            [char]'я' = "ia"
            [char]'Я' = "ia"
            [char]' ' = "_"
        }
        $outChars = ""
        $TwoLetter_To_LAT = @{
            [string]'ьи' = 'yi'
            [string]'ъи' = 'yi'
        }
        $TwoLetter_To_LAT.GetEnumerator().name | % {
            $inString = $inString.Replace($_, $TwoLetter_To_LAT.Item($_))
        }
        foreach ($c in $inChars = $inString.ToCharArray()) {
            if ($Translit_To_LAT[$c] -cne $Null )
            { $outChars += $Translit_To_LAT[$c] }
            else
            { $outChars += $c }
        }
        Write-Output $outChars
    }
    
    $text = Read-Host "Введите текст"
    $log = TranslitToLAT $text
    $log


    Грамотная постановка вопроса - уже 50% решения.
    SCCM User Group Russia на FaceBook и в Telegram

    • Предложено в качестве ответа Vector BCOModerator 4 октября 2019 г. 15:27
    • Помечено в качестве ответа Vector BCOModerator 4 октября 2019 г. 15:27
    4 октября 2019 г. 7:42
  • Большое спасибо за ответ, помогли сильно.
    • Предложено в качестве ответа Vector BCOModerator 6 октября 2019 г. 9:37
    • Помечено в качестве ответа Vector BCOModerator 6 октября 2019 г. 9:37
    • Снята пометка об ответе Vector BCOModerator 6 октября 2019 г. 9:37
    • Отменено предложение в качестве ответа Vector BCOModerator 6 октября 2019 г. 9:37
    • Изменено Shargas 6 октября 2019 г. 14:24
    6 октября 2019 г. 8:27