Java程序 - 计算从一个文本文件中的所有单词,并计算每个单词的频率 [英] Java program - Counts all the words from a text file, and counts frequency of each word

查看:345
本文介绍了Java程序 - 计算从一个文本文件中的所有单词,并计算每个单词的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学编程的,我尝试这样做,会打开一个大的文本中的文本文件一个程序,然后计算有多少字包含的内容。
那么就应该写不同的字有多少的文字,并写在文本每个单词的frecuency。
我不得不使用一个阵列串来存储所有的唯一字和一个INT-字符串存储频率的意向。

该方案计算的话,但我是一个有点不确定,我怎么能正确地写入code以获得他们重复在文本中的单词和频率的列表中。

我写这样的:

 进口easyIO *。
进口的java.util。*;类Oblig3A {
    公共静态无效的主要(字串[] args){
        INT CONT = 0;
        在读=新的In(alice.txt);
        在READ2 =新的In(alice.txt);        而(read.endOfFile()== FALSE)
        {
            字符串信息= read.inWord();
            的System.out.println(信息);
            续续= + 1;
        }        的System.out.println(UniqueWords);        最终诠释AN_WORDS =续;        的String []字=新的String [AN_WORDS]
        INT []频率=新INT [AN_WORDS]        INT I = 0;
        而(les2.endOfFile()== FALSE){
           字[I] = read2.inWord();
           I = I + 1;
        }
    }
}


解决方案

好吧,这里是你需要做什么:结果
1.使用的BufferedReader 来从文件中读取文本行,一个接一个。结果
2.创建一个的HashMap<字符串,整数方​​式> 来存储的话,频率的关系结果
3.当你阅读文本的每一行,使用拆分()来得到文本行的所有单词在的数组的String [] 结果
4.迭代每一个字。对于每一个词,检索从的HashTable 的值。如果你得到一个空值,你已经找到了第一次的单词。因此,创建一个新的整数值为1,并把它放回的HashMap 结果
如果你得到一个非空值,然后增加值,并把它放回的HashMap 。结果
5.执行此操作,直到你不EOF到达。

完成!

I'm a beginner programmer and I'm trying to do one program that opens a text file with a large text inside and then it counts how many words it contains. Then it should write how many different words are in the text, and write the frecuency of each word in the text. I had the intention to use one array-string to store all unique words and one int-string to store the frequency.

The program counts the words, but I'm a little bit unsure about how could I write the code correctly to get the list of the words and the frequency them are repeated in the text.

I wrote this:

import easyIO.*;
import java.util.*;

class Oblig3A{
    public static void main(String[] args){
        int cont = 0;
        In read = new In (alice.txt);
        In read2 = new In (alice.txt);

        while(read.endOfFile() == false)
        {
            String info = read.inWord();
            System.out.println(info);
            cont = cont + 1;
        }

        System.out.println(UniqueWords);

        final int AN_WORDS = cont;

        String[] words = new String[AN_WORDS];
        int[] frequency = new int[AN_WORDS];

        int i = 0;
        while(les2.endOfFile() == false){
           word[i] = read2.inWord();
           i = i + 1;
        }
    }
}

解决方案

Ok, here is what you need to do:
1. Use a BufferedReader to read the lines of text from the file, one by one.
2. Create a HashMap<String,Integer> to store the word, frequency relations.
3. When you read each line of text, use split() to get all the words in the line of text in an array of String[]
4. Iterate over each word. For each word, retrieve the value from the HashTable. if you get a null value, you have found the word for the first time. Hence, create a new Integer with value 1 and place it back in the HashMap
If you get a non-null value, then increment the value and place it back in the HashMap.
5. Do this till you do not reach EOF.

Done !

这篇关于Java程序 - 计算从一个文本文件中的所有单词,并计算每个单词的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆