繁体   English   中英

字长频率计数器

[英]Word Length Frequency Counter

我到目前为止的代码是

import java.io.*;

import static java.lang.System.*;

public class Curtis_Rodney_group8 {

    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("body.txt"); 
            BufferedReader br = new BufferedReader(fr); 

            String body;
            while ((body = br.readLine()) != null) { //read a line at a time
                out.println(body + "\n"); //disply the text untill the end of the file
            }

            br.close();
        } catch (IOException e) {
            out.println("File not found"); //if the file name is incorrect 


        }

    }
}

这段代码打印出我想要的文件body.txt的内容。

但是我现在希望能够有一个字长频率计数器。 例如,句子“ I am a man将产生输出2, 1, 1 (即,两个单词的长度为1,一个单词的长度为2,一个单词的长度为3)。

我不是一个非常有经验的程序员,即时通讯也不在寻找直接的答案。 我想知道现在如何开始下一段代码,我想我使用主体部分,因为它是字符串,并且使用了body = br.readLine() 我不确定下一个代码和平如何开始。 我是否要为下一部分代码创建一个新类。 希望您能理解我的要求,感谢您的帮助。

请看下面的代码

public class FrequencyCounter {
public static void main(String args[]) {
    try {
        FileReader fr = new FileReader("body.txt");
        BufferedReader br = new BufferedReader(fr);
        Map<Integer, Integer> lengthCounter = new HashMap<Integer, Integer>();

        String body;
        while ((body = br.readLine()) != null) { // read a line at a time
            System.out.println(body);
            String[] textSplit = body.split(" ");
            for(int i=0;i<textSplit.length;i++){
                if(lengthCounter.keySet().contains(textSplit[i].length())){
                    lengthCounter.put(textSplit[i].length(),lengthCounter.get(textSplit[i].length())+1);
                } else {
                    lengthCounter.put(textSplit[i].length(),1);
                }
            }
        }

        Iterator<Integer> iter = lengthCounter.keySet().iterator();
        while(iter.hasNext()){
            int x=iter.next();
            System.out.println("Length : "+ x + " ... Freq : "+ lengthCounter.get(x));
        }
        br.close();
    } catch (IOException e) {
        System.out.println("File not found"); // if the file name is
                                                // incorrect
    }

}
}

基本上,这里的想法是我正在使用映射来存储字符串的每个长度和该长度的单词的频率。

您执行拆分操作以获取从文本文件读取的行中的每个单词,然后检查之前是否遇到过相同长度的单词。 如果不是,则将该长度添加到地图中,否则将该长度的先前现有值作为键增加1。

我得到的以下代码的输出是:

hello
my name is Abhi
I am a guy

Length : 1 ... Freq : 2
Length : 2 ... Freq : 3
Length : 3 ... Freq : 1
Length : 4 ... Freq : 2
Length : 5 ... Freq : 1

你好,我叫阿比,我是一个男人

是从文件读取的文本。

希望能有所帮助。

以下是使用数组的解决方案。 这应该更容易理解。 该解决方案的唯一缺点是,我们假设您的文本中一个单词的最大长度可以为99。

int[] lengthCounterArray = new int[100];

如果您可以使用类似的约束条件,则此解决方案将为您工作。

public class FrequencyCounter{
public static void main(String[] args) {
    try {
        FileReader fr = new FileReader("body.txt");
        BufferedReader br = new BufferedReader(fr);
        Map<Integer, Integer> lengthCounter = new HashMap<Integer, Integer>();
        int[] lengthCounterArray = new int[100]; // assuming the maximum
                                                    // word length would be
                                                    // 99 for this program
        Arrays.fill(lengthCounterArray, 0);// initializing array values to 0
        String body;
        while ((body = br.readLine()) != null) { // read a line at a time
            System.out.println(body);
            String[] textSplit = body.split(" ");
            for (int i = 0; i < textSplit.length; i++) {
                lengthCounterArray[textSplit[i].length()] += 1;
            }
        }

        for(int i =0;i<100;i++) {
            if(lengthCounterArray[i]==0)
                continue;
            else {
                System.out.println(" Length : "+i+" ... Freq : "+lengthCounterArray[i]);
            }
        }
        br.close();
    } catch (IOException e) {
        System.out.println("File not found"); // if the file name is
                                                // incorrect
    }
}

此代码段的输出与上一个代码相同

hello
my name is Abhi
I am a guy
 Length : 1 ... Freq : 2
 Length : 2 ... Freq : 3
 Length : 3 ... Freq : 1
 Length : 4 ... Freq : 2
 Length : 5 ... Freq : 1

希望能有所帮助。

您可能想要另一个类(我们将其称为FrequencyCounter),该类将一行文本(在方法中-称为processLine ),将其拆分为单词,并使用每个单词的长度将计数器更新为特定长度。 您可以使用Map或List,但是如果您知道最大可能的字长(例如,在大多数情况下, int [100]应该绰绰有余),则使用数组可能会更简单快捷。 例如,在processLine()中 ,如果遇到单词“ man”,则将length设置为3,然后更新计数器( this.counter [length] ++ )。

在您现有的代码中,在循环中,您将调用myFrequencyCounter.processLine(body) - myFrequencyCounter是新类(FrequencyCounter)的实例,您需要在while循环开始之前对其进行实例化。

当while循环完成时,myFrequencyCounter将具有其计数器字段,即一个整数数组,其中索引是长度,而值是频率计数。 您可以为FrequencyCounter提供一种打印频率的方法,并在while循环之后调用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM