我编写了一个c#程序,它读取5个文本文件中的数据,并根据某个给定的关键词计算它们
string[] word_1 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D1_H1.txt").Split(' ');
string[] word_2 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D2_H1.txt").Split(' ');
string[] word_3 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D3_H2.txt").Split(' ');
string[] word_4 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D4_H2.txt").Split(' ');
string[] word_5 = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D5_H2.txt").Split(' ');
string[] given_doc = File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\Given_doc.txt").Split(' ');
这是我从文本文件中读取的内容,在读取后我使用for循环和if循环来计算软管文件中的每个单词
for (int i = 0; i < word_1.Length; i++)
{
string s = word_1[i];
if ("Red".Equals(word_1[i]))
{
//Console.WriteLine(word[i]);
h1_r++;
}
if ("Green".Equals(word_1[i]))
{
h1_g++;
}
if ("Blue".Equals(word_1[i]))
{
h1_b++;
}
}
这是我用来从一个文件获取计数的循环,它的工作正常,我做了5次这个过程来读取所有文件,我的问题是如何使用一个for循环读取这5个文件并将它们存储在数组(每个关键字的计数)
提前感谢!!
答案 0 :(得分:2)
LINQ查询是您最简单的解决方案:
var filenames = new[] { "D1_H1.txt", "D2_H1.txt", "D3_H2.txt" };
var words = new[] { "Red", "Green", "Blue" };
var counters =
filenames.Select(filename => Path.Combine(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment", filename))
.SelectMany(filepath => File.ReadAllLines(filepath))
.SelectMany(line => line.Split(new[] { ' ' }))
.Where(word => words.Contains(word))
.GroupBy(word => word, (key, values) => new
{
Word = key,
Count = values.Count()
})
.ToDictionary(g => g.Word, g => g.Count);
然后你在所有文件中都有字典词典:
int redCount = counters["Red"];
如果要为每个文件存储计数器,可以使用稍微修改过的查询:
var filenames = new[] { "D1_H1.txt", "D2_H1.txt", "D3_H2.txt" };
var words = new[] { "Red", "Green", "Blue" };
var counters =
filenames.Select(filename => Path.Combine(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment", filename))
.Select(filepath => new
{
Filepath = filepath,
Count = File.ReadAllLines(filepath)
.SelectMany(line => line.Split(new[] { ' ' }))
.Where(word => words.Contains(word))
.GroupBy(word => word, (key, values) => new
{
Word = key,
Count = values.Count()
})
.ToDictionary(g => g.Word, g => g.Count)
})
.ToDictionary(g => g.Filepath, g => g.Count);
然后相应地使用它:
int redCount = counters[@"C:\Users\(...)\D1_H1.txt"]["Red"];
答案 1 :(得分:1)
List<KeyValuePair<string, string>> completeList = new List<KeyValuePair<string, string>>();
completeList.AddRange("D1_H1.txt",File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D1_H1.txt").Split(' '));
completeList.AddRange("D1_H2.txt", File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D2_H1.txt").Split(' '));
completeList.AddRange("D1_H3.txt", File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D3_H2.txt").Split(' '));
completeList.AddRange("D1_H4.txt", File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D4_H2.txt").Split(' '));
completeList.AddRange("D1_H5.txt", File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\D5_H2.txt").Split(' '));
completeList.AddRange("D1_H6.txt", File.ReadAllText(@"C:\Users\Niyomal N\Desktop\Assignment\Assignment\Given_doc.txt").Split(' '));
var result = completeList.GroupBy(r => r.Key).Select(r => new {File = r.Key, Red = r.Count(s => s.Value == "red"), Green = r.Count(s => s.Value == "green"), Blue = r.Count(s => s.Value == "blue") });
foreach (var itm in result)
{
Console.WriteLine(itm.File);
Console.WriteLine(itm.Red);
Console.WriteLine(itm.Green);
Console.WriteLine(itm.Blue);
}
答案 2 :(得分:1)
复制粘贴代码通常不好。它会导致代码违反“不要重复自己”(DRY)规则。重组您的代码:
const string path = @"C:\Users\Niyomal N\Desktop\Assignment\Assignment";
string[] files = new string[] { "D1_H1.txt", "D2_H1.txt", "D3_H1.txt", ... };
foreach (string file in files) {
string fullPath = Path.Combine(path, file);
//TODO: count words of file `fullPath`
}
将单词count存储在数组中并不是最佳的,因为您必须遍历文件中遇到的每个单词的数组。 使用具有恒定查找时间的字典。那要快得多。
var wordCount = new Dictionary<string, int>();
然后你可以计算这样的词:
int count;
if (wordCount.TryGetValue(word, out count)) {
wordCount[word] = count + 1;
} else {
wordCount[word] = 1;
}
<强>更新强>
您可以测试此类关键字
var keywords = new HashSet<string> { "Red", "Green", "Blue" };
string word = "Green";
if (keywords.Contains(word)) {
...
}
HasSets和字典一样快。
小心套管这个词。 HashSets
默认情况下区分大小写。如果必须在alltogehter中找到“red”,“Red”和“RED”,请像这样初始化HashSet
:
var keywords = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase)
{ "Red", "Green", "Blue" };