diff --git a/pom.xml b/pom.xml index 4a3c5ff..fe1e58b 100644 --- a/pom.xml +++ b/pom.xml @@ -293,6 +293,17 @@ + + + com.kennycason + kumo-core + 1.28 + + + com.kennycason + kumo-tokenizers + 1.28 + diff --git a/src/main/java/com/sztzjy/linkCommerce/controller/stu/CptfController.java b/src/main/java/com/sztzjy/linkCommerce/controller/stu/CptfController.java index 9f9c926..f61b682 100644 --- a/src/main/java/com/sztzjy/linkCommerce/controller/stu/CptfController.java +++ b/src/main/java/com/sztzjy/linkCommerce/controller/stu/CptfController.java @@ -8,6 +8,7 @@ import com.sztzjy.linkCommerce.mapper.*; import com.sztzjy.linkCommerce.service.ScoreService; import com.sztzjy.linkCommerce.util.CompareListsUtil; import com.sztzjy.linkCommerce.util.ResultEntity; +import com.sztzjy.linkCommerce.util.StringUtils; import com.sztzjy.linkCommerce.util.file.IFileUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -55,106 +56,106 @@ public class CptfController { public String url; -// @PostMapping("/commitCpdjAnswer") -// @ApiOperation("发布渠道-提交") -// @AnonymousAccess -// public ResultEntity commitFbqdAnswer(@RequestBody FbqdStuAnswer stuAnswer) { -// //先查询是否提交过 -// StuGradeExample stuGradeExample = new StuGradeExample(); -// stuGradeExample.createCriteria().andUserIdEqualTo(stuAnswer.getUserId()).andProjectEqualTo(Constant.PRO_CPTFCS).andModuleEqualTo(Constant.MODULE_CPDJ); -// List stuGradeList = stuGradeMapper.selectByExampleWithBLOBs(stuGradeExample); -// if(CollectionUtils.isEmpty(stuGradeList)){ -// FbqdAnswer answer = fbqdAnswerMapper.selectByExampleWithBLOBs(new FbqdAnswerExample()).get(0); -// int count = 0; -// -// //1.数据标准化结果 -// String stuExposureData = stuAnswer.getExposureData(); -// -// String exposureData = answer.getExposureData(); -// //数据标准化结果 校验 -// -// //错误次数 -// int monthlyDataErrorCount =compareListsUtil.compareErrorCount(stuExposureData, exposureData).getErrorCount(); -// count = count + monthlyDataErrorCount; -// -// -// //2.渠道中的比重 -// String stuAnswerClickData = stuAnswer.getClickData(); -// -// String clickData = answer.getClickData(); -// //错误次数 -// int cheapSumCount =compareListsUtil.compareErrorCount(stuAnswerClickData, clickData).getErrorCount(); -// count = count + cheapSumCount; -// -// -// -// //3.信息滴 -// String stuAnswerAdCost = stuAnswer.getAdCost(); -// -// String adCost = answer.getAdCost(); -// //错误次数 -// int cheapPercentageCount =compareListsUtil.compareErrorCount(stuAnswerAdCost, adCost).getErrorCount(); -// count = count + cheapPercentageCount; -// -// -// //每个指标的权重 -// String stuAnswerOrderAmount = stuAnswer.getOrderAmount(); -// -// String orderAmount = answer.getOrderAmount(); -// //错误次数 -// int costEffectiveSumCount =compareListsUtil.compareErrorCount(stuAnswerOrderAmount, orderAmount).getErrorCount(); -// count = count + costEffectiveSumCount; -// -// -// -// //综合评分 -// String stuAnswerKeywords = stuAnswer.getKeywords(); -// -// String keywords = answer.getKeywords(); -// //错误次数 -// int costEffectivePercentageCount =compareListsUtil.compareErrorCount(stuAnswerKeywords, keywords).getErrorCount(); -// count = count + costEffectivePercentageCount; -// -// -// -// //主观题 -// int i = compareListsUtil.calculateLengthPenaltyEnhanced(stuAnswer.getScenePromotion()); -// count = count + i; -// stuAnswer.setId(IdUtil.simpleUUID()); -// -// fbqdStuAnswerMapper.insertSelective(stuAnswer); -// -// Boolean aBoolean = scoreService.insertScore(stuAnswer.getUserId(), Constant.PRO_CPTFCS, Constant.MODULE_FBQDPG, BigDecimal.valueOf(100 - count), stuAnswer.getScenePromotion()); -// if(aBoolean){ -// return new ResultEntity<>(HttpStatus.OK, "提交成功"); -// }else { -// return new ResultEntity<>(HttpStatus.BAD_REQUEST, "提交失败"); -// } -// }else { -// return new ResultEntity<>(HttpStatus.BAD_REQUEST, "已提交过"); -// } -// } -// -// -// -// @ApiOperation("发布渠道-回显接口") -// @GetMapping("/selectByCpdjUserId") -// @AnonymousAccess -// public ResultEntity selectByCpdjUserId(@ApiParam("用户ID") String userId) { -// -// FbqdStuAnswerExample cpdjStuAnswerExample = new FbqdStuAnswerExample(); -// cpdjStuAnswerExample.createCriteria().andUserIdEqualTo(userId); -// -// List stuAnswerList = fbqdStuAnswerMapper.selectByExampleWithBLOBs(cpdjStuAnswerExample); -// -// if (CollectionUtils.isEmpty(stuAnswerList)) { -// return new ResultEntity<>(HttpStatus.OK); -// } else { -// return new ResultEntity<>(HttpStatus.OK, stuAnswerList.get(0)); -// } -// -// } -// + @PostMapping("/commitCpdjAnswer") + @ApiOperation("发布渠道-提交") + @AnonymousAccess + public ResultEntity commitFbqdAnswer(@RequestBody FbqdStuAnswer stuAnswer) { + //先查询是否提交过 + StuGradeExample stuGradeExample = new StuGradeExample(); + stuGradeExample.createCriteria().andUserIdEqualTo(stuAnswer.getUserId()).andProjectEqualTo(Constant.PRO_CPTFCS).andModuleEqualTo(Constant.MODULE_CPDJ); + List stuGradeList = stuGradeMapper.selectByExampleWithBLOBs(stuGradeExample); + if(CollectionUtils.isEmpty(stuGradeList)){ + FbqdAnswer answer = fbqdAnswerMapper.selectByExampleWithBLOBs(new FbqdAnswerExample()).get(0); + int count = 0; + + //1.数据标准化结果 + String stuExposureData = stuAnswer.getExposureData(); + + String exposureData = answer.getExposureData(); + //数据标准化结果 校验 + + //错误次数 + int monthlyDataErrorCount =compareListsUtil.compareErrorCount(stuExposureData, exposureData).getErrorCount(); + count = count + monthlyDataErrorCount; + + + //2.渠道中的比重 + String stuAnswerClickData = stuAnswer.getClickData(); + + String clickData = answer.getClickData(); + //错误次数 + int cheapSumCount =compareListsUtil.compareErrorCount(stuAnswerClickData, clickData).getErrorCount(); + count = count + cheapSumCount; + + + + //3.信息滴 + String stuAnswerAdCost = stuAnswer.getAdCost(); + + String adCost = answer.getAdCost(); + //错误次数 + int cheapPercentageCount =compareListsUtil.compareErrorCount(stuAnswerAdCost, adCost).getErrorCount(); + count = count + cheapPercentageCount; + + + //每个指标的权重 + String stuAnswerOrderAmount = stuAnswer.getOrderAmount(); + + String orderAmount = answer.getOrderAmount(); + //错误次数 + int costEffectiveSumCount =compareListsUtil.compareErrorCount(stuAnswerOrderAmount, orderAmount).getErrorCount(); + count = count + costEffectiveSumCount; + + + + //综合评分 + String stuAnswerKeywords = stuAnswer.getKeywords(); + + String keywords = answer.getKeywords(); + //错误次数 + int costEffectivePercentageCount =compareListsUtil.compareErrorCount(stuAnswerKeywords, keywords).getErrorCount(); + count = count + costEffectivePercentageCount; + + + + //主观题 + int i = compareListsUtil.calculateLengthPenaltyEnhanced(stuAnswer.getScenePromotion()); + count = count + i; + stuAnswer.setId(IdUtil.simpleUUID()); + + fbqdStuAnswerMapper.insertSelective(stuAnswer); + + Boolean aBoolean = scoreService.insertScore(stuAnswer.getUserId(), Constant.PRO_CPTFCS, Constant.MODULE_FBQDPG, BigDecimal.valueOf(100 - count), stuAnswer.getScenePromotion()); + if(aBoolean){ + return new ResultEntity<>(HttpStatus.OK, "提交成功"); + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "提交失败"); + } + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "已提交过"); + } + } + + + + @ApiOperation("发布渠道-回显接口") + @GetMapping("/selectByCpdjUserId") + @AnonymousAccess + public ResultEntity selectByCpdjUserId(@ApiParam("用户ID") String userId) { + + FbqdStuAnswerExample cpdjStuAnswerExample = new FbqdStuAnswerExample(); + cpdjStuAnswerExample.createCriteria().andUserIdEqualTo(userId); + + List stuAnswerList = fbqdStuAnswerMapper.selectByExampleWithBLOBs(cpdjStuAnswerExample); + + if (CollectionUtils.isEmpty(stuAnswerList)) { + return new ResultEntity<>(HttpStatus.OK); + } else { + return new ResultEntity<>(HttpStatus.OK, stuAnswerList.get(0)); + } + + } + @@ -186,20 +187,30 @@ public class CptfController { return new ResultEntity<>(HttpStatus.BAD_REQUEST, "重新实训失败"); } } -// -// if (Constant.MODULE_CPJGGH.equals(module)) { -// Boolean b=scoreService.deleteScore(userId,Constant.PRO_CPGH,module); -// if(b){ -// CpjgghStuAnswerExample cpjgghStuAnswerExample = new CpjgghStuAnswerExample(); -// cpjgghStuAnswerExample.createCriteria().andUserIdEqualTo(userId); -// cpjgghStuAnswerMapper.deleteByExample(cpjgghStuAnswerExample); -// return new ResultEntity<>(HttpStatus.OK, "重新实训成功"); -// }else { -// return new ResultEntity<>(HttpStatus.BAD_REQUEST, "重新实训失败"); -// } -// } + if (Constant.MODULE_CPTGCS.equals(module)) { + Boolean b=scoreService.deleteScore(userId,Constant.PRO_CPGH,module); + if(b){ + CptgcsStuAnswerExample cptgcsStuAnswerExample = new CptgcsStuAnswerExample(); + cptgcsStuAnswerExample.createCriteria().andUserIdEqualTo(userId); + cptgcsStuAnswerMapper.deleteByExample(cptgcsStuAnswerExample); + return new ResultEntity<>(HttpStatus.OK, "重新实训成功"); + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "重新实训失败"); + } + } + if (Constant.MODULE_CPYXDW.equals(module)) { + Boolean b=scoreService.deleteScore(userId,Constant.PRO_CPGH,module); + if(b){ + CpyxdwStuAnswerExample cpyxdwStuAnswerExample = new CpyxdwStuAnswerExample(); + cpyxdwStuAnswerExample.createCriteria().andUserIdEqualTo(userId); + cpyxdwStuAnswerMapper.deleteByExample(cpyxdwStuAnswerExample); + return new ResultEntity<>(HttpStatus.OK, "重新实训成功"); + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "重新实训失败"); + } + } return new ResultEntity<>(HttpStatus.BAD_REQUEST, "重新实训失败"); @@ -217,7 +228,7 @@ public class CptfController { @PostMapping("/commitCptgcsAnswer") - @ApiOperation("发布推广测试-提交") + @ApiOperation("产品推广测试-提交") @AnonymousAccess public ResultEntity commitCptgcsAnswer(@RequestBody CptgcsStuAnswer stuAnswer) { //先查询是否提交过 @@ -298,15 +309,17 @@ public class CptfController { - @ApiOperation("发布渠道-回显接口") - @GetMapping("/selectByCpdjUserId") + + + @ApiOperation("产品推广测试-回显接口") + @GetMapping("/selectCptgcsByUserId") @AnonymousAccess - public ResultEntity selectByCpdjUserId(@ApiParam("用户ID") String userId) { + public ResultEntity selectCptgcsByUserId(@ApiParam("用户ID") String userId) { CptgcsStuAnswerExample cptgcsStuAnswerExample = new CptgcsStuAnswerExample(); cptgcsStuAnswerExample.createCriteria().andUserIdEqualTo(userId); - List< CptgcsStuAnswer> stuAnswerList = cptgcsStuAnswerMapper.selectByExampleWithBLOBs(cptgcsStuAnswerExample); + List stuAnswerList = cptgcsStuAnswerMapper.selectByExampleWithBLOBs(cptgcsStuAnswerExample); if (CollectionUtils.isEmpty(stuAnswerList)) { return new ResultEntity<>(HttpStatus.OK); @@ -316,6 +329,85 @@ public class CptfController { } + @Autowired + private CpyxdwStuAnswerMapper cpyxdwStuAnswerMapper; + + + + + + @PostMapping("/commitCpyxdwAnswer") + @ApiOperation("产品营销定位-提交") + @AnonymousAccess + public ResultEntity commitCpyxdwAnswer(@RequestBody CpyxdwStuAnswer stuAnswer) { + //先查询是否提交过 + StuGradeExample stuGradeExample = new StuGradeExample(); + stuGradeExample.createCriteria().andUserIdEqualTo(stuAnswer.getUserId()).andProjectEqualTo(Constant.PRO_CPTFCS).andModuleEqualTo(Constant.MODULE_CPYXDW); + List stuGradeList = stuGradeMapper.selectByExampleWithBLOBs(stuGradeExample); + if(CollectionUtils.isEmpty(stuGradeList)){ + + int count = 0; + + //1.产品点击率 + String axis = stuAnswer.getxAxis(); + if (axis!=null) + { + if (!"点击率".equals(axis)) { + count++; + } + } + + + String yAxis = stuAnswer.getyAxis(); + if (yAxis!=null) + { + if (!"转换率".equals(yAxis)) { + count++; + } + } + + + + + //主观题 + int i = compareListsUtil.calculateLengthPenaltyEnhanced(stuAnswer.getAnalysis()); + count = count + i; + stuAnswer.setId(IdUtil.simpleUUID()); + + cpyxdwStuAnswerMapper.insertSelective(stuAnswer); + + Boolean aBoolean = scoreService.insertScore(stuAnswer.getUserId(), Constant.PRO_CPTFCS, Constant.MODULE_CPYXDW, BigDecimal.valueOf(100 - count), stuAnswer.getAnalysis()); + if(aBoolean){ + return new ResultEntity<>(HttpStatus.OK, "提交成功"); + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "提交失败"); + } + }else { + return new ResultEntity<>(HttpStatus.BAD_REQUEST, "已提交过"); + } + } + + + + + + @ApiOperation("产品营销定位-回显接口") + @GetMapping("/selectCpyxdwByUserId") + @AnonymousAccess + public ResultEntity selectCpyxdwByUserId(@ApiParam("用户ID") String userId) { + + CpyxdwStuAnswerExample cpyxdwStuAnswerExample = new CpyxdwStuAnswerExample(); + cpyxdwStuAnswerExample.createCriteria().andUserIdEqualTo(userId); + + List stuAnswerList = cpyxdwStuAnswerMapper.selectByExampleWithBLOBs(cpyxdwStuAnswerExample); + + if (CollectionUtils.isEmpty(stuAnswerList)) { + return new ResultEntity<>(HttpStatus.OK); + } else { + return new ResultEntity<>(HttpStatus.OK, stuAnswerList.get(0)); + } + + } diff --git a/src/main/java/com/sztzjy/linkCommerce/controller/stu/DownloadDataController.java b/src/main/java/com/sztzjy/linkCommerce/controller/stu/DownloadDataController.java new file mode 100644 index 0000000..cf9ee5c --- /dev/null +++ b/src/main/java/com/sztzjy/linkCommerce/controller/stu/DownloadDataController.java @@ -0,0 +1,152 @@ +package com.sztzjy.linkCommerce.controller.stu; + +import cn.hutool.core.util.IdUtil; +import com.sztzjy.linkCommerce.annotation.AnonymousAccess; +import com.sztzjy.linkCommerce.entity.dto.StuWordCreateDto; +import com.sztzjy.linkCommerce.util.TagWordCloudService; +import com.sztzjy.linkCommerce.util.file.IFileUtil; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; + +/** + * @author 17803 + * @date 2024-06-17 10:29 + */ + +@Api(tags = "用户画像模型下载数据") +@RestController +@RequestMapping("api/downDataByUserPortrait") +public class DownloadDataController { + + + @Autowired + private IFileUtil iFileUtil; + + @Value("${file.path}") + private String filePath; + + + //@AnonymousAccess + @ApiOperation("RFM分析方法下载数据") + @GetMapping("/analysisByRfmDown") + @AnonymousAccess + public void analysisByRfmDown(HttpServletResponse response){ + + iFileUtil.download(response,"RFM分析方法data.xls"); + + } + + + //@AnonymousAccess + @ApiOperation("帕累托分析方法下载数据") + @GetMapping("/analysisByPareto") + @AnonymousAccess + public void analysisByPareto(HttpServletResponse response){ + + iFileUtil.download(response,"帕累托分析方法data.xls"); + + } + + + + //@AnonymousAccess + @ApiOperation("波士顿矩阵分析图下载数据") + @GetMapping("/bostonMatrixAnalysisData") + @AnonymousAccess + public void bostonMatrixAnalysisData(HttpServletResponse response){ + + iFileUtil.download(response,"波士顿矩阵分析图data.xls"); + + } + + + + + //@AnonymousAccess + @ApiOperation("漏斗模型分析下载数据") + @GetMapping("/analysisByFunnelModel") + @AnonymousAccess + public void analysisByFunnelModel(HttpServletResponse response){ + + iFileUtil.download(response,"漏斗模型分析data.xls"); + + } + + + //@AnonymousAccess + @ApiOperation("购物篮分析下载数据") + @GetMapping("/analysisByShopBasket") + @AnonymousAccess + public void analysisByShopBasket(HttpServletResponse response){ + + iFileUtil.download(response,"购物篮分析data.xls"); + + } + + + //@AnonymousAccess + @ApiOperation("复购分析下载数据") + @GetMapping("/analysisByRepurchase") + @AnonymousAccess + public void analysisByRepurchase(HttpServletResponse response){ + + iFileUtil.download(response,"复购分析data.xls"); + + } + + //@AnonymousAccess + @ApiOperation("留存分析下载数据") + @GetMapping("/analysisByRetention") + @AnonymousAccess + public void analysisByRetention(HttpServletResponse response){ + + iFileUtil.download(response,"留存分析data.xls"); + + } + + //@AnonymousAccess + @ApiOperation("用户粘性分析下载数据") + @GetMapping("/analysisByStickiness") + @AnonymousAccess + public void analysisByStickiness(HttpServletResponse response){ + + iFileUtil.download(response,"用户粘性分析data.xls"); + + } + + + //@AnonymousAccess + @ApiOperation("情感分析/文本挖掘词云生成") + @PostMapping("/wordCreateByAna") + @AnonymousAccess + public void wordCreateByAna(@RequestBody StuWordCreateDto stuWordCreateDto) throws IOException { + + TagWordCloudService tagWordCloudService = new TagWordCloudService(); + + File file1 = new File(filePath + "/word/"); + + if (! file1.exists()) + { + file1.mkdir(); + } + + //1成图片路径,2.图云背景图片 + String url = filePath+"/word/" + IdUtil.simpleUUID()+"."+"png"; + + + tagWordCloudService.generate(url,filePath+"/1.png",stuWordCreateDto.getContext()); + + + } + + + + +} diff --git a/src/main/java/com/sztzjy/linkCommerce/controller/stu/UserBehaviorProfilingAnaController.java b/src/main/java/com/sztzjy/linkCommerce/controller/stu/UserBehaviorProfilingAnaController.java index 0dba64c..9bbdc8d 100644 --- a/src/main/java/com/sztzjy/linkCommerce/controller/stu/UserBehaviorProfilingAnaController.java +++ b/src/main/java/com/sztzjy/linkCommerce/controller/stu/UserBehaviorProfilingAnaController.java @@ -2,10 +2,12 @@ package com.sztzjy.linkCommerce.controller.stu; import com.hankcs.hanlp.collection.sequence.SString; +import com.sztzjy.linkCommerce.annotation.AnonymousAccess; import com.sztzjy.linkCommerce.entity.StuMachineLearning; import com.sztzjy.linkCommerce.service.UserBehaviorProfilingAnaService; import com.sztzjy.linkCommerce.util.ResultDataEntity; import com.sztzjy.linkCommerce.util.ResultEntity; +import com.sztzjy.linkCommerce.util.file.IFileUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; @@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletResponse; import java.util.List; @@ -54,5 +57,17 @@ public class UserBehaviorProfilingAnaController { } + @Autowired + private IFileUtil iFileUtil; + + @ApiOperation("RFM分析方法下载数据") + @GetMapping("/analysisByRfmDown") + @AnonymousAccess + public void analysisByRfmDown(HttpServletResponse response){ + + iFileUtil.download(response,"RFM分析方法data.xls"); + + } + } diff --git a/src/main/java/com/sztzjy/linkCommerce/entity/CpyxdwStuAnswer.java b/src/main/java/com/sztzjy/linkCommerce/entity/CpyxdwStuAnswer.java index 3f0a788..d126d5b 100644 --- a/src/main/java/com/sztzjy/linkCommerce/entity/CpyxdwStuAnswer.java +++ b/src/main/java/com/sztzjy/linkCommerce/entity/CpyxdwStuAnswer.java @@ -13,10 +13,10 @@ public class CpyxdwStuAnswer { @ApiModelProperty(notes = "主键(格式:CPYXSA_时间戳_哈希值,例:CPYXSA_2023110614_9d7b6c5e)") private String id; - @ApiModelProperty(notes = "横轴定位维度(如:价格水平、技术含量等)") + @ApiModelProperty(notes = "横轴定位维度") private String xAxis; - @ApiModelProperty(notes = "纵轴定位维度(如:市场份额、品牌认知度等)") + @ApiModelProperty(notes = "纵轴定位维度") private String yAxis; @ApiModelProperty(notes = "用户ID(关联userinfo表)") @@ -31,7 +31,7 @@ public class CpyxdwStuAnswer { @ApiModelProperty(notes = "定位坐标数据(JSON格式,") private String positionData; - @ApiModelProperty(notes = "定位分析说明(500字以内)") + @ApiModelProperty(notes = "主观题(500字以内)") private String analysis; public String getId() { diff --git a/src/main/java/com/sztzjy/linkCommerce/entity/dto/StuWordCreateDto.java b/src/main/java/com/sztzjy/linkCommerce/entity/dto/StuWordCreateDto.java new file mode 100644 index 0000000..b27a1f9 --- /dev/null +++ b/src/main/java/com/sztzjy/linkCommerce/entity/dto/StuWordCreateDto.java @@ -0,0 +1,16 @@ +package com.sztzjy.linkCommerce.entity.dto; + +import lombok.Data; + +/** + * @author 17803 + * @date 2024-06-21 14:21 + */ + +@Data +public class StuWordCreateDto { + + private String context; + private String userId; + +} diff --git a/src/main/java/com/sztzjy/linkCommerce/util/TagWordCloudService.java b/src/main/java/com/sztzjy/linkCommerce/util/TagWordCloudService.java new file mode 100644 index 0000000..7fa5a68 --- /dev/null +++ b/src/main/java/com/sztzjy/linkCommerce/util/TagWordCloudService.java @@ -0,0 +1,111 @@ +package com.sztzjy.linkCommerce.util; + +import com.kennycason.kumo.CollisionMode; +import com.kennycason.kumo.WordCloud; +import com.kennycason.kumo.WordFrequency; +import com.kennycason.kumo.bg.CircleBackground; +import com.kennycason.kumo.font.KumoFont; +import com.kennycason.kumo.font.scale.SqrtFontScalar; +import com.kennycason.kumo.image.AngleGenerator; +import com.kennycason.kumo.nlp.FrequencyAnalyzer; +import com.kennycason.kumo.nlp.tokenizers.ChineseWordTokenizer; +import com.kennycason.kumo.palette.ColorPalette; + +import java.awt.*; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.List; + +public class TagWordCloudService { + + // 照片纵横比 + private double ratio = 1; + // 获取词云图片 + // wordFile:单词及其频率文件路径 + // pngOutputPath:图片输出路径,应该以.png结尾 + // shapePicPath:词云形状图片路径,其背景应为透明背景,格式为png + public void generate(String pngOutputPath, String shapePicPath,String context) throws IOException { +// final FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer(); + // 共检索多少个词 +// frequencyAnalyzer.setWordFrequenciesToReturn(1000); + // 单词最短长度,一个汉字和一个英文字符都是1 +// frequencyAnalyzer.setMinWordLength(2); +// frequencyAnalyzer.setStopWords(loadStopWords()); + // 设置中文支持,另一种加载方式不用设置 +// frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer()); +// final List wordFrequencies; +// // 加载词云有两种方式,一种是在txt文件中统计词出现的个数,另一种是直接给出每个词出现的次数,这里使用第二种 +// // 文件格式如下 +//// 100: frog +//// 94: dog +//// 43: cog +//// 20: bog +// FrequencyFileLoader frequencyFileLoader = new FrequencyFileLoader(); +// File file = new File(wordFile); +// wordFrequencies = frequencyFileLoader.load(file); + // 可以直接从文件中读取 + FrequencyAnalyzer frequencyAnalyzer = new FrequencyAnalyzer(); + frequencyAnalyzer.setWordFrequenciesToReturn(600); + frequencyAnalyzer.setMinWordLength(2); + frequencyAnalyzer.setWordTokenizer(new ChineseWordTokenizer()); + InputStream inputStream = null; + List wordFrequencies = null; + + try { + inputStream = new ByteArrayInputStream(context.getBytes(StandardCharsets.UTF_8)); + wordFrequencies = frequencyAnalyzer.load(inputStream); + + }catch (Exception e) { + e.printStackTrace(); + }finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + +// final Dimension dimension = new Dimension(600, 600); +// final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT); +// wordCloud.setPadding(2); +// wordCloud.setBackground(new CircleBackground(300)); + // wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF))); +// wordCloud.setFontScalar(new SqrtFontScalar(10, 40)); + + + // 生成图片的像素大小 + //设置图片分辨率 + Dimension dimension = new Dimension(600, 600); +// final Dimension dimension = new Dimension(1024, (int)(1024*ratio)); + final WordCloud wordCloud = new WordCloud(dimension, CollisionMode.PIXEL_PERFECT); + // 调节词云的稀疏程度,越高越稀疏 + wordCloud.setPadding(2); + +// //设置背景色 + wordCloud.setBackgroundColor(new Color(255,255,255)); + //设置背景图片 + //wordCloud.setBackground(new PixelBoundaryBackground(shapePicPath)); +// wordCloud.setBackground(new CircleBackground(300)); + wordCloud.setBackground(new CircleBackground(300)); + + // 颜色模板,不同频率的颜色会不同 + wordCloud.setColorPalette(new ColorPalette(new Color(0x4055F1), new Color(0x408DF1), new Color(0x40AAF1), new Color(0x40C5F1), new Color(0x40D3F1), new Color(0xFFFFFF))); + // 设置字体 + Font font = new Font("楷体", 0, 25); + wordCloud.setKumoFont(new KumoFont(font)); + // 设置偏转角,角度为0时,字体都是水平的 +// wordCloud.setAngleGenerator(new AngleGenerator(0, 90, 9)); + wordCloud.setAngleGenerator(new AngleGenerator(0)); + // 字体的大小范围,最小是多少,最大是多少 + wordCloud.setFontScalar(new SqrtFontScalar(10, 40)); + + wordCloud.build(wordFrequencies); + wordCloud.writeToFile(pngOutputPath); + + + } +}