数字营销实训算法第四轮修改

master
@t2652009480 7 months ago
parent 64a8a05022
commit 8f2dafbc5a

@ -3,6 +3,7 @@ package com.sztzjy.marketing.controller.stu;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.sztzjy.marketing.annotation.AnonymousAccess; import com.sztzjy.marketing.annotation.AnonymousAccess;
import com.sztzjy.marketing.entity.dto.SentimentAnalyDTO;
import com.sztzjy.marketing.util.ResultEntity; import com.sztzjy.marketing.util.ResultEntity;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -105,8 +106,19 @@ public class StuPythonController {
// 等待进程执行完成 // 等待进程执行完成
int exitCode = process.waitFor(); int exitCode = process.waitFor();
if (exitCode == 0) { if (exitCode == 0) {
JSONObject jsonObject = new JSONObject(Boolean.parseBoolean(output.toString()));
// 访问JSON对象的属性
String pos = jsonObject.getString("pos");
String neg = jsonObject.getString("neg");
SentimentAnalyDTO sentimentAnalyDTO=new SentimentAnalyDTO();
sentimentAnalyDTO.setPos(pos);
sentimentAnalyDTO.setNeg(neg);
System.out.println("Python code output:\n" + output.toString()); System.out.println("Python code output:\n" + output.toString());
return new ResultEntity(HttpStatus.OK, output.toString()); return new ResultEntity(HttpStatus.OK, sentimentAnalyDTO);
} else { } else {
System.err.println("Error executing Python code:\n" + errors.toString()); System.err.println("Error executing Python code:\n" + errors.toString());
return new ResultEntity(HttpStatus.INTERNAL_SERVER_ERROR, errors.toString()); return new ResultEntity(HttpStatus.INTERNAL_SERVER_ERROR, errors.toString());

@ -0,0 +1,13 @@
package com.sztzjy.marketing.entity.dto;
import lombok.Data;
/**
* @author tz
* @date 2024/8/22 10:32
*/
@Data
public class SentimentAnalyDTO {
private String pos;
private String neg;
}

@ -34,4 +34,6 @@ public interface StuTableNameMapper {
List<String> getTableName(String userId); List<String> getTableName(String userId);
List<Map<String,Object>> selectByFields(@Param("fieldList")List<String> fieldList, String table); List<Map<String,Object>> selectByFields(@Param("fieldList")List<String> fieldList, String table);
List<Map<String, Object>> selectBuildTable(List<String> fieldList, String table, String userId, String tableName);
} }

@ -34,4 +34,5 @@ public interface StuUploadExcelUserMapper {
List<String> selectDistModuleName(String userId); List<String> selectDistModuleName(String userId);
List<String> selectByString(String userId, String tableName);
} }

@ -1,6 +1,7 @@
package com.sztzjy.marketing.service.impl; package com.sztzjy.marketing.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.hankcs.hanlp.HanLP; import com.hankcs.hanlp.HanLP;
@ -30,6 +31,7 @@ import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.sztzjy.marketing.util.StringUtils.convertCamelCaseToUnderscore;
import static com.sztzjy.marketing.util.algorithm.BaiDuZhiNengYun.getAccessToken; import static com.sztzjy.marketing.util.algorithm.BaiDuZhiNengYun.getAccessToken;
/** /**
@ -45,6 +47,9 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
@Resource @Resource
StuTableNameMapper stuTableNameMapper; StuTableNameMapper stuTableNameMapper;
@Resource
StuUploadExcelUserMapper uploadExcelUserMapper;
@Resource @Resource
StuSelectIndicatorsMapper indicatorsMapper; StuSelectIndicatorsMapper indicatorsMapper;
@Resource @Resource
@ -346,6 +351,31 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
list.add("id"); list.add("id");
list.add("user_behavior_type"); list.add("user_behavior_type");
} }
}else {
//查询导入的表数据
List<String> stringList=uploadExcelUserMapper.selectByString(userId,tableName);
for (String s : stringList) {
List<String> rightList = JSON.parseArray(s, String.class);
List<String> strings=new ArrayList<>();
for (int i = 0; i < rightList.size(); i++) { //转为驼峰命名
String string = convertCamelCaseToUnderscore(rightList.get(i));
strings.add(string);
}
if(!list.isEmpty()){
if(list.size()<strings.size()){
list=strings;
}
}else {
list=strings;
}
}
} }
} }
@ -353,6 +383,9 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
return list; return list;
} }
@Override @Override
public ResultEntity viewAnalyzeData(AnalyzeDataDTO analyzeDataDTO) { public ResultEntity viewAnalyzeData(AnalyzeDataDTO analyzeDataDTO) {
@ -363,22 +396,47 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
table="stu_user_attribute"; table="stu_user_attribute";
} }
if(analyzeDataDTO.getTableName().equals(Constant.YHDLHYB)){ //查询用户登录活跃表 else if(analyzeDataDTO.getTableName().equals(Constant.YHDLHYB)){ //查询用户登录活跃表
table="stu_user_login_active"; table="stu_user_login_active";
} }
if(analyzeDataDTO.getTableName().equals(Constant.YHXFNLB)){ //查询用户消费能力表 else if(analyzeDataDTO.getTableName().equals(Constant.YHXFNLB)){ //查询用户消费能力表
table="stu_user_consumption_ability"; table="stu_user_consumption_ability";
} }
if(analyzeDataDTO.getTableName().equals(Constant.YHPLB) || analyzeDataDTO.getTableName().equals(Constant.YHXWB)){ //查询用户评论或行为表 else if(analyzeDataDTO.getTableName().equals(Constant.YHPLB) || analyzeDataDTO.getTableName().equals(Constant.YHXWB)){ //查询用户评论或行为表
table="stu_user_behavior"; table="stu_user_behavior";
}else {
table="stu_upload_excel_user"; //查询用户自建表
List<String> fieldList = analyzeDataDTO.getFieldList();
String userId = analyzeDataDTO.getUserId();
String tableName = analyzeDataDTO.getTableName();
List<Map<String,Object>> attributes = stuTableNameMapper.selectBuildTable(fieldList,table,userId,tableName);
for (int i = 0; i < attributes.size(); i++) {
if(attributes.get(i).size()<fieldList.size()){
Collections.rotate(attributes,-1);
}
}
return new ResultEntity(HttpStatus.OK,attributes);
} }
List<String> fieldList = analyzeDataDTO.getFieldList(); List<String> fieldList = analyzeDataDTO.getFieldList();
List<Map<String,Object>> attributes = stuTableNameMapper.selectByFields(fieldList,table); List<Map<String,Object>> attributes = stuTableNameMapper.selectByFields(fieldList,table);
for (int i = 0; i < attributes.size(); i++) {
if(attributes.get(i).size()<fieldList.size()){
Collections.rotate(attributes,-1);
}
}
// //将日期类型转为时间戳返回 // //将日期类型转为时间戳返回
@ -425,6 +483,7 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
if(method.equals("缺失值处理--剔除数据")){ if(method.equals("缺失值处理--剔除数据")){
deduplicatedDataList.removeIf(record -> record.containsValue(null)); deduplicatedDataList.removeIf(record -> record.containsValue(null));
@ -493,5 +552,20 @@ public class StuDigitalMarketingModelServiceImpl implements StuDigitalMarketingM
} }
return mode != null ? mode : ""; // 如果存在众数则返回,否则返回空字符串 return mode != null ? mode : ""; // 如果存在众数则返回,否则返回空字符串
} }
public static List<Map<String, Object>> filterEmptyValues(List<Map<String, Object>> dataList) {
List<Map<String, Object>> filteredList = new ArrayList<>();
for (Map<String, Object> map : dataList) {
boolean allValuesEmpty = true;
for (Object value : map.values()) {
if (value != null) {
allValuesEmpty = false;
break;
}
}
if (!allValuesEmpty) {
filteredList.add(map);
}
}
return filteredList;
}
} }

@ -0,0 +1,24 @@
package com.sztzjy.marketing.util;
public class StringUtils {
/**
* 线
* @param camelCaseStr
* @return 线
*/
public static String convertCamelCaseToUnderscore(String camelCaseStr) {
// 使用Pattern和Matcher处理字符串
StringBuilder underscoreStr = new StringBuilder();
for (int i = 0; i < camelCaseStr.length(); i++) {
char c = camelCaseStr.charAt(i);
if (Character.isUpperCase(c) && i > 0) {
// 如果当前字符是大写,并且不是第一个字符,则添加下划线
underscoreStr.append("_");
}
// 将字符添加到结果字符串中,如果是大写,则转换为小写
underscoreStr.append(Character.toLowerCase(c));
}
return underscoreStr.toString();
}
}

@ -199,10 +199,10 @@ public class KMeansResult {
} }
} }
System.out.println("最终聚类结果:"); // System.out.println("最终聚类结果:");
for (int i = 0; i < k; i++) { // for (int i = 0; i < k; i++) {
System.out.println("簇 " + i + ": " + clusters.get(i)); // System.out.println("簇 " + i + ": " + clusters.get(i));
} // }
return clusters; return clusters;
} }

@ -222,6 +222,13 @@
</foreach> </foreach>
FROM ${table} FROM ${table}
</select> </select>
<select id="selectBuildTable" resultType="java.util.Map">
SELECT
<foreach collection="fieldList" item="field" separator=",">
${field}
</foreach>
FROM ${table} where user_id=#{userId} and module=#{tableName} limit 0,1000
</select>
</mapper> </mapper>

@ -962,5 +962,8 @@
FROM stu_upload_excel_user FROM stu_upload_excel_user
WHERE user_id = #{userId} WHERE user_id = #{userId}
</select> </select>
<select id="selectByString" resultType="java.lang.String">
select DISTINCT step_eleven_c from stu_upload_excel_user where user_id = #{userId} and module=#{tableName}
</select>
</mapper> </mapper>
Loading…
Cancel
Save