Pluralize it!​​

The challenge:

Write a program or function in any language that takes as an argument (command-line or function) a single English word. It returns or outputs the pluralized form of the word.

(To clarify, the program or function will be executed many times on different words, but separately - the list of words won't be passed all at once.)

The scoring:

The score of a program or function is -(characters in program + 1) * number of wrong answers. Highest score wins. Joke answers will likely lose by a huge margin (I'm planning on putting in maybe 1,000 words) and if you're amazing you get the highest score of 0.

Example test data:

woman
turn
fly
court
finger
blitz
pie
injury
vertex
alleyway
alloy
ox
goose
mouse
house
louse
waffle
right
bacterium
virus
cherub
chaetognath
boy
man
shaman
figure
prefix
etc.


Those are just some that I picked off the top of my head. The real test cases will have even stranger plurals, but the distribution between types of suffixes will be more even.

Enjoy!

No Mathematica. Sorry.

• The problem with your proposed scoring is that a program which gets everything right can be arbitrarily big, it will always have score 0. That's hardly code-golf-ish, you'd simply need to include an entire dictionary! I'd suggest (characters_in_program + 1) * (wrong_answers + 1) instead. – ceased to turn counterclockwis Feb 25 '12 at 21:31
• @leftaroundabout: I'll throw in a plural that people are unlikely to have ever heard of. If it includes that, well... it deserves the highest score! :) – Ry- Feb 25 '12 at 21:33
• Also, you should perhaps specify whether answers like this will be accepted. – ceased to turn counterclockwis Feb 25 '12 at 21:43
• @leftaroundabout: Ah, yes. I was meaning to add that. And the answer is NO! But for the other possible "loopholes" and rule-bending, they're still allowed. – Ry- Feb 25 '12 at 21:45
• By the way, something which hadn't occurred to me earlier: how does your test script handle cases where there's more than one correct answer? – Peter Taylor Feb 26 '12 at 8:46

sed - 155 (not including #! line)

#!/bin/sed -Ef
s/man$/men!/ s/([aeiou](x|z))$/\1es!/
s/ium$/ia!/ s/([^!aeiou])y$/\1ies!/
s/((r|gh|l|s)[^!aeiou])$/\1s!/ s/([^!aeiou]{2}|.s)$/\1es!/
s/([^!])$/\1s/ s/!$//


This will read from stdin, and I think it will do an okay job for most input. The basic idea is that it tries each regex line by line. Whenever it matches it makes a replacement that appends ! to make sure that no other regexes will match, then it drops the ! on the last line.

I tried to deal with most special cases, but it will definitely fail on some (goose and mouse for example)

Sample invocation:

chmod a+x GordonBailey.sed
echo $word | ./GordonBailey.sed  • Sorry, how do I run it? echo "test" > sed -f GordonBailey.sed doesn't output anything. – Ry- Feb 25 '12 at 22:18 • woops, added a sample invocation – Gordon Bailey Feb 25 '12 at 22:26 • Really? If it makes it easier, since it's sed, it will read from a file line by line, so you could put all the words into a file and run it on the whole file: cat words.txt | ./GordonBailey.sed > output.txt – Gordon Bailey Feb 25 '12 at 23:33 • I don't know @minitech...I want to respect you, but I'm beginning to think you're not a unix user. ::gasps and shocked silence from the audience:: Whatever is the world coming to? – dmckee --- ex-moderator kitten Feb 26 '12 at 2:22 • @dmckee: Isn't unix that thing that webservers use? (No, this is not serious.) – Ry- Feb 26 '12 at 14:44 sed, 6 chars Someone should give this a try... s/$/s/


Invoked with sed -f myscript.sed < words.txt > output.txt
I can afford 22 times more mistakes, compared to Gordon Bailey's solution, and still win.

With the sample input I do win, because he misses "mice" and "lice" (and maybe more, there are some I need to look up), and this sample is too short for me to have 44 mistakes...

• Nice. I'll be curious to see the final stats with a longer input list, this may well be the best approach. – Gordon Bailey Feb 27 '12 at 23:51
require 'open-uri'

def p(w)

perl -mLingua::EN::Inflect=PL -pe's/.*/PL$&/e'  Extended BrainFuck: 9 ,[.,]||s|  Compiling: beef ebf.bf < plural.ebf > plural.bf  Turns into the following BrainFuck code: (39) ,[.,]>++++++++++[-<+++++++++++>]<+++++.  Sample invocation: $ echo -n test | beef plural.bf

Any BrainFuck interpreter can be used instead of beef. It takes one word in stdin and ends it with EOF. Alternatively you can use echo test | bf -n plural.bf where in this particular interpreter-n assumes EOF on first linefeed.