2017-11-12 6 views
0

全文(ファイル)からCOUNT(出現頻度)のWORDSを含む辞書がすでにあります。C#:リストにある文字列(テキスト)の辞書のキー(単語)の出現をカウントする

次のステップでは、Textの各文にある各WORDの出現を確認します。

単語の出現頻度が高い文章を保存します。例えば

:チェック

List<string> list = new List<string>(); 
list.Add("This this is a string."); 
     list.Add("String words accurences needs to be checked."); 
     list.Add("how many times do this string text conatin words?"); 
     list.Add("how how how word words words"); 
     list.Add("This this This this"); 

     Dictionary<string, int> dict = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase); 
     dict.Add("this", 7); 
     dict.Add("how", 4); 
     dict.Add("string", 2); 
     dict.Add("words", 2); 
     dict.Add("occurences", 1); 
     dict.Add("checked", 1); 

コード「4は、どのように、」第二と第三のリストの文字列と第三列に来ることの高い発生を持っています。 3番目の "文"文字列を出現頻度の高い3つのintを単語HOWで出力する必要があります。 など..辞書内の他の単語を確認してください。

辞書は次のようになります。 {文、単語、オカレンス} オカレンスは高いか同じカウントである必要があります。

次のタスクは、各センテンスの語長をカウントすることです。

+0

...そして質問は? –

+0

http://idownvotedbecau.se/noattempt/ – pmcilreavy

+0

質問は、どのように辞書に存在する単語の出現数が最も多い文章を得ることができるかです。 確認後に辞書から単語を削除する。 辞書にcountを含む文章を保存します。 – Nab

答えて

0

Stackoverflowでのスプーンフィードはありません。あなた自身で試してみてください。私はコードを追加しましたが、時間の複雑さは増しましたが、単語が最大に出現する文章を検索することができます。 あなたの仕事はあなたの問題と互換性を持たせ、自分で問題を解決しようとすることです。

//Function to print maximum occurances of word from dictionary with sentence 
    public static void FindSentenceWithMaxOcc(List<string> list, Dictionary<string, int>dict){ 

     int maxSentenceIndex = 0, index = 0; 
     int maxCount = int.MinValue; 
     string word = ""; 

     //Iterate through dictionary containing words with total occurances 
     foreach(KeyValuePair<string, int> kv in dict){ 

      //Iterating through sentences present in list 
      foreach(string element in list){ 
       //Split all words using space 
       string[] words = element.Split(' '); 
       //Count all occurrances of dictionary key in sentence 
       int temp = Array.FindAll(words, s => s.Equals(kv.Key.Trim())).Length; 

       //Get max occurrances 
       if(temp > maxCount){ 
        maxCount = temp; 
        maxSentenceIndex = index; 
        word = kv.Key; 
       } 
       index++; 
      } 
      index = 0; 
     } 

     //Print result 
     Console.WriteLine("Maximum count: " +maxSentenceIndex); 
     Console.WriteLine("Word: " +word); 
     Console.WriteLine("Sentence" +list[maxSentenceIndex]); 
    } 

時間複雑:O(N^2)

実装:DotNet Fiddler

は、我々はその時の複雑さを軽減することができたり、我々はそれをより効率的にすることができますか?私がやろうとした何

+0

コメントは議論の延長ではありません。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/158968/discussion-on-answer-by-prasad-telkikar-c-count-a-dictionary-key-word-occurr) 。 – Andy

0

、印刷用

 void count(List<string> l, Dictionary<string, int> d) 
     { 
      var text = l; 
      var wp = new Regex(@"\w+"); 

      foreach (Match m in wp.Matches(text[0])) //using text[0] for 1st string 
      { 
       if (!d.ContainsKey(m.Value)) 
       { d.Add(m.Value, 1); } 
       else 
        d[m.Value]++; 
      } 
     } 

     foreach (var pair in dict) 
     { Console.WriteLine("{0 },{1}", pair.Key, pair.Value); } 

: foreachの(リスト内のvar語) {Console.WriteLineを( "{0}、{1}"、単語、辞書[言葉]); }

+0

ここであなたはその単語の出現を辞書で増やしています。あなたが4つの方法を得ているからです。 –

+0

いいえ、私は実際には私が初期化したのと同じ辞書を取得していますが、さらに2つのエントリが追加されています。 – Nab

関連する問題