Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计算词干后的词频_C#_C# 4.0 - Fatal编程技术网

C# 计算词干后的词频

C# 计算词干后的词频,c#,c#-4.0,C#,C# 4.0,假设我有以下字符串: "present present present presenting presentation do do doing " 我在按频率降序计算字符串中的单词: I'm using GroupBy count present 3 do 2 doing 1 presenting 1 presentation 1 然后,我用词干表示: using array [ , ] or any other structure present

假设我有以下字符串:

"present present present presenting presentation do  do doing " 
我在按频率降序计算字符串中的单词:

I'm using GroupBy count 
present    3
do         2
doing      1
presenting 1
presentation 1
然后,我用词干表示:

using array [ , ] or any other structure

present  3
do       2
do       1
present  1
present  1
最后,我想把这些单词重新编入词典。因此,输出应为:

present 5
do      3

有人能帮忙吗??。提前谢谢

LINQ GroupBy或Aggregate是计算此类计数的好方法

如果你想用手做的话。。。看起来您希望有两组结果:一个是无词干单词,另一个是词干单词:

void incrementCount(Dictionary<string, int> counts, string word)
{
  if (counts.Contains(word))
  {
    counts[word]++;
  }
  else
  {
    counts.Add(word, 0);
  }
}

var stemmedCount = new Dictionary<string, int>();
var nonStemmedCount = new Dictionary<string, int>();

foreach(word in words)
{
  incrementCount(stemmedCount, Stem(word));
  incrementCount(nonStemmedCount, word);
}
void incrementCount(字典计数、字符串字)
{
如果(计数.包含(字))
{
计数[字]+;
}
其他的
{
计数。添加(字,0);
}
}
var stemedCount=新字典();
var nonsemmedcount=new Dictionary();
foreach(单词中的单词)
{
递增计数(词干计数,词干(单词));
递增计数(非计数,字);
}

//使用列表而不是字典来允许键的多重性: 列表>单词=新列表()

string text=“演示做什么”;
var ws=text.Split(“”);
//将单词传递到列表中:
words=(从w到ws)
将w按w分组为wsgroup
选择新的KeyValuePair(
wsGroups.Key,ws.Count()
)
).ToList();
//订购:
words.OrderBy(w=>w.Value);
//词干:
words=(从w开始,以words表示)
选择新的KeyValuePair
(
斯特姆沃德(西基),
w、 价值观
)).ToList();
//排序并放入字典:
var wordsRef=(从w开始,以文字表示)
按w分组。按w键分组
选择新的
{
计数=组。计数(),
word=组。关键字
}).ToDictionary(w=>w.word,w=>w.count);

有什么问题吗?你似乎表示你了解如何进行计数,所以一旦你停止,就再做一次。这就是问题所在吗?我认为这个问题应该编辑成。。你能推荐一个词干库吗?@Qaesar在你把单词词干后,再分组,但把之前分组的次数加起来。phoog,太好了,这就是我想要的。但是怎么做呢?阿列克谢,我想先把单词分组,然后进行词干分析器。词干分析器之后,会再次复制一些单词。我怎么把重复的单词数和上一个加起来?@Qaesar,我不知道你想要什么。请在您的问题中编写代码,以便能够找出您的数据结构以及计数的哪个部分会导致问题。好的,让我们有一个数组(2D),它包含:present 3,present 1,do 2,do 1。我是如何再次叙述它们的。因此,输出将是:现在4,做3???@Qaesar,请用你的代码编辑你的问题。如果没有实际的代码,你很难理解你有什么问题。但是如果我有一个名为stemword(word)的过程,它返回每个单词的词干,如果我想在第一次分组后对单词进行词干分析,我必须在代码中找到它??你的问题是你不能在字典中输入两个相同的键,或者什么?是的,在运行词干分析器后,我不能在字典中输入两个相同的键。请看这个例子。还有一件事,如果我输入的是一串单词,如何对单词进行分组以获得包含例如(“present”,3)和“son on”的列表??
        string text = "present present present presenting presentation do  do doing";
        var ws = text.Split(' ');

        //Passing the words into the list:
        words = (from w in ws
                 group w by w into wsGroups
                 select new KeyValuePair<string, int>(
                     wsGroups.Key, ws.Count()
                     )
                 ).ToList<KeyValuePair<string, int>>();

        //Ordering:
        words.OrderBy(w => w.Value);

        //Stemming the words:
        words = (from w in words
                 select new KeyValuePair<string, int>
                     (
                         stemword(w.Key),
                         w.Value
                     )).ToList<KeyValuePair<string, int>>();

        //Sorting and put into Dictionary:
        var wordsRef = (from w in words
                        group w by w.Key into groups
                        select new
                        {
                            count = groups.Count(),
                            word = groups.Key
                        }).ToDictionary(w => w.word, w => w.count);