最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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());
        }
    }

}

相關(guān)文章

最新評論

五大连池市| 南阳市| 南和县| 金溪县| 通河县| 锡林浩特市| 伊金霍洛旗| 庆云县| 宜州市| 睢宁县| 罗山县| 衢州市| 方城县| 资阳市| 娱乐| 会东县| 玉溪市| 原阳县| 襄城县| 利川市| 永定县| 尼玛县| 安阳市| 平顺县| 和田市| 和平县| 麻阳| 扬州市| 宝兴县| 湛江市| 博乐市| 长岛县| 梁平县| 普兰县| 阿瓦提县| 砀山县| 南宁市| 武冈市| 云安县| 南雄市| 文昌市|