Selecting grammatical form depending on programatically defined cardinal number (a la ngettext)

There are many languages where grammatical form depends on number of elements described. In English it is singular and plural form (plural usually but not always is generated by adding suffix 's', but see e.g. mouse / mice), in other languages it can be more complicated, as described in gettext manual.

What I'd like to have is a macro which choses grammatical form depending on number, e.g. \nn{\nmice}{mouse}{mice} for English, which would output 1 mouse but 2 mice.

For Polish I would also have to employ some package for integer calculations involving conditionals and modulo

Plural-Forms: nplurals=3; \
plural=n==1 ? 0 : \
n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;


and e.g. \nn{\nmice}{mysz}{mysze}{myszy}.

The above formula (taken from mentioned above gettext documentation) means that in Polish you have 1 mysz (singular, 0th form), 2, 3, 4, 22, 23, 24 mysze (remains of old dual form, 1st plural form), but 5, 6, 7, 11, 12, 13, 14, 25 myszy (plural form, 2nd plural form). Nb. Slovenian has 4 plural forms.

-
I guess I could adapt the solution used in nth package. –  Jakub Narębski Jun 15 at 16:19
add comment

2 Answers

LuaTeX is ideally suited for such macros. For example, here is an implementation in ConTeXt (which can be easily translated to LaTeX):

\startluacode
-- Function to choose amoung n alternatives
function commands.ngettext(n, f, t)
-- The function f should return an integer 1, 2, etc.
-- The table t contains the corresponding list of plurals
context("%d %s", n, t[f(n)])
end

\stopluacode

\define[1]\Mice
{\ctxcommand{
ngettext(#1,
function(n) if n == 1 then return 1 else return 2 end; end,
{"mouse", "mice"})}}

\define[1]\Mysz
{\ctxcommand{
ngettext(#1,
function(n)
if n == 1 then
return 1
elseif n \letterpercent 10 >= 2 and n \letterpercent 10<=4 and (n \letterpercent 100<10 or n \letterpercent 100 >= 20)  then
return 2
else
return 3
end
end,
{"mysz", "mysze", "myszy"})}}
\starttext
Mouse: \Mice{1} \Mice{11} \Mice{22}

Mysz:   \Mysz{1} \Mysz{11} \Mysz{22}
\stoptext


which gives

-
add comment

A brutal-force solution for every word separately, here mysz. (Dwie mysze, hihi)

\documentclass{article}
\usepackage{polski}
\usepackage[cp1250]{inputenc}
\begin{document}

\def\mysz#1{\ifcase#1 myszy\or mysz \or mysze (naprawdę istnieje liczba podwójna?) \or myszy \else myszy \fi}

\mysz0
\mysz1
\mysz2
\mysz{126}

\end{document}


When mysz is replaced by pani, it becomes more complicated. 0 pań, 4 panie, 14 pań, 24 panie, 25 pań, etc. And człowiek has only two forms. If you are planning a limited number of words, it is no problem, but generally it might be a huge work.

-
What is the rule for pani? How many different forms exist, and what sets of integers (or non-integers) do they correspond to? –  Bruno Le Floch Jun 15 at 15:14
@BrunoLeFloch 0 pań, 1 pani, 2-4 panie, 5-21 pań, and then forms of number modulo 10. –  Przemysław Scherwentke Jun 15 at 15:20
Nothing special mod 100 or 1000? I could expect that (cf, 103rd vs 113th in English). –  Bruno Le Floch Jun 15 at 15:37
@BrunoLeFloch Not for pani. But for ordinal numbers (and the other forms) in Polish it is also more complicated than in case of nouns. Hence I wish to know, what OP really needs. –  Przemysław Scherwentke Jun 15 at 15:42
@BrunoLeFloch: Thanks for pointing the similarity between multiple plural forms and ordinal numbering style, 1st 2nd 3rd 4th 5th... 12th... 101st 102nd... in English. This lead me to considering adapting nth package for this. –  Jakub Narębski Jun 15 at 16:21
add comment