字符串词频统计 java 字符串词频统计实例代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

字符串词频统计 java 字符串词频统计实例代码

  2021-03-18 我要评论
想了解java 字符串词频统计实例代码的相关内容吗,在本文为您仔细讲解字符串词频统计的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:字符串,词频统计,下面大家一起来学习吧。

复制代码 代码如下:

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

}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们