Skip to content

Commit

Permalink
Support possessive form in Grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed May 29, 2024
1 parent e615061 commit 057afa5
Showing 1 changed file with 73 additions and 7 deletions.
80 changes: 73 additions & 7 deletions src/games/stendhal/common/grammar/Grammar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
* (C) Copyright 2009-2023 - Stendhal *
* (C) Copyright 2009-2024 - Stendhal *
***************************************************************************
***************************************************************************
* *
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 057afa5

Please sign in to comment.