キーワードを除く、文字列に単語の出現頻度(最上位ワード)を数えます
-
25-09-2019 - |
質問
私は、文字列に(いくつかのキーワードを除く)単語の頻度をカウントし、それらDESCをソートしたいと思います。だから、私はそれを行うことができますか?
次の文字列で...
This is stackoverflow. I repeat stackoverflow.
を除くキーワードは
ありますExKeywords() ={"i","is"}
出力は次のようにする必要があります。
stackoverflow
repeat
this
P.S。 NO!私は再設計グーグルありませんよ! :)
解決
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);
所属していません StackOverflow