用javaIO流读取文本中英文字母和英文单词的出现次数及频率

一、读取文本中英文字母出现的次数并降序输出英文字母的百分比

源码;

package total;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

public class Statistics_letter {

    public static void main(String[] args) throws IOException  {
        // TODO Auto-generated method stub
        FileReader fr=new FileReader("a.txt");
        BufferedReader bufr=new BufferedReader(fr);
        StringBuffer str=new StringBuffer();
        String Line=null;
        while((Line=bufr.readLine())!=null) {
            str.append(Line);
        }
        bufr.close();

        double capitalletter[]=new double[26];
        double lowercaseletter[]=new double[26];
        int count=0;
        for(int i=0;i<str.length();i++) {
            char ch=str.charAt(i);
            if(ch>=‘A‘&&ch<=‘Z‘||ch>=‘a‘&&ch<=‘z‘) {
                for(int j=0;j<26;j++) {
                    if(ch==‘A‘+j)
                    capitalletter[j]++;
                }
                for(int k=0;k<26;k++) {
                    if(ch==‘a‘+k)
                        lowercaseletter[k]++;
                }
                count++;
            }
        }

        double percentage1[]=new double[52];
        double percentage2[]=new double[52];
        for(int i=0;i<26;i++) {
            percentage1[i]=capitalletter[i]/count;
            percentage2[i]=percentage1[i];
        }
        for(int i=26;i<52;i++) {
            percentage1[i]=lowercaseletter[i-26]/count;
            percentage2[i]=percentage1[i];
        }
        Arrays.sort(percentage1);
        for(int i=51;i>=0;i--) {
            int max=0;
            for(int j=0;j<52;j++) {
                if(percentage2[j]==percentage1[i])
                    max=j;
            }
            if(max>=26)
                System.out.print(((char)(‘a‘+max-26))+":");
            else
            System.out.print(((char)(‘A‘+max))+":");
            System.out.println(String.format("%.2f",percentage1[i]*100)+‘%‘);
        }
        System.out.println("英文字母总数为:"+count);
    }
}

运行结果截图:

二、读取文本中的英文单词并按出现次数降序输出结果

源码:

package total;

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.util.HashMap;
import java.util.Map;

public class Statistics_words {
    public Map<String, Integer> map1 = new HashMap<String, Integer>();

    public static void main(String arg[]) throws IOException {
        String sz[];
        Integer num[];
        final int MAXNUM = 20; 

        sz = new String[MAXNUM + 1];
        num = new Integer[MAXNUM + 1];
        Statistics_words Statistics_words = new Statistics_words();
        int account = 1;
        // Vector<String> ve1=new Vector<String>();
            Statistics_words.textImport();
        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 : Statistics_words.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());
            }
            Statistics_words.map1.remove(sz[g_run]);
        }
        int g_count = 1;
        String tx1 = 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++;
        }
            Statistics_words.textExport(tx1);

    }

    public void textImport() throws IOException {

        File a = new File("C:\\Users\\22400\\Desktop\\a.txt");
        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 (!isWord(string1)) {
                if (map1.containsKey(string2)) {
                    Integer num1 = map1.get(string2) + 1;
                    map1.put(string2, num1);
                } else {
                    Integer num1 = 1;
                    map1.put(string2, num1);
                }
                string2 = "";
            } else {
                string2 += string1;
            }
        }
        if (!string2.isEmpty()) {
            if (map1.containsKey(string2)) {
                Integer num1 = map1.get(string2) + 1;
                map1.put(string2, num1);
            } else {
                Integer num1 = 1;
                map1.put(string2, num1);
            }
            string2 = "";
        }
        c.close();
        b.close();
    }

    public void textExport(String txt) throws IOException {
        File fi = new File("StatisticsWord.txt");
        FileOutputStream fop = new FileOutputStream(fi);
        OutputStreamWriter ops = new OutputStreamWriter(fop, "UTF-8");
        ops.append(txt);
        ops.close();
        fop.close();
    }

    public boolean isWord(char a) {
        if (a <= ‘z‘ && a >= ‘a‘ || a <= ‘Z‘ && a >= ‘A‘)
            return true;
        return false;
    }

}

原文地址:https://www.cnblogs.com/weixiao1717/p/12151740.html

时间: 2024-08-07 00:12:45

用javaIO流读取文本中英文字母和英文单词的出现次数及频率的相关文章

统计文本中英文字母及英文单词的次数并排序

一.读取文本中英文字母出现的次数并降序输出英文字母的百分比 源码: package total; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; public class Statistics_letter { public static void main(String[] args) throws IOException

C#读取文本播放相应语音【转】

第一种方案: 利用微软text to speech引擎(TTS),读取文本 (1)添加Microsoft Speech Object Library的项目引用 (2)引入using SpeechLib名称控件 (3)读取文本 SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync;         SpVoice voice = new SpVoice(); //默认使用“控制面板”—>“声音”—>“录制”选项卡的“配置

javaIo流

转载自:http://ggicci.blog.163.com/blog/static/2103640962012813997493/ Java : 关于 Java 流的笔记 State : Java 流分为 字符流 和 字节流 两种.分别四个抽象类 Reader/Writer.InputStream/OutputStream 表示. Hierachy : Note :    抽象类       无公共构造器      已弃用   父类/超类 -----> 子类 How to use ? Read

使用字符流读取文本文件

1.字符输入流Reader类 Reader类是读取字符流的抽象类,它提供了常用的方法. Reader rd= new FileReader("Test/xy.txt");// int b=rd.read(); //从输入流中读取单个字符// System.out.println((char)b); char[] chars = new char[10]; //从输入流中读取chars.length长度的字符 int length = rd.read(chars);   System.o

容易使用的读取文本播放器 Text to Speech Maker 2.5

FilmConvert Stand Alone 1.216 MacOSXAutodesk.Smoke.v2015.SP1.MacOSX 1DVDAutodesk Smoke 2015提供更快的效能和更平易近人的价格专 为以Mac计算机作业的小型工作室设计,Smoke 2015专业影音特效和剪辑工具现在具备了 3D追踪.新的Timeline FX工作流程.针对搭载OS X Mavericks操作系统的新版Mac Pro新增硬件支持和系统运作的最佳化,并与Final Cut Pro X提供更佳的互通

javaIO流实现读写txt文件

javaIO流实现文件读写 文件写入: InputStreamReader BufferedReader文件读取:FileOutputStream package javatest.basic22; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStr

HTML 文档流和文本流的理解

文本流,概括地说其实就是一系列字符,是文档的读取和输出顺序,也就是我们通常看到的由左到右.由上而下的读取和输出形式,在网页中每个元素都是按照这个顺序进行排序和显示的,而position属性可以将元素从文本流脱离出来显示. 文档流,英文原版文档为"normal flow",翻译成常规流.普通流也就更好理解它了. 从直观上理解,常规流指的是元素按照其在 HTML 中的位置顺序决定排布的过程,主要的形式是自上而下(块级元素),一行接一行,每一行从左至右(行内元素). 定位类型包括三种: 常规

面试题目java读取文本内容方式

面试题目java读取文本内容方式二种方式 第一种通过FileInputStream()方式读取 FileInputStream fis = new FileInputStream("a.txt"); //创建流对象 byte[] arr = new byte[4]; int len; while((len = fis.read(arr)) != -1) { System.out.print(new String(arr,0,len)); } fis.close(); 第二种通过:Fil

python 逐行读取文本

f = open("foo.txt") # 返回一个文件对象line = f.readline() # 调用文件的 readline()方法while line: print line, # 后面跟 ',' 将忽略换行符 # print(line, end = '') # 在 Python 3中使用 line = f.readline() f.close() 也可以写成以下更简洁的形式 for line in open("foo.txt"): print line,