如何在Java中计算文本文件中每个字母[a-z]的实际频率

如何在Java中计算文本文件中每个字母[a-z]的实际频率,java,Java,我已经成功地转换了任何文本文件,并删除了除[a-z]和空格之外的所有字符和数字。现在我想确定文本文件中每个字母的相对频率。请有人给我一些提示。您可以使用hashmap计算每个字符的计数,并从中计算相对频率 public class RelativeFrequency { public static void main (String[] args) { Map<Character, Integer> c

我已经成功地转换了任何文本文件,并删除了除[a-z]和空格之外的所有字符和数字。现在我想确定文本文件中每个字母的相对频率。请有人给我一些提示。

您可以使用hashmap计算每个字符的计数,并从中计算相对频率

        public class RelativeFrequency
    {

        public static void main (String[] args)
        {
            Map<Character, Integer> characterCountMap = new HashMap<Character, Integer>();

            String text = "asda";// Replace it with your text

            for (int index = 0; index < text.length(); index++)
            {
                char c = text.charAt(index);
                if (null == characterCountMap.get(c))
                {
                    characterCountMap.put(c, 1);
                }
                else
                {
                    characterCountMap.put(c,
                            characterCountMap.get(c).intValue() + 1);

                }
            }

            Set<Entry<Character, Integer>> entrySet = characterCountMap.entrySet();

            for (Iterator iterator = entrySet.iterator(); iterator.hasNext();)
            {
                Entry<Character, Integer> entry = (Entry<Character, Integer>) iterator
                        .next();
                System.out.println(entry.getKey() + " relative frequency ="
                        + ((float) entry.getValue()) / text.length());

            }
        }
    }
公共类相对频率
{
公共静态void main(字符串[]args)
{
Map characterCountMap=新HashMap();
String text=“asda”//将其替换为您的文本
对于(int index=0;index
您有相对频率的公式吗?相对于文本文件中的字母总数,字母频率计数。您可以将频率存储在
HashMap
new int[65536]
中。