#!/usr/bin/env python # -*- coding: utf-8 -*- def dgs(number, word=u''): """Pluralization for Lithuanian Arguments: - an integer - a noun in a singular nominative case Examples: 1) As Django template filter: {{ 10|dgs:"Komentaras" }} {{ 22|dgs:"Knyga" }} 2) As a standalone function: dgs(10, "Komentaras") dgs(22, "Knyga") Output: "10 Komentarų" "22 Knygos" """ if number in range(11,20) or (number % 10) == 0: #masculine if word.endswith(u'as'): word = word[:-2] + u'ų' elif word.endswith(u'dis') or word.endswith(u'dys'): word = word[:-2] + u'žių' elif word.endswith(u'ys') or word.endswith(u'is'): word = word[:-2] + u'ių' #feminine elif word.endswith(u'a'): word = word[:-1] + u'ų' elif word.endswith(u'dė'): word = word[:-1] + u'žių' elif word.endswith(u'tė'): word = word[:-2] + u'čių' elif word.endswith(u'ė'): word = word[:-1] + u'ių' elif number % 10 in range(2, 10): #masculine if word.endswith(u"as"): word = word[:-1] + u'i' elif word.endswith(u'dis') or word.endswith(u'dys'): word = word[:-2] + u'žiai' elif word.endswith(u"is") or word.endswith(u"ys"): word = word[:-2] + u'iai' #feminine elif word.endswith(u"a"): word = word[:-1] + u'os' elif word.endswith(u"ė"): word = word + u's' #default value is used in all other cases return "%d %s" % (number, word) def test(): fail = 0 words = { u'Litas': {5: u'Litai', 10: u'Litų'}, u'medis': {5: u'medžiai', 10: u'medžių'}, u'gaidys': {5: u'gaidžiai', 10: u'gaidžių'}, u'švyturys': {5: u'švyturiai', 10: u'švyturių'}, u'peilis': {5: u'peiliai', 10: u'peilių'}, u'šaka': {5: u'šakos', 10: u'šakų'}, u'veltėdė': {5: u'veltėdės', 10: u'veltėdžių'}, u'katė': {5: u'katės', 10: u'kačių'}, u'rožė': {5: u'rožės', 10: u'rožių'}, u'boba': {5: u'bobos', 10: u'bobų'}, u'interviu': {5: u'interviu', 10: u'interviu'}, u'emo': {5: u'emo', 10: u'emo'}, } for word in words: for num in words[word]: #take word only plural = dgs(num, word).split(' ')[1] if not plural == words[word][num]: fail = fail + 1 print "Error %d %s came out as %d %s" % (num, words[word][num], num, plural) if fail: print "Total errors:", str(fail) else: print "Test completed successfully. No errors found." if __name__ == '__main__': test()