【发布时间】:2020-05-10 16:14:28
【问题描述】:
我需要使用字典查找文本文件的词频,但是我无法为字典创建键和值。
private void Form1_Load(object sender, EventArgs e)
{
StreamReader inputFile; //read the file
string words; //hold words from file
int wordCount; //keep track of times words are repeated
//create dictionary
Dictionary<string, int> wordFrequency = new Dictionary<string, int>();
//open file
inputFile = File.OpenText("Kennedy.txt");
//read lines from file
while (!inputFile.EndOfStream)
{
words = inputFile.ReadLine();
wordFrequency.Add(words, wordCount); //add elements to dictionary?
//add words to list box
lstboxwords.Items.Add(words);
}
【问题讨论】:
-
您可能需要在其中的某个地方将行拆分为单词。这些词需要与
Dictionary条目匹配,以便它们的计数可以更新或添加到Dictionary(如果它们是新词)。第一部分很难,例如处理标点符号,二是容易。 (“为什么是鸭子?”、“鸭子汤”、“鸭子!”、“鸭子、小袋鼠和雪貂走进酒吧……”有多少只鸭子?)
标签: c# dictionary