Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Vb.net 计算字符串中的词频(最有意义的词),不包括关键字_Vb.net_Linq_Count_Word Count_Word Frequency - Fatal编程技术网

Vb.net 计算字符串中的词频(最有意义的词),不包括关键字

Vb.net 计算字符串中的词频(最有意义的词),不包括关键字,vb.net,linq,count,word-count,word-frequency,Vb.net,Linq,Count,Word Count,Word Frequency,我想计算字符串中单词(不包括一些关键字)的频率,并对它们进行排序。那么,我该怎么做呢 在下面的字符串中 This is stackoverflow. I repeat stackoverflow. 其中排除的关键字是 ExKeywords() ={"i","is"} 输出应该是 stackoverflow repeat this 别这样!我不是在重新设计谷歌!:) string input=“这是stackoverflow。我重复stackove

我想计算字符串中单词(不包括一些关键字)的频率,并对它们进行排序。那么,我该怎么做呢

在下面的字符串中

This is stackoverflow. I repeat stackoverflow.
其中排除的关键字是

ExKeywords() ={"i","is"}
输出应该是

stackoverflow  
repeat         
this           
别这样!我不是在重新设计谷歌!:)

string input=“这是stackoverflow。我重复stackoverflow。”;
字符串[]关键字=新[]{“i”,“is”};
正则表达式正则表达式=新正则表达式(\\w+);
foreach(regex.Matches中的变量组)(输入)
第()类
.Select(c=>c.Value.ToLowerInvariant())
.Where(c=>!keywords.Contains(c))
.GroupBy(c=>c)
.OrderByDescending(c=>c.Count())
.ThenBy(c=>c.Key))
{
控制台写入线(组键);
}
string input=“这是stackoverflow。我重复stackoverflow。”;
字符串[]关键字=新[]{“i”,“is”};
正则表达式正则表达式=新正则表达式(\\w+);
foreach(regex.Matches中的变量组)(输入)
第()类
.Select(c=>c.Value.ToLowerInvariant())
.Where(c=>!keywords.Contains(c))
.GroupBy(c=>c)
.OrderByDescending(c=>c.Count())
.ThenBy(c=>c.Key))
{
控制台写入线(组键);
}

如果这是一个非常大的字符串(比如12000个单词),Regex仍然是正确的方法吗?@discorax试试吧!要在单词边界上拆分一个大字符串,正则表达式应该和简单的自定义实现一样高效。您可能会从自定义解析器获得更好的性能,但我怀疑这是否值得努力。对于较大的输入长度n,Linq的性能可能是限制因素。然而,我相信Linq经过了合理的优化,因此我怀疑上述产品是O(nk)。如果关键字的数量要大得多(例如>4),最好将它们放在一个字典中,使总的时间复杂度为O(n)。如果这是一个非常大的字符串(比如12000个单词),Regex仍然是正确的方法吗?@discorax试试吧!要在单词边界上拆分一个大字符串,正则表达式应该和简单的自定义实现一样高效。您可能会从自定义解析器获得更好的性能,但我怀疑这是否值得努力。对于较大的输入长度n,Linq的性能可能是限制因素。然而,我相信Linq经过了合理的优化,因此我怀疑上述产品是O(nk)。如果关键字的数量要大得多(例如>4),最好将它们放在字典中,使总体时间复杂度为O(n)。
string input = "This is stackoverflow. I repeat stackoverflow.";
string[] keywords = new[] {"i", "is"};
Regex regex = new Regex("\\w+");

foreach (var group in regex.Matches(input)
    .OfType<Match>()
    .Select(c => c.Value.ToLowerInvariant())
    .Where(c => !keywords.Contains(c))
    .GroupBy(c => c)
    .OrderByDescending(c => c.Count())
    .ThenBy(c => c.Key))
{
    Console.WriteLine(group.Key);
}
string s = "This is stackoverflow. I repeat stackoverflow.";
string[] notRequired = {"i", "is"};

var myData =
    from word in s.Split().Reverse()
    where (notRequired.Contains(word.ToLower()) == false)
    group word by word into g
    select g.Key;

foreach(string item in myData)
    Console.WriteLine(item);