Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
better handling of multi-word phrase pluralization
Browse files Browse the repository at this point in the history
  • Loading branch information
pappy74 committed Jun 2, 2011
1 parent 4427b6e commit 19a930c
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions utils/word-problems.js
Expand Up @@ -31,7 +31,7 @@ jQuery.extend( KhanUtil, {
// - plural(NUMBER, singular, plural):
// - return "NUMBER word"
plural: (function() {
var one_offs = {
var oneOffs = {
'quiz': 'quizzes',
'shelf': 'shelves',
'loaf': 'loaves',
Expand All @@ -46,46 +46,53 @@ jQuery.extend( KhanUtil, {
// determine if our word is all caps. If so, we'll need to
// re-capitalize at the end
var isUpperCase = (word.toUpperCase() == word);
var one_off = one_offs[word.toLowerCase()];
var oneOff = oneOffs[word.toLowerCase()];
var words = word.split(/\s+/);

// first handle simple one-offs
if ( one_off ) {
word = one_off;
if ( oneOff ) {
return oneOff;
}

// for 3-word phrases where the middle word is 'in' or 'of',
// pluralize the first word
else if ( words.length == 3 && /\b(in|of)\b/i.test(words[1]) ) {
words[0] = KhanUtil.plural( words[0] );
return words.join(" ");
}
// multiple words
else if ( words.length > 1 ) {
// for 3-word phrases where the middle word is 'in' or 'of',
// pluralize the first word
if ( words.length == 3 && /\b(in|of)\b/i.test(words[1]) ) {
words[0] = KhanUtil.plural( words[0] );
}

// if two words, pluralize the second word
else if ( words.length == 2 ) {
words[1] = KhanUtil.plural( words[1] );
return words.join(" ");
}
// otherwise, just pluraize the last word
else {
words[ words.length-1 ] =
KhanUtil.plural( words[ words.length-1 ] );
}

// "-y" => "-ies"
else if ( /[^aeiou]y$/i.test( word ) ) {
word = word.replace(/y$/i, "ies");
return words.join(" ");
}

// add "es"; things like "fish" => "fishes"
else if ( /[sxz]$/i.test( word ) || /[bcfhjlmnqsvwxyz]h$/.test( word ) ) {
word += "es";
}

// all the rest, just add "s"
// single words
else {
word += "s";
}
// "-y" => "-ies"
if ( /[^aeiou]y$/i.test( word ) ) {
word = word.replace(/y$/i, "ies");
}

if ( isUpperCase ) {
word = word.toUpperCase();
// add "es"; things like "fish" => "fishes"
else if ( /[sxz]$/i.test( word ) || /[bcfhjlmnqsvwxyz]h$/.test( word ) ) {
word += "es";
}

// all the rest, just add "s"
else {
word += "s";
}

if ( isUpperCase ) {
word = word.toUpperCase();
}
return word;
}
return word;
};

return function(value, arg1, arg2) {
Expand Down

0 comments on commit 19a930c

Please sign in to comment.