…
public String pluralize(String word) {
if (word.endsWith("s")) {
return word;
}
if (word.endsWith("y")) {
return word.replaceAll("/y$/", "ies");
}
if(word.matches("(fe?)$")){
return word.replaceAll("/fe?$/", "ves");
}
if (word.matches("(s|z|ch|sh|x)$")) {
return word += "es";
}
return word += "s";
}
…
$ cat lingtest.rb
#!/bin/ruby
require 'linguistics'
Linguistics::use( :en )
inventory = %w{mouse sword canary sword canary mouse mace
lantern sack amulet arrow arrow shield arrow arrow arrow}
puts "Bubba is carrying " + inventory.en.conjunction + "."
$ ./lingtest.rb
Bubba is carrying five arrows, two mice, two swords, two canaries, a mace, a lantern, a sack, an amulet, and a shield.
…
import static org.jvnet.inflector.Noun;
…
assertEquals(pluralOf("knife"), "knives"); //yay
assertEquals(pluralOf("day"), "days"); //yay
assertEquals(pluralOf("calf"), "calves"); // yay
assertEquals(pluralOf("branch"), "branches"); // yay
assertEquals(pluralOf("fox"), "foxes"); // yay
assertEquals(pluralOf("loaf"), "loaves"); //yay
assertEquals(pluralOf("thief"), "thieves"); // << FAILS
assertEquals(pluralOf("man"), "men"); // yay
assertEquals(pluralOf("matrix", "matrices"); // << FAILS
assertEquals(pluralOf("woman"), "women"); // yay
assertEquals(pluralOf("bottle of water"), "bottles of water"); // yay!
…
/*
Makeplural function by Jessica Chen goodbitster_at_gmail_com.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
char *makeplural(char *oldstr)
{
char newword[512/4]={"\0"};
strcpy(newword,oldstr);
char* p=newword, *c;
char input[512/4]={"\0"};
int found=0;
if((c=strstr(newword, "man"))!=NULL && strcmp(oldstr, "human"))
{
sprintf(input, "men");
found=1;
}
else if((c=strstr(newword,"tooth"))!=NULL)
{
sprintf(input, "teeth");
found=1;
}
else if((c=strstr(newword, "fe"))!=NULL || (c=strstr(newword, "ff"))!=NULL)
{
sprintf(input,"ves");
found=1;
}
else if((c=strstr(newword,"foot"))!=NULL)
{
sprintf(input, "feet");
found=1;
}
else if((c=strstr(newword,"ium"))!=NULL)
{
sprintf(input, "ia");
found=1;
}
else /* letter endings */
{
p=newword;
while(*p!='\0')
p++;
p–;
c=p-1;
if (strchr("zxs",*p))
sprintf(++p, "es");
else if(*p=='y' && !strchr("aeiouAEIOU", *(p-1)))
sprintf(p, "ies");
else if((p=strstr(c,"ch"))!=NULL)
sprintf(p+2, "es");
else if((p=strstr(c,"sh"))!=NULL)
sprintf(p+2, "es");
else
sprintf(newword, "%ss",oldstr);
return strdup(newword);
}
if(found)
{
int len=strlen(input);
int len2=strlen©;
int i=0;
char* tc=c;
for(i=0;i<len2;i++)
tc++;
if (!isalnum(*tc))
{
sprintf(c,"%s",input);
return strdup(newword);
}
}
return strdup(oldstr);
}
char* tp = makeplural(objname); //if objname is the singular form, the plural form will be strduped into tp.
/* <Use tp here> */
free(tp);
char *makeplural(char *oldstr)
{
char newword[512/4]={"\0"};
strcpy(newword,oldstr);
char* p=newword, *c;
char input[512/4]={"\0"};
int found=0;
if((c=strstr(newword, "man"))!=NULL && strcmp(oldstr, "human") && !isalnum(*(c+3)))
{
sprintf(input, "men");
found=1;
}
else if((c=strstr(newword,"tooth"))!=NULL && !isalnum(*(c+5)))
{
sprintf(input, "teeth");
found=1;
}
else if((c=strstr(newword, "fe"))!=NULL || (c=strstr(newword, "ff"))!=NULL)
{
sprintf(input,"ves");
found=1;
}
else if((c=strstr(newword,"foot"))!=NULL && !isalnum(*(c+4)))
{
sprintf(input, "feet");
found=1;
}
else if((c=strstr(newword,"ium"))!=NULL)
{
sprintf(input, "ia");
found=1;
}
else /* letter endings */
{
p=newword;
while(*p!='\0')
p++;
p–;
c=p-1;
if (strchr("zxs",*p))
sprintf(++p, "es");
else if(*p=='y' && !strchr("aeiouAEIOU", *(p-1)))
sprintf(p, "ies");
else if((p=strstr(c,"ch"))!=NULL)
sprintf(p+2, "es");
else if((p=strstr(c,"sh"))!=NULL)
sprintf(p+2, "es");
else
sprintf(newword, "%ss",oldstr);
return strdup(newword);
}
if(found)
{
int len=strlen(input);
int len2=strlen©;
int i=0;
char* tc=c;
tc+=len2;
if (!isalnum(*(tc)))
{
sprintf(c,"%s",input);
return strdup(newword);
}
}
return strdup(oldstr);
}
Right now I'm thinking of having a function that compares a given word to a table of known exceptions then applies a specific rule to the word if it matches one and a general rule if it doesn't.