zoukankan      html  css  js  c++  java
  • (第一周)英文词频统计

    需求分析

      1、读取文本文件并在结果中显示(也可以选择不显示)

      2、统计单词出现的次数

      3、对结果进行排序、输出

    功能实现

      1、在结果中显示文本内容

      

            File yw = new File("java.txt");            //使用循环读取本地文本文件“java.txt”中的字符并显示出来
            FileInputStream cp = new FileInputStream(yw);
            char yy;
            System.out.println("该文件中的内容如下:");
            for(int i=0;i<yw.length();i++){
                yy=(char)cp.read();
                System.out.print(yy);
            }
            System.out.println();
            cp.close();

      2、用BuffereReader类读取文本,用TreeSet对单词进行排序 ,并用String[ ]保存分隔都的字符

            BufferedReader bf = new BufferedReader(new FileReader("java.txt"));//使用缓冲区创造缓冲字符输入流
            StringBuffer sbf = new StringBuffer();     //动态字符串数组
            Set<String> SetSer = new TreeSet<String>();//String类型的对象的集合Set,并且是词语的首字母是有序的
            String temp = bf.readLine();               //读取文本行
            while(temp !=null ){
                sbf.append(temp+" ");                  //往动态字符串数组里添加数据
                temp = bf.readLine();
            }
            String[] Str = sbf.toString().split("[^A-Za-z0-9]"); //进行分隔字符,并保存到数组里

      3、使用循环对单词出现次数进行统计

            for(String string:Str){                     //使用循环将分离出来的词语与整体进行对比、统计,若词语重复,则数量增加;否则词语更新
                SetSer.add(string);
            }                   
            for(String childString:SetSer){   
                System.out.print(childString);
                int count = 0;
                for(String fatherString : Str){
                    if(fatherString.equals(childString)){
                        count++;
                    }
                }
                System.out.println("出现"+count+"次");    //结果输出    
            }

    结果输出显示

     1、文本内容显示

    该文件中的内容如下:
      General Westmoreland, General Grove, distinguished guests, and gentlemen of the 
      Corps!
      As I was leaving the hotel this morning, a doorman asked me, "Where are you 
      bound for, General?" And when I replied, "West Point," he remarked, "Beautiful 
      place. Have you ever been there before?"
      No human being could fail to be deeply moved by such a tribute as this [Thayer 
      Award]. Coming from a profession I have served so long, and a people I have loved 
      so well, it fills me with an emotion I cannot express. But this award is not intended 
      primarily to honor a personality, but to symbolize a great moral code -- the code of 
      conduct and chivalry of those who guard this beloved land of culture and ancient 
      descent. That is the animation of this medallion. For all eyes and for all time, it is an 
      expression of the ethics of the American soldier. That I should be integrated in this 
      way with so noble an ideal arouses a sense of pride and yet of humility which will be 
      with me always: Duty, Honor, Country.
      Those three hallowed words reverently dictate what you ought to be, what you can 
      be, what you will be. They are your rallying points: to build courage when courage 
      seems to fail; to regain faith when there seems to be little cause for faith; to create 
      hope when hope becomes forlorn.
      Unhappily, I possess neither that eloquence of diction, that poetry of imagination, 
      nor that brilliance of metaphor to tell you all that they mean. The unbelievers will 
      say they are but words, but a slogan, but a flamboyant phrase. Every pedant, every 
      demagogue, every cynic, every hypocrite, every troublemaker, and I am sorry to 
      say, some others of an entirely different character, will try to downgrade them even 
      to the extent of mockery and ridicule.
      But these are some of the things they do. They build your basic character. They 
      mold you for your future roles as the custodians of the nation's defense. They make 
      you strong enough to know when you are weak, and brave enough to face yourself 
      when you are afraid. They teach you to be proud and unbending in honest failure, 
      but humble and gentle in success; not to substitute words for actions, not to seek 
      the path of comfort, but to face the stress and spur of difficulty and challenge; to 
      learn to stand up in the storm but to have compassion on those who fall; to master 
      yourself before you seek to master others; to have a heart that is clean, a goal that 
      is high; to learn to laugh, yet never forget how to weep; to reach into the future yet 
      never neglect the past; to be serious yet never to take yourself too seriously; 
    
      true wisdom, the meekness of true strength. They give you a temper of the will, a 
      quality of the imagination, a vigor of the emotions, a freshness of the deep springs 
      of life, a temperamental predominance of courage over timidity, of an appetite for 
      adventure over love of ease. They create in your heart the sense of wonder, the 
      unfailing hope of what next, and the joy and inspiration of life. They teach you in this 
      way to be an officer and a gentleman.
      And what sort of soldiers are those you are to lead? Are they reliable? Are they brave? 
      Are they capable of victory? Their story is known to all of you. It is the story of the 
      American man-at-arms. My estimate of him was formed on the battlefield many, 
      many years ago, and has never changed. I regarded him then as I regard him now 
      -- as one of the world's noblest figures, not only as one of the finest military 
      characters, but also as one of the most stainless. His name and fame are the 
      birthright of every American citizen. In his youth and strength, his love and loyalty, 
      he gave all that mortality can give. 
      He needs no eulogy from me or from any other man. He has written his own history 
      and written it in red on his enemy's breast. But when I think of his patience under 
      adversity, of his courage under fire, and of his modesty in victory, I am filled with an 
      emotion of admiration I cannot put into words. He belongs to history as furnishing 
      one of the greatest examples of successful patriotism. He belongs to posterity as the 
      instructor of future generations in the principles of liberty and freedom. He belongs 
      to the present, to us, by his virtues and by his achievements. In 20 campaigns, on 
      a hundred battlefields, around a thousand campfires, I have witnessed that 
      enduring fortitude, that patriotic self-abnegation, and that invincible determination 
      which have carved his statue in the hearts of his people. From one end of the world 
      to the other he has drained deep the chalice of courage.
      As I listened to those songs [of the glee club], in memory's eye I could see those 
      staggering columns of the First World War, bending under soggy packs, on many a 
      weary march from dripping dusk to drizzling dawn, slogging ankle-deep through the 
      mire of shell-shocked roads, to form grimly for the attack, blue-lipped, covered with 
      sludge and mud, chilled by the wind and rain, driving home to their objective, and 
      for many, to the judgment seat of God. 
      I do not know the dignity of their birth, but I do know the glory of their death. 
      They died unquestioning, uncomplaining, with faith in their hearts, and on their lips 
      the hope that we would go on to victory.
      Always, for them: Duty, Honor, Country; always their blood and sweat and tears, as 
      we sought the way and the light and the truth. 
      And 20 years after, on the other side of the globe, again the filth of murky foxholes, 
      the stench of ghostly trenches, the slime of dripping dugouts; those boiling suns of 
      relentless heat, those torrential rains of devastating storms; the loneliness and 
      utter desolation of jungle trails; the bitterness of long separation from those they 
      loved and cherished; the deadly pestilence of tropical disease; the horror of stricken 
      areas of war; their resolute and determined defense, their swift and sure attack, 
      their indomitable purpose, their complete and decisive victory -- always victory. 
      Always through the bloody haze of their last reverberating shot, the vision of gaunt, 
      ghastly men reverently following your password of: Duty, Honor, Country. 
      The code which those words perpetuate embraces the highest moral laws and will 
      stand the test of any ethics or philosophies ever promulgated for the uplift of 
      mankind. Its requirements are for the things that are right, and its restraints are 
      from the things that are wrong. 
      The soldier, above all other men, is required to practice the greatest act of religious 
      training -- sacrifice.
      In battle and in the face of danger and death, he discloses those divine attributes 
      which his Maker gave when he created man in his own image. No physical courage 
      and no brute instinct can take the place of the Divine help which alone can sustain 
      him. 

      2、单词统计结果

    20出现2次
    Always出现2次
    American出现3次
    And出现3次
    Are出现3次
    As出现2次
    Award出现1次
    Beautiful出现1次
    But出现3次
    Coming出现1次
    Corps出现1次
    Country出现3次
    Divine出现1次
    Duty出现3次
    Every出现1次
    First出现1次
    For出现1次
    From出现1次
    General出现3次
    God出现1次
    Grove出现1次
    Have出现1次
    He出现5次
    His出现1次
    Honor出现3次
    I出现18次
    In出现3次
    It出现1次
    Its出现1次
    Maker出现1次
    My出现1次
    No出现2次
    Point出现1次
    That出现2次
    Thayer出现1次
    The出现3次
    Their出现1次
    They出现9次
    Those出现1次
    Unhappily出现1次
    War出现1次
    West出现1次
    Westmoreland出现1次
    Where出现1次
    World出现1次
    a出现20次
    abnegation出现1次
    above出现1次
    achievements出现1次
    act出现1次
    actions出现1次
    admiration出现1次
    adventure出现1次
    adversity出现1次
    afraid出现1次
    after出现1次
    again出现1次
    ago出现1次
    all出现6次
    alone出现1次
    also出现1次
    always出现3次
    am出现2次
    an出现7次
    ancient出现1次
    and出现43次
    animation出现1次
    ankle出现1次
    any出现2次
    appetite出现1次
    are出现13次
    areas出现1次
    arms出现1次
    around出现1次
    arouses出现1次
    as出现9次
    asked出现1次
    at出现1次
    attack出现2次
    attributes出现1次
    award出现1次
    basic出现1次
    battle出现1次
    battlefield出现1次
    battlefields出现1次
    be出现10次
    becomes出现1次
    been出现1次
    before出现2次
    being出现1次
    belongs出现3次
    beloved出现1次
    bending出现1次
    birth出现1次
    birthright出现1次
    bitterness出现1次
    blood出现1次
    bloody出现1次
    blue出现1次
    boiling出现1次
    bound出现1次
    brave出现2次
    breast出现1次
    brilliance出现1次
    brute出现1次
    build出现2次
    but出现9次
    by出现4次
    campaigns出现1次
    campfires出现1次
    can出现4次
    cannot出现2次
    capable出现1次
    carved出现1次
    cause出现1次
    chalice出现1次
    challenge出现1次
    changed出现1次
    character出现2次
    characters出现1次
    cherished出现1次
    chilled出现1次
    chivalry出现1次
    citizen出现1次
    clean出现1次
    club出现1次
    code出现3次
    columns出现1次
    comfort出现1次
    compassion出现1次
    complete出现1次
    conduct出现1次
    could出现2次
    courage出现6次
    covered出现1次
    create出现2次
    created出现1次
    culture出现1次
    custodians出现1次
    cynic出现1次
    danger出现1次
    dawn出现1次
    deadly出现1次
    death出现2次
    decisive出现1次
    deep出现3次
    deeply出现1次
    defense出现2次
    demagogue出现1次
    descent出现1次
    desolation出现1次
    determination出现1次
    determined出现1次
    devastating出现1次
    dictate出现1次
    diction出现1次
    died出现1次
    different出现1次
    difficulty出现1次
    dignity出现1次
    discloses出现1次
    disease出现1次
    distinguished出现1次
    divine出现1次
    do出现3次
    doorman出现1次
    downgrade出现1次
    drained出现1次
    dripping出现2次
    driving出现1次
    drizzling出现1次
    dugouts出现1次
    dusk出现1次
    ease出现1次
    eloquence出现1次
    embraces出现1次
    emotion出现2次
    emotions出现1次
    end出现1次
    enduring出现1次
    enemy出现1次
    enough出现2次
    entirely出现1次
    estimate出现1次
    ethics出现2次
    eulogy出现1次
    even出现1次
    ever出现2次
    every出现5次
    examples出现1次
    express出现1次
    expression出现1次
    extent出现1次
    eye出现1次
    eyes出现1次
    face出现3次
    fail出现2次
    failure出现1次
    faith出现3次
    fall出现1次
    fame出现1次
    figures出现1次
    filled出现1次
    fills出现1次
    filth出现1次
    finest出现1次
    fire出现1次
    flamboyant出现1次
    following出现1次
    for出现11次
    forget出现1次
    forlorn出现1次
    form出现1次
    formed出现1次
    fortitude出现1次
    foxholes出现1次
    freedom出现1次
    freshness出现1次
    from出现6次
    furnishing出现1次
    future出现3次
    gaunt出现1次
    gave出现2次
    generations出现1次
    gentle出现1次
    gentleman出现1次
    gentlemen出现1次
    ghastly出现1次
    ghostly出现1次
    give出现2次
    glee出现1次
    globe出现1次
    glory出现1次
    go出现1次
    goal出现1次
    great出现1次
    greatest出现2次
    grimly出现1次
    guard出现1次
    guests出现1次
    hallowed出现1次
    has出现3次
    have出现6次
    haze出现1次
    he出现5次
    heart出现2次
    hearts出现2次
    heat出现1次
    help出现1次
    high出现1次
    highest出现1次
    him出现4次
    his出现13次
    history出现2次
    home出现1次
    honest出现1次
    honor出现1次
    hope出现4次
    horror出现1次
    hotel出现1次
    how出现1次
    human出现1次
    humble出现1次
    humility出现1次
    hundred出现1次
    hypocrite出现1次
    ideal出现1次
    image出现1次
    imagination出现2次
    in出现14次
    indomitable出现1次
    inspiration出现1次
    instinct出现1次
    instructor出现1次
    integrated出现1次
    intended出现1次
    into出现2次
    invincible出现1次
    is出现8次
    it出现3次
    its出现1次
    joy出现1次
    judgment出现1次
    jungle出现1次
    know出现3次
    known出现1次
    land出现1次
    last出现1次
    laugh出现1次
    laws出现1次
    lead出现1次
    learn出现2次
    leaving出现1次
    liberty出现1次
    life出现2次
    light出现1次
    lipped出现1次
    lips出现1次
    listened出现1次
    little出现1次
    loneliness出现1次
    long出现2次
    love出现2次
    loved出现2次
    loyalty出现1次
    make出现1次
    man出现3次
    mankind出现1次
    many出现4次
    march出现1次
    master出现2次
    me出现4次
    mean出现1次
    medallion出现1次
    meekness出现1次
    memory出现1次
    men出现2次
    metaphor出现1次
    military出现1次
    mire出现1次
    mockery出现1次
    modesty出现1次
    mold出现1次
    moral出现2次
    morning出现1次
    mortality出现1次
    most出现1次
    moved出现1次
    mud出现1次
    murky出现1次
    name出现1次
    nation出现1次
    needs出现1次
    neglect出现1次
    neither出现1次
    never出现4次
    next出现1次
    no出现2次
    noble出现1次
    noblest出现1次
    nor出现1次
    not出现5次
    now出现1次
    objective出现1次
    of出现75次
    officer出现1次
    on出现8次
    one出现5次
    only出现1次
    or出现2次
    other出现4次
    others出现2次
    ought出现1次
    over出现2次
    own出现2次
    packs出现1次
    password出现1次
    past出现1次
    path出现1次
    patience出现1次
    patriotic出现1次
    patriotism出现1次
    pedant出现1次
    people出现2次
    perpetuate出现1次
    personality出现1次
    pestilence出现1次
    philosophies出现1次
    phrase出现1次
    physical出现1次
    place出现2次
    poetry出现1次
    points出现1次
    possess出现1次
    posterity出现1次
    practice出现1次
    predominance出现1次
    present出现1次
    pride出现1次
    primarily出现1次
    principles出现1次
    profession出现1次
    promulgated出现1次
    proud出现1次
    purpose出现1次
    put出现1次
    quality出现1次
    rain出现1次
    rains出现1次
    rallying出现1次
    reach出现1次
    red出现1次
    regain出现1次
    regard出现1次
    regarded出现1次
    relentless出现1次
    reliable出现1次
    religious出现1次
    remarked出现1次
    replied出现1次
    required出现1次
    requirements出现1次
    resolute出现1次
    restraints出现1次
    reverberating出现1次
    reverently出现2次
    ridicule出现1次
    right出现1次
    roads出现1次
    roles出现1次
    s出现4次
    sacrifice出现1次
    say出现2次
    seat出现1次
    see出现1次
    seek出现2次
    seems出现2次
    self出现1次
    sense出现2次
    separation出现1次
    serious出现1次
    seriously出现1次
    served出现1次
    shell出现1次
    shocked出现1次
    shot出现1次
    should出现1次
    side出现1次
    slime出现1次
    slogan出现1次
    slogging出现1次
    sludge出现1次
    so出现3次
    soggy出现1次
    soldier出现2次
    soldiers出现1次
    some出现2次
    songs出现1次
    sorry出现1次
    sort出现1次
    sought出现1次
    springs出现1次
    spur出现1次
    staggering出现1次
    stainless出现1次
    stand出现2次
    statue出现1次
    stench出现1次
    storm出现1次
    storms出现1次
    story出现2次
    strength出现2次
    stress出现1次
    stricken出现1次
    strong出现1次
    substitute出现1次
    success出现1次
    successful出现1次
    such出现1次
    suns出现1次
    sure出现1次
    sustain出现1次
    sweat出现1次
    swift出现1次
    symbolize出现1次
    take出现2次
    teach出现2次
    tears出现1次
    tell出现1次
    temper出现1次
    temperamental出现1次
    test出现1次
    that出现13次
    the出现70次
    their出现11次
    them出现2次
    then出现1次
    there出现2次
    these出现1次
    they出现7次
    things出现3次
    think出现1次
    this出现7次
    those出现10次
    thousand出现1次
    three出现1次
    through出现2次
    time出现1次
    timidity出现1次
    to出现46次
    too出现1次
    torrential出现1次
    trails出现1次
    training出现1次
    trenches出现1次
    tribute出现1次
    tropical出现1次
    troublemaker出现1次
    true出现2次
    truth出现1次
    try出现1次
    unbelievers出现1次
    unbending出现1次
    uncomplaining出现1次
    under出现3次
    unfailing出现1次
    unquestioning出现1次
    up出现1次
    uplift出现1次
    us出现1次
    utter出现1次
    victory出现5次
    vigor出现1次
    virtues出现1次
    vision出现1次
    war出现1次
    was出现2次
    way出现3次
    we出现2次
    weak出现1次
    weary出现1次
    weep出现1次
    well出现1次
    what出现5次
    when出现8次
    which出现5次
    who出现2次
    will出现6次
    wind出现1次
    wisdom出现1次
    with出现6次
    witnessed出现1次
    wonder出现1次
    words出现5次
    world出现2次
    would出现1次
    written出现2次
    wrong出现1次
    years出现2次
    yet出现4次
    you出现16次
    your出现5次
    yourself出现3次
    youth出现1次

    Https: https://git.coding.net/yanzouzhe/ywcptj.git

    SSH:git@git.coding.net:yanzouzhe/ywcptj.git

    总结

      因为之前的基础不是很好,所以参考了网上和书上的很多资料,但是通过自己的学习,对文本文件的读取及字符统计等方面有了深刻的了解

      希望自己能不断努力,在学习中不断进步。

  • 相关阅读:
    Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
    分布式架构2:Nginx下Session存储
    分布式架构3:MySQL集群(Mysql Cluster7.5.5)
    分布式架构1:Nginx实现负载均衡
    Oracle 触发器记录
    [Deprecation Notice] 本博客弃用, 迁移至新博客
    KM算法 O(n^3)最大权完美匹配
    Python 学习笔记(附 Pytorch)
    你一定看的懂的:vlan与交换机端口的三种模式access,trunk和hybrid
    Git代码回滚
  • 原文地址:https://www.cnblogs.com/huangxman/p/5844627.html
Copyright © 2011-2022 走看看