zoukankan      html  css  js  c++  java
  • 作业3

    作业3

    要求:

    1. 实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词的出现频率。

    2.性能分析:

    作业提示:

    • 字母: A-Z, a-z.
    • 字母数字: A-Z, a-z, 0-9.
    • 分隔符: 字母数字
    • 单词:
    • 包含有4个4个以上的字母
    • 单词分隔符分开
    • 如果一个字符串包含_非_字母数字,则不是单词
    • 单词大小写不敏感,例如 “file”、“FILE”和“File”可以看作同一个单词
    • 单词必须是字母开头,“file123”是单词,“123file”不是单词   

    本次题目我个人觉得十分的难,由于java才开始学许多的库不会用,只得上百度去寻找许许多多库的用法,在自己一遍一遍的翻书,来了解到本次编程中所需要用到的库,算法并不是太难,首先定义一个字符型,将计数的英语句子放入其中,然后用循环对其进行遍历,再把分隔符定义一下,例如“  ” “,” “.”等符号。

    再利用HashMap不允许重复的key,来对单词开始计数,大小写String b = c.toLowerCase();使用这个语句将所有大写变为小写,写到这里就只剩输出的是,还有大于4个字母才输出,最后写一个输出函数,最后将单词中长度大于3的输出,就完成了下面的代码。

    由于才开始学习java,还是觉得这个作业很有难度。

    刚刚又进行了完善补充了判断 “file123”是单词,“123file”不是单词的语句。

    源代码

    package sad;
     
    import java.util.Map;  
    import java.util.StringTokenizer;  
    import java.util.Map.Entry; 
    import java.util.ArrayList;  
    import java.util.HashMap;  
    import java.util.List;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    import java.util.Collections;  
    import java.util.Comparator; //所用到的库
        
    public class u  {  
    public static void main(String arg[]){      
      
        String sentence="Yesterday was my birthday, so some of my classmates sent me presents. Mother prepared a tea party for me."; //英文句子存放的地方
        Map<String,Integer> map=new HashMap<String,Integer>();  
        String turn_sentence= sentence.toLowerCase();//大小写转换语句
        StringTokenizer token=new StringTokenizer(turn_sentence); 
        while(token.hasMoreTokens()){   //遍历改英文句子
            
            String word=token.nextToken(", :"".“”");  
            if(map.containsKey(word)){      
                int count=map.get(word);  
                map.put(word, count+1);     
            }  
            else  
                map.put(word, 1);          
        }  
        small(map);                        
    }  
    public static boolean isNumeric(String str) {//判断是否为单词
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(str.charAt(0)+"");
    if (!isNum.matches()) {
    return false;
    }
    return true;
    }
    public static void small(Map<String,Integer> map){  //输出函数
        List<Map.Entry<String, Integer>> infoids = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());   
        Collections.sort(infoids, new Comparator<Map.Entry<String, Integer>>() {     //排序
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {     
                return (o2.getValue() - o1.getValue());     
            }     
    });
        for (int i = 0; i <infoids.size(); i++) {   
            Entry<String, Integer> id =infoids.get(i);  
            if(id.getKey().length()>3){//字符大于的输出
        System.out.println(id.getKey()+":"+id.getValue()); 
    }
            }  
    }  
    }  

    运行结果如下图:

    输入的句子为:Word is case insensitive, i.e. “file”, “FILEandFile” are considered the same word.

                         

    输入的句子为:

    Yesterday was my birthday, so some of my classmates sent me presents. Mother prepared a tea party for me.

                

    最后一次测试输入下面的句子:

    Yesterday was my birthday, so some of my classmates sent me presents. Mother prepared a tea party for me. I invited all of them to come and take part in it.The tea party began at half past six. There were cold drinks and refreshments. We ate, talked and laughed. We felt that we were the happiest men in the world.

              

    测试完毕程序无误。

    此次作业一写,感觉自己还有很多的知识仍然需要去学习。关于NetBeans IDE 6.0这个软件,暂时不会,日后学会了再写。

    Github

     

     

     

  • 相关阅读:
    ubuntu apt 主要命令及参数
    加速度计
    window下Opengl与vs2012环境配置
    java文本输入输出小结
    markdown + 七牛云,让写文更容易
    SpringBoot系列:Spring Boot使用模板引擎Thymeleaf
    SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
    SpringBoot系列:Spring Boot集成Spring Cache
    SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
    SpringBoot系列:Spring Boot使用模板引擎JSP
  • 原文地址:https://www.cnblogs.com/wjlxq/p/5276811.html
Copyright © 2011-2022 走看看