Java实验--统计字母出现频率及其单词个数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java实验--统计字母出现频率及其单词个数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5635字,纯文字阅读大概需要9分钟。
内容图文
本周的实验要求在之前实现统计单词的基础之上(可以见之前博客的统计单词的那个实验),对其进行修改成所需要的格式,统计字母出现频率的功能,并按照一定的格式把最终结果的用特定的格式在文本中显示出来
统计过程的实现并不太麻烦,在原来的基础上导入导出函数的基础上修改成通用的类型,统计单词的那一部分的单个字符读取那一段加上统计字母的情况,并加上判断把大小写字母统一起来。
同时,在统计单词的那里加上一个无用字母的表格。这样就可以统计有用意义的前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>(); staticint g_Wordcount[]=newint[27]; staticint g_Num[]=newint[27]; static String []unUse=new String[] { "it", "in", "to", "of", "the", "and", "that", "for" }; publicstaticvoid main(String arg[]) { daoruFiles("piao.txt","tongji"); traverseFolder2("C:\\Users\\Halo\\javatest\\pipei\\piao"); } publicstaticvoid daoruFiles(String a,String dc) { map1.clear(); try { daoru(a); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } String sz[]; Integer num[]; finalint 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(); } //------------------------------ } publicstaticvoid 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(); } publicstaticvoid 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(); } publicstaticboolean isWord(char a) { if(a<=‘z‘&&a>=‘a‘||a<=‘Z‘&&a>=‘A‘||a==‘\‘‘) returntrue; returnfalse; } publicstaticboolean isInitWord(char a) { if(a<=‘z‘&&a>=‘a‘||a<=‘Z‘&&a>=‘A‘||a>‘0‘&&a<‘9‘||a==‘\‘‘) returntrue; returnfalse; } publicstaticboolean isBaseWord(String word) { for(int i=0;i<unUse.length;i++) { if(unUse[i].equals(word)||word.length()==1) returntrue; } returnfalse; } publicstaticint WordNum(char a) { if(a<=‘z‘&&a>=‘a‘) return a-‘a‘; elseif(a<=‘Z‘&&a>=‘A‘) return a-‘A‘; return -1; } //----递归文件夹publicstaticvoid 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秒以内,处理大文件及其多文件的过程在测试过程中没有出现问题。
原文:https://www.cnblogs.com/halone/p/10809496.html
内容总结
以上是互联网集市为您收集整理的Java实验--统计字母出现频率及其单词个数全部内容,希望文章能够帮你解决Java实验--统计字母出现频率及其单词个数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。