Pluralize it!​​

1

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!

Additional Rule

No Mathematica. Sorry.

Ry-

Posted 2012-02-25T21:13:43.777

Reputation: 5 283

9The 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 – 2012-02-25T21:31:55.517

@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- – 2012-02-25T21:33:44.783

Also, you should perhaps specify whether answers like this will be accepted.

– ceased to turn counterclockwis – 2012-02-25T21:43:01.900

@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- – 2012-02-25T21:45:44.300

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 – 2012-02-26T08:46:56.633

@PeterTaylor: I was originally going to allow any correct answer to be counted as correct, but I've now decided that only the plural forms of the world that break the most rules will be correct. Just to make things a little more fun :) – Ry- – 2012-02-26T14:43:17.100

Meh. That makes it even more of a guessing game than it already was. – Peter Taylor – 2012-02-26T15:28:02.273

@PeterTaylor: Fine, fine, anything accepted. No more guessing game. – Ry- – 2012-02-26T15:34:26.380

Answers

3

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

Gordon Bailey

Posted 2012-02-25T21:13:43.777

Reputation: 708

Sorry, how do I run it? echo "test" > sed -f GordonBailey.sed doesn't output anything. – Ry- – 2012-02-25T22:18:52.947

woops, added a sample invocation – Gordon Bailey – 2012-02-25T22:26:53.193

Ah, thanks! Also, I'm looking for tests, not testes ;) Good job anyway, though - 1kw testing will now begin. – Ry- – 2012-02-25T22:30:27.760

Hahaha, if it's not too late, I just modified to catch that case – Gordon Bailey – 2012-02-25T22:47:33.353

Retesting now.​ – Ry- – 2012-02-25T22:51:30.387

much appreciated – Gordon Bailey – 2012-02-25T22:53:52.723

Shoot, it doesn't want to be automated. Lucky you, manual test :) – Ry- – 2012-02-25T23:22:04.700

3Really? 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 – 2012-02-25T23:33:30.100

Ahh... okay. Thanks. – Ry- – 2012-02-25T23:46:06.557

6I 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 – 2012-02-26T02:22:16.223

1@dmckee: Isn't unix that thing that webservers use? (No, this is not serious.) – Ry- – 2012-02-26T14:44:38.690

@minitech ::laughs:: Touche. – dmckee --- ex-moderator kitten – 2012-02-26T16:37:41.297

Best answer for now! – Ry- – 2012-02-27T16:02:02.933

By the same token, it's also the worst answer :) – Gordon Bailey – 2012-02-27T16:24:21.737

5

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...

ugoren

Posted 2012-02-25T21:13:43.777

Reputation: 16 527

Nice. I'll be curious to see the final stats with a longer input list, this may well be the best approach. – Gordon Bailey – 2012-02-27T23:51:26.123

4

require 'open-uri'

def p(w)
  open("http://en.wiktionary.org/wiki/#{w}"){|f| f.read}.match(/plural-form-of.*?title=\"(.*?)\"/)[1]
end

ruby at 135. It got all of the sample input (though it will eventually begin returning 403s if you type quickly enough).

Moose

Posted 2012-02-25T21:13:43.777

Reputation: 49

Nicely done. If it does return 403s, though... (automated testing, so typing speed is not an issue.) – Ry- – 2012-02-27T20:34:51.867

0

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.

Sylwester

Posted 2012-02-25T21:13:43.777

Reputation: 3 678

0

perl -mLingua::EN::Inflect=PL -pe's/.*/PL$&/e'

Lynn

Posted 2012-02-25T21:13:43.777

Reputation: 55 648