Better output for centuries in format_wikibase_time + tests.

This commit is contained in:
Edward Betts 2021-11-13 16:27:46 +00:00
parent fd5829f8d2
commit 7b181cd1b3
2 changed files with 22 additions and 1 deletions

View file

@ -7,6 +7,7 @@ import user_agents
import re
import pattern.en
from datetime import date
from num2words import num2words
metres_per_mile = 1609.344
feet_per_metre = 3.28084
@ -187,4 +188,5 @@ def format_wikibase_time(v):
if p == 9:
return t[1:5]
if p == 7:
return t[1:5]
century = ((int(t[:5]) - 1) // 100) + 1
return num2words(century, to="ordinal_num") + " century"

19
tests/test_utils.py Normal file
View file

@ -0,0 +1,19 @@
from matcher import utils
def test_pluralize_label():
v = {"language": "en", "value": "building"}
assert utils.pluralize_label(v) == "buildings"
v = {"language": "en", "value": "mine"}
assert utils.pluralize_label(v) == "mines"
def test_format_wikibase_time_year():
v = {"time": "+1950-00-00T00:00:00Z", "precision": 9}
assert utils.format_wikibase_time(v) == "1950"
def test_format_wikibase_time_century():
v = {"time": "+0800-00-00T00:00:00Z", "precision": 7}
assert utils.format_wikibase_time(v) == "8th century"
v = {"time": "+1950-00-00T00:00:00Z", "precision": 7}
assert utils.format_wikibase_time(v) == "20th century"