From bd24ea872a1d87e7c7d12c4b163333c58103396a Mon Sep 17 00:00:00 2001 From: Gary Coady Date: Tue, 29 Nov 2022 22:51:05 +0100 Subject: [PATCH] Improve handling of plurals in generator. The generator tries to turn plural into singular nouns. However, some words turn out badly, including: - Addresse (from Addresses) - Prefixe (from Prefixes) - Bookshelve (from Bookshelves) - Sery (from Series) By using a library, we can get a slightly better outcome. I've added an exception for "Data", because seeing that turned into "Datum" is a bit jarring. --- requirements.txt | 1 + src/generator/lib/util.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index 89ad107608..97e93ab92e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,3 +7,4 @@ codecov ghp-import pyright types-PyYAML +inflect diff --git a/src/generator/lib/util.py b/src/generator/lib/util.py index 275f1247a1..0ddc7616b1 100644 --- a/src/generator/lib/util.py +++ b/src/generator/lib/util.py @@ -2,6 +2,7 @@ import os import re import subprocess +import inflect from dataclasses import dataclass from typing import Any, Dict, List, Mapping, Tuple from copy import deepcopy @@ -94,6 +95,10 @@ data_unit_multipliers = { '%': 1, } + +inflection = inflect.engine() + + HUB_TYPE_PARAMETERS = ('S',) @@ -284,11 +289,15 @@ def md_italic(l): def singular(s): - if s.endswith('ies'): - return s[:-3] + 'y' - if s[-1] == 's': - return s[:-1] - return s + if s.lower().endswith('data'): + return s + + single_noun = inflection.singular_noun(s) + + if single_noun is False: + return s + else: + return single_noun def split_camelcase_s(s):