piotrmurach / strings-inflection

Convert between singular and plural forms of English nouns
MIT License
31 stars 3 forks source link

singularize Returns Incorrect Values #1

Open sshaw opened 4 years ago

sshaw commented 4 years ago
irb [2.3.7] (class2)$ Strings::Inflection.singularize("a")  # expect "a"
=> "on"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("gas")  # expect "gas"
=> "ga"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("gas", term: :verb) # expect "gas"
=> "gasses"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("la")   # expect "la"
=> "lon"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("ala")   # expect "ala"
=> "alon"
irb [2.3.7] (class2)$ Strings::Inflection.singularize("ss")  # expect "s"
=> "s"

Using a lot of .* for regexes. Well at least in the case of "a" this is certainly the problem.

Also not sure if this is a feature or bug:

irb [2.3.7] (class2)$ Strings::Inflection.singularize("cars\ntrucks\nbikes")
=> "car\ntrucks\nbikes"

I think bug but saw you were using \A and $ instead of \A and \z so just asking.

piotrmurach commented 4 years ago

Thanks for sharing these!

The gem could do with 'tightening up' some regexes and rules to keep such edge cases at bay. I guess the single characters can be dealt with relatively easily. With others, I don't know, tweaking the rules is rather hard. It may be that some of the rules could do with replacing .* for .+. Do you have time to contribute a PR?

Just curious how did you come about these? Are you actively using the gem in your project? Always keen to get feedback.

The intention was for the methods to accept a single word.

The methods could be extended to accept a variable number of nouns/verbs but I would be less inclined to support chunks of text with newline delimiters and such. So this would be my preferred approach:

Strings::Inflection.singularize("cars", "trucks", "bikes")
sshaw commented 4 years ago

Do you have time to contribute a PR?

I'm not opposed to a good ol' fashioned PR. Feel free to remind me in 2 months if I don't do it!

Just curious how did you come about these? Are you actively using the gem in your project?

Trying, to use it in my project: class2. Currently it uses ActiveSupport for these sorts of things and I'd like to add the word "lightweight" to class2's docs but this would be a programming crime considering it's using ActiveSupport!

The branch with your gems is here.

The intention was for the methods to accept a single word.

I don't want more than 1 I just noticed that your regex was matching on end of line so wasn't sure if it was a bug or a feature.