课堂练习6--统计txt文本

统计文本中26个字母的频率:

package bao;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat; public class Text2 { private static String formattedDecimalToPercentage(double decimal)
{
//获取格式化对象
NumberFormat nt = NumberFormat.getPercentInstance();
//设置百分数精确度2即保留两位小数
nt.setMinimumFractionDigits(2);
return nt.format(decimal);
}
public static void main(String []args) {
String a1;
char a='A';
int a2[]=new int[27];
char b1[]=new char[26];
char b2[]=new char[26]; for(int i=0;i<26;i++)
{
b1[i]=a;
b2[i]=(char)(a+32);
a++;
} for(int i=0;i<26;i++) { System.out.println(b1[i]);
System.out.println(b2[i]); } try {
BufferedReader in = new BufferedReader(new FileReader("D:\\Downloadbaidu\\2.txt"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
char[] d=str.toCharArray();
for(int i=0;i<d.length-1;i++) {
for(int j=0;j<26;j++) {
if(b1[j]==d[i]||b2[j]==d[i]) {
a2[j]++;
} } } } a2[26]=0;
for(int i=0;i<26;i++) {
a2[26]=a2[i]+a2[26];
} for(int i=0;i<26;i++) {
System.out.print(b1[i]);
System.out.print("和");
System.out.print(b2[i]);
System.out.print("出现的次数为:");
System.out.println(a2[i]);
double d=(double)((double)a2[i]/(double)a2[26]);
String result2=formattedDecimalToPercentage(d);
System.out.println("result = "+result2); } } catch (IOException e) {
} }
}

统计文本中单词出现次数:

package bao;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*; public class Text3 {
public static void main(String [] args) throws Exception { BufferedReader br = new BufferedReader(new FileReader("D:\\Downloadbaidu\\2.txt")); StringBuffer sb = new StringBuffer();
String text =null;
while ((text=br.readLine())!= null){
sb.append(text);// 将读取出的字符追加到stringbuffer中
}
br.close(); // 关闭读入流 String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
Map<String ,Integer> map = new HashMap<String, Integer>() ; for(String word :words){
if(map.get(word)==null){ // 若不存在说明是第一次,则加入到map,出现次数为1
map.put(word,1);
}else{
map.put(word,map.get(word)+1); // 若存在,次数累加1
}
} // 排序
List<Map.Entry<String ,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet()); Comparator<Map.Entry<String,Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
return (left.getValue().compareTo(right.getValue()));
}
};
// 集合默认升序升序
Collections.sort(list,comparator); for(int i=0;i<list.size();i++){// 由高到低输出
System.out.println(list.get(list.size()-i-1).getKey() +":"+list.get(list.size()-i-1).getValue());
} }

学习于网络。

上一篇:Server Develop (七) Linux 守护进程


下一篇:快速排序的c++实现 和 python 实现