单词词干分析器类 C#



我正在尝试以下词干类:

static class StemmerSteps
{
    public static string stepSufixremover(this string str, string suffex)
    {
        if (str.EndsWith(suffex))
        {
            ................
        }
        return str;
    } 
    public static string stepPrefixemover(this string str, string prefix)
    {
        if (str.StartsWith(prefix) 
        {
            .....................
        }
        return str;
    }
}

此类使用一个前缀或后缀。 是否有任何建议允许前缀或后缀列表遍历类并与每个 (str) 进行比较。 您的善举真的很感激。

与其从头开始创建自己的类(除非这是家庭作业),我会最终使用现有的库。此答案提供了实现波特词干分析算法的代码示例:

https://stackoverflow.com/questions/7611455/how-to-perform-stemming-in-c

将后缀/前缀放在集合中(如List<>),然后遍历并应用每个可能的后缀/前缀。 此集合需要传递到方法中。

List<string> suffixes = ...;
for (suffix in suffixes)
    if (str.EndsWith(suffix))
        str = str.Remove(str.Length - suffix.Length, suffix.Length);

编辑

考虑您的评论:

"只是想看看字符串是否以任何传递的字符串开头/结尾"

可能这样的东西可以满足您的需求:

public static string stepSufixremover(this string str, IEnumerable<string> suffex)
{           
   string suf = suffex.Where(x=>str.EndsWith(x)).SingleOrDefault();
   if(!string.IsNullOrEmpty(suf))
   {            
    str = str.Remove(str.Length - suf.Length, suf.Length);
   }
   return str;
} 

如果您像这样使用它:

"hello".stepone(new string[]{"lo","l"}).Dump();

它产生:

hel

最简单的代码将涉及正则表达式。

例如,这将标识一些英语后缀:

'^(.*?)(ing|ly|ed|ious|ies|ive|es|s|ment)?$'

一个问题是词干提取不如词形还原准确。Lematization需要POS标记以确保准确性。例如,如果 -ing 后缀是名词,则不希望将其添加到 dove

另一个问题是某些后缀也需要前缀。例如,您必须将 en- 添加到 -rich- 才能在 en-rich-ment 中添加 -ment 后缀 - 与 -govern 等根不同,您可以在其中添加没有任何前缀的后缀。

最新更新