N
N
nak-alexey2020-08-06 08:51:15
Python
nak-alexey, 2020-08-06 08:51:15

Transliterator, where is the mistake?

I am making a translator whose task is to translate the Russian text written in Latin into Russian. The loop stops only at the first match i.e. prints "ja" to the console, where did I make a mistake? Or what is missing?

def transliterate(name):
    dictionary = {'a':'а','b':'б','v':'в','g':'г','d':'д','e':'e',
      'zh':'ж','z':'з','i':'и','y':'й','k':'к','l':'л','m':'м','n':'н',
      'o':'о','p':'п','r':'р','s':'с','t':'т','u':'у','f':'ф','kh':'х',
      'ts':'ц','ch':'ч','sh':'ш','shch':'щ','ъ':'','ie':'ы','ь':'','ei':'э',
      'yu':'ю','ja':'я'}

    i=0

    while i<len(name):
        for key in dictionary:
            index = 1
            while (index!=4):
                if (name[i:index] == key[0:index]):
                    print(name[i:index] + ' = ' + key[0:index])
                    index+=1
                else:
                    index=4
        i+=1

transliterate("jayu")

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Pankov, 2020-08-06
@nak-alexey

In cuts. Both slice boundaries must be specified absolutely, and your index is reset to 1 each time. Such a purl slice will return an empty string.
"abcdef"[3:2] -> ""

D
Dr. Bacon, 2020-08-06
@bacon

Not all transliteration can be "translated" normally, but there are special schemes for unambiguous "translation" back and forth. Well, better use the ready-made solution https://pypi.org/project/iuliia/

I
IDzone-x, 2020-08-11
@IDzone-x

One question, why do you care about such a complex translator algorithm if you can make it simpler?:

def transliter(string):
  l_dict = {'a':'а','b':'б','v':'в','g':'г','d':'д','e':'e',
      'zh':'ж','z':'з','i':'и','y':'й','k':'к','l':'л','m':'м','n':'н',
      'o':'о','p':'п','r':'р','s':'с','t':'т','u':'у','f':'ф','kh':'х',
      'ts':'ц','ch':'ч','sh':'ш','shch':'щ','ъ':'','ie':'ы','ь':'','ei':'э',
      'yu':'ю','ja':'я'}
  translit_string = []

  for char in string:
    translit_string.append(l_dict[char])
  return "".join(translit_string)
  
  
print(transliter("opa")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question