• JAVA:实现文件中出现频率最高的K个单词以及出现的次数算法(附完整源码)


    JAVA:实现文件中出现频率最高的K个单词以及出现的次数算法

    package com.thealgorithms.others;
    
    import java.io.*;
    import java.util.*;
    public class TopKWords {
    
        static class CountWords {
    
            private String fileName;
    
            public CountWords(String fileName) {
                this.fileName = fileName;
            }
    
            public Map<String, Integer> getDictionary() {
                Map<String, Integer> dictionary = new HashMap<>();
                FileInputStream fis = null;
    
                try {
    
                    fis = new FileInputStream(fileName); // open the file
                    int in = 0;
                    String s = ""; // init a empty word
                    in = fis.read(); // read one character
    
                    while (-1 != in) {
                        if (Character.isLetter((char) in)) {
                            s += (char) in; // if get a letter, append to s
                        } else {
                            // this branch means an entire word has just been read
                            if (s.length() > 0) {
                                // see whether word exists or not
                                if (dictionary.containsKey(s)) {
                                    // if exist, count++
                                    dictionary.put(s, dictionary.get(s) + 1);
                                } else {
                                    // if not exist, initiate count of this word with 1
                                    dictionary.put(s, 1);
                                }
                            }
                            s = ""; // reInit a empty word
                        }
                        in = fis.read();
                    }
                    return dictionary;
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        // you always have to close the I/O streams
                        if (fis != null) {
                            fis.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        }
    
        public static void main(String[] args) {
            // you can replace the filePath with yours
            CountWords cw = new CountWords("/Users/lisanaaa/Desktop/words.txt");
            Map<String, Integer> dictionary
                    = cw.getDictionary(); // get the words dictionary: {word: frequency}
    
            // we change the map to list for convenient sort
            List<Map.Entry<String, Integer>> list = new ArrayList<>(dictionary.entrySet());
    
            // sort by lambda valueComparator
            list.sort(Comparator.comparing(m -> m.getValue()));
    
            Scanner input = new Scanner(System.in);
            int k = input.nextInt();
            while (k > list.size()) {
                System.out.println("Retype a number, your number is too large");
                input = new Scanner(System.in);
                k = input.nextInt();
            }
            for (int i = 0; i < k; i++) {
                System.out.println(list.get(list.size() - i - 1));
            }
            input.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    QRunnable与外界互传对象
    docker安装RabbitMQ教程
    Gvim计数器模板经典练习
    代码随想录算法训练营第五十八天| LeetCode 583 两个字符串的删除操作、LeetCode 72 编辑距离、编辑距离总结
    LM2904DT运算放大器中文资料规格书PDF数据手册引脚图参数图片功能概述
    一款低 EMI,无需滤波器, AB/D 类可选式音频功率放大器 SL2018
    CocosCreator 面试题(十一)Cocos Creator 屏幕适配
    文字的选择与排版
    UGUI性能优化学习笔记(二)合批
    Apple 注销账户 Revoke Token
  • 原文地址:https://blog.csdn.net/it_xiangqiang/article/details/126314762