GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve and add exceptions for singular method by slawekjaranowski · Pull Request #493 · codehaus-plexus/modello

Conversation

@slawekjaranowski

@slawekjaranowski slawekjaranowski commented May 13, 2025

Copy link
Copy Markdown
Member

No description provided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@slawekjaranowski
slawekjaranowski requested review from cstamas, gnodet and slachiewicz May 13, 2025 16:36
@slawekjaranowski slawekjaranowski added the bug Something isn't working label May 13, 2025
@slawekjaranowski
slawekjaranowski force-pushed the singular-fix branch from 6c3654d to f475c2c Compare May 13, 2025 19:12
gnodet
gnodet reviewed May 14, 2025
View reviewed changes

@gnodet gnodet left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a proposal:

    private static final Map<String, String> PLURAL_EXCEPTIONS = new HashMap<>();

    static {
        // Irregular plurals
        PLURAL_EXCEPTIONS.put("men", "man");
        PLURAL_EXCEPTIONS.put("women", "woman");
        PLURAL_EXCEPTIONS.put("children", "child");
        PLURAL_EXCEPTIONS.put("mice", "mouse");
        PLURAL_EXCEPTIONS.put("people", "person");
        PLURAL_EXCEPTIONS.put("teeth", "tooth");
        PLURAL_EXCEPTIONS.put("feet", "foot");
        PLURAL_EXCEPTIONS.put("geese", "goose");

        // Invariant plurals
        PLURAL_EXCEPTIONS.put("series", "series");
        PLURAL_EXCEPTIONS.put("species", "species");
        PLURAL_EXCEPTIONS.put("sheep", "sheep");
        PLURAL_EXCEPTIONS.put("fish", "fish");
        PLURAL_EXCEPTIONS.put("deer", "deer");
        PLURAL_EXCEPTIONS.put("aircraft", "aircraft");

        // Special "oes" exceptions
        PLURAL_EXCEPTIONS.put("heroes", "hero");
        PLURAL_EXCEPTIONS.put("potatoes", "potato");
        PLURAL_EXCEPTIONS.put("tomatoes", "tomato");
        PLURAL_EXCEPTIONS.put("echoes", "echo");
        PLURAL_EXCEPTIONS.put("vetoes", "veto");
        PLURAL_EXCEPTIONS.put("torpedoes", "torpedo");
        PLURAL_EXCEPTIONS.put("cargoes", "cargo");
        PLURAL_EXCEPTIONS.put("haloes", "halo");
        PLURAL_EXCEPTIONS.put("mosquitoes", "mosquito");
        PLURAL_EXCEPTIONS.put("buffaloes", "buffalo");
    }

    public static String singular(String plural) {
        if (plural == null || plural.isEmpty()) return plural;

        String lower = plural.toLowerCase();

        if (PLURAL_EXCEPTIONS.containsKey(lower)) {
            return PLURAL_EXCEPTIONS.get(lower);
        }

        // Suffix-based rules
        if (lower.endsWith("ies") && plural.length() > 3) {
            return plural.substring(0, plural.length() - 3) + "y";
        }
        if (lower.endsWith("ves")) {
            return plural.substring(0, plural.length() - 3) + "f";
        }
        if (lower.endsWith("zzes")) {
            return plural.substring(0, plural.length() - 2);
        }
        if (lower.endsWith("sses")) {
            return plural.substring(0, plural.length() - 2);
        }
        if (lower.endsWith("ches") || lower.endsWith("shes")) {
            return plural.substring(0, plural.length() - 2);
        }
        if (lower.endsWith("xes")) {
            return plural.substring(0, plural.length() - 2);
        }
        if (lower.endsWith("oes")) {
            return plural.substring(0, plural.length() - 1);
        }
        if (lower.endsWith("s") && plural.length() > 1) {
            return plural.substring(0, plural.length() - 1);
        }

        return plural;
    }

With a more complete test pairs:

    // Known exceptions
    "men", "man",
    "women", "woman",
    "children", "child",
    "mice", "mouse",
    "people", "person",
    "teeth", "tooth",
    "feet", "foot",
    "geese", "goose",

    "series", "series",
    "species", "species",
    "sheep", "sheep",
    "fish", "fish",
    "deer", "deer",
    "aircraft", "aircraft",

    "heroes", "hero",
    "potatoes", "potato",
    "tomatoes", "tomato",
    "echoes", "echo",
    "vetoes", "veto",
    "torpedoes", "torpedo",
    "cargoes", "cargo",
    "haloes", "halo",
    "mosquitoes", "mosquito",
    "buffaloes", "buffalo",

    // Regular plural forms with suffixes
    "voes", "voe",
    "hoes", "hoe",
    "canoes", "canoe",
    "toes", "toe",
    "foes", "foe",
    "oboes", "oboe",
    "noes", "no",
    "boxes", "box",
    "wishes", "wish",
    "dishes", "dish",
    "brushes", "brush",
    "classes", "class",
    "buzzes", "buzz",
    "cars", "car",
    "dogs", "dog",
    "cats", "cat",
    "horses", "horse",

    // Some test cases with different rules
    "wolves", "wolf",
    "knives", "knife",
    "leaves", "leaf",
    "wives", "wife",
    "lives", "life",
    "babies", "baby",
    "parties", "party",
    "cities", "city",
    "buses", "bus",
    "boxes", "box",
    "churches", "church",
    "matches", "match",
    "watches", "watch",
    "riches", "rich",
    "dresses", "dress",
    "crosses", "cross",

    // More edge cases
    "heroes", "hero",
    "vetoes", "veto",
    "torpedoes", "torpedo",
    "tomatoes", "tomato",
    "potatoes", "potato",
    "echoes", "echo",
    "mosquitoes", "mosquito",
    "buffaloes", "buffalo",
    "volcanoes", "volcano",
    "goes", "go"

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java Outdated

static {
PLURAL_EXCEPTION.put("children", "child");
PLURAL_EXCEPTION.put("licenses", "license");

@gnodet gnodet May 14, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that one an exception?
The plural just adds an 's'

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@slawekjaranowski slawekjaranowski May 14, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end with es

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread modello-core/src/test/java/org/codehaus/modello/plugin/AbstractModelloGeneratorTest.java Outdated
"repositories, repository",
"roles, role",
"rushes, rush",
"series, series"

@gnodet gnodet May 14, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a few more, here's a more extensive list:

"women", "woman", "men", "man", "children", "child", "mice", "mouse", "people", "person", "series", "series", "species", "species", "roses", "rose", "fezzes", "fez", "kisses", "kiss", "buses", "bus", "glasses", "glass", "heroes", "hero", "potatoes", "potato", "tomatoes", "tomato", "echoes", "echo", "torpedoes", "torpedo", "vetoes", "veto", "cargoes", "cargo", "haloes", "halo", "mosquitoes", "mosquito", "babies", "baby", "wolves", "wolf", "knives", "knife", "leaves", "leaf", "wives", "wife", "lives", "life", "boxes", "box", "wishes", "wish", "dishes", "dish", "churches", "church", "brushes", "brush", "classes", "class", "buzzes", "buzz", "cars", "car", "dogs", "dog", "voes", "voe", "does", "doe", "hoes", "hoe", "canoes", "canoe"

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread modello-core/src/main/java/org/codehaus/modello/plugin/AbstractModelloGenerator.java Outdated
} else if (name.endsWith("xes")) {
} else if (name.endsWith("zzes")) {
return name.substring(0, name.length() - 3);
} else if (name.endsWith("ches") || name.endsWith("xes") || name.endsWith("ses") || name.endsWith("oes")

@gnodet gnodet May 14, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The oes rule is a bit more complicated.
It usually looses the final s, but there are exceptions such as heroes, potatoes, tomatoes, echoes, torpedoes, vetoes, cargoes, haloes, and mosquitoes.
I think the rule should be loose the final s, with exceptions such as:

    "heroes", "hero",
    "potatoes", "potato",
    "tomatoes", "tomato",
    "echoes", "echo",
    "vetoes", "veto",
    "torpedoes", "torpedo",
    "cargoes", "cargo",
    "haloes", "halo",
    "mosquitoes", "mosquito",
    "buffaloes", "buffalo"

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@slawekjaranowski

slawekjaranowski commented May 14, 2025

Copy link
Copy Markdown
Member Author

rule:

        if (lower.endsWith("ves")) {
            return name.substring(0, name.length() - 3) + "f";
        }

not works for:

"knives, knife"
"wives, wife"
"lives, life",

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gnodet

gnodet commented May 14, 2025

Copy link
Copy Markdown
Member

rule:

        if (lower.endsWith("ves")) {
            return name.substring(0, name.length() - 3) + "f";
        }

not works for:

"knives, knife"
"wives, wife"
"lives, life",

Those are actually incorrect plurals. the correct ones are ending with ves, knives, wives and lives.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slawekjaranowski

slawekjaranowski commented May 14, 2025

Copy link
Copy Markdown
Member Author

rule:

        if (lower.endsWith("ves")) {
            return name.substring(0, name.length() - 3) + "f";
        }

not works for:

"knives, knife"
"wives, wife"
"lives, life",

Those are actually incorrect plurals. the correct ones are ending with ves, knives, wives and lives.

I see knives the same - in my example and in your 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slawekjaranowski

slawekjaranowski commented May 14, 2025

Copy link
Copy Markdown
Member Author

@gnodet - based on your proposition I have a next fix ...

I'm afraid that it will be difficult to support all cases, so I added a parameter to Mojo, when we can add special exclusion in project.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@slawekjaranowski
slawekjaranowski requested a review from gnodet May 14, 2025 20:51
@slawekjaranowski

slawekjaranowski commented May 14, 2025

Copy link
Copy Markdown
Member Author

I have found it: https://github.com/dry-rb/dry-inflector/blob/main/lib/dry/inflector/inflections/defaults.rb

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hboutemy

hboutemy commented May 17, 2025

Copy link
Copy Markdown
Member

defining what "improving" means would be useful: as such, it's just a vague personal judgement

but I read the content, and it's related to being able to add exceptions and have a default list of classical ones

question: is there an official list somewhere?
i did a search, I could find https://www.thoughtco.com/irregular-plural-nouns-in-english-1692634 and many others
it would be nice to keep a pointer to the reference list used as a default

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@hboutemy hboutemy added enhancement New feature or request and removed bug Something isn't working labels May 17, 2025
@slawekjaranowski

slawekjaranowski commented May 17, 2025

Copy link
Copy Markdown
Member Author

As I started work on it .... I've hoped it will be only simple improvement ....

But the problem turned out to be more complicated I have added a list of some irregular form @gnodet comments - I don't know a source.

Finally I added a parameter to allow providing more special cases, as it is difficult to discover all irregular noun

Of course we can adjust change title to show what exactly was done.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

gnodet
gnodet approved these changes May 18, 2025
View reviewed changes
slawekjaranowski added 3 commits May 23, 2025 18:33
@slawekjaranowski
Improve singular method
bf4001c
@slawekjaranowski
Improve singular method - next try
cf33c69
@slawekjaranowski
Next examples
31295ab
@slawekjaranowski
slawekjaranowski force-pushed the singular-fix branch from 988a6b0 to 31295ab Compare May 23, 2025 18:42
@slawekjaranowski slawekjaranowski changed the title Improve singular method Improve and add exceptions for singular method May 26, 2025
@slawekjaranowski
slawekjaranowski merged commit 6cb3ab2 into master May 26, 2025
@slawekjaranowski
slawekjaranowski deleted the singular-fix branch May 26, 2025 21:17
@cstamas cstamas mentioned this pull request Jun 3, 2025
Blocker: one does not change ANYTHING in model, just updates modello from 2.4.0 to 2.5.0 and project explodes #502
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gnodet gnodet gnodet approved these changes
@slachiewicz slachiewicz Awaiting requested review from slachiewicz
@cstamas cstamas Awaiting requested review from cstamas

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

enhancement New feature or request

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@slawekjaranowski @gnodet @hboutemy
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.