写一个脚本以统计一个文本文件 words.txt 中每个单词出现的频率。 为了简单起见,你可以假设: words.txt只包括小写字母和 ‘ ‘ ; 每个单词只由小写字母组成。 单词间由一个或多个空格字符分隔。 示例: 假设 words.txt 内容如下: the day is sunny the the
the sunny is is 你的脚本应当输出(以词频降序排列): the 4 is 3 sunny 2 day 1
public class WordFrequency { public static void main(String[] args) { long startTime=System.nanoTime(); //获取开始时间 String string = ""; Map<String, Integer> map = new HashMap<String,Integer>(); try { //[1] 读取 2.txt 文本 FileInputStream fis = new FileInputStream("/Users/sweetgirl/Documents/MyCode/2.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String temp = ""; try { while((temp = br.readLine()) != null) { string = string + temp; } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } //[2] 分割字符串 StringTokenizer st = new StringTokenizer(string); //用于切分字符串 int count; String word; while(st.hasMoreTokens()) { word = st.nextToken(",?.!:\"\"' '\n"); if (map.containsKey(word)) { //[3] HashMap 保存数据 count = map.get(word); map.put(word, count + 1); }else { map.put(word, 1); } } //[4] 排序 Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String,Integer>>() { public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) { return o2.getValue()-o1.getValue(); } }; //[5] 输出结果 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); Collections.sort(list,valueComparator); System.out.println("---------------------map 按照 value 降序排序----------"); for(Map.Entry<String, Integer> entry:list) { System.out.println(entry.getKey() + ":"+ entry.getValue()); } long endTime=System.nanoTime(); //获取结束时间 System.out.println("程序运行时间: "+(endTime-startTime)+"ns"); }
}
测试文本:
Photo sphere panoramic camera function; keyboard gesture input function; improved lock screen function, including support for desktop pendant and direct opening camera function in lock screen state; expandable notification, allowing users to directly open the application; Gmail mail zoom display; Daydream screen saver Program; the user can zoom in on the entire display three times, and can also rotate and zoom display with two fingers, as well as voice output and gesture mode navigation designed for blind users; support Miracast wireless display sharing function; Google Now is now available Allow users to use Gamail as a new source of data, such as improved flight tracking, hotel and restaurant reservations, and music and movie recommendations.Photo sphere panoramic camera function; keyboard gesture input function; improved lock screen function, including support for desktop pendant and direct opening camera function in lock screen state; expandable notification, allowing users to directly open the application; Gmail mail zoom display; Daydream screen saver Program; the user can zoom in on the entire display three times, and can also rotate and zoom display with two fingers, as well as voice output and gesture mode navigation designed for blind users; support Miracast wireless display sharing function.