C #에 단 수화-단어를 복수화하는 알고리즘이 있습니까?
C #에 단어를 복수화 (영어로)하는 알고리즘이 있습니까? 아니면이를 수행하기위한 .net 라이브러리가 있습니까 (다른 언어로도 가능)?
System.Data.Entity.Design.PluralizationServices.PluralizationService 도 있습니다 .
업데이트 : 이전 답변은 업데이트 할 가치가 있습니다. 이제 Humanizer도 있습니다 : https://github.com/MehdiK/Humanizer
특별한 경우없이 에스페란토로 할 수 있습니다!
string plural(string noun) { return noun + "j"; }
영어의 경우 정규 복수형 명사 및 불규칙 복수형 명사 규칙에 익숙해지는 것이 유용 할 것 입니다. 영어 복수형 에 대한 전체 Wikipedia 기사가 있으며,이 기사 에는 유용한 정보도있을 수 있습니다.
대부분의 ORM은 일반적으로 완벽하지는 않지만 찌르는 경향이 있습니다. 캐슬에는 아마 당신이 찌를 수있는 인플 렉터 클래스 가 있다는 것을 알고 있습니다. "완벽하게"하는 것은 쉬운 일이 아닙니다 (영어 "규칙"은 실제로 규칙이 아닙니다 :)). 따라서 "합리적인 추측"접근 방식에 만족하는지 여부에 따라 다릅니다.
나는 자바에서 속임수를 썼다. "There were n something (s)"에 대한 올바른 문자열을 생성하고 싶었 기 때문에 다음 글을 썼다. 약간의 과부하 유틸리티 방법 :
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));
}
그렇게 호출
System.out.println("There were "+pluralize(count,"something"));
System.out.println("You have broken "+pluralize(count,"knife","knives"));
.net (C #)에서 Pluralizer (당연히)라는 작은 라이브러리를 만들었습니다.
String.Format과 같이 전체 문장으로 작업하기위한 것입니다.
기본적으로 다음과 같이 작동합니다.
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);
그것은 또한 그 이상을 할 수 있습니다. 내 블로그에서 자세한 내용을 읽어보십시오 . NuGet에서도 사용할 수 있습니다.
Rails pluralizer를 기반으로 하나를 채찍질했습니다. 여기 에서 내 블로그 게시물을 보거나 여기 github에서 볼 수 있습니다.
output = Formatting.Pluralization(100, "sausage");
질문이 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;
}
사용법은 다음과 같습니다.
"Item".Pluralize(1) = "Item"
"Item".Pluralize(2) = "Items"
"Person".Pluralize(1, "People") = "Person"
"Person".Pluralize(2, "People") = "People"
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.
Here's a snippet:
AddSingularRule("^(ox)en", "$1");
AddSingularRule("(vert|ind)ices$", "$1ex");
AddSingularRule("(matr)ices$", "$1ix");
AddSingularRule("(quiz)zes$", "$1");
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.
Update:
It appears Subsonic's Inflector
is infact the Castle ActiveRecord Inflector
class!
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.
[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
}
}
This page shows how to use PluralizationService
of System.Data.Entity
(.NET Framework 4.0)
http://zquanghoangz.blogspot.it/2012/02/beginner-with-pluralizationservices.html
Using Microsoft's Northwind example database:
System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new System.Globalization.CultureInfo("en-US"));
Singularize does not Singularize "Order_Details" It returns "Order_Details" with the s
at the end. What is the work around?
'Programing' 카테고리의 다른 글
Git 브랜치를 닫는 방법? (0) | 2020.08.16 |
---|---|
문자열에 숫자 만 포함되어 있는지 파이썬에서 어떻게 확인합니까? (0) | 2020.08.16 |
단순 HTTP 서버에서 액세스 제어 사용 (0) | 2020.08.16 |
GoogleTest : 테스트를 건너 뛰는 방법? (0) | 2020.08.16 |
Rails 애플리케이션의 쿠키 오버플로? (0) | 2020.08.16 |