Matching plurals using regex in C#

僤鯓⒐⒋嵵緔 提交于 2019-12-08 16:58:57

问题


I'm looking to use regex in C# to search for terms and I want to include the plurals of those terms in the search. For example if the user wants to search for 'pipe' then I want to return results for 'pipes' as well.

So I can do this...

string s ="\\b" + term + "s*\\b";
if (Regex.IsMatch(bigtext, s) {  /* do stuff */ }

How would I modify the above to allow me to match, say, 'stresses' when the user enters 'stress' and still work for 'pipe'/'pipes'?


回答1:


Here's a regex created to remove the plurals:

 /(?<![aei])([ie][d])(?=[^a-zA-Z])|(?<=[ertkgwmnl])s(?=[^a-zA-Z])/g

(Demo & source)

I know it's not exactly what you need, but it may help you find something out.




回答2:


The problem you can face is that there are a lot of irregular nouns such as man, fish and index. So you should consider using the PluralizationService that has a Pluralize method. Here is an example that shows how to use it.

After you get the plural of the term, you can easily construct a regex that searches for both the plural or the singular term.

PluralizationService ps = PluralizationService.CreateService(CultureInfo.CurrentCulture);
string plural = ps.Pluralize(term);
string s = @"("+term+"|"+plural+")";
if (Regex.IsMatch(bigtext, s)) {
    /* do stuff */
}



回答3:


If you are using SQL server as your backend couldn't you utilize Soundex? I am unsure what you are trying to search for. I assume you are trying to create dynamic SQL as search input. If not I think there is SoundEx for LINQ.

EDIT: I stand corrected, it appears there is some linq to sql entity stuff that can be done for SoundEx.

However, MSDN does have a soundex example, which for the simple tests I ran this morning seems to do fine as far as what I tested. http://msdn.microsoft.com/en-us/library/bb669073.aspx

The change I made was instead of .ToUpper(invariant) i used .ToUpperInvariant() and instead of passing (string word) i used an extension method (this string word)

Here is an example of what I ran

List<string> animals = new List<string>();
animals.Add("dogs");
animals.Add("dog");
animals.Add("cat");
animals.Add("rabbits");
animals.Add("doggie");

string dog = "dog";
var data = from animal in animals
where animal.SoundEx() == dog.SoundEx()
select animal;

data : dogs, dog, doggie

Now with SQL server, using the Contains/FreeText/ContainsTable etc and using SoundEx against a catalog (I am not familiar with the newer versions of SQL server - going back to SQLServer 2000 implementation I used), you could also rank your results.

Also if you have the ability to use sql server you may want to look into this option: LINQ to SQL SOUNDEX - possible?

The concern with the Pluralization solution, you must be able to utilize .Net 4.

There is also the Levenshtein distance algorithm that may be useful.



来源:https://stackoverflow.com/questions/10297047/matching-plurals-using-regex-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!