atteo / evo-inflector

Singular to plural english word converter
Apache License 2.0
341 stars 44 forks source link

Singularization #8

Open cristcost opened 9 years ago

cristcost commented 9 years ago

Would be interesting to have the capability to transform plurals to singular:

System.out.println(English.singular("words")); // == "word"

System.out.println(English.singular("words", 1)); // == "word"
System.out.println(English.singular("words", 2)); // == "words"
yuriy-yarosh commented 8 years ago

@cristcost +1

mhyeon-lee commented 8 years ago

👍

sndmails2arun commented 8 years ago

+1. My data values are > 1 almost all the time and I occasionally hit singular values

mikeholler commented 8 years ago

I agree. I downloaded this library because I assumed it would support singularization too. Sadly, I was mistaken. This project has a lot of potential though!

maqdev commented 8 years ago

If anyone is interested, I've ported this to Scala and implemented singularization here: https://github.com/hypertino/inflector To use it add dependency:

<dependency>
    <groupId>com.hypertino</groupId>
    <artifactId>inflector_2.11</artifactId>
    <version>1.0.0</version>
</dependency>

And make a call: com.hypertino.inflector.English.singular("cats") - returns 'cat'

ctmay4 commented 6 years ago

I realize this is an old issue, but did anyone ever find a way to accomplish this? I saw the Scala port, but would prefer to keep the libraries I use pure Java. In addition when I tried pumping many values through the Scala port I got exceptions. For example:

System.out.println(English.singular("days"));

produces this exception

java.lang.IllegalArgumentException: Illegal group reference
    at java.base/java.util.regex.Matcher.appendExpandedReplacement(Matcher.java:1068) ~[na:na]
    at java.base/java.util.regex.Matcher.appendReplacement(Matcher.java:998) ~[na:na]
    at java.base/java.util.regex.Matcher.replaceFirst(Matcher.java:1408) ~[na:na]
    at scala.util.matching.Regex.replaceFirstIn(Regex.scala:516) ~[scala-library-2.11.8.jar:na]
    at com.hypertino.inflector.RegExpRule.getSingular(TwoFormInflector.scala:46) ~[inflector_2.11-1.0.0.jar:1.0.0]
    at com.hypertino.inflector.TwoFormInflector$$anonfun$singular$1.apply(TwoFormInflector.scala:88) ~[inflector_2.11-1.0.0.jar:1.0.0]
    at com.hypertino.inflector.TwoFormInflector$$anonfun$singular$1.apply(TwoFormInflector.scala:88) ~[inflector_2.11-1.0.0.jar:1.0.0]
    at scala.collection.immutable.Stream.flatMap(Stream.scala:493) ~[scala-library-2.11.8.jar:na]
    at com.hypertino.inflector.TwoFormInflector.singular(TwoFormInflector.scala:88) ~[inflector_2.11-1.0.0.jar:1.0.0]
    at com.hypertino.inflector.English.singular(English.scala) ~[inflector_2.11-1.0.0.jar:1.0.0]

Many other words do work so I don't know what is wrong with that.

maqdev commented 6 years ago

@ctmay4 thanks, I fixed that error in latest version 1.0.11, if you find any other issue please report to https://github.com/hypertino/inflector/issues/new