3

In QGIS I need to do a string conversion, from serbian latin to serbian cyrilic.

In field calculator, i have only this inelegant solution :

replace(replace(replace(replace( etc... )))) 

It's long to type all translitterations (about 30), is there a better way to do this ?


Inpired by given links, i did this :

from qgis.core import *
from qgis.gui import *
import string

@qgsfunction(args='auto', group='Custom')
def serbian_latin_to_cyrillic(n):
    capital_letters = {
        u'Џ': u'DŽ',
        u'Њ': u'NJ',
        u'Љ': u'LJ',
        u'А': u'A',
        u'Б': u'B',
        u'В': u'V',
        u'Г': u'G',
        u'Д': u'D',
        u'Ђ': u'Ð',
        u'Е': u'E',
        u'Ж': u'Ž',
        u'З': u'Z',
        u'И': u'I',
        u'Ј': u'J',
        u'К': u'K',
        u'Л': u'L',
        u'М': u'M',
        u'Н': u'N',
        u'О': u'O',
        u'П': u'P',
        u'Р': u'R',
        u'С': u'S',
        u'Т': u'T',
        u'Ћ': u'Ć',
        u'У': u'U',
        u'Ф': u'F',
        u'Х': u'H',
        u'Ц': u'C',
        u'Ч': u'Č',
        u'Ш': u'Š'
    }

    lower_case_letters = {
        u'dž': u'џ',
        u'nj': u'њ',
        u'lj': u'љ',
        u'a': u'а',
        u'b': u'б',
        u'v': u'в',
        u'g': u'г',
        u'd': u'д',
        u'ð': u'ђ',
        u'e': u'е',
        u'ž': u'ж',
        u'z': u'з',
        u'i': u'и',
        u'j': u'ј',
        u'k': u'к',
        u'l': u'л',
        u'm': u'м',
        u'n': u'н',
        u'o': u'о',
        u'p': u'п',
        u'r': u'р',
        u's': u'с',
        u't': u'т',
        u'ć': u'ћ',
        u'u': u'у',
        u'f': u'ф',
        u'h': u'х',
        u'c': u'ц',
        u'č': u'ч',
        u'š': u'ш'
    }

translit_string = ""

    for index, char in enumerate(string):
        if char in lower_case_letters.keys():
            char = lower_case_letters[char]
        elif char in capital_letters.keys():
            char = capital_letters[char]
            if len(string) > index+1:
                if string[index+1] not in lower_case_letters.keys():
                    char = char.upper()
            else:
                char = char.upper()
        translit_string += char

    return translit_string

But when i use the function, and put a field form the table in the parameter of the function, it says the function use only one paramater and it says i put 3 value. Why that ?


I try the second solution, and it gives me some strange result. Beside this, i cannot get the value of a field transmitted in function paramater.

@qgsfunction(args='auto', group='Python')
def serbian_latin_to_cyrillic(field, feature, parent):

    sr_latin = u"Bačka Palanka"

    symbols = (u"džnjljabvgdðežzijklmnoprstćufhcčšDŽNJLJABVGDÐEŽZIJKLMNOPRSTĆUFHCČŠ",
    u"џњљабвгдђежзијклмнопрстћуфхцчшЏЊЉАБВГДЂЕЖЗИЈКЛМНОПРСТЋУФХЦЧШ")

    """tr = {ord(a):ord(b) for a, b in zip(*symbols)}"""
    tr = dict( [ (ord(a), ord(b)) for (a, b) in zip(*symbols) ] )


    return sr_latin.translate(tr)

It gives me this : ÐнµÐ ÐÐÐÐеÐ

If i disable the transliteration function, it gives me this : BaÄka Palanka Maybe problem with character encoding ?

2
  • Do you have any python skills? A simple python dictionary could be used. You could create a function which passes your text and returns the transliterated result.
    – Fezter
    Commented Mar 13, 2017 at 23:46
  • @Mane Please edit your question to include additional information or response to comments. Only post an Answer if you are answering your own question.
    – Midavalo
    Commented Mar 16, 2017 at 18:42

2 Answers 2

3

It looks like this has been done successfully in Python. Have a look at this post on Stack Overflow.

In order to replicate this in QGIS, you'll need to create a function to use in the Field Calculator.

Your custom function will look similar to this:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def serbian(n):
    symbols = (u"abvgdeejzijklmnoprstufhzcss_y_euaABVGDEEJZIJKLMNOPRSTUFHZCSS_Y_EUA"),u"абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ")
    tr = {ord(a):ord(b) for a, b in zip(*symbols)}
    
    return n.translate(tr)

If you want to go the other way (cyrillic to latin) which is what was done in the example on Stack Overflow, you just need to reverse the order of the symbols tuple.

You should be able to call this function in your field calculator and pass your fields into it.

Disclaimer: I'm not a QGIS user and I have not tested the above.

1
  • The problem is i do a mistake, it's not purely a transliterations, just changing alphabetic latin to alphabetic cyrillic. For exemple, 'Lj' become 'љ' in cyrillic, whereas 'L' alone become 'л'. I have to prioritize transliterations before changing alone symbols.
    – Mane
    Commented Mar 16, 2017 at 11:20
0

@mane maybe you can find a solution here > how-to-transliterate-in-qgis-using-python-3. Install the module linked there, Serbian is included in its Natural Languages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.