第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Java計算字符串中的詞頻

標(biāo)簽:
Java

方法一 采用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);
        }
    }
}






點擊查看更多內(nèi)容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
1.5萬
獲贊與收藏
8507

關(guān)注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消