Pluralization


#1

I got curious about Active Record playing tricks with pluralization, and
found a few places in my rig where it might be useful. If you want to
play
with it yourself, gem install localization, and you’ll get it. There’s
some
other cool stuff in there too!

Anyhow, I’m a bit stuck, or braindead. (actually both tonight) and could
use
some fresh ideas.

What I want to do is test a string to see if it is a plural, and if so
branch the code and do something else with it. Localization has a
method to
convert from singular to plural, and back again, but as far as I can
tell,
no way just to test plurality. Can you think of a way to do this test?
Here are some tests that should make my meaning clear.

require File.dirname(FILE) + ‘/…/test_helper’
require ‘linguistics’
Linguistics::use( :en)

class LinguisticsTest < Test::Unit::TestCase
def test_conversion
assert_equal(“mice”,“mouse”.en.plural)
end

def test_converting_plural_word
assert_equal(“mice”,“mice”.en.plural, “mice not recognized as
plural”)
end

def test_converting_plural_word_2
assert_equal(“cats”, “cats”.en.plural, “cats not recognized as
plural”)
end

#if there were a truth function, this would be it
def test_plural_truth
assert “cats”.en.plural?
end
end

============== RESULTS ==================

Loaded suite linguistics_test
Started
.FFE
Finished in 0.112234 seconds.

  1. Failure:
    test_converting_plural_word(LinguisticsTest) [linguistics_test.rb:12]:
    mice not recognized as plural.
    <“mice”> expected but was
    <“mices”>.

  2. Failure:
    test_converting_plural_word_2(LinguisticsTest) [linguistics_test.rb:16]:
    cats not recognized as plural.
    <“cats”> expected but was
    <“cat”>.

  3. Error:
    test_plural_truth(LinguisticsTest):
    NoMethodError: undefined method plural?' for <English languageProxy for String object "cats">:#<Class:0xb7627b44> /usr/lib/ruby/gems/1.8/gems/Linguistics-1.0.3/lib/linguistics.rb:90:inmethod_missing’
    linguistics_test.rb:21:in `test_plural_truth’

4 tests, 3 assertions, 2 failures, 1 errors


#2

there is a plurization paper and Perl program at
http://www.csse.monash.edu.au/~damian/papers/
(look for ‘English Plurals’ on the page).
You may be able to steal\h\h\h\h\hrecycle the algorithm
and make a ruby port.

matthew clark wrote:

method to convert from singular to plural, and back again, but as far as
I can tell, no way just to test plurality.

[snip]


#3

Hi,

On 12/7/05, matthew clark removed_email_address@domain.invalid wrote:

What I want to do is test a string to see if it is a plural, and if so
branch the code and do something else with it.

Not sure about the localization gem, but it’s pretty trivial using
Active Support’s inflector:

class String
def plural?
self == self.pluralize
end
end

‘cow’.plural? # => false
‘cows’.plural? # => true


sam