32

Are there any Java API(s) which will provide plural form of English words (e.g. cacti for cactus)?

pigrammer
  • 2,603
  • 1
  • 11
  • 24
Joe
  • 14,513
  • 28
  • 82
  • 144
  • 1
    For which natural languages, and which programming languages? –  May 06 '11 at 05:51
  • 3
    Also, the plural forms of cactus are cacti (single i) and cactuses according to the New Oxford American Dictionary. –  May 06 '11 at 05:56
  • We are looking at words in the English language and our programming environment is based on Java – Joe May 06 '11 at 05:59
  • 1
    I’m not sure whether there is a library for Java. In case there isn’t, a good starting point is the Lingua::EN::Inflect Perl module, available [here](http://search.cpan.org/~dconway/Lingua-EN-Inflect-1.893/lib/Lingua/EN/Inflect.pm). Hopefully someone’s ported Inflect to Java. –  May 06 '11 at 06:06

7 Answers7

55

Check Evo Inflector which implements English pluralization algorithm based on Damian Conway paper "An Algorithmic Approach to English Pluralization". The library is tested against data from Wiktionary and reports 100% success rate for 1000 most used English words and 70% success rate for all the words listed in Wiktionary.

If you want even more accuracy you can take Wiktionary dump and parse it to create the database of singular to plural mappings. Take into account that due to the open nature of Wiktionary some data there might by incorrect.

Example Usage:

English.plural("Facility", 1)); // == "Facility"
English.plural("Facility", 2)); // == "Facilities"
AlecBrooks
  • 524
  • 4
  • 12
Sławek
  • 1,450
  • 14
  • 14
6

jibx-tools provides a convenient pluralizer/depluralizer.

Groovy test:

NameConverter nameTools = new DefaultNameConverter();
assert nameTools.depluralize("apples") == "apple"
nameTools.pluralize("apple") == "apples"
Jonathan Schneider
  • 26,852
  • 13
  • 75
  • 99
  • do not recommend that, it does not even work for `mouse`, it converts the word to `mouses`, which is incorrect – idmitriev Sep 06 '18 at 06:39
2

I know there is simple pluralize() function in Ruby on Rails, maybe you could get that through JRuby. The problem really isn't easy, I saw pages of rules on how to pluralize and it wasn't even complete. Some rules are not algorithmic - they depend on stem origin etc. which isn't easily obtained. So you have to decide how perfect you want to be.

Gabriel Ščerbák
  • 18,240
  • 8
  • 37
  • 52
2

considering java, have a look at modeshapes Inflector-Class as member of the package org.modeshape.common.text. Or google for "inflector" and "randall hauch".

Sebastian Schmitt
  • 433
  • 1
  • 5
  • 18
  • I compare the library to jibx-tools. The library is better. In some words, for example `leaves`, jibx-tools is better. – diguage Mar 03 '18 at 01:26
1

If you can harness javascript, I created a lightweight (7.19 KB) javascript for this. Or you could port my script over to Java. Very easy to use:

pluralizer.run('goose') --> 'geese'
pluralizer.run('deer') --> 'deer'
pluralizer.run('can') --> 'cans'

https://github.com/rhroyston/pluralizer-js

BTW: It looks like cacti to cactus is a super special conversion (most ppl are going to say '1 cactus' anyway). Easy to add that if you want to. The source code is easy to read / update.

Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
1

Its hard to find this kind of API. rather you need to find out some websservice which can serve your purpose. Check this. I am not sure if this can help you.. (I tried to put word cacti and got cactus somewhere in the response).

Nirmit Shah
  • 758
  • 4
  • 10
0

Wolfram|Alpha return a list of inflection forms for a given word.

See this as an example:

http://www.wolframalpha.com/input/?i=word+cactus+inflected+forms

And here is their API:

http://products.wolframalpha.com/api/

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Meng Lu
  • 13,726
  • 12
  • 39
  • 47