1. 程式人生 > >Java實驗--統計字母出現頻率及其單詞個數

Java實驗--統計字母出現頻率及其單詞個數

trace ise 出現次數 [] 頻率 getparent 小寫字母 throws ole

本周的實驗要求在之前實現統計單詞的基礎之上(可以見之前博客的統計單詞的那個實驗),對其進行修改成所需要的格式,統計字母出現頻率的功能,並按照一定的格式把最終結果的用特定的格式在文本中顯示出來

統計過程的實現並不太麻煩,在原來的基礎上導入導出函數的基礎上修改成通用的類型,統計單詞的那一部分的單個字符讀取那一段加上統計字母的情況,並加上判斷把大小寫字母統一起來。

同時,在統計單詞的那裏加上一個無用字母的表格。這樣就可以統計有用意義的前n個最常用的單詞了。

實驗的代碼如下所示:

package pipei;
//洪鼎淇 20173627 信1705-3
import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.text.DecimalFormat; import java.util.HashMap; import java.util.Map; //哈利波特單詞統計 public class Pipei { public static
Map<String,Integer> map1=new HashMap<String,Integer>(); static int g_Wordcount[]=new int[27]; static int g_Num[]=new int[27]; static String []unUse=new String[] { "it", "in", "to", "of", "the", "and", "that",
"for" }; public static void main(String arg[]) { daoruFiles("piao.txt","tongji"); traverseFolder2("C:\\Users\\Halo\\javatest\\pipei\\piao"); } public static void daoruFiles(String a,String dc) { map1.clear(); try { daoru(a); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } String sz[]; Integer num[]; final int MAXNUM=10; //統計的單詞出現最多的前n個的個數 for(int i=0;i<g_Wordcount.length;i++) { g_Wordcount[i]=0; g_Num[i]=i; } sz=new String[MAXNUM+1]; num=new Integer[MAXNUM+1]; Pipei pipei=new Pipei(); int account =1; //Vector<String> ve1=new Vector<String>(); try { daoru(a); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } System.out.println("英文單詞的出現情況如下:"); int g_run=0; for(g_run=0;g_run<MAXNUM+1;g_run++) { account=1; for(Map.Entry<String,Integer> it : Pipei.map1.entrySet()) { if(account==1) { sz[g_run]=it.getKey(); num[g_run]=it.getValue(); account=2; } if(account==0) { account=1; continue; } if(num[g_run]<it.getValue()) { sz[g_run]=it.getKey(); num[g_run]=it.getValue(); } //System.out.println("英文單詞: "+it.getKey()+" 該英文單詞出現次數: "+it.getValue()); } Pipei.map1.remove(sz[g_run]); } int g_count=1; String tx1=new String(); String tx2=new String(); for(int i=0;i<g_run;i++) { if(sz[i]==null) continue; if(sz[i].equals("")) continue; tx1+="出現次數第"+(g_count)+"多的單詞為:"+sz[i]+"\t\t\t出現次數: "+num[i]+"\r\n"; System.out.println("出現次數第"+(g_count)+"多的單詞為:"+sz[i]+"\t\t\t出現次數: "+num[i]); g_count++; } try { daochu(tx1,dc+"2.txt"); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } //------------------------------ int temp=g_Wordcount[0]; int numtemp=0; for(int i=0;i<26;i++) { for(int j=i;j<26;j++) { if(g_Wordcount[j]>g_Wordcount[i]) { temp=g_Wordcount[i]; g_Wordcount[i]=g_Wordcount[j]; g_Wordcount[j]=temp; numtemp=g_Num[i]; g_Num[i]=g_Num[j]; g_Num[j]=numtemp; } } } int sum=0; for(int i=0;i<26;i++) { sum+=g_Wordcount[i]; } for(int i=0;i<26;i++) { char c=(char) (‘a‘+g_Num[i]); tx2+=c+":"+String.format("%.2f%% \r\n", (double)g_Wordcount[i]/sum*100); } try { daochu(tx2,dc+"1.txt"); } catch (IOException e) { // TODO 自動生成的 catch 塊 e.printStackTrace(); } //------------------------------ } public static void daoru(String s) throws IOException { File a=new File(s); FileInputStream b = new FileInputStream(a); InputStreamReader c=new InputStreamReader(b,"UTF-8"); String string2=new String(""); while(c.ready()) { char string1=(char) c.read(); if(WordNum(string1)>=0) { g_Wordcount[WordNum(string1)]+=1; } //------------------------ if(!isWord(string1)) { if(!isBaseWord(string2)) { if(map1.containsKey(string2.toLowerCase())) { Integer num1=map1.get(string2.toLowerCase())+1; map1.put(string2.toLowerCase(),num1); } else { Integer num1=1; map1.put(string2.toLowerCase(),num1); } } string2=""; } else { if(isInitWord(string1)) { string2+=string1; } } } if(!string2.isEmpty()) { if(!isBaseWord(string2)) { if(map1.containsKey(string2.toLowerCase())) { Integer num1=map1.get(string2.toLowerCase())+1; map1.put(string2.toLowerCase(),num1); } else { Integer num1=1; map1.put(string2.toLowerCase(),num1); } } string2=""; } c.close(); b.close(); } public static void daochu(String txt,String outfile) throws IOException { File fi=new File(outfile); FileOutputStream fop=new FileOutputStream(fi); OutputStreamWriter ops=new OutputStreamWriter(fop,"UTF-8"); ops.append(txt); ops.close(); fop.close(); } public static boolean isWord(char a) { if(a<=‘z‘&&a>=‘a‘||a<=‘Z‘&&a>=‘A‘||a==‘\‘‘) return true; return false; } public static boolean isInitWord(char a) { if(a<=‘z‘&&a>=‘a‘||a<=‘Z‘&&a>=‘A‘||a>‘0‘&&a<‘9‘||a==‘\‘‘) return true; return false; } public static boolean isBaseWord(String word) { for(int i=0;i<unUse.length;i++) { if(unUse[i].equals(word)||word.length()==1) return true; } return false; } public static int WordNum(char a) { if(a<=‘z‘&&a>=‘a‘) return a-‘a‘; else if(a<=‘Z‘&&a>=‘A‘) return a-‘A‘; return -1; } //----遞歸文件夾 public static void traverseFolder2(String path) { File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (null == files || files.length == 0) { System.out.println("文件夾是空的!"); return; } else { for (File file2 : files) { if (file2.isDirectory()) { System.out.println("文件夾:" + file2.getAbsolutePath()); traverseFolder2(file2.getAbsolutePath()); } else { System.out.println("文件:" + file2.getAbsolutePath()); String name=file2.getName(); daoruFiles(file2.getAbsolutePath(), file2.getParentFile()+"\\"+name.replace(".txt", "")+"tongji"); } } } } else { System.out.println("文件不存在!"); } } }

將飄的整本小說及其分章節放在一個文件夾中,最終的實驗結果如下:

技術分享圖片

tongji1位後綴的是文章字母構成比例(以整本飄的英文小說為例子):

技術分享圖片

tongji2的實驗結果是有意義單詞的出現次數前10的排名:

技術分享圖片

對整本飄小說處理的時間級別在1秒以內,處理大文件及其多文件的過程在測試過程中沒有出現問題。

Java實驗--統計字母出現頻率及其單詞個數