java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼
更新時(shí)間:2013年03月30日 09:51:29 作者:
java 字符串詞頻統(tǒng)計(jì)實(shí)例代碼,需要的朋友可以參考一下
復(fù)制代碼 代碼如下:
package com.gpdi.action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordsStatistics {
class Obj {
int count ;
Obj(int count){
this.count = count;
}
}
public List<WordCount> statistics(String word) {
List<WordCount> rs = new ArrayList<WordCount>();
Map <String,Obj> map = new HashMap<String,Obj>();
if(word == null ) {
return null;
}
word = word.toLowerCase();
word = word.replaceAll("'s", "");
word = word.replaceAll(",", "");
word = word.replaceAll("-", "");
word = word.replaceAll("\\.", "");
word = word.replaceAll("'", "");
word = word.replaceAll(":", "");
word = word.replaceAll("!", "");
word = word.replaceAll("\n", "");
String [] wordArray = word.split(" ");
for(String simpleWord : wordArray) {
simpleWord = simpleWord.trim();
if (simpleWord != null && !simpleWord.equalsIgnoreCase("")) {
Obj cnt = map.get(simpleWord);
if ( cnt!= null ) {
cnt.count++;
}else {
map.put(simpleWord, new Obj(1));
}
}
}
for(String key : map.keySet()) {
WordCount wd = new WordCount(key,map.get(key).count);
rs.add(wd);
}
Collections.sort(rs, new java.util.Comparator<WordCount>(){
@Override
public int compare(WordCount o1, WordCount o2) {
int result = 0 ;
if (o1.getCount() > o2.getCount() ) {
result = -1;
}else if (o1.getCount() < o2.getCount()) {
result = 1;
}else {
int strRs = o1.getWord().compareToIgnoreCase(o2.getWord());
if ( strRs > 0 ) {
result = 1;
}else {
result = -1 ;
}
}
return result;
}
});
return rs;
}
public static void main(String args[]) {
String word = "Pinterest is might be aa ab aa ab marketer's dream - ths site is largely used to curate products " ;
WordsStatistics s = new WordsStatistics();
List<WordCount> rs = s.statistics(word);
for(WordCount word1 : rs) {
System.out.println(word1.getWord()+"*"+word1.getCount());
}
}
}
您可能感興趣的文章:
- Java編程實(shí)現(xiàn)統(tǒng)計(jì)一個(gè)字符串中各個(gè)字符出現(xiàn)次數(shù)的方法
- java統(tǒng)計(jì)字符串中重復(fù)字符出現(xiàn)次數(shù)的方法
- java統(tǒng)計(jì)字符串中指定元素出現(xiàn)次數(shù)方法
- java中對(duì)字符串每個(gè)字符統(tǒng)計(jì)的方法
- java實(shí)現(xiàn)統(tǒng)計(jì)字符串中字符及子字符串個(gè)數(shù)的方法示例
- Java簡(jiǎn)單統(tǒng)計(jì)字符串中漢字,英文字母及數(shù)字?jǐn)?shù)量的方法
- Java統(tǒng)計(jì)字符串中字符出現(xiàn)次數(shù)的方法示例
- java8 統(tǒng)計(jì)字符串字母?jìng)€(gè)數(shù)的幾種方法總結(jié)(推薦)
- java統(tǒng)計(jì)字符串單詞個(gè)數(shù)的方法解析
- Java實(shí)現(xiàn)統(tǒng)計(jì)字符串出現(xiàn)的次數(shù)
相關(guān)文章
java中多個(gè)@Scheduled定時(shí)器不執(zhí)行的解決方法
在應(yīng)用開(kāi)發(fā)中經(jīng)常需要一些周期性的操作,比如每5分鐘執(zhí)行某一操作等,這篇文章主要給大家介紹了關(guān)于java中多個(gè)@Scheduled定時(shí)器不執(zhí)行的解決方法,需要的朋友可以參考下2023-04-04Java中MyBatis Plus知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理一篇關(guān)于Java中MyBatis Plus知識(shí)點(diǎn)總結(jié),需要的朋友們參考下。2019-10-10java 如何遠(yuǎn)程控制tomcat啟動(dòng)關(guān)機(jī)
這篇文章主要介紹了java 遠(yuǎn)程控制tomcat啟動(dòng)關(guān)機(jī)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了JAVA操作HDFS案例的簡(jiǎn)單實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況
這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08JAXB命名空間及前綴_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要給大家介紹了關(guān)于JAXB命名空間及前綴的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08在idea中將java項(xiàng)目中的單個(gè)類(lèi)打包成jar包操作
這篇文章主要介紹了在idea中將java項(xiàng)目中的單個(gè)類(lèi)打包成jar包操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08