20200917-2 词频统计

此作业的要求参见[https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206]

github链接:[https://github.com/userTeng/yang]

词频统计 SPEC
老五在寝室吹牛他熟读过《鲁滨逊漂流记》,在女生面前吹牛热爱《呼啸山庄》《简爱》和《飘》,在你面前说通读了《战争与和平》。但是,他的四级至今没过。你们几个私下商量,这几本大作的单词量怎么可能低于四级,大家听说你学习《构建之法》,一致推举你写个程序名字叫wf,统计英文作品的单词量并给出每个单词出现的次数,准备用于打脸老五。

希望实现以下效果。以下效果中数字纯属编造。

功能1 小文件输入。 为表明程序能跑,结果真实而不是迫害老五,请他亲自键盘在控制台下输入命令。
这一功能的需要我们去读取文件,然后对读取后的文件进行切割操作。
难点:对于读取到的数据数组,找出重复的单词,我使用HasMap来进行存储,单词第一出现放入Hash Map中,Key为单词,value为1,当出现重复单词时value加1。


重要文件读取代码如下:

public static String  fileRead(String name) throws Exception {
    File file = new File(name);
    FileReader reader = new FileReader(file);
    BufferedReader bReader = new BufferedReader(reader);
    StringBuilder sb = new StringBuilder();
    String s = "";
    while ((s =bReader.readLine()) != null) {
        sb.append(s);
    }
    bReader.close();
    String str = sb.toString();
    return str;
}

通过HashMap来对读取到的字符数组进行处理,代码如下:

public static void deal(String[] word,int len){
  int len2=0;
  HashMap<String,Integer> map=new HashMap<String,Integer>();
  for(int i=0;i<len;i++){
    int sight=0;
    len2=map.size();
    if(len2==0){
      map.put(word[i],1);
    }
    else{    
      for (Map.Entry<String, Integer> vo : map.entrySet()) {
        if(vo.getKey().equals(word[i])){
          map.put(vo.getKey(),vo.getValue()+1);
          sight=1;
          break;
         }
       }
    if(sight==0)
      map.put(word[i],1);
    }            
  }
  System.out.printf("total          %2d   words
",map.size());
  List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
  list.sort(new Comparator<Map.Entry<String, Integer>>() {
    @Override
    public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
      return o2.getValue().compareTo(o1.getValue());
    }
   });
  map.put(null,null);
  int temp=0;
  for (Map.Entry<String, Integer> mapping : list){
    temp++;
    if(temp==10)
      break;
    System.out.printf("%-15s%2d
",mapping.getKey(),mapping.getValue());
    }
}

 功能2 支持命令行输入英文作品的文件名,请老五亲自录入

 

重要代码片段:

public static void function1(String arg){
  int len=0;
  String txt
="";
  String name
=arg+".txt";
  
try{
    txt
=fileRead(name);//fileRead函数已在功能一中插入,在此不再重复
  }
catch(Exception e){
    e.printStackTrace();
  }
  System.
out.println(txt);
  txt
=txt.replaceAll("[\pP\p{Punct}]", " ");
  String[] word
=txt.split(" +");
  len
=word.length;
  deal(word,len); //deal函数已在功能一中插入,在此不再重复。
}

功能3 支持命令行输入存储有英文作品文件的目录名,批量统计。

重要代码片段:

public static void function3(String arg){
  String filepath="";
  filepath="E:\GIT\cangku\yang\"+arg+"\";
  ArrayList<String> files = new ArrayList<String>();
  File file = new File(filepath);
  File[] tempLists = file.listFiles();
  for (int i = 0; i < tempLists.length; i ++) {
    if (tempLists[i].isFile()) {
      files.add(tempLists[i].toString());     }   }   
for (int i = 0; i < files.size(); i++) {     control(files.get(i));     System.out.println("--------------------------------------------------");   } }
public static void  control(String name){    
  int len=0;
  String txt="";
  System.out.println(name.substring(24));
  try{
    txt=fileRead(name);
  }catch(Exception e){
    e.printStackTrace();
  }
  txt=txt.replaceAll("[\pP\p{Punct}]", " ");
  String[] word=txt.split(" +");
  len=word.length;    
  deal(word,len);        
 }  

 功能4 从控制台读入英文单篇作品,这不是为了打脸老五,而是为了向你女朋友炫酷,表明你能提供更适合嵌入脚本中的作品(或者如她所说,不过是更灵活的接口)。如果读不懂需求,请教师兄师姐,或者 bing: linux 重定向,尽管这个功能在windows下也有,搜索关键词中加入linux有利于迅速找到。

功能四效果如下:

重要代码片段如下:

public static void function4(){
  int len=0;
  Scanner sc=new Scanner(System.in);
  String txt="";
  txt=sc.nextLine();
  String[] word=txt.split(" |\.|,");
  len=word.length;        
  deal(word,len);    
}

PSP:







原文地址:https://www.cnblogs.com/shizhiteng/p/13720915.html