java 找出文档中十个高频词 修改版

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class li1 {
 static String[] a;
 public static void main(String[] args) throws IOException {
  //获取文件的内容
  String msg=getFromTxt("D:/li.txt");
  //消除标点符号的影响
  msg=delectChar(msg);
  //用空格分隔字符窜成字符数组
  String[] b=msg.split(" ");
  //存储键值对的键
  a=new String[b.length];
  int c=0;
  //以键值对的形势存储单个字符窜,重复的键值对,键不变,值加1;初次出现的键值对,值置为1
  Map map=new HashMap<String,Integer>();
  for(int i=0;i<b.length;i++){
   boolean isHas=false;
   String sub=b[i];
   for(int j=0;j<i;j++){
    if(isNormalWord(sub)==true){
     continue;
    }
    if(sub.equals(a[j])){
     map.put(sub,Integer.parseInt(map.get(sub)+"")+1);
     isHas=true;
     break;
    }
   }
   if(!isHas){
    a[c++]=sub;
    map.put(sub, 1);
   }
  }
  //对键值对的值用直接插入排序
  map=insertionsort(map);
  //输出前十个高频词
  for(int k=0;k<10 && k<a.length;k++){
   System.out.println(a[k]+"---------"+map.get(a[k]));
  }
 }
 //直接插入排序  从大到小
 public static Map insertionsort(Map map){
  for(int i=1;i<map.size();i++){
   int item=Integer.parseInt(map.get(a[i])+"");
   String bb=a[i];
   int j=i-1;
   while(item>Integer.parseInt(map.get(a[j])+"")){
    a[j+1]=a[j];
    if(j>=1){
     j=j-1;
    }else{
     j=j-1;
     break;
    }
   }
   a[j+1]=bb;

  }
  return map;
 }
 //消除标点符号
 public static String delectChar(String msg){
  msg=msg.replace(",", "");
  msg=msg.replace(".", "");
  msg=msg.replace("!", "");
  msg=msg.replace("?", "");
  msg=msg.replace(";", "");
  return msg;
 }
 //消除无用词
 public static boolean isNormalWord(String msg){
  if(msg.equals("and") ||
    msg.equals("in")||
    msg.equals("to")||
    msg.equals("had")||
    msg.equals("to")||
    msg.equals("have")||
    msg.equals("a")||
    msg.equals("the")||
    msg.equals("me")||
    msg.equals("would")||
    msg.equals("I")||
    msg.equals("as")||
    msg.equals("of")||
    msg.equals("but")||
    msg.equals("were")||
    msg.equals("still")||
    msg.equals("below")||
    msg.equals("he")||
    msg.equals("was")||
    msg.equals("that")||
    msg.equals("his")||
    msg.equals("could")||
    msg.equals("for")||
    msg.equals("He")||
    msg.equals("on")||
    msg.equals("it")||
    msg.equals("us")||
    msg.equals("not")||
    msg.equals("We")||
    msg.equals("we")||
    msg.equals("with")||
    msg.equals("When")||
    msg.equals("when")||
    msg.equals("him")||
    msg.equals("our")||
    msg.equals("before")||
    msg.equals("few")||
    msg.equals("how")||
    msg.equals("at"))
   return true;
  else
   return false;
 }
 //读取文本内容
 public static String getFromTxt(String filePath) throws IOException{
  File file=new File(filePath);
  BufferedReader br=new BufferedReader(new FileReader(file));
  String result ="";
  String data=null;
  while((data=br.readLine())!=null){
   result+=data;
  }
  return result;
 }
}

 

第一篇测试文档

 

运行结果


性能测试结果

 

 

测试文档2

 

测试结果

 

 

性能测试

 

 

心得体会

做高频词的查找需要考虑一些无用词的干扰,需要建立一个消除干扰词和标点符号影响的类,才能达到更加准确地找出有用的高频次。在写程序的时候也需要多多考虑程序的性能,尽可能写出高效简洁的程序。这对于提高自己的编程水平有着很重要的意义。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值