我设法转换任何文本文件并删除除[a-z]和空格以外的所有字符和数字。我现在想确定文本文件中每个字母的相对频率。请有人给我一些指示。
答案 0 :(得分:1)
您可以使用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());
}
}
}