统计英文单词

本文档介绍了WordStatistic.java程序,用于读取英文文本文件,统计单词并找出无重复的单词。OutputWordMess.java展示了如何使用这个统计类,输出单词总数、唯一单词数及按频率排序的单词列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WordStatistic.java

import java.io.*;
import java.util.*;
public class WordStatistic {
    Vector<String> allWord,noSameWord;
    File file = new File("english.txt");
    Scanner sc = null;
    String regex;
    WordStatistic() {
        allWord = new Vector<String>();
        noSameWord = new Vector<String>();
//regex 是由空格、数字和符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)组成的正则表达式
        regex= "[\\s\\d\\p{Punct}]+";
        try{  sc = new Scanner(file); //创建指向file的sc
            sc.useDelimiter(regex);//sc调用useDelimiter(String regex)方法,向参数传递regex
        }
        catch(IOException exp) {
            System.out.println(exp.toString());
        }
    }
    void setFileName(String name) {
        file = new File(name);
        try{ sc = new Scanner(file);
            sc.useDelimiter(regex);
        }
        catch(IOException exp) {
            System.out.println(exp.toString());
        }
    }
    public void wordStatistic() {
        try{
            while(sc.hasNext()){
                String word = sc.next();
                allWord.add(word);
                if(!noSameWord.contains(word))
                    noSameWord.add(word);
            }
        }
        catch(Exception e){}
    }public Vector<String> getAllWord() {
        return allWord;
    }
    public Vector<String> getNoSameWord() {
        return noSameWord;
    }
}

OutputWordMess.java

import java.util.*;
public class OutputWordMess{
    public static void main(String args[]) {
        Vector<String> allWord,noSameWord;
        WordStatistic statistic =new WordStatistic();
        statistic.setFileName("hello.txt");
        statistic.wordStatistic(); //statistic调用wordStatistic()方法
        allWord=statistic.getAllWord();
        noSameWord=statistic.getNoSameWord();
        System.out.println("共有"+allWord.size()+"个英文单词");
        System.out.println("有"+noSameWord.size()+"个互不相同英文单词");
        System.out.println("按出现频率排列:");
        int count[]=new int[noSameWord.size()];
        for(int i=0;i<noSameWord.size();i++) {
            String s1 = noSameWord.elementAt(i);
            for(int j=0;j<allWord.size();j++) {
                String s2=allWord.elementAt(j);
                if(s1.equals(s2))
                    count[i]++;
            }
        }
        for(int m=0;m<noSameWord.size();m++) {
            for(int n=m+1;n<noSameWord.size();n++) {
                if(count[n]>count[m]) {
                    String temp=noSameWord.elementAt(m);
                    noSameWord.setElementAt(noSameWord.elementAt(n),m);
                    noSameWord.setElementAt(temp,n);
                    int t=count[m];
                    count[m]=count[n];
                    count[n]=t;
                }
            }
        }
        for(int m=0;m<noSameWord.size();m++) {
            double frequency=(1.0*count[m])/allWord.size();
            System.out.printf("%s:%-7.3f",noSameWord.elementAt(m),frequency);
        }
    }
}

运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值