根据某些集合大小处理单数/复数单词的有效方法

发布于 2024-09-08 02:30:36 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

回梦 2024-09-15 02:30:36

看一下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"), 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.

四叶草在未来唯美盛开 2024-09-15 02:30:36

嗯,我不太明白为什么你需要一个图书馆。我认为执行此操作的函数很简单:

public String singlePlural(int count, String singular, String plural)
{
  return count==1 ? singular : plural;
}

调用看起来像:

singlePlural(count, "user", "users");
singlePlural(count, "baby", "babies");
singlePlural(count, "person", "people");
singlePlural(count, "cherub", "cherubim");
... etc ...

也许这个库做了一大堆其他使它有用的事情。我想你可以说它提供了一本包含所有复数形式的字典,但在任何给定的程序中,你并不关心该语言中所有单词的复数,只关心你在这个程序中使用的单词。我想如果在编译时不知道可以是单数或复数的单词,如果它是用户输入的内容,那么我想要一本第三方词典,而不是尝试自己构建一个。

编辑

突然我想到你正在寻找的是一个通用的复数函数,体现了一组规则,例如“通常只添加'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:

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.

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.

爱本泡沫多脆弱 2024-09-15 02:30:36

在英语以外的语言中,这会变得复杂,该变形器旨在将来支持。

我熟悉捷克语,其中 user = uživatel 并且:

1 uživatel
2 uživatelé
3 uživatelé
4 uživatelé
5 uživatelů

...

您可以看到为什么用硬编码单数+复数编写的程序将无法实现国际化。


编辑:
Java11 允许您使用以下内容:

ChoiceFormat fmt = new ChoiceFormat("1#uživatel | 1.0< uživatelé | 4< uživatelů");
System.out.println(fmt.format(1));
System.out.println(fmt.format(4));
System.out.println(fmt.format(5));

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:

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.


Edit:
Java11 allows you to use the following:

ChoiceFormat fmt = new ChoiceFormat("1#uživatel | 1.0< uživatelé | 4< uživatelů");
System.out.println(fmt.format(1));
System.out.println(fmt.format(4));
System.out.println(fmt.format(5));

ChoiceFormat documentation

孤独患者 2024-09-15 02:30:36

此功能内置于 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文