2017-10-11 79 views
0

我做了一個需要.cs文件的小項目,讀取它們並返回文件中最頻繁的單詞。但是,現在它返回最常用的單詞是逗號。 我怎樣才能讓它分裂字符串忽略逗號?如何通過忽略逗號c#來拆分字符串?

例如:我有一個字符串:

,。 ? AA,B CDEF cfed,ABEF ABEF ABEF,

現在它返回一個最常見的詞是「ABEF」,它發生2次(程序不計第三ABEF,一個是與到底逗號)

又如:

,。 ? AA,B CDEF cfed,ABEF ABEF ABEF,,,

這個現在返回最常見的詞是一個逗號「」和它發生3次,但事情是 - 我希望我的程序忽略逗號並僅僅關注單詞。

namespace WindowsFormsApp8 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
    } 


    private async void button1_Click(object sender, EventArgs e) 
    { 
     using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents |*.cs;*.txt", ValidateNames = true, Multiselect = false }) //openfiledialog (all .cs; all.txt) 
     { 
      if (ofd.ShowDialog() == DialogResult.OK) //if in file dialog a file gets selected 
      { 
       using (StreamReader sr = new StreamReader(ofd.FileName)) //text reader 
       { 
        richTextBox1.Text = await sr.ReadToEndAsync(); //reads the file and returns it into textbox 
       } 
      } 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    {   
     string[] userText = richTextBox1.Text.ToLower().Split(' '); 
     var frequencies = new Dictionary<string, int>(); // variable frequencies, dictionary with key string, value int. 
     string highestWord = null; //declare string highestword with starting value null. 
     int highestFreq = 0; //declare integer highestfreq with starting value zero. 

     foreach (string word in userText) //search words in our array userText that we declared at the beginning. 
     { 
      int freq; //declare integer freq. 
      frequencies.TryGetValue(word, out freq); //trygetvalue from dictionary key, out value. 
      freq += 1; //count it. 

      if (freq > highestFreq) 
      { 
       highestFreq = freq; 
       highestWord = word; 
      } 
      frequencies[word] = freq; //assign most frequent word in frequencies dictionary to freq 
     } 
     MessageBox.Show("the most occuring word is: " + highestWord + ", it occured " + highestFreq + " times"); //display data to messagebox. 
    } 
    } 
} 
+1

向我們顯示您的代碼。取決於你如何做,這可能是1行代碼或5. – Amy

+0

對不起,代碼現在被添加 –

+2

僅供參考,評論如「//聲明字符串的最高字的起始值爲null」。不要提供任何價值。從它旁邊的代碼可以看出,您聲明瞭一個具有空值的變量。這樣的評論只是混亂。 – Amy

回答

6

拆分可以採取字符數組分裂上。所以你可以分割空間和逗號。然後用適當的StringSplitOption

string[] userText = richTextBox1.Text.ToLower().Split(new char[] { ' ', ','}, StringSplitOptions.RemoveEmptyEntries); 

你也可以使用LINQ來計算一個詞的出現次數與這樣的代碼

var g = userText.GroupBy(x => x) 
       .Select(z => new 
       { word = z.Key, count = z.Count()}) 
       .ToList(); 
string mostUsed = g.OrderByDescending(x => x.count) 
        .Select(x => x.word) 
        .FirstOrDefault(); 
+0

謝謝你的解決方案,但是我偶然發現了問題,可以說空間和逗號並不是我想要移除的唯一東西 - 我想也刪除所有大括號和點和等號 - 當我這樣做時,'字符串[] userText = richTextBox1.Text.ToLower()。Split(new char [] {'',',','=','+','}','{'},StringSplitOptions.RemoveEmptyEntries);'it開始再次把逗號作爲一個字符串中最常用的單詞,我錯過了什麼? –

+0

你能舉出一個產生問題的例子嗎? – Steve

+0

沒關係,它只是我的文本框中的逗號!然而,它仍然會返回一些看不見的東西是最常見的詞,不知道它是什麼?我使用的字符串來自.cs文件,所以我將使用pastebin,因爲評論部分有一個字符限制:https://pastebin.com/mVH3CEYb(它說最常用的字是,它出現在6次這個字符串 –

2

另一種選擇是刪除空條目,讓分裂更容易擴展使用正則表達式,Regex.Split更具體:

string input = ", . ? a a, b cdef cfed, abef abef abef, , ,"; 
    string[] result = Regex.Split(input, @"\w+"); 

檢查live testing這裏。

如果?是一個有效的單詞,那麼正則表達式可能是@"\w+|\?"

所以,我的建議是使用正則表達式,即使split方法對於現在來說已經足夠了,因爲它更強大並且可以輕鬆適應以後的更改。

作爲獎勵,學習正則表達式是一件好事。

相關問題