我有一项任务,我必须在Java中使用GUI的用户找到给定文本中的字母频率。问题是我是阿尔巴尼亚人,我们的字母表有36个字母:a,b,c ......等等,但也有'dh''ll''nj''th''xh''zh''''''''''。
我应该使用'if'来比较下一个字母:如果我有'd'而下一个是'h'我认为它是一个字母'dh'并找到它的频率? Java中还有其他方法吗?最后两个字母怎么样? Java会将它们识别为字符吗? 感谢。
答案 0 :(得分:1)
您需要使用支持Unicode的方法,例如Character.isLetter()
。以下是http://rosettacode.org/wiki/Letter_frequency#Java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class LetterFreq {
public static int[] countLetters(String filename) throws IOException{
int[] freqs = new int[26];
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(char ch:line.toCharArray()){
if(Character.isLetter(ch)){
freqs[ch - 'A']++;
}
}
}
in.close();
return freqs;
}
public static void main(String[] args) throws IOException{
System.out.println(Arrays.toString(countLetters("filename.txt")));
}
}
这里我修改了它以包含Unicode字符
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class LetterFreq {
public static Map<Character, Integer> countLetters(String filename) throws IOException{
Map<Character, Integer> freqs = new HashMap<Character, Integer>();
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
Integer f;
while((line = in.readLine()) != null){
line = line.toUpperCase();
for(Character ch:line.toCharArray()){
if(Character.isLetter(ch)){
f = freqs.get(ch);
if (f == null) {
freqs.put(ch, 0);
}
else {
freqs.put(ch, ++f);
}
}
}
}
in.close();
return freqs;
}
public static void main(String[] args) throws IOException{
Map<Character, Integer> freqs = countLetters("filename.txt");
Set<Character> keySet = freqs.keySet();
for (Character key : keySet) {
System.out.println("Char: " + key + " Count: " + freqs.get(key));
}
}
}