質問

私は、文字列に(いくつかのキーワードを除く)単語の頻度をカウントし、それら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);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top