计算文本文件中重复的单词
本文关键字:单词 文本 文件 计算 | 更新日期: 2023-09-27 18:15:18
我想计算文本文件中重复的单词数,我编写了以下代码
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字。这段代码有什么问题?谁来帮帮我。
如果你需要从文件中设置文本,你可以使用ReadAllLines
方法如下,当前代码的问题是在while循环中每次迭代你替换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";
}