1

好的,所以我有一个 C# 控制台应用程序,它应该读取一个 .txt 文件……并计算不同的单词……它可以工作……但是我用 100MB 读取文件中每个不同单词的文件文件它持续了好几天。我想要一种方法来读取文件一次并计算所有不同的单词。到目前为止,这是一些应用程序:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Data;
using System.IO.MemoryMappedFiles;

namespace CompressionApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //read all text
            string FilePath = (@"D:\Test\testing.txt");
            string FullText;
            using (StreamReader streamReader = new StreamReader(FilePath))
            {
                FullText = streamReader.ReadToEnd();
            }
            FileInfo Info = new FileInfo(FilePath);
            int FileSize = Convert.ToInt32(Info.Length);
//some code

            string[] Words = FullText.Split(' ');

            var DistinctWords = new List<string>(Words.Distinct());

//some code

            int P = 0;
            int ID = 0;
            int Length = 0;
            int ByteWorth;
            double Perc;
            double PPerc = 0;
            bool display = false;

            using (var mappedFile1 = MemoryMappedFile.CreateFromFile(FilePath))
            {
                using (Stream mmStream = mappedFile1.CreateViewStream())
                {
                    using (StreamReader sr = new StreamReader(mmStream, ASCIIEncoding.ASCII))
                    {
                        Parallel.ForEach(DistinctWords, new ParallelOptions { MaxDegreeOfParallelism = 1 }, Word =>
                        {
                            DataRow dr = dt.NewRow();
                            string SearchTerm = Word;
                            var MatchQuery = from word in Words
                                             where word == SearchTerm
                                             select word;

                            int WordCount = MatchQuery.Count();
                            Length = SearchTerm.Length;
                            if (Length > 1)
                            {
                                if (WordCount > 1)
                                {
                                    ID = ID + 1;
                                    ByteWorth = (Length * 8) * WordCount;
                                    dr["Word"] = SearchTerm;
                                    dr["Count"] = WordCount;
                                    dr["ID"] = ID;
                                    dr["Length"] = Length;
                                    dr["ByteWorth"] = ByteWorth;
                                    dt.Rows.Add(dr);
                                }
                            }
//some code below

这是迄今为止完整的应用程序......我知道不是很整洁。但我是编码新手。

欢迎任何提示,提示或建议。

4

2 回答 2

2

因此,据我了解,您将获得不同的单词,然后对于每个单词,您将遍历整个文件以计算该单词的出现次数。我敢打赌,找到不同的词只需要很少的时间,但是计算出现次数的循环大约需要永远。

您可以使用 LINQ 获得不同的单词及其计数。替换这行代码:

var DistinctWords = new List<string>(Words.Distinct());

var DistinctWithCount = from word in Words
                        group word by word
                        into g
                        select new {Word = g.Key, Count = g.Count()};

然后,您可以使用以下计数枚举单词:

foreach (var g in DistinctWithCount)
{
    Console.WriteLine("{0},{1}", g.Word, g.Count);
}
于 2013-06-19T14:57:19.277 回答
0

我无法为您编写整个逻辑,但这里有一些指针。我使用的是字典而不是表。您可以稍后从字典中构建表。如果您想拥有 id,请使用复杂的值类型而不是“int”。该 int 值当前指示该单词的计数。

var CheckedWords = new Dictionary<string, int>();

下面是我在 foreach 循环中的代码的样子:

                        /*DataRow dr = dt.NewRow();
                        string SearchTerm = Word;
                        var MatchQuery = from word in Words
                                         where word == SearchTerm
                                         select word;

                        int WordCount = MatchQuery.Count();

                        Length = SearchTerm.Length;*/

                        if (Word.Length > 1)
                        {
                            if (!CheckedWords.ContainsKey(Word))
                                CheckedWords.Add(Word,1);
                            else
                                CheckedWords[Word]++;
                        }
于 2013-06-19T13:33:59.433 回答