Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c# - How to add all words with length more than X to the list from all X files in Directory?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace FunkcjaSpilit
{
    class Program2
    {
        static int _MinWordLength = 7;

        static void Main()
        {
            DirectoryInfo filePaths = new DirectoryInfo(@"D:project_IAD");
            FileInfo[] Files = filePaths.GetFiles("*.sgm");
            List<int> firstone = new List<int>();

            foreach (FileInfo file in Files)
            {
                int longWordsCount = CalculateLongWordsCount(file, _MinWordLength);
                string justFileName = file.Name;
                firstone.Add(longWordsCount);
                Console.WriteLine(("W pliku: " + justFileName) + " liczba d?ugich s?ów to " + longWordsCount);
            }

            Console.WriteLine(firstone.Count);
            Console.ReadLine();
        }

        private static int CalculateLongWordsCount(FileInfo file, int _MinWordLength)
        {
            return File.ReadLines(file.FullName).
                Select(line => line.Split(' ').Count(word => word.Length > _MinWordLength)).Sum();
        }
    }
}

I this code at line firstone.Add(longWordsCount); I'd like to add all words with length more than 7 to the list, but after running this code, firstone.Add(longWordsCount); adds only sum of files in directory (not the sum of all words with length more then 7 in all .sgm files in directory).

How can I fix it?

Expected results:

firstone.Add(longWordsCount);

should add all words with length more than 7 to the list from all files in the directory.

question from:https://stackoverflow.com/questions/65645252/how-to-add-all-words-with-length-more-than-x-to-the-list-from-all-x-files-in-dir

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're print the list length in the line:

Console.WriteLine(firstone.Count);

It should be:

Console.WriteLine(firstone.Sum(f => f));

EDIT:

But as you don't really use the list at all, I would suggest you replace the foreach with this, to avoid saving a list that is not actually used.

int wordsWithTheRequiredLenghtCount = 0;
foreach (FileInfo file in Files)
{
    int longWordsCount = CalculateLongWordsCount(file, 7);
    wordsWithTheRequiredLenghtCount += longWordsCount;
    Console.WriteLine(("W pliku: " + file.Name) + " liczba d?ugich s?ów to " + longWordsCount);
}

If you don't really need to print the file name in front of the file word count. And just want the global word count, the @Dmitry Bychenko solution is way better.

But he deleted it, so I will just replicate it here since I found it very clever.

var words = Directory
.EnumerateFiles(@"D:project_IAD", "*.sgm")
  .SelectMany(file => File.ReadLines(file))
  .SelectMany(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries)
  .Where(word => word.Length > minWordLength))
  .Count();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...