【问题标题】:How can we perform reverse stemming in C# using any stemming algorithm?我们如何使用任何词干算法在 C# 中执行反向词干提取?
【发布时间】:2017-06-14 13:24:26
【问题描述】:

是否有任何算法可以执行任何 Stemmers 算法的逆运算。即给定一个词干“require”,如何找到所有词干为“require”的词?

我们总是会发现相同单词的变体,例如要求,要求,要求,要求。都有“要求”的共同点。如果我们能加入一个功能来识别所有带有“require”词干的单词,那就太好了。

我们尝试了 Stemmers 算法,结果如下:

  • 例外 - 除外
  • 没有 - 没有
  • 防止 - 防止
  • 临床 - 临床
  • 披露,-披露
  • 收集,-收集,

我们已经尝试了如下代码:

 class Program
{
    static void Main(string[] args)
    {
        string strStemPhrase = @"generate generates generated generating generously";

        string result = Regex.Replace(strStemPhrase, @"[\W_]+", " ");

        string[] strStemmedWords = result.Split(new[] { " " }, StringSplitOptions.None);

        TestStemmer(new EnglishStemmer(), strStemmedWords);

        Console.ReadKey();
        return;           
    }

    private static void TestStemmer(IStemmer stemmer, params string[] words)
    {
        Console.WriteLine("Stemmer: " + stemmer);

        foreach (string word in words)
        {
            Console.WriteLine(word + " --> " + stemmer.Stem(word));
        }
    }
    }

【问题讨论】:

  • "我们已经尝试了如下代码:"...但是?你有什么问题?代码有效吗?
  • 是的,这段代码是有效的......但我的问题是“如何找到所有带有词干“require”的单词?”

标签: c# algorithm stemming porter-stemmer


【解决方案1】:

我建议使用字典。假设您有一组英语单词:

  string[] EnglishWords = new string[] {
    "a", 
    "abacus",
     ...
    "generate", 
    "generated",
    "generates",
    "generating",
    "generously",
     ...
    "zymotic",
  }; 

然后你可以建立一个字典:

  // key: stem
  // value: array of the original words  
  Dictionary<string, string[]> reversed = EnglishWords
    .GroupBy(word => Stem(word)) //TODO: put stemming here
    .ToDictionary(chunk => chunk.Key,
                  chunk => chunk.ToArray(),
                  StringComparer.OrdinalIgnoreCase);

stem你可以轻松找到原话:

  string stem = "require";

  string[] words;

  if (reversed.TryGetValue(stem, out words)) {
    //TODO: put relevant code here 
  } 

【讨论】:

    猜你喜欢
    • 2017-10-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多