继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Java计算字符串中的词频

霜花似雪
关注TA
已关注
手记 163
粉丝 1.5万
获赞 8507

方法一 采用Java8的方式计算

import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName WordFrequencyStatsJava
 * @Desc 计算字符串中的词频
 * @Author diandian
 **/
public class WordFrequencyStatsJava {

    /**
     * 采用Java8的方式计算字符串中的词频
     * @param str
     * @return
     */
    public Map<String, Long> getWordFreqStats(String str){
        Stream<String> stream = Stream.of(str.toLowerCase(Locale.ROOT).split("\\W+")).parallel();
        Map<String, Long> wordFreq = stream.collect(Collectors.groupingBy(String :: toString, Collectors.counting()));
        return wordFreq;
    }

    public static void main(String[] args) {
        String str = "A good persuasion : therefore , hear me , Hermia .\n" +
                "I have a widow aunt , a dowager \n" +
                "Of great revenue , and she hath no child :\n" +
                "From Athens is her house remote seven leagues ;\n" +
                "And she respects me as her only son .\n" +
                "There , gentle Hermia , may I marry thee ,\n" +
                "And to that place the sharp Athenian law \n" +
                "Cannot pursue us . If thou lov'st me then ,\n" +
                "Steal forth thy father's house to-morrow night ,\n" +
                "And in the wood , a league without the town ,\n" +
                "Where I did meet thee once with Helena ,\n" +
                "To do observance to a morn of May ,\n" +
                "There will I stay for thee .";
        WordFrequencyStatsJava statsJava = new WordFrequencyStatsJava();
        Map<String, Long> stringLongMap = statsJava.getWordFreqStats(str);
        stringLongMap.forEach((k,v) -> System.out.println(k + " 出现的个数:" + v));

    }
}


方法二:使用Apach 的 commons-math3包来计算

引入依赖:

<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-math3</artifactId>
     <version>3.6.1</version>
</dependency>
import org.apache.commons.math3.stat.Frequency;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @ClassName WordFrequencyStats
 * @Desc 计算字符串中的词频
 * @Author diandian
 **/
public class WordFrequencyStats {

    /**
     * 计算单词词频
     * @param words
     * @return
     */
    public Map<String, Integer> getWordFreqStats(String[] words){
        Frequency freq = new Frequency();
        for(int i = 0; i < words.length; i++){
            freq.addValue(words[i].trim());
        }

        Map<String, Integer> stringIntegerMap = new HashMap<>();
        for(int i = 0; i < words.length; i++){
            stringIntegerMap.put(words[i], (int)freq.getCount(words[i]));
        }
        return stringIntegerMap;
    }

    public static void main(String[] args) {
        WordFrequencyStats wordFreq = new WordFrequencyStats();
        String str = "A good persuasion : therefore , hear me , Hermia .\n" +
                "I have a widow aunt , a dowager \n" +
                "Of great revenue , and she hath no child :\n" +
                "From Athens is her house remote seven leagues ;\n" +
                "And she respects me as her only son .\n" +
                "There , gentle Hermia , may I marry thee ,\n" +
                "And to that place the sharp Athenian law \n" +
                "Cannot pursue us . If thou lov'st me then ,\n" +
                "Steal forth thy father's house to-morrow night ,\n" +
                "And in the wood , a league without the town ,\n" +
                "Where I did meet thee once with Helena ,\n" +
                "To do observance to a morn of May ,\n" +
                "There will I stay for thee .";

        String[] words = str.toLowerCase(Locale.ROOT).split("\\W+");
        Map<String, Integer> strMap = wordFreq.getWordFreqStats(words);
        for(String key : strMap.keySet()){
            Integer value = strMap.get(key);
            System.out.println(key + " 个数:" + value);
        }
    }
}






打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP