Pluralize - .NET单词转复数形式

微软Entity Framework中,包含一个单词单数转复数形式的函数,简单易用。

Pluralize - .NET单词转复数形式

1 Pluralize

1.1 API

1
2
3
System.Data.Entity.Design.PluralizationServices.
PluralizationService.CreateService(new CultureInfo("en")).
Pluralize(englishWord);

1.2 Demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Globalization;

namespace Pluralize
{
class Program
{
static void Main(string[] args)
{
string str_out, str_in;

while (true)
{
str_in = Console.ReadLine();
str_out = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new CultureInfo("en")).Pluralize(str_in);

Console.WriteLine("plural form: " + str_out);
}
}
}
}

  • 注意:reference中默认不包含System.Data.Entity.Design,需要手动在solution下的References中,Add Reference...执行添加

运行测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
process

plural form: processes

datum

plural form: data

fly

plural form: flies

ability

plural form: abilities

person

plural form: people

  • 可以看出,Pluralize函数的转换能力强大,各种复数形式变形均有覆盖。
  • 不规则变形,如:datum => data, person => people,均正确转换。应该有词典型数据结构作为转换支撑。

1.3 encapsulation

1
2
3
4
5
6
7
8
9
10
11
/// <summary>
/// return plural form of the specified word
/// </summary>
/// <param name="word"></param>
/// <returns></returns>
public static string PluralFormat(string word)
{
return System.Data.Entity.Design.PluralizationServices.
PluralizationService.CreateService(new CultureInfo("en")).
Pluralize(word);
}

2 Reference

C# 将英文转为复数形式 - CSDN01postpublish0111005090c221a9417a4bf0afa8a7ac91d5f