当前位置 : 主页 > 编程语言 > c++ >

java统计英语单词词频并按降序排列

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import j
gistfile1.txt
import java.io.BufferedReader;    
import java.io.FileReader;    
import java.util.ArrayList;    
import java.util.Collections;    
import java.util.Comparator;    
import java.util.List;    
import java.util.Map;    
import java.util.Map.Entry;    
import java.util.TreeMap;    
    

public class Frequence {    
    
    public static void main(String[] args) throws Exception {    
        
        BufferedReader br = new BufferedReader(new FileReader("F:/source.txt"));    
        List
 
   lists = new ArrayList
  
   (); //存储过滤后单词的列表 String readLine = null; while((readLine = br.readLine()) != null){ String[] wordsArr1 = readLine.split(" "); //过滤出只含有字母的 [^a-zA-Z] 非单独字母 for (String word : wordsArr1) { if(word.length() != 0){ lists.add(word); } } } br.close(); Map
   
     wordsCount = new TreeMap
    
     (); //存储单词计数信息,key值为单词,value为单词数 for (String li : lists) { if(wordsCount.get(li) != null){ wordsCount.put(li,wordsCount.get(li) + 1); }else{ wordsCount.put(li,1); } } SortMap(wordsCount); } //按value的大小进行排序 public static void SortMap(Map
     
       oldmap){ ArrayList
      
       > list = new ArrayList
       
        >(oldmap.entrySet()); Collections.sort(list,new Comparator
        
         >(){ public int compare(Entry
         
           o1, Entry
          
            o2) { return o2.getValue().compareTo(o1.getValue()); //降序 } }); for(int i = 0; i
          
         
        
       
      
     
    
   
  
 
网友评论