詞頻項目

github:https://github.com/zhonghongyao/WordSplit.git
java

1.需求分析


使用JAVA編程語言,獨立完成一個英文文本詞頻統計的軟件開發。軟件基本功能要求以下:
1.程序可讀入任意英文文本文件,該文件中英文詞數大於等於1個。
2.程序須要很壯健,能讀取容納英文原版《哈利波特》10萬詞以上的文章。
3.指定單詞詞頻統計功能:用戶可輸入從該文本中想要查找詞頻的一個或任意多個英文單詞,運行程序的統計功能可顯示對應單詞在文本中出現的次數和柱狀圖。
4.高頻詞統計功能:用戶從鍵盤輸入高頻詞輸出的個數k,運行程序統計功能,可按文本中詞頻數降序顯示前k個單詞的詞頻及單詞。
5.統計該文本全部單詞數量及詞頻數,並能將單詞及詞頻數按字典順序輸出到文件words.txt。

2. 功能設計


利用java的輸入輸出實現對文本文件的讀取、利用map<String,Integer>存儲數據,利用Comparator排序,進行詞頻統計後輸出單詞的使用次數。

3.測試


圖片名稱


圖片名稱


圖片名稱

4.代碼塊


while ((line = br.readLine()) != null) {

switch (count){
                case 1:String strSource[]=line.split(" ");
                    for (int i = 0; i < strArray.length; i++) {
                        for (int j = 0; j < strSource.length; j++) {
                            if(strArray[i].equals(strSource[j])){
                                arrCount[i]++;
                            }

                        }

                    }
                default:String strAll[]=line.split(" ");
                    for (int i = 0; i <strAll.length ; i++) {
                        if(map.size()==0){

                          map.put(strAll[i],1);
                            continue;
                        }
                        int size=map.size();

                        boolean flag=false;
                        for (Map.Entry<String, Integer> entry : map.entrySet()) {

                            if(entry.getKey().equals(strAll[i])){
                                int temp=entry.getValue();

                                entry.setValue(++temp);
                                flag=true;
                            }
                        }
                        if(flag==false){

                            map.put(strAll[i],1);
                        }
                    }

                    break;
            }
        }
        //輸出
        if(count==1){
            for (int i = 0; i <strArray.length ; i++) {

                System.out.print(strArray[i]+"-----數量:"+arrCount[i]+" ");
                //柱形圖
            }
        }else if(count==2){

            Map<String,Integer>mapResult=MapSort.sortMapByValue(map);
            for (Map.Entry<String, Integer> entry : mapResult.entrySet()) {
                if(k==0){
                    break;
                }else {
                    System.out.println("單詞= " + entry.getKey() + "  數量= " + entry.getValue());
                    k--;
                }
            }

        }else if(count==3){
           Map<String,Integer>mapRe= MapSort.sortMapByValue(map);
            writeFile(mapRe);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

}

5.總結

本項目採用類思想分塊處理,實現了軟件工程的「高內聚,低耦合」思想git

6.psp展現

圖片名稱

相關文章
相關標籤/搜索