gpt4 book ai didi

c# - 如何跟踪文本文件中的字数

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:34 24 4
gpt4 key购买 nike

我正在尝试计算文本文件中每个单词的出现次数(不区分大小写)并将单词及其计数存储在列表中。

这是我要存储在列表中的每个单词的对象类,

public class WordItem
{
public string Word { get; set; }
public int Count { get; set; }
}

和我的代码函数来解析文本文件

public List<WordItem> FindWordCount()
{
//I've successfully parsed the text file into a list
//of words and stripped punctuation up to this point
//and stored them in List<string> wordlist.

List<string> wordlist;
List<WordEntry> entries = new List<WordEntry>();

foreach (string word in wordlist)
{
WordItem temp = new WordItem();
temp.Word = word;
temp.Count = 1;
entries.Add(temp);
}
}

如何编辑我的字数统计功能以防止列表中出现重复的字词,并在每次我再次找到该字词时增加计数值?

最佳答案

我会使用带有不区分大小写的字符串比较器的 Dictionary:

public IEnumerable<WordItem> FindWordCount(IEnumerable<string> wordlist)
{
var wordCount = new Dictionary<string, int>(StringComparer.CurrentCultureIgnoreCase);
foreach (string word in wordlist)
{
int count = 0;
bool contained = wordCount.TryGetValue(word, out count);
count++;
wordCount[word] = count;
}
foreach (var kv in wordCount)
yield return new WordItem { Word = kv.Key, Count = kv.Value };
}

你可以这样使用它:

var wordList = new string[] { "A", "a", "b", "C", "a", "b" };
var wordCounts = FindWordCount(wordList).ToList();

关于c# - 如何跟踪文本文件中的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30733819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com