java 基于某些集合大小处理单复数词的有效方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3189432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Effective way to handle singular/plural word based on some collection size
提问by limc
There are many instances in my work projects where I need to display the size of some collection in a sentence. For example, if the collection's size is 5, it will say "5 users". If it is size of 1 or 0, it will say "1 user" or "0 user". Right now, I'm doing it with if-else statements to determine whether to print the "s" or not, which is tedious.
在我的工作项目中有很多实例需要在一个句子中显示某个集合的大小。例如,如果集合的大小为 5,它将显示“5 个用户”。如果大小为 1 或 0,则会显示“1 个用户”或“0 个用户”。现在,我正在使用 if-else 语句来确定是否打印“s”,这很乏味。
I'm wondering if there's an open source JSP custom tag library that allows me to accomplish this. I know I can write one myself... basically, it will have 2 parameters like this: <lib:display word="user" collection="userList" />. Depending on the collection size, it will determine whether to append an "s" or not. But then, this implementation is not going to be too robust because I also need to handle "ies" and some words don't use any of those. So, instead of creating a half-baked tool, I'm hoping there's a more robust library I could utilize right away. I'm not too worried about prefixing the word with is/are in this case.
我想知道是否有一个开源 JSP 自定义标记库可以让我完成这个任务。我知道我可以自己写一个...基本上,它将有 2 个参数,如下所示:<lib:display word="user" collection="userList" />. 根据集合大小,它将决定是否附加“s”。但是,这个实现不会太健壮,因为我还需要处理“ies”,而有些词不使用其中任何一个。因此,与其创建一个半生不熟的工具,我希望有一个我可以立即使用的更强大的库。在这种情况下,我不太担心在单词前加上 is/are。
I use Java, by the way.
顺便说一句,我使用Java。
Thanks much.
非常感谢。
回答by Jacob Mattison
Take a look at inflector, a java project which lets you do Noun.pluralOf("user"), or Noun.pluralOf("user", userList.size()), and which handles a bunch of variations and unusual cases (person->people, loaf->loaves, etc.), as well as letting you define custom mapping rules when necessary.
看看inflector,这是一个 Java 项目,它可以让你做Noun.pluralOf("user"), or Noun.pluralOf("user", userList.size()), 并处理一堆变化和异常情况(person->people, loaf->loaves 等),以及让你定义自定义映射规则必要时。
回答by Jay
Hmm, I don't quite see why you need a library for this. I would think the function to do it is trivial:
嗯,我不太明白为什么你需要一个图书馆。我认为这样做的功能是微不足道的:
public String singlePlural(int count, String singular, String plural)
{
return count==1 ? singular : plural;
}
Calls would look like:
调用看起来像:
singlePlural(count, "user", "users");
singlePlural(count, "baby", "babies");
singlePlural(count, "person", "people");
singlePlural(count, "cherub", "cherubim");
... etc ...
Maybe this library does a whole bunch of other things that make it useful. I suppose you could say that it supplies a dictionary of what all the plural forms are, but in any given program you don't care about the plurals of all the words in the language, just the ones you are using in this program. I guess if the word that could be singular or plural is not known at compile time, if it's something entered by the user, then I'd want a third party dictionary rather than trying to build one myself.
也许这个库做了一大堆其他使它有用的事情。我想你可以说它提供了所有复数形式的字典,但在任何给定的程序中,你不关心语言中所有单词的复数形式,只关心你在这个程序中使用的那些。我想如果在编译时不知道可以是单数还是复数的单词,如果它是用户输入的东西,那么我想要一个第三方字典而不是试图自己构建一个。
Edit
编辑
Suddenly it occurs to me that what you were looking for was a function for making plurals generically, embodying a set of rules like "normally just add 's', but if the word ends in 'y' change the 'y' to 'ies', if it ends in 's' change it to 'ses', ..." etc. I think in English that would be impossible for any practical purpose: there are too many special cases, like "person/people" and "child/children" etc. I think the best you could do would be to have a generic "add an 's'" rule, maybe a few other common cases, and then a long list of exceptions. Perhaps in other languages one could come up with a fairly simple rule.
我突然想到你要找的是一个通用的复数形式的函数,体现了一组规则,比如“通常只添加 's',但如果单词以 'y' 结尾,请将 'y' 更改为 'ies ', 如果它以 's' 结尾,则将其更改为 'ses', ..." 等等。我认为在英语中这对于任何实际目的都是不可能的:有太多特殊情况,例如 "person/people" 和 "孩子/儿童”等。我认为你能做的最好的事情就是有一个通用的“添加一个's'”规则,也许是其他一些常见的情况,然后是一长串例外。也许在其他语言中,人们可以提出一个相当简单的规则。
So as I say, if the word is not known at compile time but comes from some user input, then yes, a third-party dictionary is highly desirable.
因此,正如我所说,如果该词在编译时未知,而是来自某些用户输入,那么是的,非常需要第三方词典。
回答by Roman Zenka
This gets complicated in languages other than English, that inflector aims to support in the future.
这在英语以外的语言中变得复杂,该 inflector 旨在在未来支持。
I am familiar with Czech where user = u?ivatel and:
我熟悉捷克语,其中 user = u?ivatel 和:
1 u?ivatel
2 u?ivatelé
3 u?ivatelé
4 u?ivatelé
5 u?ivatel?
...
...
You can see why programs written with hardcoded singular+plural would get un-i18n-able.
您可以看到为什么用硬编码的单数+复数编写的程序会变得不支持 i18n。
回答by Carl Smotricz
This functionality is built into Ruby on Rails. I don't know exactly where, but it should be easy enough to find in the source code, and then you could simply crib the code.
此功能内置于 Ruby on Rails 中。我不知道确切的位置,但是在源代码中应该很容易找到,然后您可以简单地编写代码。
EDIT: Found you some code:
编辑:找到你一些代码:
- inflector.rb(very helpful comments!)
- inflections.rb(extensive word list)
- inflector.rb(非常有用的评论!)
- inflections.rb(广泛的单词列表)
If I remember correctly, it's mainly a matter of appending an "s" to most words, though I believe there is a list (probably hash, err dictionary) of some common exceptions. Notable is the conversion from "person" to "people" :)
如果我没记错的话,这主要是在大多数单词后附加一个“s”,尽管我相信有一些常见例外的列表(可能是哈希,错误字典)。值得注意的是从“人”到“人”的转换:)
You would of course be in for a world of pain if you decided you want to internationalize this to other languages than English. Welcome to the world of highly irregular grammars, and good luck!
如果您决定要将其国际化为英语以外的其他语言,您当然会陷入痛苦的世界。欢迎来到高度不规则语法的世界,祝你好运!

