2017-10-18 8 views
-1

私は私の問題に助けが必要です:どのように文字列をループしていくつの単語があるのか​​調べるには?

今私はボタンとテキストボックスを含むフォームでWPFアプリケーションを持っています。

また、私は開いているファイルディレクトリを持っています。これは.csと.txtファイルを開きます。

これらのファイルの文字列をループして、最も大きい単語から最も小さい単語まで、最も一般的な単語を表示する必要があります。

たとえば、文字列は次のようになります。

は「太陽は明るい太陽が黄色です。」。

は返します:

を= 2;

sun = 2;

は2であり;

明るい= 1;

イエロー= 1;今のよう

マイコード:

private void btn1_Click(object sender, RoutedEventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false }; 
     if (ofd.ShowDialog() == true) 
      rtb.Text = File.ReadAllText(ofd.FileName); 

     string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries); 

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

     foreach (string word in userText) //search words in our array userText that we declared at the beginning. 
     { 

     } 
    } 

私はここから継続するかどうかはわかりませんが...ヘルプは大歓迎です。あなたが辞書に単語を置き、その数、あなたがそれらを見つけるたびに増分することができます

IDictionary<string, int> actualDictionary = new Dictionary<string, int>(); 

:それは開始するには良い場所かもしれませんので

答えて

1

私は@ GTown-Coderのアプローチをもっとも簡単に使うつもりです。しかし、あなたは本当にただ

private void btn1_Click(object sender, RoutedEventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false }; 
    if (ofd.ShowDialog() == true) 
     rtb.Text = File.ReadAllText(ofd.FileName); 

    string[] userText = rtb.Text.ToLower().Split(new char[] { ' ', ',', '=', '+', '}', '{', '\r', '\n', '(', ')', ';' }, StringSplitOptions.RemoveEmptyEntries); 

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

    foreach (string word in userText) //search words in our array userText that we declared at the beginning. 
    { 
     // Sample here 
     if (frequencies.Contains(word)) 
     { 
     frequencies[word]++; 
     } 
     else 
     { 
     frequencies.Add(word,1); 
     } 
    } 

    foreach (var kvp in frequencies) 
     Console.WriteLine("Word: {0} \t Frequency: {1}",kvp.Key, kvp.Value); 
} 
+0

...あなたのサンプルのように辞書を使用して同じコードを実装する方法を知りたい場合、私はあなた 'Dictionary'の大文字小文字を区別しない...' VAR周波数=新しい辞書することを検討します(StringComparer.CurrentCultureIgnoreCase); ' – bigtlb

+0

あなたのソリューションをありがとう!どのように正確に戻り値をテキストボックスに入れますか? –

+0

TextBoxが 'txt'の場合、' txt.Text'フィールドを設定してください。 {tv} .Top.Text = string.Join( "\ r"、frequencies.Select(kvp => $ "Word:{kvp.Key}頻度:{kvp.Value}")ToArray()) '。 ListBoxを使用して各行をListBoxItemに割り当てる方が良いかもしれませんが。 – bigtlb

1

これはほとんど、辞書の元の定義のように聞こえます。

IDictionary<string, int> actualDictionary = new Dictionary<string, int>(); 

    foreach (string word in userText) //search words in our array userText that we declared at the beginning. 
    { 
     if (!actualDictionary.ContainsKey(word)) { 
      actualDictionary[word] = 0; 
     } 

     actualDictionary[word]++; 
    } 

    foreach(var thing in actualDictionary) { 
     Console.WriteLine(thing.Key + " " + thing.Value); 
    } 

.NET Fiddleの実行例を参照してください。

2

あなたが私たちに提供した例と期待される出力を使って、私は.GroupByと匿名クラスを作成することでこれを達成できました。

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     // example string 
     var myString = "The sun is bright. The sun is yellow"; 

     // split the string into an array based on space characters and the period 
     var stringArray = myString.Split(new char[] {' ', '.'}, StringSplitOptions.RemoveEmptyEntries); 

     // group the items in the array based on the value, then create an anonymous class with the properties `Word` and `Count` 
     var groupedWords = stringArray.GroupBy(x => x).Select(x => new { Word = x.Key, Count = x.Count() }).ToList(); 

     // print the properties based on order of largest to smallest count to screen 
     foreach(var item in groupedWords.OrderByDescending(x => x.Count)) 
     { 
      Console.WriteLine(item.Word + " = " + item.Count); 
     } 


     // Output 
     --------- 
     // The = 2 
     // sun = 2 
     // is = 2 
     // bright = 1 
     // yellow = 1 
    } 
} 

これが役立つ場合はお知らせください。