存储地图对象中文本文件的单词频率

时间:2017-06-04 02:21:07

标签: java treemap

我正在创建以下创建WordDistribution对象的类。该对象本质上是一个TreeMap,其格式设置为包含文本文件中的每个单词(所有单词都被转换为小写)以及文本中该单词的频率。键是单词,值是频率。我已经尝试过测试构造函数,但是当我打印它时,对象仍然返回空。我想知道构造函数是否正确运行并在给定正确的文件输入的情况下创建正确的对象。

public class WordDistribution {

// fields
   private static TreeMap<String, Integer> wordDistribution;

// constructors
   public WordDistribution(File f) throws FileNotFoundException{

   Scanner file = new Scanner(f);
   wordDistribution = new TreeMap<String, Integer>();
   while (file.hasNext()) {
       String word = file.next().toLowerCase();
       if(!wordDistribution.containsKey(word)){
           wordDistribution.put(word, 1);
       }else{
           int count = wordDistribution.get(word);
           wordDistribution.put(word, count + 1);
       }
   }
}

1 个答案:

答案 0 :(得分:1)

您的File可能不在您认为的位置,请记录下来。接下来,您的wordDistribution不应该是static(否则您会在每次构造函数调用时重置它)。您还应该关闭Scanner(或者你可能泄漏文件句柄),我在这里使用了try-with-resources。最后,我更倾向于使用count进行更广泛的看涨期权,更喜欢接口Map(和菱形运算符<>)。

public class WordDistribution {
    // fields
    private Map<String, Integer> wordDistribution = new TreeMap<>();

    // constructors
    public WordDistribution(File f) throws IOException {
        System.out.printf("Reading: %s%n", f.getCanonicalPath());
        if (!f.canRead()) {
            throw new FileNotFoundException(String.format("File %s can not be read",
                    f.getCanonicalPath()));
        }

        try (Scanner file = new Scanner(f)) {
            while (file.hasNext()) {
                String word = file.next().toLowerCase();
                int count = 0;
                if (wordDistribution.containsKey(word)) {
                    count = wordDistribution.get(word);
                }
                wordDistribution.put(word, count + 1);
            }
        }
    }
}
相关问题