Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 如何修复某些迭代的方法打印空值?_File_Bufferedreader_Filereader - Fatal编程技术网

File 如何修复某些迭代的方法打印空值?

File 如何修复某些迭代的方法打印空值?,file,bufferedreader,filereader,File,Bufferedreader,Filereader,编写一个程序,计算文件中文本的词汇丰富度和最常用单词的频率。词汇丰富度是文本中的单词数量除以不同单词的数量。一个单词的频率是该单词在文本中被提及的次数除以文本中的单词总数 定义并实现类WordCounter,其中包含两个私有字段String word和int count、构造函数WordCounter(String word)、公共方法String getName()、int getCount()和void addToCounter() 使用一个私有字段ArrayListwords、构造函数Co

编写一个程序,计算文件中文本的词汇丰富度和最常用单词的频率。词汇丰富度是文本中的单词数量除以不同单词的数量。一个单词的频率是该单词在文本中被提及的次数除以文本中的单词总数

定义并实现类
WordCounter
,其中包含两个私有字段String word和int count、构造函数
WordCounter(String word)
、公共方法
String getName()
int getCount()
void addToCounter()

使用一个私有字段
ArrayList
words、构造函数
Corpus(BufferedReader Infire)
、公共方法
double getVocabularyRichness()
String getMostFrequentWord()
定义并实现类
Corpus
(与文本语料库中一样)

执行一个测试程序(作为
Corpus
中的公共静态void main方法),读取特定文件夹中的所有文件,从每个(以前打开的)文件创建一个
Corpus
对象,并将请求的统计信息保存到另一个文件stats.csv中。您可以为每个文件创建一个新的语料库对象,也可以定义语料库的
ArrayList

CSV文件的每一行都必须由三个字段组成,三个字段之间用逗号隔开(但不能用空格!):文件名、词汇丰富性和最常用的单词。在所有莎士比亚戏剧上运行您的程序。将CSV文件与Java文件一起提交

我写了我认为是HW问题的正确实现,因为它对一些文本文件正常工作,但是只有
words.get(I).getName()
(我用
words.get(I).getCount()
)方法会为一些文件打印空白。我什么都试过了,似乎都想不出来。关于如何解决这个问题,你能给我一个提示或一些指导吗

public class Corpus {

    private ArrayList<WordCounter> words = new ArrayList <WordCounter>() ;

    Corpus(BufferedReader infile){
    String ln;
        try {
            while((ln = infile.readLine()) != null) {
                for (String word : ln.toLowerCase().split("([,.\\s]+)")) {
                    int reference = 0;
                    for(int i = 0; i < words.size(); i++) { 
                        if (word.equals(words.get(i).getName())) { 
                            reference++; 
                            words.get(i).addToCounter();
                        } }
                    if (reference==0) { words.add(new WordCounter(word)); }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    public double getVocabularyRichness() {
        int word_count=0;
        for(int i = 0; i < words.size(); i++) {
            word_count=word_count+words.get(i).getCount();
        }
        return (double)word_count/(double)words.size();

    }
    public String getMostFrequentWord() {
     String winner = "*AN ERROR OCCURRED*";
        int max_count = 0;
        for(int i = 0; i < words.size(); i++) {
            if(words.get(i).getCount() > max_count){ 
                max_count = words.get(i).getCount(); 
            }
        }
        for(int i = 0; i < words.size(); i++) {
            if(words.get(i).getCount() == max_count){ 
                winner = words.get(i).getName();

            }
        }

        //winner="Test " + String.valueOf(words.get(i).getName());;
        //return String.valueOf(max_count);
        return winner;   
    }

    public static void main(String [] args) throws Exception{
            BufferedWriter writer = null;
            File folder_location = new File("/Users/joaquindelaguardia/Desktop/Shakespeare");
            File[] file_array = folder_location.listFiles();
            for(File iteration_file: file_array) {
                FileReader current_file = new FileReader(iteration_file);
                BufferedReader infile = new BufferedReader(current_file);

                Corpus obj1 = new Corpus(infile);
                String file_name = iteration_file.getName();
                String frequent_word = obj1.getMostFrequentWord();
                String vocabulary_richness = String.valueOf(obj1.getVocabularyRichness());

                System.out.println(file_name);
                System.out.println(frequent_word);
                System.out.println(vocabulary_richness);
                System.out.println("-----------------------------");


                //FileWriter file_writer = new FileWriter("/Users/joaquindelaguardia/Desktop/stats.csv");
                //writer = new BufferedWriter(file_writer);

                //String output = file_name+", "+frequent_word+", "+vocabulary_richness + "\n";

                //writer.append(output);

            }
            //writer.close();

    }

}

public class WordCounter {

    private String word;
    private int count=1;

    WordCounter(String word){
        this.word=word;
    }

    public String getName() {
        return word;
    }

    public int getCount() {
        return count;
    }

    public void addToCounter() {
        count++;
    }
}
公共类语料库{
private ArrayList words=new ArrayList();
语料库(BufferedReader infle){
字符串ln;
试一试{
而((ln=infle.readLine())!=null){
用于(字符串字:ln.toLowerCase().split(([,。\\s]+))){
int reference=0;
对于(inti=0;imax_count){
max_count=words.get(i).getCount();
}
}
for(int i=0;i
我通过在附加到文件之前打印来测试信息,正如您从下面包含的输出的小片段中看到的,在某些情况下,它打印最常见的单词(and),而在第二种情况下,它不打印任何内容

莎士比亚-情人-62.txt

2.2409948542024014 莎士比亚-朱利叶斯-26.txt

6.413205537806177