博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
单词统计
阅读量:6136 次
发布时间:2019-06-21

本文共 6054 字,大约阅读时间需要 20 分钟。

1统计字母出现的概率(不分大小写)

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.text.DecimalFormat;import java.text.NumberFormat;public class a {    private static NumberFormat nf = new DecimalFormat("0.00");    public static void main(String[] args) throws IOException {        BufferedReader br = new BufferedReader(new FileReader("f:/piao.txt"));        StringBuilder sb = new StringBuilder();        while (true) {            String line = br.readLine();            if (line == null)                break;            sb.append(line);        }        br.close();        int[] characters = new int[128];        for (byte bt : sb.toString().getBytes())            if (bt > 0 && bt < 128)                characters[bt]++;        int totalCount = 0;        for (int i = 'a'; i <= 'z'; i++) {            totalCount += (characters[i] + characters[i - 32]);        }        double c;                      for (int i = 'a'; i <= 'z'; i++) {            c=(double)(Math.round((characters[i] + characters[i - 32]) * 100 / totalCount)/100.0);                    System.out.print((char) i + "=" + (characters[i] + characters[i - 32]) + "(" +c + "),");                     }    }}

截图:

2统计单词出现的次数

import java.io.BufferedReader;import java.io.FileReader;import java.util.*;public class b {    public static void main(String [] args) throws Exception {    	        BufferedReader br = new BufferedReader(new FileReader("f:/飘c1.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
map = new HashMap
() ; 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
> list = new ArrayList
>(map.entrySet()); Comparator
> comparator = new Comparator
>() { public int compare(Map.Entry
left, Map.Entry
right) { return (left.getValue().compareTo(right.getValue())); } }; // 集合默认升序升序 Collections.sort(list,comparator); for(int i=0;i

  截图

3去除无用词

public class English_word {   public static void main(String[] args) throws FileNotFoundException {  File file = new File("D:\\Englis_letter.txt");// 读取文件  String words[] = new String [100000];  int out_words[] = new int [100000];  if (!file.exists()) {// 如果文件打不开或不存在则提示错误   System.out.println("文件不存在");   return;  }  Scanner x = new Scanner(file);  HashMap
hashMap = new HashMap
(); while (x.hasNextLine()) { String line = x.nextLine(); String[] lineWords = line.split("[\\s+\t”“();,.?!\n]"); Set
wordSet = hashMap.keySet(); for (int i = 0; i < lineWords.length; i++) { if (wordSet.contains(lineWords[i])) { Integer number = hashMap.get(lineWords[i]); number++; hashMap.put(lineWords[i], number); } else { hashMap.put(lineWords[i], 1); } } } Iterator
iterator = hashMap.keySet().iterator(); int max = 0,i=0; while (iterator.hasNext()) { String word = iterator.next(); if(!"".equals(word)&&word!=null&&!"a".equals(word)&&!"the".equals(word)&&!"  ".equals(word)) { System.out.println(word); words[i]=word; out_words[i]=hashMap.get(word); i++; } } int change=0; String change_word=null; for(int j=0;j<=i;j++) { for(int k=j;k<=i;k++) { if(out_words[k]>out_words[j]) { change=out_words[j]; change_word=words[j]; out_words[j]=out_words[k]; words[j]=words[k]; out_words[k]=change; words[k]=change_word; } } } Scanner scan = new Scanner(System.in); int ms = scan.nextInt(); for(int j=0;j

  

4遍历文件统计

public class test {     static String words[] = new String [100000];    static   int out_words[] = new int [100000];    static int i=0;    static HashMap
hashMap = new HashMap
(); public static void English_words(File ms) throws FileNotFoundException { File file = new File(ms.toString());// 读取文件 if (!file.exists()) {
// 如果文件打不开或不存在则提示错误 System.out.println("文件不存在"); return; } Scanner x = new Scanner(file); while (x.hasNextLine()) { String line = x.nextLine(); String[] lineWords = line.split("[\\s+\t”“();,.?!\n]"); Set
wordSet = hashMap.keySet(); for (int i = 0; i < lineWords.length; i++) { if (wordSet.contains(lineWords[i])) { Integer number = hashMap.get(lineWords[i]); number++; hashMap.put(lineWords[i], number); } else { hashMap.put(lineWords[i], 1); } } } } public static void main(String[] args) throws FileNotFoundException { String path = "d:/"; File file = new File(path); File[] tempList = file.listFiles(); for (int i = 0; i < tempList.length; i++) { if (tempList[i].toString().endsWith("txt")) { System.out.println("文 件:" + tempList[i]); English_words(tempList[i]); } } Iterator
iterator = hashMap.keySet().iterator(); int max = 0; while (iterator.hasNext()) { String word = iterator.next(); if(!"".equals(word)&&word!=null&&!"a".equals(word)&&!"the".equals(word)&&!"  ".equals(word)) { words[i]=word; out_words[i]=hashMap.get(word); i++; } } int change=0; String change_word=null; for(int j=0;j<=i;j++) { for(int k=j;k<=i;k++) { if(out_words[k]>out_words[j]) { change=out_words[j]; change_word=words[j]; out_words[j]=out_words[k]; words[j]=words[k]; out_words[k]=change; words[k]=change_word; } } } Scanner scan = new Scanner(System.in); int ms = scan.nextInt(); for(int j=0;j

 

转载于:https://www.cnblogs.com/xuange1/p/10994422.html

你可能感兴趣的文章
架构师之路(一)- 什么是软件架构
查看>>
jquery的冒泡和默认行为
查看>>
USACO 土地购买
查看>>
【原创】远景能源面试--一面
查看>>
B1010.一元多项式求导(25)
查看>>
10、程序员和编译器之间的关系
查看>>
前端学习之正则表达式
查看>>
配置 RAILS FOR JRUBY1.7.4
查看>>
AndroidStudio中导入SlidingMenu报错解决方案
查看>>
修改GRUB2背景图片
查看>>
Ajax异步
查看>>
好记性不如烂笔杆-android学习笔记<十六> switcher和gallery
查看>>
JAVA GC
查看>>
codeforce 599B Spongebob and Joke
查看>>
3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
查看>>
9、Dubbo-配置(4)
查看>>
前端第七天
查看>>
图解SSH原理及两种登录方法
查看>>
[转载] 七龙珠第一部——第058话 魔境圣地
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>