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

在这里插入图片描述

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

使用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

记录每一个学习瞬间

  • 48
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 87
    评论
评论 87
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忆~遂愿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值