springboot使用JFoolNLTK中文分词获取关键词和数量

克莉丝汀,曾在上海拥有500多家门店,现陷入经营异常,拖欠5700万元,所有门店暂停营业。公司通过贷款和变卖资产补充现金流,面临供应商法律程序和银行账户冻结。烘焙市场竞争激烈,品牌老化和创新能力不足或是其衰落原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 导入相关依赖
<!--JFoolNLTK分词-->
<dependency>
    <groupId>me.midday</groupId>
    <artifactId>JFoolNLTK</artifactId>
    <version>1.0</version>
</dependency>
2. 相关类DataIntegerMap.java
import lombok.Data;

/**
 * 关键词和数量对象
 */
@Data
public class DataIntegerMap {
    private String name;
    private Integer value;
}

3. 相关类CommonUtil.java
public class CommonUtil {

 /**
     * 数组根据表达式转换为String
     *
     * @param list
     * @param exp(分割符号,如逗号)
     * @return
     */
    public static String listToString(List<?> list, String exp) {
        StringBuffer resourceIdsSB = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            resourceIdsSB.append(list.get(i));
            if (i < list.size() - 1) {
                resourceIdsSB.append(exp);
            }
        }
        return resourceIdsSB.toString();
    }



    /**
     * 找出重复字符、记录重复字符次数、记录重复字符位置
     * @param str
     * @return map
     */
    public static Map get_str_count_index(String[] str){
        Map<String,Map<Integer,ArrayList>> map = new HashMap<String ,Map<Integer,ArrayList>>();//key值记录重复的字符串,value记录出现的次数和位置
        int i = 0;
        for (String s:str ){
            if (map.get(s)==null){
                Map<Integer,ArrayList> count_where = new HashMap<Integer ,ArrayList>();//key值记录重复字符串出现的次数,value记录重复字符出现的位置
                int count = 1;//重复字符串计数器
                ArrayList<Integer> list = new ArrayList<Integer>();
                list.add(i);//重复字符串下标
                count_where.put(count,list);
                map.put(s,count_where);
                i++;
            }else {
                for (int c:map.get(s).keySet()){
                    ArrayList index = map.get(s).get(c);
                    index.add(i);//增加出现index位置
                    c++;
                    map.get(s).put(c,index);//更新计数器和下标信息
                    map.get(s).remove(--c);//删除掉之前的记录信息
                }
                i++;
            }
        }
        return map;
    }
}
3.分词工具类IKUtil.java

import com.rongyun.common.utils.CommonUtil;
import com.rongyun.system.api.zhxy.map.DataIntegerMap;
import me.midday.FoolNLTK;
import me.midday.lexical.LexicalAnalyzer;
import me.midday.lexical.Word;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.*;

public class IKUtil {
    //去除空格和特殊字符
    public static final String regEx = "[\n\r\t`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?^p]";
   
    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<String> splitKeyWordByFoolNLTK(String keyword) throws IOException {
        keyword = keyword.replaceAll(regEx, "").replaceAll(" ", "").replaceAll(" ", "");
        LexicalAnalyzer lexicalAnalyzer = FoolNLTK.getLSTMLexicalAnalyzer();
        ArrayList<String> result = new ArrayList<>();
        List<List<Word>> words = lexicalAnalyzer.cut(keyword);
        for(List<Word> ws: words){
            for (Word word: ws){
            	//不需要单字
                if (word.toString().length() > 1 ){
                    result.add(word.toString());
                }
            }
        }
        return result;
    }
    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<String> splitKeyWordByFoolNLTK(List<String> keyword) throws IOException {
        String content = CommonUtil.listToString(keyword,",");
        List<String> contentList = IKUtil.splitKeyWordByFoolNLTK(content);
        return contentList;
    }
    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<DataIntegerMap> splitKeyWordByFoolNLTKToMap(String keyword) throws Exception {
        List<String> contentList = IKUtil.splitKeyWordByFoolNLTK(keyword);
        List<DataIntegerMap> mapList = getMapByArray(CommonUtil.listToString(contentList,",").split(","));
        return mapList;
    }
    /**
     *  分词
     * @param keyword 需要分词的文本
     * @return
     */
    public static List<DataIntegerMap> splitKeyWordByFoolNLTKToMap(List<String> keyword) throws Exception {
        String content = CommonUtil.listToString(keyword,",");
        List<String> contentList = IKUtil.splitKeyWordByFoolNLTK(content);
        List<DataIntegerMap> mapList = getMapByArray(CommonUtil.listToString(contentList,",").split(","));
        return mapList;
    }
   

    /**
     *
     * 根据String[] 通过取重复值和重复数量,进行排序
     * @param arry
     * @return
     * @throws Exception
     */
    public static List<DataIntegerMap> getMapByArray(String[] arry) throws Exception{
        if (arry.length == 0 ){
            return new ArrayList<>();
        }
        List<DataIntegerMap> list = new ArrayList<>();
        Map<String, HashMap<Integer,ArrayList>> map = CommonUtil.get_str_count_index(arry);
        //输出指定重复次数的字符串信息
        for (String key :map.keySet()){
            for (int num:map.get(key).keySet()){
                if (num > 1 ){
                    DataIntegerMap dataIntegerMap = new DataIntegerMap();
                    dataIntegerMap.setName(key);
                    dataIntegerMap.setValue(num);
                    list.add(dataIntegerMap);
                }
//                System.out.printf("重复字符串:%s,重复次数:%d\n",key,num);
            }
        }
        //根据关键词数量降序排序
        Collections.sort(list, Comparator.comparing(DataIntegerMap::getValue).reversed());
        return list;
    }

	/**
     *
     * 测试分词效果
     * @param args
     * @return
     * @throws Exception
     */
    public static void main(String[] args)  throws Exception {
        String content ="来源:新闻坊、第一财经等版权归原作者所有,如有侵权请及时联系创立于1993年的克莉丝汀颠峰时期在上海拥有543家门店但就在去年7月这一知名烘培连锁品牌疑似经营异常最近新消息传来:克莉丝汀总部被曝已经人去楼空。克莉丝汀国际控股有限公司于3月10日发布公告称截至2023年2月28日,集团共拖欠金额人民币5700万元;银行账户已被冻结;已暂时关闭旗下所有零售门店。目前克莉丝汀已经通过贷款变卖资产等方式补充现金流3月18日,第一财经记者来到位于金沙江路33号的克莉丝汀上海总部,目前仅剩一位保安在一楼留守。记者在现场看见偌大而昏暗的门店内没有一个员工玻璃柜台里仍陈列着一些已经积灰的盒装糕点据保安介绍,3楼和4楼以前是克莉丝汀的办公点,现在也没有人了。而这位保安现在的工作任务就是看守门店内仍有的物品。门外的墙上贴了一张徐汇区人民法院的公告公告显示,作为被告的上海克莉丝汀食品有限公司已经拖欠了半年左右的房租,金额为12.16万元。金沙江路的克莉丝汀旗舰店只是公司目前情况的一个缩影克莉丝汀旗下所有门店自2022年12月起均已暂停营业一位克莉丝汀十几年的消费者表示:“大概半年前耀华路的克莉丝汀就关门了,不过克莉丝汀没有什么创新产品,一直是那几样东西。”来源:第一财经今年3月,克莉丝汀(01210.HK)在公告中提及,公司在支付店铺租金、供应商货款、员工薪酬方面出现延误,截至2023年2月28日,拖欠金额约为人民币5700万元。由于拖欠货款,若干供应商已展开法律程序,冻结了本集团的银行账户,被冻结金额约人民币350万元至人民币400万元。克莉丝汀目前已经通过贷款、变卖资产等方式补充现金流。克莉丝汀的经营状况一直不佳官网资料显示,克莉丝汀自1993年起生产及销售烘焙产品,主要在长三角地区的黄金地段及主要城市进行营运。2012年2月,克莉丝汀登陆香港联交所。财报显示,2013年至2021年,克莉丝汀已连续9年亏损。其中2019年、2020年、2021年、2022年上半年归母净利润分别为-2.24亿元、-1.1亿元、-1.7亿元和-0.73万元。而且,克莉丝汀的门店数目一直在减少。年报显示,2021年,克莉丝汀关闭了55家门店,2020年关闭了99家门店,2019年,共关闭了117家门店。中国食品产业分析师朱丹蓬认为,对于中国的烘焙行业来说,创新、升级、迭代一定是整个行业发展的主旋律。一些老品牌的倒闭,一方面与疫情有关,一方面也和自身缺乏创新机制的僵化、团队的老化有关。烘焙行业的竞争较为激烈,近些年新消费品牌不断涌现,它们也在该赛道分了一杯羹。艾媒咨询数据显示,2021年中国烘焙食品市场规模约为2600.8亿元,同比增长19.9%,并预计2023年中国烘焙食品市场规模将进一步增长至3069.9亿元。商业竞争不进则退↓↓↓";
        List<DataIntegerMap> contentList = splitKeyWordByFoolNLTKToMap(content);
        for (DataIntegerMap map : contentList){
            System.out.println(map);
        }

    }
}

4.运行效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值