← All posts

· Casimiro Ferreira & Claude· 9 min read

numtag: Why “Duas Dúzias” Is 24, Not 2

  • numtag
  • NLP
  • numbers
  • OVOS
  • TTS

A number in a text is rarely just digits:

TextValue
o vigésimo terceiro lugar (“twenty-third place”)23rd
tenho duas dúzias de ovos (“two dozen eggs”)24
dreihundertzweiundvierzig Bücher342
un milione e duecentomila1,200,000
2 milhões e meio2,500,000

A voice assistant must read these in commands. A speech synthesiser must say them back. A script that extracts data must find them in documents. Each job needs the exact value, and most of them need to know where the number is in the text.

A billion is not always a billion

A news story in Lisbon says “um bilião de euros”. A story in São Paulo says “um bilhão de reais”. The two words look like the same word, and both look like the English “billion”. They are not the same number:

TextLanguageValue
one billion dollarsen1,000,000,000
um bilhão de reaispt-BR1,000,000,000
um bilião de eurospt-PT1,000,000,000,000
un milliard d’eurosfr-FR1,000,000,000
un billionfr-FR1,000,000,000,000
eine Billionde-DE1,000,000,000,000

English and Brazilian Portuguese use the short scale: each new word (billion, trillion) is a thousand times the one before it. European Portuguese, French and German use the long scale: each new word is a million times the one before it, and the steps between have their own names (milliard, Milliarde) or a phrase (“mil milhões”). A translator who maps “bilião” to “billion” makes an error of a factor of 1,000.

numtag reads the word in the scale of the language tag that you give it, and it writes values the same way:

from numtag import spell

for lang in ["en", "pt-BR", "pt-PT", "fr-FR", "de-DE", "es-ES", "it-IT"]:
    print(lang, spell("1" + "0" * 9, lang)[0].text, "|", spell("1" + "0" * 12, lang)[0].text)
en one billion | one trillion
pt-BR um bilhão | um trilhão
pt-PT mil milhões | um bilião
fr-FR un milliard | un billion
de-DE eine Milliarde | eine Billion
es-ES mil millones | un billón
it-IT un miliardo | un bilione

Portugal and Brazil show the problem inside one language. They do not only count the large numbers differently, they also spell small ones differently:

Valuept-PTpt-BR
14catorzequatorze
16dezasseisdezesseis
19dezanovedezenove
10^9mil milhõesum bilhão
10^12um biliãoum trilhão
10^30um quintiliãoum nonilhão

The reader accepts every spelling in both varieties: “dezesseis” in a European text is still 16. The writer gives the form of the variety that you ask for. A Brazilian voice says “dezesseis” and a European voice says “dezasseis”.

The difference grows with the size of the number. 10^30 is “one nonillion” in English and “um quintilião” in European Portuguese. Because numtag keeps values as digit strings, it has no upper limit: 10^39 in Russian is “дуодециллион”.

Why we wrote our own

The usual design has one module per language, with its own reader and writer, and it returns an int or a float. That design has three limits:

  • Each language had its own bugs. The same idea was written again in each language, so a fix in Portuguese did nothing for Italian.
  • One value per text. It returned one number for the whole text, with no position. “Set a timer for 5 minutes and call mum at 6” holds two numbers.
  • Binary floats. A float cannot hold 0.1 exactly, so 0.1 + 0.2 is 0.30000000000000004, and a writer reads that error aloud. A float also keeps only about 16 significant digits.

numtag starts from a different design.

uv pip install --prerelease=allow numtag

Every number, with its place in the text

from numtag import tag_numbers

text = "Chegou em vigésimo terceiro lugar, com 2 milhões e meio de votos e um quarto dos lugares."
for m in tag_numbers(text, "pt-PT"):
    print(m.text, m.char_span, m.value, m.kind)
vigésimo terceiro  (10, 27)  '23'       ORDINAL
2 milhões e meio   (39, 55)  '2500000'  CARDINAL
um quarto          (67, 76)  '0.25'     FRACTION

Each mention has its text, its character span, its value, its kind (cardinal, ordinal or fraction), its form (digits or words) and a confidence. “2 milhões e meio” mixes digits and words, and it is still one number. All outputs are from numtag 0.50.0a1.

The span matters. chronologia, the date reader, uses it to find the numbers inside “daqui a duas semanas” or “vinte e sete de novembro”. text2spoken calls spell to say each number in words.

Values are strings

numtag gives each value as a digit string: "1200000", "0.25", "3/4". It never converts to a float, so no binary rounding enters. A string also has no ceiling.

from numtag import spell

spell("0.3", "en")[0].text            # 'zero point three'
spell("1" + "0" * 30, "pt-PT")[0].text  # 'um quintilião'
spell("1" + "0" * 39, "ru")[0].text     # 'дуодециллион'

If a caller needs a number, it converts the string itself: int(m.value) or Decimal(m.value). That choice belongs to the caller, not to the parser.

Ambiguity is real

Numbers hide in ordinary words. In Portuguese, “segundo” is “second”, but it is also “according to”:

tag_numbers("segundo o jornal choveu", "pt-PT")
# []    "according to the newspaper, it rained"

numtag gives nothing here, and that is the correct answer. A number reader that sees a number in every “segundo” breaks every sentence that cites a source.

Some rules looked correct and were dropped before they merged. One rule made “só um” (“just one”) a sure count of 1. Then “É só um exemplo” (“it is just an example”) became a certain 1, so the rule was removed. A Kabyle rule for fractions (“N seg M”) also read “one of the three” as a fraction, so it was removed too. Both now have tests that must stay negative.

Language is data

In numtag, each language is one JSON lexicon: its number words, its scale words, its ordinals and its fractions. One engine reads all of them, and one writer speaks them. No language has its own Python code.

Each form in a lexicon cites a source: a grammar, a dictionary or a native speaker. A form that only follows a pattern, with no source that shows it, is marked SILVER. The reader accepts every spelling that people write. The writer gives the standard spelling first, and the variants after it. Kabyle shows why both matter. A native speaker writes 100 as “mya”. The 1982 dictionary by Dallet writes “meyya”. numtag reads both, writes “mya”, and lists “meyya” as a variant.

What we did not do

  • Test against num2words or babel. Another library can be wrong in the same way. The tests compare with cited rows, not with other code.

Against other number libraries

How does numtag compare with the number packages that people already use? We tested it against text2num 3.1.0 and number-parser 0.3.2 on 3,510 sentences in 12 languages, about 300 per language. A model wrote each sentence around one number (cardinals, ordinals, decimals and fractions), and a second model checked it. It is a regression set, not held-out data.

The packages do different jobs, so we compared like with like:

PackageInputOutputPositions
numtag tag_numbersa sentenceevery number, with its kindyes
text2num text2numthe number words onlyone valueno
text2num alpha2digita sentencethe sentence, with digitsno
number-parser parsea sentencethe sentence, with digitsno

The test gives each package the whole sentence and checks the value it finds:

Bar chart of the share of sentences read correctly per language. numtag: pt-PT 0.97, pt-BR 0.98, es-CO 0.95, fr 0.94, it 0.93, de 0.98, nl 0.89, da 1.00, hi 0.86, kab 0.60, bn 0.47, mwr 0.73. text2num: 0.76 to 0.85 in the eight European languages, no support for hi, kab, bn and mwr. number-parser: es-CO 0.34, hi 0.37.

numtag reads 93 to 100 % of the sentences in seven European languages, and 89 % in Dutch. text2num reads 76 to 85 % in the same languages. number-parser supports two of the twelve languages, and reads about a third of them. For Kabyle, Bengali and Marwari, numtag is the only package that reads them at all. Its weakest languages are Bengali (0.47) and Kabyle (0.60).

Only numtag gives the position of each number. The other packages rewrite the sentence with digits, so the caller must find the number again.

The scale test shows the difference most clearly:

TextCorrect valuenumtagtext2num
um bilião (pt-PT)10^1210^1210^9, wrong
um bilhão (pt-BR)10^910^910^9
un billion (fr)10^1210^12error
eine Milliarde (de)10^910^9error
eine Billion (de)10^1210^12error

numtag reads all 27 scale and spelling cases correctly, and writes them correctly. For writing, num2words is also correct on all the cases that it supports.

There is a cost. numtag takes about 2.1 ms per sentence on one laptop core, and text2num about 0.02 ms. numtag looks for every kind of number with its position and its kind, and it reads languages that the others do not support. For a voice command or a document, 2 ms is small. For a batch of millions of sentences in English or French only, text2num is faster.

Against the standard library

Python reads digit strings with float and Decimal. A float is binary, so it cannot hold most decimal values exactly:

Inputfloatnumtag value
“zero vírgula um”, then add 0.20.1 + 0.2 = 0.30000000000000004“0.1”, exact
2.675, rounded to 2 placesround(2.675, 2) = 2.67“2.675”, exact
a number with 17 significant digitsthe 17th digit is lostall 17 digits kept
one nonillion (10^30)int(float(10**30)) = 1000000000000000019884624838656“1” followed by 30 zeros
10^309float("1e309") = infa digit string, no limit

Decimal is exact too, but it reads digits only. It cannot read “dois milhões e meio”.

What you can do with it

  • Read commands. A voice assistant reads “set a timer for twenty minutes” with tag_numbers, and it gets the value and its place in the command.
  • Speak numbers. Skills for alerts, counting and diagnostics call spell directly. text2spoken uses spell to speak numbers.
  • Read dates. chronologia finds the numbers in dates with tag_numbers.
  • Extract data. Pull every quantity out of a document, with its position and an exact value. Prices, counts and percentages keep all their digits.
  • Clean transcripts. Turn “twenty-one” and “21” into the same value before you compare two transcripts.

Where it fails

The reader covers more languages than the writer: spell writes 29 lexicons (24 languages). Hindi named fractions do not read yet. ढाई सौ (“two hundred and fifty”, literally “two and a half hundred”) gives nothing.

Try it

The numtag Space runs in your browser. It installs the newest alpha from PyPI and gives sample sentences for about 50 languages.