Altering objects’ name according to number of objects (in Russian)

Say, 1 dog, 2 dogs – it’s easy in English, but not so simple in Russian.

Whatever language you are working with, try to find a general rule.With Russian, it turned out begin quite simple – for numbers ending with [1], [2, 3, 4] and [5, 6, 7, 8, 9] there are generally 3 forms. Except “teens” – [11, 12, ..., 19] that are in the third form no matter what.

Me cracking the code

If the words are unknown to you, you may want to factors things that word forms. But it is separate algorithm. This one is about sending a number and getting a word form.

def find_base(base_word, f):
"""
base_word - first form of the objects' name
f - form number (0 to 2)
"""
l = {
'фраза': ['фраза', 'фразы', 'фраз'],
'тема': ['тема', 'темы', 'тем'],
# many more
}
return l.get(base_word, ['']*3)[f]


def find_word(obj, base_word):
"""
obj - quantity of objects
base_word - first form of the objects' name
"""

obj_int = int(obj) # to determine 'teens'
obj = str(obj).strip() # to get last digit

if obj.endswith('1'):
if obj_int == 1 or obj_int > 20: k = 0
else: k = 2
elif obj.endswith('2') or obj.endswith('3') or obj.endswith('4'):
if obj_int < 10 or obj_int > 20: k = 1
else: k = 2
else: k = 2
return find_base(base_word, k)

It is not as short as possible, but it’s easily readable. I aim for that.

Basically, this code:

>> find_word(11, 'фраза')

Return

>> фраз

This thing I used for a statistics module for Lingomost.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.