簡體   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