今天老师又安排了一个任务:统计一个文件中出现最多的几个单词出现的频率。
怎么说呢,还是一点不会,只能上网搜,通过两小时的奋斗,我还是没能做出来,但是我知道了如何从读取文件中的信息的代码以及如何统计单词频率的代码;然而,我无法把他们合起来,也无法理解许多代码的作用,就只知道其功能,所以我想,我还是该再去借一本书随身背着,然后就是多花时间学习java了。
今晚又经过三个多小时,又搜了搜想了想,还把一些相似的代码进行对照,然而我还是无法改动代码分毫,我觉得我很无能,感觉三个多小时是白搭了,一事无成,也开始慌了,真的真的该下功夫学习java了。
以下是我的未实现功能的代码:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class test { public static void findEnglishNum(String text){ //找出所有的单词 String[] array = {".", " ", "?", "!"}; for (int i = 0; i < array.length; i++) { text = text.replace(array[i],","); } String[] textArray = text.split(","); //遍历 记录 Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < textArray.length; i++) { String key = textArray[i]; //转为小写 String key_l = key.toLowerCase(); if(!"".equals(key_l)){ Integer num = map.get(key_l); if(num == null || num == 0){ map.put(key_l, 1); }else if(num > 0){ map.put(key_l, num+1); } } } //输出到控制台 System.out.println("各个单词出现的频率为:"); Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext()){ String key = iter.next(); Integer num = map.get(key); System.out.println(key + "\n\t\t" + num + "次\n-------------------"); } } public static void main(String[] args) { fread("G:\\yingyu.txt"); findEnglishNum() } // 读取文件: public static void fread(String fileurl) { File file = new File(fileurl); BufferedReader bfr = null; try { bfr = new BufferedReader(new FileReader(file)); String tem = null; String value = ""; while ((tem = bfr.readLine()) != null) { value = value + tem; } System.out.println(value); // 将读取的字符串转换成字符数组: char[] c = value.toCharArray(); // 定义一个map来存储结果: // HashMap<Character,Integer> tm = new // HashMap<Character,Integer>(Collections.reverseOrder()); TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>(Collections.reverseOrder());// TreeMap可排序(传入一个反转比较器) for (int i = 0; i < c.length; i++) { char charSrc = c[i]; if (tm.containsKey(charSrc)) { // 判断该键的值是否存在 int count = tm.get(charSrc); tm.put(charSrc, count + 1); } else { tm.put(charSrc, 1); } } // 取出Map中的键和值 Iterator<Map.Entry<Character, Integer>> titer = tm.entrySet().iterator(); while (titer.hasNext()) { Map.Entry<Character, Integer> map = titer.next(); char key = map.getKey(); int valu = map.getValue(); System.out.println(key + "出现过" + valu + "次!"); } } catch (Exception e) { System.err.println("文件读取错误"); } finally { try { if (bfr != null) { bfr.close(); } } catch (Exception e2) { System.err.println("文件关闭错误"); } } } }
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; public class tongji { public static String txt2String(File file){ StringBuilder result = new StringBuilder(); try{ BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件 String s = null; while((s = br.readLine())!=null){//使用readLine方法,一次读一行 result.append(System.lineSeparator()+s); } br.close(); }catch(Exception e){ e.printStackTrace(); } return result.toString(); } //读取文件信息的方法。 public static void findEnglishNum(String text){ //找出所有的单词 String[] array = {".", " ", "?", "!"}; for (int i = 0; i < array.length; i++) { text = text.replace(array[i],","); } String[] textArray = text.split(","); //遍历 记录 Map<String, Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < textArray.length; i++) { String key = textArray[i]; //转为小写 String key_l = key.toLowerCase(); if(!"".equals(key_l)){ Integer num = map.get(key_l); if(num == null || num == 0){ map.put(key_l, 1); }else if(num > 0){ map.put(key_l, num+1); } } } //输出到控制台 System.out.println("各个单词出现的频率为:"); Iterator<String> iter = map.keySet().iterator(); while(iter.hasNext()){ String key = iter.next(); Integer num = map.get(key); System.out.println(key + "\n\t\t" + num + "次\n-------------------"); } } public static void main(String[] arg) { File file = new File("G:/yingyu.txt"); System.out.println(txt2String(file)); String abc = "Welcome welcome to ADempiere, a commons-based peer-production of Open Source ERP Applications. This Wiki is for the global community to contribute and share know-how and domain expertise. We hope you can find as much open information and participate in making it most usable for everyone. This project has a bazaar of Citizens with a Community Council Team which work in theFunctional Team and Technical Team along the Software Development Procedure supported and funded by the foundation ADempiere"; findEnglishNum(abc); } }
原文地址:https://www.cnblogs.com/sljslj/p/9775513.html
时间: 2024-11-10 11:09:02