Summary of 2024 Dewu School Recruitment Interview Questions and Answers (2)

6. How to count word frequency for an article

Word frequency statistics

Word frequency statistics refers to counting the number of times each word appears in the text. Word frequency statistics can be used in fields such as text analysis and natural language processing.

Manual statistics

Manual counting means counting each word in the text and recording the number of occurrences. This method is simple and easy, but less efficient.

Tool usage statistics

Using tool statistics refers to using special word frequency statistics tools to quickly count word frequencies in texts.

Java implementation

The following methods can be used in Java to implement word frequency statistics:

  • Use regular expressions

Regular expressions can be used to split words in text and then use a counter to record the number of times each word occurs.

Java

import java.util.regex.Pattern;

public class WordCount {

    public static void main(String[] args) throws Exception {
        String text = "今天天气很好,我去公园散步了。在公园里,我看到了许多花,也看到了许多小朋友在玩耍。";

        // 使用正则表达式分割文本中的单词
        Pattern pattern = Pattern.compile("\\W+");
        String[] words = pattern.split(text);

        // 使用计数器记录每个词出现的次数
        Map<String, Integer> wordCounts = new HashMap<>();
        for (String word : words) {
            if (word

Guess you like

Origin blog.csdn.net/cq20110310/article/details/132939115