H
H
hixr0k2019-09-30 15:04:12
PowerShell
hixr0k, 2019-09-30 15:04:12

Transliteration in Powershell. How to make multiple choices?

There is a transliteration script available.

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

Tell me how you can add several transliteration options for characters?
As an example, if the letter 'I' comes after 'b' and 'b' then it should be spelled "YI", not "I"

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Kovalev, 2019-10-18
@hixr0k

I have the following function in my script:

function Convert2Latin($inString) {

# Обрезаем пустые пробелы по краям
$inString = $inString.Trim()


# Определяем таблицу соответствия
$char_ru="а","А","б","Б","в","В","г","Г","д","Д","е","Е","ё","Ё","ж", "Ж", "з","З","и","И","й","Й", "к","К","л","Л","м","М","н","Н","о","О","п","П","р","Р","с","С","т","Т","у","У","ф","Ф","х", "Х", "ц", "Ц", "ч", "Ч", "ш", "Ш", "щ",  "Щ","ъ","Ъ","ы","Ы","ь","Ь","э","Э","ю", "Ю", "я", "Я"
$char_en="a","A","b","B","v","V","g","G","d","D","e","E","e","E","zh","Zh","z","Z","i","I","y","Iy","k","K","l","L","m","M","n","N","o","O","p","P","r","R","s","S","t","T","u","U","f","F","kh","Kh","ts","Ts","ch","Ch","sh","Sh","sch","Sch","","","y","Y","","",  "e","E","yu","Yu","ya","Ya"
$TempString = ""


# Перебираем слово по буквам
for ($i = 0; $i -lt $inString.Length; $i++)
{ 
$t = -1

# Выясняем позицию заменямой буквы в массиве
 Do {$t = $t+1}
    Until (($inString[$i] -ceq $char_ru[$t]) -or ($t -eq 100))

# Дополняем строку конвертированного одновременно производя замену русской буквы на английскую
$TempString = $TempString + ($inString[$i] -creplace $char_ru[$t], $char_en[$t])
}

return $TempString
}

A
anykey_ua, 2019-09-30
@anykey_ua

If you do not teach the program how to respond to specific conditions, then nothing. Accordingly, first parse the string and check the sequences (only describe them in the code).

K
Konstantin Tsvetkov, 2019-10-01
@tsklab

At the beginning of the array are multi-letter combinations:

Transliteration according to the instructions of the Ministry of Internal Affairs of the Russian Federation of 06/30/98 No. 3941

.
    ( 'вий', 'vy'   ),
    ( 'гий', 'gy'   ),
    ( 'дий', 'dy'   ),
    ( 'ний', 'ny'   ),
    ( 'сий', 'sy'   ),
    ( 'тий', 'ty'   ),
    ( 'жд',  'zd'   ),
    ( 'ай',  'ay'   ),
    ( 'ей',  'ey'   ),
    ( 'ёй',  'ey'   ),
    ( 'ий',  'iy'   ),
    ( 'ия',  'ia'   ),
    ( 'ой',  'oy'   ),
    ( 'уй',  'uy'   ),
    ( 'ый',  'uy'   ),
    ( 'эй',  'ey'   ),
    ( 'ья',  'ia'   ),
    ( 'ье',  'ye'   ),
    ( 'ьё',  'ye'   ),
    ( 'ьа',  'ia'   ),
    ( 'ьи',  'yi'   ),
    ( 'ьо',  'yo'   ),
    ( 'ьу',  'yu'   ),
    ( 'ьы',  'yy'   ),
    ( 'ьэ',  'ye'   ),
    ( 'ью',  'yu'   ),
    ( 'кс',  'x'    ),
    ( 'юй',  'yuy'  ),
    ( 'яй',  'yay'  ),
    ( 'лю',  'liu'  ),
    ( 'ж',   'zh'   ),
    ( 'х',   'kh'   ),
    ( 'ц',   'ts'   ),
    ( 'ч',   'ch'   ),
    ( 'ш',   'sh'   ),
    ( 'я',   'ya'   ),
    ( 'ю',   'yu'   ),
    ( 'щ',   'shch' ),
    ( 'ъ',   ''     ),
    ( 'ь',   ''     ),
    ( 'а',   'a'    ),
    ( 'б',   'b'    ),
    ( 'в',   'v'    ),
    ( 'г',   'g'    ),
    ( 'д',   'd'    ),
    ( 'е',   'e'    ),
    ( 'ё',   'e'    ),
    ( 'з',   'z'    ),
    ( 'и',   'i'    ),
    ( 'й',   'y'    ),
    ( 'к',   'k'    ),
    ( 'л',   'l'    ),
    ( 'м',   'm'    ),
    ( 'н',   'n'    ),
    ( 'о',   'o'    ),
    ( 'п',   'p'    ),
    ( 'р',   'r'    ),
    ( 'с',   's'    ),
    ( 'т',   't'    ),
    ( 'у',   'u'    ),
    ( 'ф',   'f'    ),
    ( 'ы',   'y'    ),
    ( 'э',   'e'    )

H
hixr0k, 2019-10-02
@hixr0k

And could you tell me how to integrate them into the script? In my case, it turns out that in any case, the translation of "yi" is translated into "i", and not "yi"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question