我想计算在文本文件中重复的单词,我写下面的代码
代码
private void button3_Click(object sender, EventArgs e)
{
string line;
using (StreamReader reader = new StreamReader("D:\\mun.txt"))
{
while ((line = reader.ReadLine()) != null)
{
richTextBox1.Text = reader.ToString();
}
}
Regex regex = new Regex("\\w+");
var frequencyList = regex.Matches(richTextBox1.Text)
.Cast<Match>()
.Select(c => c.Value.ToLowerInvariant())
.GroupBy(c => c)
.Select(g => new { Word = g.Key, Count = g.Count() })
.OrderByDescending(g => g.Count)
.ThenBy(g => g.Word);
Dictionary<string, int> dict = frequencyList.ToDictionary(d => d.Word, d => d.Count);
foreach (var item in frequencyList)
{
label1.Text =label1.Text+item.Word+"\n";
label2.Text = label2.Text+item.Count.ToString()+"\n";
}
}
但是此代码提供了错误的结果,此代码仅采用 StreamReader 字。这段代码出了什么问题。有人帮我。
答案 0 :(得分:2)
如果您需要从文件中设置文本,可以使用ReadAllLines
方法,如下所示,当前代码的问题在内部,而每次迭代时都会替换richTextBox1
文本。
richTextBox1.Lines =File.ReadAllLines("D:\\mun.txt")
Regex regex = new Regex("\\w+");
var frequencyList = regex.Matches(richTextBox1.Text)
.Cast<Match>()
.Select(c => c.Value.ToLowerInvariant())
.GroupBy(c => c)
.Select(g => new { Word = g.Key, Count = g.Count() })
.OrderByDescending(g => g.Count)
.ThenBy(g => g.Word);
Dictionary<string, int> dict = frequencyList.ToDictionary(d => d.Word, d => d.Count);
foreach (var item in frequencyList)
{
label1.Text =label1.Text+item.Word+"\n";
label2.Text = label2.Text+item.Count.ToString()+"\n";
}