We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 9 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
看一下inflector,一个java项目,它可以让你做
Noun.pluralOf("user ")
或Noun.pluralOf("user", userList.size())
,它处理一堆变化和异常情况(person->people、loaf-> ;面包等),并允许您在必要时定义自定义映射规则。Take a look at inflector, a java project which lets you do
Noun.pluralOf("user")
, orNoun.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.嗯,我不太明白为什么你需要一个图书馆。我认为执行此操作的函数很简单:
调用看起来像:
也许这个库做了一大堆其他使它有用的事情。我想你可以说它提供了一本包含所有复数形式的字典,但在任何给定的程序中,你并不关心该语言中所有单词的复数,只关心你在这个程序中使用的单词。我想如果在编译时不知道可以是单数或复数的单词,如果它是用户输入的内容,那么我想要一本第三方词典,而不是尝试自己构建一个。
编辑
突然我想到你正在寻找的是一个通用的复数函数,体现了一组规则,例如“通常只添加's',但如果单词以'y'结尾将“y”更改为“ies”,如果以“s”结尾,则将其更改为“ses”,...“等等。我认为在英语中,这对于任何实际目的都是不可能的:有太多特殊情况,像“人/人”和“孩子/孩子”等。我认为你能做的最好的事情就是有一个通用的“添加一个's'”规则,也许还有一些其他常见情况,然后是一长串例外情况。也许在其他语言中,人们可以想出一个相当简单的规则。
正如我所说,如果该单词在编译时未知,但来自某些用户输入,那么是的,非常需要第三方词典。
Hmm, I don't quite see why you need a library for this. I would think the function to do it is trivial:
Calls would look like:
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.
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.
在英语以外的语言中,这会变得复杂,该变形器旨在将来支持。
我熟悉捷克语,其中 user = uživatel 并且:
...
您可以看到为什么用硬编码单数+复数编写的程序将无法实现国际化。
编辑:
Java11 允许您使用以下内容:
ChoiceFormat 文档
This gets complicated in languages other than English, that inflector aims to support in the future.
I am familiar with Czech where user = uživatel and:
...
You can see why programs written with hardcoded singular+plural would get un-i18n-able.
Edit:
Java11 allows you to use the following:
ChoiceFormat documentation
此功能内置于 Ruby on Rails 中。我不知道具体在哪里,但应该很容易在源代码中找到,然后您可以简单地抄写代码。
编辑:找到你一些代码:
如果我没记错的话,这主要是在大多数单词后附加“s”的问题,尽管我相信有一些常见例外的列表(可能是散列,错误字典)。值得注意的是从“person”到“people”的转换:)
如果您决定将其国际化为英语以外的其他语言,您当然会陷入痛苦的世界。欢迎来到高度不规则语法的世界,祝你好运!
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.
EDIT: Found you some code:
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" :)
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!