计数频率的信 [英] Counting letter frequencies

查看:181
本文介绍了计数频率的信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文本文件中使用的StreamReader到程序读取。我需要在字符串中的每个字母的频率记录到一个数组(其中指数0将是一个,依此类推)。什么是最简单的方法为这个?

编辑:我有这个原来,直到我意识到这是完全错误的。

  INT计数器= 0;
INT [] =频率INT新[26]; //创建数组频//计数频率
而(计数器< inValue.Length)
{
      INT A = 65; //为A的ASCII值
      焦X = char.Parse(inValue.Substring(计数器,1)); //从字符串单个字符
       INT S =(int)的X; //投字符整数值       如果(S == A +计数器)
             频率[窗口] ++;             反++;
 }

在哪里inValue是文本文件StreamReader的读入程序。


解决方案

  VAR freqs = File.ReadAllText(myfile.txt的)
                    。凡(C => Char.IsLetter(C))
                    .GroupBy(C => C)
                    .ToDictionary(G => g.Key,G => g.Count());

这应该给你字符的字典及其数量。

更新:

如果你想不区分大小写数,只是改变了GROUPBY:

  .GroupBy(C => Char.ToUpper(C))//而不是.GroupBy(C => C)

在我看来词典是不是在这种情况下数组更好,因为字符计数属于不仅仅是隐含的经指数;相反,它是一个的明确的关键。这使得查找容易,因为你不必字符转换为索引。此外,加入国际化的支持时,这使得它更加灵活。但是,如果你绝对需要一个数组,这是一个简单的变化:

  VAR freqs = File.ReadAllText(myfile.txt的)
                    。凡(C => Char.IsLetter(C))
                    .GroupBy(C => C)
                    .OrderBy(G => g.Key)
                    。选择(G => g.Count())
                    .ToArray()

I'm reading in a text file using StreamReader to the program. I need to record the frequency of each letter in the string into an array (where index 0 would be A, and so on). What's the simplest approach for this?

Edit: I had this originally, until I realized it was completely wrong.

int counter = 0;
int[] freq = new int[26]; // create frequency array

// counts frequency
while (counter < inValue.Length)
{
      int A = 65; // ASCII value for "A"
      char x = char.Parse(inValue.Substring(counter, 1)); // get individual characters from string
       int s = (int)x; // cast character to integer value

       if (s == A + counter)
             freq[counter]++;

             counter++;
 }

Where inValue is the text file StreamReader reads into the program.

解决方案

var freqs = File.ReadAllText("myfile.txt")
                    .Where(c => Char.IsLetter(c))
                    .GroupBy(c => c)
                    .ToDictionary(g => g.Key, g => g.Count());

This should give you a Dictionary of characters and their count.

Update:

If you want case insensitive counts, just change the GroupBy:

.GroupBy(c => Char.ToUpper(c)) // instead of .GroupBy(c => c)

And in my opinion a dictionary is better than an array in this case because the character that the "count" belongs to is not just implied by the index; instead, it is an explicit key. This makes lookups easier because you don't have to convert the character to an index. Additionally, this makes it more flexible when adding internationalization support. However, if you absolutely need an array, it is a simple change:

var freqs = File.ReadAllText("myfile.txt")
                    .Where(c => Char.IsLetter(c))
                    .GroupBy(c => c)
                    .OrderBy(g => g.Key) 
                    .Select(g => g.Count())
                    .ToArray()

这篇关于计数频率的信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆