Using Raku (formerly known as Perl_6)
raku -pe 'tr/MALCREX/THEOFIS/; s:g/ <+:Uppercase_Letter - [THEOFIS]> /_/;'
Sample Input:
MAL TIRRUEZF CR MAL RKZYIOL EX MAL OIY UAE RICF "MAL ACWALRM DYEUPLFWL CR ME DYEU MAIM UL IZL RKZZEKYFLF GH OHRMLZH"
Sample Output (running Raku code at top):
THE TIFF_I_F OF THE F___IOE IS THE OI_ _HI FIOF "THE HO_HEFT __I__EF_E OF TI __I_ THIT _E I_E F___I__FEF _H OHFTE_H"
One advantage of Raku for tackling this question is that Unicode is supported by default (not addressed in this answer, however).
The second statement in the code above uses a global s///
substitution with a bespoke character class <+:Uppercase_Letter - [THEOFIS]>
, which can be expressed more simply as <+:Lu - [THEOFIS]>
. As noted by @Stéphane Chazelas in the comments the :Lu
can be replaced with :Letter
or [\w]
so it can handle all kinds of letters.
For the simple case presented (only "
doublequote and
space non-alnum characters), the second statement in the code above can written using tr///
with the :complement
adverb. The statement tr:c/THEOFIS" /_/;
, adds the "
doublequote and
space characters to THEOFIS
list. (In other words, 'Take the :complement
of all the characters between the first two /…/
and change them to the character listed between the second two /…/
, which in this case is _
underscore.' )
raku -pe 'tr/MALCREX/THEOFIS/; tr:c/THEOFIS" /_/;'
Sample Output:
THE TIFF_I_F OF THE F___IOE IS THE OI_ _HI FIOF "THE HO_HEFT __I__EF_E OF TI __I_ THIT _E I_E F___I__FEF _H OHFTE_H"
EDITED:
Breaking the transliteration into two steps as I've done above means I run the danger of overwriting a 'final' character with the second step. To avoid this, I can convert to lowercase characters in both steps, which produces the same output seen by others (but--see below).
Finally, it looks as though the OP made an error in their posted transliteration table, since the updated table/code (below) gives much more sensible output:
raku -pe 'tr/MALCREX/theisof/; tr:c/theisof" /_/;'
Updated Output:
the __ss_o__ is the s_____e of the ___ _ho s_i_ "the hi_hest __o__e__e is to __o_ th_t _e __e s___o___e_ __ __ste__"
https://docs.raku.org/language/regexes#Predefined_character_classes
https://raku.org
A = B
andB = C
, etc. can occur in your real data then include that in your example in a way that demonstrates if, givenCAB
as input for example, the result should beCBC
orCCC
.tr MALCREX THEOFIS
" andtr
does only one translation per character, i.e. it mapsL
toE
, period, notL
=>E
=>I
. (I don't know how you'd go fromE
toE
using those rules, though.) Also, if the rules were to be applied in sequence, even to already-translated characters, then it would seem to me the "any other letter =>_
" rule would eat all characters (E
would translate toI
, andTHOFIS
don't appear on the left side, so would match the "any other" condition)A
=>B
;B
=>A
", which are impossible if the rules are applied in sequence. And even ifL
=>E
=>I
is intended, it can be achieved by just fixing the table so that there's theL
=>I
rule explicitly.tr
andsed
code that he/she was happy with. The real question is how rule 2 and rule 3 apply to previously substituted characters. Also, you could interpret the 'any other letter' condition as just that--anything other than the 13 letters not previously mentioned--which obviates the question of whether they've been already-translated or not.