使用ChatGPT4协助完成读取文件中不同字的数量
        【摘要】 使用ChatGPT4协助完成读取文件中不同字的数量使用ChatGPT4识别:用java读取文件中不同字的个数。解析:该程序将读取名为“file.txt”的文件,并计算文件中每个不同字的出现次数。它使用一些字符串操作来清理单词,并使用HashMap来存储每个单词的出现数。最后,程序打印出不同字的总数。//ChatGPT4完成的代码和我所需要的有点差异import java.io.Buffere...
    
    
    
    
使用ChatGPT4识别:用java读取文件中不同字的个数。
解析:该程序将读取名为“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);
            }
        }
    }
}
测试样例:
a b c d dsvsdvsdvsdvdsvd汪汪汪
鲁迅《从百草园到三味书屋》滴滴滴
结果:
不同中文的个数: 13
记录每一个学习瞬间
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)