2017-10-18 20 views
-1

我需要幫助解決我的問題:我如何通過字符串循環並找出有多少單詞?

現在我有一個包含按鈕和文本框的窗體的WPF應用程序。

此外,我有一個打開的文件目錄 - 打開.cs和.txt文件。

我需要遍歷這些文件的字符串,並顯示最常用的單詞,從最大到最小。

例如,一個字符串是:

「太陽是明亮的太陽是黃色的。」

將返回:

的= 2;

sun = 2;

is = 2;

bright = 1;

yellow = 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. 
     { 

     } 
    } 

我不知道如何從這裏繼續...幫助表示讚賞。

回答

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'不區分大小寫...'變種頻率=新詞典 (StringComparer.CurrentCultureIgnoreCase);' – bigtlb

+0

謝謝你的解決方案!我如何將返回值放入文本框? –

+0

如果您有一些TextBox'txt',那麼只需設置'txt.Text'字段。 'txt.Text = string.Join(「\ r」,frequencies.Select(kvp => $「Word:{kvp.Key} Frequency:{kvp.Value}」)。ToArray())'。儘管使用ListBox並將每行分配給ListBoxItem可能會更好。 – bigtlb

1

這幾乎聽起來像一本字典的原始定義,所以這可能是一個良好的開端:

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

你可以把字典中的單詞,每一次你發現它們增加它們的數量。

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 
    } 
} 

讓我知道這是否有幫助!