c# 中是否有任何算法可以将单词单数化 - 将单词复数化?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/475705/
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
Is there any algorithm in c# to singularize - pluralize a word?
提问by Ronnie
Is there any algorithm in c# to singularize - pluralize a word (in english) or does exist a .net library to do this (may be also in different languages)?
c# 中是否有任何算法可以单数化 - 将一个单词(英语)复数化,或者确实存在一个 .net 库来做到这一点(也可能是不同的语言)?
采纳答案by Daniel
You also have the System.Data.Entity.Design.PluralizationServices.PluralizationService.
您还有System.Data.Entity.Design.PluralizationServices.PluralizationService。
UPDATE: Old answer deserves update. There's now also Humanizer: https://github.com/MehdiK/Humanizer
更新:旧答案值得更新。现在还有 Humanizer:https: //github.com/MehdiK/Humanizer
回答by Steven Robbins
Most ORMs have a stab at it, although they generally aren't perfect. I know Castle has it's Inflector Classyou can probably poke around. Doing it "perfectly" isn't an easy task though (English "rules" aren't really rules :)), so it depends if you are happy with a "reasonable guess" approach.
大多数 ORM 都对其进行了尝试,尽管它们通常并不完美。我知道 Castle 有它的Inflector 类,你可以四处看看。尽管“完美地”做到这一点并不是一件容易的事(英语“规则”并不是真正的规则:)),因此这取决于您是否对“合理猜测”方法感到满意。
回答by Greg Hewgill
I can do it for Esperanto, with no special cases!
我可以为世界语做到这一点,没有特殊情况!
string plural(string noun) { return noun + "j"; }
For English, it would be useful to become familiar with the rules for Regular Plurals of Nouns, as well as Irregular Plurals of Nouns. There is a whole Wikipedia article on the English plural, which may have some helpful information too.
对于英语,这将是有益的,熟悉的规则名词的复数定期,以及名词的不规则复数。维基百科有一整篇关于英语复数的文章,其中可能也有一些有用的信息。
回答by Lawrence Dol
I cheated in Java - I wanted to be able to produce a correct string for "There were n something(s)", so I wrote the foll. little overloaded utility method:
我在 Java 中作弊 - 我希望能够为“There are n something(s)”生成正确的字符串,所以我写了 foll. 小重载实用方法:
static public String pluralize(int val, String sng) {
return pluralize(val,sng,(sng+"s"));
}
static public String pluralize(int val, String sng, String plu) {
return (val+" "+(val==1 ? sng : plu));
}
invoked like so
像这样调用
System.out.println("There were "+pluralize(count,"something"));
System.out.println("You have broken "+pluralize(count,"knife","knives"));
回答by Chris S
Subsonic 3 has an Inflector
class which impressed me by turning Person
into People
. I peeked at the source and found it naturally cheats a little with a hardcoded list but that's really the only way of doing it in English and how humans do it - we remember the singular and plural of each word and don't just apply a rule. As there's not masculine/feminine(/neutral) to add to the mix it's a lot simpler.
Subsonic 3 有一Inflector
门课让我印象深刻,因为它变成Person
了People
. 我偷看了源代码,发现它自然会用硬编码列表作弊,但这确实是用英语和人类如何做的唯一方法 - 我们记住每个单词的单数和复数,而不仅仅是应用规则. 由于没有男性/女性(/中性)添加到混合中,因此要简单得多。
Here's a snippet:
这是一个片段:
AddSingularRule("^(ox)en", "");
AddSingularRule("(vert|ind)ices$", "ex");
AddSingularRule("(matr)ices$", "ix");
AddSingularRule("(quiz)zes$", "");
AddIrregularRule("person", "people");
AddIrregularRule("man", "men");
AddIrregularRule("child", "children");
AddIrregularRule("sex", "sexes");
AddIrregularRule("tax", "taxes");
AddIrregularRule("move", "moves");
AddUnknownCountRule("equipment");
It accounts for some words not having plural equivalents, like the equipment example. As you can probably tell it does a simple Regex
replace using $1.
它解释了一些没有复数等价物的单词,例如设备示例。正如您可能知道的那样,它Regex
使用 $1 进行了简单的替换。
Update:
It appears Subsonic's Inflector
is infact the Castle ActiveRecord Inflector
class!
更新:
看起来 SubsonicInflector
实际上是Castle ActiveRecordInflector
类!
回答by Matt Grande
I whipped one together based on the Rails pluralizer. You can see my blog post here, or on github here
我根据 Rails 复数词组合了一个。你可以在这里看到我的博客文章,或者在这里的 github 上
output = Formatting.Pluralization(100, "sausage");
回答by Jay Querido
I've created a tiny library for this in .net (C#), called Pluralizer (unsurprisingly).
我在 .net (C#) 中为此创建了一个小库,称为 Pluralizer(不出所料)。
It's meant to work with full sentences, something like String.Format does.
它旨在处理完整的句子,就像 String.Format 那样。
It basically works like this:
它基本上是这样工作的:
var target = new Pluralizer();
var str = "There {is} {_} {person}.";
var single = target.Pluralize(str, 1);
Assert.AreEqual("There is 1 person.", single);
// Or use the singleton if you're feeling dirty:
var several = Pluralizer.Instance.Pluralize(str, 47);
Assert.AreEqual("There are 47 people.", several);
It can also do way more than that. Read more about it on my blog. It's also available in NuGet.
回答by Zaid Masud
As the question was for C#, here is a nice variation on Software Monkey's solution (again a bit of a "cheat", but for me really the most practical and reusable way of doing this):
由于问题是针对 C# 的,这里是 Software Monkey 解决方案的一个很好的变体(再次有点“作弊”,但对我来说确实是最实用和可重用的方法):
public static string Pluralize(this string singularForm, int howMany)
{
return singularForm.Pluralize(howMany, singularForm + "s");
}
public static string Pluralize(this string singularForm, int howMany, string pluralForm)
{
return howMany == 1 ? singularForm : pluralForm;
}
The usage is as follows:
用法如下:
"Item".Pluralize(1) = "Item"
"Item".Pluralize(2) = "Items"
"Person".Pluralize(1, "People") = "Person"
"Person".Pluralize(2, "People") = "People"
回答by riofly
This page shows how to use PluralizationService
of System.Data.Entity
(.NET Framework 4.0)
这个页面展示如何使用PluralizationService
的System.Data.Entity
(.NET Framework 4.0中)
http://zquanghoangz.blogspot.it/2012/02/beginner-with-pluralizationservices.html
http://zquanghoangz.blogspot.it/2012/02/beginner-with-pluralizationservices.html
回答by Ryan Rodemoyer
Not much documentation from MSDN on the specific usage of the PluralizationService class so here is a unit test class (NUnit) to show basic usage. Notice the odd test case at the bottom that shows the service isn't perfect when it comes to non-standard plural forms.
MSDN 中关于 PluralizationService 类的具体用法的文档不多,所以这里有一个单元测试类 (NUnit) 来显示基本用法。请注意底部的奇怪测试用例,它表明该服务在涉及非标准复数形式时并不完美。
[TestFixture]
public class PluralizationServiceTests
{
[Test]
public void Test01()
{
var service = PluralizationService.CreateService(CultureInfo.CurrentCulture);
Assert.AreEqual("tigers", service.Pluralize("tiger"));
Assert.AreEqual("processes", service.Pluralize("process"));
Assert.AreEqual("fungi", service.Pluralize("fungus"));
Assert.AreNotEqual("syllabi", service.Pluralize("syllabus")); // wrong pluralization
}
}