• 查询单词出现次数


    一、要求

      输出单个文件(一篇英语文章)中的前 N 个最常出现的英语单词,并将结果输入到文本文件中。(我的程序没有确定N的值,而是将所有单词按出现次数由多到少排序输出到文件中)

    二、TXT文件(摘自https://www.lz13.cn/lizhiyingyu/6970.html)

    You Have Only One Life.txt:

      There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.

      May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.

      The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people

      who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.

      When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you're the one who is smiling and everyone around you is crying.

      Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.

    三、源代码

    package word;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Collections;
    import java.util.TreeMap;
    
    public class FindWord {
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new FileReader("You Have Only One Life.txt"));
            TreeMap<String,Integer> tm = new TreeMap<>();
            int c;
            String s = "";
            while((c = br.read()) != -1) {
                if(c<39 || (c>39&&c<65) || (c>90&&c<96) || (c>122&&c<8217) || c>8217)    //ASCII码为39和8217的分别为文中两个不同的缩写符号
                {
                    if(s != "")
                        tm.put(s, tm.containsKey(s) ?  tm.get(s)+1:1);
                    s = "";
                }
                else
                    s += (char)c;
            }
            br.close();
            sortTreeMap(tm);
        }
        public static void sortTreeMap(TreeMap<String,Integer> tm) throws IOException {
            TreeMap<Integer,String> tm2 = new TreeMap<>(Collections.reverseOrder());   //按从大到小顺序存储,key为出现次数,value为出现相应次数的单词
            for(String key:tm.keySet())
                tm2.put(tm.get(key), tm2.containsKey(tm.get(key)) ? tm2.get(tm.get(key))+ "、"+ key : key);
            BufferedWriter bw = new BufferedWriter(new FileWriter("单词出现次数.txt"));
            bw.write("说明:两个单词缩写当做一个单词处理。");
            bw.newLine(); 
            bw.newLine(); 
            for(Integer key:tm2.keySet()){
                bw.write("出现" + key + "次的单词有:" + tm2.get(key));
                bw.newLine(); 
                bw.newLine(); 
            }
            bw.close();
        }
    }

    四、运行结果

    单词出现次数.txt:

    说明:两个单词缩写当做一个单词处理。

    出现31次的单词有:you

    出现19次的单词有:to

    出现10次的单词有:who

    出现9次的单词有:those

    出现8次的单词有:the

    出现7次的单词有:and、have

    出现6次的单词有:make、of、that、want

    出现4次的单词有:a、enough、go、in、life、one、when、with、your

    出现3次的单词有:be、don’t、for、it、just、on、people、their、them、will

    出现2次的单词有:The、appreciate、are、around、crying、do、everyone、everything、hurts、is、let、message、miss、only、past、really、smile、smiling、so、they、things、this、touched、way、were、what

    出现1次的单词有:Always、And、Dream、Happiness、If、Live、Love、May、Please、There、When、all、along、always、another、bad、based、because、begins、best、born、brighten、brighter、brightest、can、can’t、chance、comes、cry、day、die、down、dream、dreams、ends、failures、feel、forgotten、friendship、from、future、grows、happen、happiest、happiness、happy、heartaches、hope、hug、human、hurt、if、importance、keep、kiss、know、lies、lifeuntil、lives、mean、moments、most、much、necessarily、need、nothing、opportunity、or、other、others’shoes、out、person、pick、probably、put、real、searched、see、send、side、someone、someone’s、something、sorrow、strong、sweet、tear、too、trials、tried、was、well、where、worry、you're、yourself

     五、总结

      暑假看过类似的案例,案例中是求文件中所有字符出现次数,用到了TreeMap双列集合,用起来很便捷,于是就记到了笔记上。程序中tm是将所有单词存到双列集合中,key为单词,value为单词出现次数;tm2是将tm中键值对倒置,即tm的value当做tm2的key,tm的key当做tm2的value并且按出现次数从大到小排列。

      Java中有许多简单便捷的类,也许我们平时编程用不到这些类,但是等到需要时,这些类能让我们在编程时省下许多力气。所以我们应当多多了解Java的类,多记笔记,为编程打下良好基础。

  • 相关阅读:
    记swoole数据库连接池消失问题的解决方式
    表格逻辑的几个想法
    Java代码实现热部署
    一个接口代理demo
    thinkphp5 关于跨域的一些坑
    CoSky-Mirror 就像一个镜子放在 Nacos、CoSky 中间,构建一个统一的服务发现平台
    CoSky 高性能 服务注册/发现 & 配置中心
    Govern Service 基于 Redis 的服务治理平台
    Govern EventBus
    mysql中查看视图代码
  • 原文地址:https://www.cnblogs.com/dream0-0/p/9775677.html
Copyright © 2020-2023  润新知