From 057afa534ad1b754f5bc1fedf906c7f37d315bbc Mon Sep 17 00:00:00 2001 From: Jordan Irwin Date: Tue, 28 May 2024 20:14:32 -0700 Subject: [PATCH] Support possessive form in Grammar --- .../stendhal/common/grammar/Grammar.java | 80 +++++++++++++++++-- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/src/games/stendhal/common/grammar/Grammar.java b/src/games/stendhal/common/grammar/Grammar.java index 7f802c2b05e..21f176cb4b1 100644 --- a/src/games/stendhal/common/grammar/Grammar.java +++ b/src/games/stendhal/common/grammar/Grammar.java @@ -1,5 +1,5 @@ /*************************************************************************** - * (C) Copyright 2009-2023 - Stendhal * + * (C) Copyright 2009-2024 - Stendhal * *************************************************************************** *************************************************************************** * * @@ -777,6 +777,23 @@ public static String plnounCreature(final int quantity, final String noun) { return plnoun(quantity, noun); } + /** + * Converts a noun to possessive form. + * + * @param noun + * The noun to examine. + * @param plural + * Whether the noun is considered to be plural. + * @return + * Possessive form. + */ + public static String possessive(final String noun, final boolean plural) { + if (plural && noun.endsWith("s")) { + return noun + "'"; + } + return noun + "'s"; + } + /** * Returns either the plural or singular form of the given noun, depending * on the quantity; also prefixes the quantity. @@ -826,27 +843,76 @@ public static String quantityplnounCreature(final int quantity, final String nou * @param noun * The noun to examine * @param one replacement for "1". + * @param possessive Whether the noun should be considered to be in possessive form. * @return Either "[quantity] [noun]" or "[quantity]" + plural("[noun]") as * appropriate */ - public static String quantityplnoun(final int quantity, final String noun, final String one) { + public static String quantityplnoun(final int quantity, String noun, final String one, + final boolean possessive) { final String word = plnoun(quantity, noun); if (quantity == 1) { + String phrase; if (one.equals("a")) { - return a_noun(word); + phrase = a_noun(word); } else if (one.equals("A")) { - return A_noun(word); + phrase = A_noun(word); } else if (one.equals("")) { - return word; + phrase = word; } else { - return one + " " + word; + phrase = one + " " + word; + } + if (possessive) { + phrase = phrase.replaceAll(word + "$", possessive(word, false)); } + return phrase; } else { - return Integer.toString(quantity) + " " + plural(noun); + noun = plural(noun); + if (possessive) { + noun = possessive(noun, true); + } + return Integer.toString(quantity) + " " + noun; } } + /** + * Returns either the plural or singular form of the given noun, depending + * on the quantity; also prefixes the quantity. In case the quantity is exactly + * 1, the specified prefix is used. Note: There is some additional magic to convert + * "a" and "A" to "an" and "An" in case that is required by the noun. + * + * @param quantity + * The quantity to examine + * @param noun + * The noun to examine + * @param one replacement for "1". + * @return Either "[quantity] [noun]" or "[quantity]" + plural("[noun]") as + * appropriate + */ + public static String quantityplnoun(final int quantity, final String noun, final String one) { + return quantityplnoun(quantity, noun, one, false); + } + + + /** + * Returns either the plural or singular possessive form of the given noun, depending + * on the quantity; also prefixes the quantity. In case the quantity is exactly + * 1, the specified prefix is used. Note: There is some additional magic to convert + * "a" and "A" to "an" and "An" in case that is required by the noun. + * + * @param quantity + * The quantity to examine + * @param noun + * The noun to examine + * @param one replacement for "1". + * @return Either "[quantity] [noun]" or "[quantity]" + plural("[noun]") as + * appropriate + */ + public static String quantityplnounPossessive(final int quantity, final String noun, + final String one) { + return quantityplnoun(quantity, noun, one, true); + } + /** * Returns either the plural or singular form of the given noun, depending on * the quantity; also prefixes the quantity and prints the noun with a hash prefix.