使用ChatGPT4协助完成读取文件中不同字的数量

科技资讯 投稿 6000 0 评论

使用ChatGPT4协助完成读取文件中不同字的数量

解析:该程序将读取名为“file.txt”的文件,并计算文件中每个不同字的出现次数。它使用一些字符串操作来清理单词,并使用HashMap来存储每个单词的出现数。最后,程序打印出不同字的总数。

//ChatGPT4完成的代码和我所需要的有点差异
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

public class CountDistinctWords {

    public static void main(String[] args {

        try {
            BufferedReader reader = new BufferedReader(new FileReader("file.txt";
            String line = reader.readLine(;
            Map<String, Integer> wordCount = new HashMap<String, Integer>(;
            while (line != null {
                String[] words = line.split(" ";
                for (String word : words {
                    String cleanedWord = word.replaceAll("[^a-zA-Z0-9]", "".toLowerCase(;
                    if (!cleanedWord.isEmpty( {
                        if (wordCount.containsKey(cleanedWord {
                            int count = wordCount.get(cleanedWord;
                            wordCount.put(cleanedWord, count + 1;
                        } else {
                            wordCount.put(cleanedWord, 1;
                        }
                    }
                }
                line = reader.readLine(;
            }
            reader.close(;
            System.out.println("Distinct words: " + wordCount.size(;
        } catch (Exception e {
            e.printStackTrace(;
        }

    }

}

使用ChatGPT4实现的是读取文件中除去中文的字母,而且用空格间隔的数量。

主要流程:使用缓冲流读取文件,使用Hashmap来读取每一行的中文,通过正则表达式来过滤出中文,用每一个中文当键,最后只需要读取HashMap的大小即可知道有几个中文。

//根据需求修改后的代码
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class readWordCount {
    public static void main(String[] args {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("read_word_count/file.txt";
            String line = null;
            Map<Character, Integer> wordCount = null;
            int count = 0;
            wordCount = new HashMap<Character, Integer>(;
            while (true {
                line = reader.readLine(;
                if(line == null{
                    break;
                }
                line = line.replaceAll("[^\\u4E00-\\u9FA5]", "";
                char[] words = line.toString(.toCharArray(;
                for (char word : words {
                    if (wordCount.containsKey(word {
                        count = wordCount.get(word;
                        wordCount.put(word, count + 1;
                    } else {
                        wordCount.put(word, 1;
                    }
                }
            }
            System.out.println("不同中文的个数: " + wordCount.size(;
        } catch (Exception e {
            e.printStackTrace(;
        }finally {
            try {
                reader.close(;
            } catch (IOException e {
                throw new RuntimeException(e;
            }
        }
    }
}

测试样例:

鲁迅《从百草园到三味书屋》滴滴滴

不同中文的个数: 13

记录每一个学习瞬间

编程笔记 » 使用ChatGPT4协助完成读取文件中不同字的数量

赞同 (27) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽