strings-inflection icon indicating copy to clipboard operation
strings-inflection copied to clipboard

Irregular plural words: plural? and plural both fail (people => peoples)

Open DannyBen opened this issue 5 years ago • 7 comments
trafficstars

Of course you also have a gem for string inflection...

You are my one stop shop for "gem supplies" 🛒

Problem

Strings::Inflection.plural? on irregular plurals fails.

Reproduction

require 'strings-inflection'
require 'strings/inflection/extensions'
using Strings::Inflection::Extensions

# These fail
p 'people'.pluralize == 'people' # => false
p 'people'.plural? == true       # => false
p 'children'.plural? == true     # => false

# These are all ok, just here as a "full test suite"
p 'person'.singular? == true    
p 'child'.singular? == true     
p 'people'.singular? == false   
p 'children'.singular? == false 
p 'person'.plural? == false     
p 'child'.plural? == false

Additional information

The underlying root cause of this problem, is that the gem returns "peoples" as the plural form of people, which is wrong. In ActiveSupport Inflector, it is returned properly:

>> require 'active_support/inflector'
=> true
>> "people".singularize
=> "person"
>> "people".pluralize
=> "people"

DannyBen avatar May 15 '20 15:05 DannyBen

Thanks for reporting this! This gem is part of the strings family that I'm trying to put together. It's relatively young so still has some rough edges and growing to do 😉

Do you have time to look into this? There are regexes for irregular nouns that need tweaking. Or a better method to detect singular vs plural. Any help appreciated.

piotrmurach avatar May 15 '20 15:05 piotrmurach

I will take a look.

DannyBen avatar May 15 '20 15:05 DannyBen

Well - I found the problem, I have no idea how to untangle it.

  1. The #plural? method compares the word to its plural form:

https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/term.rb#L64-L68

  1. The #plural of people is currently returned as peoples, which is not equal to the input word people.

  2. The Noun#plural method, uses Nouns#plurals - the condition below returns peoples when hitting find_match(Nouns.plurals)

https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/noun.rb#L50-L55

  1. This is due to the "catch all" rule in the @plural_rules, which just adds s to everything.

https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/nouns.rb#L679

DannyBen avatar May 15 '20 16:05 DannyBen

I think that this inflector is being more general here, thinking of it as in "the Inuit people" versus "the indigenous peoples of the Arctic region". I admit that's quite a stretch. Perhaps you need to add an inflector rule for that word. Perhaps ActiveSupport already does this and we're not used to seeing the need.

Walter

On May 15, 2020, at 12:25 PM, Danny Ben Shitrit [email protected] wrote:

Well - I found the problem, I have no idea how to untangle it.

• The #plural? method compares the word to its plural form: https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/term.rb#L64-L68

• The #plural of people is currently returned as peoples, which is not equal to the input word people.

• The Noun#plural method, uses Nouns#plurals - the condition below returns peoples when hitting find_match(Nouns.plurals)

https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/noun.rb#L50-L55

• This is due to the "catch all" rule in the @plural_rules, which just adds s to everything. https://github.com/piotrmurach/strings-inflection/blob/b51b34d1a49853653303725122fbcc0f3ede9233/lib/strings/inflection/nouns.rb#L679

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

walterdavis avatar May 15 '20 16:05 walterdavis

The root cause of this issue is that the gem does not properly handle pluralization of already pluralized irregular words. I have updated the issue description with more info.

DannyBen avatar May 15 '20 16:05 DannyBen

Thanks, @DannyBen for investigating. I think the root cause is not necessarily pluralization of plural words, but more precise, pluralization of irregular plural words.

Strings::Inflection.pluralize("words") # => "words"
Strings::Inflection.pluralize("mice") # => "mices"

As far as I can see the solution is to add irregular plurals that are already plural to the list:

[/people\z/, "people"]

@walterdavis It's not that sophisticated 😄 Though it's already much more powerful that ActiveSupport, it handles plenty irregular forms:

Strings::Inflection.pluralize("goose") # => "geese"
ActiveSupport::Inflector.pluralize("goose") #=> "gooses"

piotrmurach avatar May 15 '20 17:05 piotrmurach

Sorry for closing - pressed mouse somehow with a pointer on 'close and comment'...

piotrmurach avatar May 15 '20 17:05 piotrmurach