方法一 采用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);
}
}
}點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦