Java combines the Chinese word segmentation library jieba to count the number of occurrences of each word in a bunch of text [code record]

Article directory

1. Demand

insert image description here

2. Code

package com.zibo.main;

import com.huaban.analysis.jieba.JiebaSegmenter;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordCount {
    
    

    public static void main(String[] args) {
    
    
        String file = "C:\\Users\\Administrator\\Desktop\\video_film_no_spaces.txt";

        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
    
    
            // 读取文本
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
    
    
                content.append(line);
            }
            String text = content.toString();
            // 创建一个HashMap用于存储词语和出现次数
            Map<String, Integer> wordCounts = new HashMap<>();

            // 使用"jieba"分词库进行中文分词
            JiebaSegmenter segmenter = new JiebaSegmenter();
            List<String> words = segmenter.sentenceProcess(text);

            // 遍历每个词语并统计出现次数
            for (String word : words) {
    
    
                // 如果词语已经在HashMap中,则将其计数加1
                if (wordCounts.containsKey(word)) {
    
    
                    int count = wordCounts.get(word);
                    wordCounts.put(word, count + 1);
                }
                // 如果词语不在HashMap中,则将其添加到HashMap并设置计数为1
                else {
    
    
                    wordCounts.put(word, 1);
                }
            }

            // 输出每个词语及其出现次数
            for (Map.Entry<String, Integer> entry : wordCounts.entrySet()) {
    
    
                String word = entry.getKey();
                // 如果文字长度小于 2 ,跳过
                if (word.length() < 2) {
    
    
                    continue;
                }
                // 如果文字不是中文,跳过
                if (!word.matches("[\\u4e00-\\u9fa5]+")) {
    
    
                    continue;
                }
                int count = entry.getValue();
                System.out.println(word + " 出现了 " + count + " 次");
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
}

3. Results

D:\MySoft\Environment\Java\jdk-17.0.5\bin\java.exe "-javaagent:D:\MySoft\JetBrains\IntelliJ IDEA 2022.3.1\lib\idea_rt.jar=54655:D:\MySoft\JetBrains\IntelliJ IDEA 2022.3.1\bin" -Dfile.encoding=UTF-8 -classpath 
......
com.zibo.main.WordCount
main dict load finished, time elapsed 603 ms
model load finished, time elapsed 29 ms.
惊栗 出现了 2 次
经典 出现了 9 次
剧情片 出现了 1 次
音乐喜剧 出现了 32 次
儿童 出现了 23 次
灾难 出现了 39 次
同性 出现了 23 次
犯罪 出现了 963 次
喜剧片 出现了 1 次
动画 出现了 81 次
传记 出现了 57 次
惊悚 出现了 804 次
冒险 出现了 548 次
奇幻 出现了 490 次
爱情喜剧 出现了 370 次
枪战 出现了 1 次
历史 出现了 120 次
伦理 出现了 18 次
其他 出现了 1 次
音乐 出现了 30 次
剧情 出现了 1725 次
爱情 出现了 502 次
家庭 出现了 205 次
动作 出现了 3004 次
喜剧 出现了 2807 次
情色 出现了 4 次
性喜剧 出现了 28 次
脱口秀 出现了 9 次
科幻 出现了 347 次
运动 出现了 74 次
恐怖 出现了 291 次
戏曲 出现了 2 次
黑色 出现了 1 次
电影 出现了 1 次
西部 出现了 53 次
悬疑剧 出现了 33 次
纪录片 出现了 8 次
歌舞 出现了 38 次
文艺 出现了 6 次
纪录 出现了 2 次
悬疑 出现了 252 次
古装 出现了 208 次
古装剧 出现了 92 次
短片 出现了 9 次
歌舞剧 出现了 7 次
武侠 出现了 194 次
战争 出现了 212 次
鬼怪 出现了 1 次

Process finished with exit code 0


Guess you like

Origin blog.csdn.net/qq_29689343/article/details/131326619