文本文档中多个词语出现的次数
由于女友最近要交毕业论文,想要将自己整理的文档,查询其中的高频词汇,我就找了一段查词代码并进行修改,算是满足了基本要求!!!分享一下
源码
public static void main(String[] args) { String path = null; int j =1; String[] a = { "时间", "小时", "酒店", "景区", "游客", "公里", "景点", "地方", "分钟", "晚上", "选择", "开车", "出发", "到达", "租车", "公园", "停车场", "门票", "下午", "然后", "前往", "游览", "当地", "停车", "体验", "住宿", "游记", "附近", "高速", "吃饭" }; for (;j < 61; j++) { path= "D:\\" + j + ".txt"; System.out.println("\n"); System.out.println("第"+j+"篇"); for (int i = 0; i < a.length; i++) { serchKey(j,a[i],path); } } } private static void serchKey(int j,String key,String path) { BufferedReader br = null; try { br = new BufferedReader(new FileReader(path)); StringBuffer sb = new StringBuffer(); String str = null; while ((str = br.readLine()) != null) { sb.append(str); } String regex = key.trim(); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(sb); int num = 0; while (matcher.find()) { num++; } System.out.println(regex + ":" + num); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (null != br) { // 关闭资源 br.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
比对才是核心!!!

浙公网安备 33010602011771号