|
@@ -0,0 +1,213 @@
|
|
|
+package cn.cslg.pas.service.dify.chaxing;
|
|
|
+
|
|
|
+import cn.cslg.pas.common.dto.PatentStarListDTO;
|
|
|
+import cn.cslg.pas.common.model.dify.DifyChatMessageDTO;
|
|
|
+import cn.cslg.pas.common.model.dify.chaxing.KeyWordVo;
|
|
|
+import cn.cslg.pas.common.model.dify.generateDiscoveryResult.DiscoryResultVO;
|
|
|
+import cn.cslg.pas.common.utils.StringUtils;
|
|
|
+import cn.cslg.pas.common.vo.StarPatentVO;
|
|
|
+import cn.cslg.pas.service.common.DifyService;
|
|
|
+import cn.cslg.pas.service.common.PatentStarApiService;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ChaXingService {
|
|
|
+ @Autowired
|
|
|
+ private DifyService difyService;
|
|
|
+ @Value("app-1h8h1wm1bw9RMrynYVuiI1ym")
|
|
|
+ private String key;
|
|
|
+ @Autowired
|
|
|
+ private PatentStarApiService patentStarApiService;
|
|
|
+
|
|
|
+ public List<KeyWordVo> getKeyWord(String InventionPoint, DifyChatMessageDTO difyChatMessageDTO) throws Exception {
|
|
|
+ difyChatMessageDTO.setUser("lrj");
|
|
|
+ difyChatMessageDTO.setResponseMode("blocking");
|
|
|
+ difyChatMessageDTO.setQuery(InventionPoint);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("operate_type", "1");
|
|
|
+ difyChatMessageDTO.setInputs(map);
|
|
|
+ Integer cycle = 0;
|
|
|
+ Boolean flag = false;
|
|
|
+ String re = "";
|
|
|
+ String answer = "";
|
|
|
+ String conversionId = "";
|
|
|
+ List<KeyWordVo> keyWordVos = new ArrayList<>();
|
|
|
+ while (cycle < 5 && flag == false) {
|
|
|
+ re = difyService.chatMessage(difyChatMessageDTO, key);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(re);
|
|
|
+ Object status = jsonObject.get("status");
|
|
|
+ if (status != null) {
|
|
|
+ cycle++;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ answer = jsonObject.get("answer").toString();
|
|
|
+ conversionId = jsonObject.get("conversation_id").toString();
|
|
|
+ if (difyChatMessageDTO.getConversationId() == null) {
|
|
|
+ difyChatMessageDTO.setConversationId(conversionId);
|
|
|
+ }
|
|
|
+ keyWordVos = JSONArray.parseArray(answer, KeyWordVo.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ cycle++;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(answer);
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return keyWordVos;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getSynonym(String keyword, DifyChatMessageDTO difyChatMessageDTO) throws Exception {
|
|
|
+ difyChatMessageDTO.setUser("lrj");
|
|
|
+ difyChatMessageDTO.setResponseMode("blocking");
|
|
|
+ difyChatMessageDTO.setQuery("生成同义词");
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("operate_type", "2");
|
|
|
+ map.put("keyword", keyword);
|
|
|
+ difyChatMessageDTO.setInputs(map);
|
|
|
+ Integer cycle = 0;
|
|
|
+ Boolean flag = false;
|
|
|
+ String re = "";
|
|
|
+ String answer = "";
|
|
|
+ String conversionId = "";
|
|
|
+ List<String> synonyms = new ArrayList<>();
|
|
|
+ while (cycle < 5 && flag == false) {
|
|
|
+ re = difyService.chatMessage(difyChatMessageDTO, key);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(re);
|
|
|
+ Object status = jsonObject.get("status");
|
|
|
+ if (status != null) {
|
|
|
+ cycle++;
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ answer = jsonObject.get("answer").toString();
|
|
|
+ conversionId = jsonObject.get("conversation_id").toString();
|
|
|
+ if (difyChatMessageDTO.getConversationId() == null) {
|
|
|
+ difyChatMessageDTO.setConversationId(conversionId);
|
|
|
+ }
|
|
|
+ synonyms = JSONArray.parseArray(answer, String.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ cycle++;
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(answer);
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return synonyms;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void compare(String path, String outPath) throws Exception {
|
|
|
+ String readPath = "E:\\20240716-工作记录文件\\功能文件\\小世系统\\20250929-查新检索-lrj-v1\\test.xlsx";
|
|
|
+ List<Map<String, String>> map = new ArrayList<>();
|
|
|
+ FileInputStream inputStream = new FileInputStream(new File(readPath));
|
|
|
+
|
|
|
+ //1.创建工作簿,使用excel能操作的这边都看看操作
|
|
|
+ Workbook workbook = new XSSFWorkbook(inputStream);
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ int rowNum = sheet.getLastRowNum() + 1;
|
|
|
+ DifyChatMessageDTO difyChatMessageDTO = new DifyChatMessageDTO();
|
|
|
+ difyChatMessageDTO.setUser("lrj");
|
|
|
+ difyChatMessageDTO.setResponseMode("blocking");
|
|
|
+ List<String> res = new ArrayList<>();
|
|
|
+ for (int i = 1; i < rowNum; i++) {
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ Cell cell = row.getCell(1);
|
|
|
+ String inventionPoint = cell.getStringCellValue();
|
|
|
+ difyChatMessageDTO.setQuery(inventionPoint);
|
|
|
+ List<KeyWordVo> keyWordVos = this.getKeyWord(inventionPoint, difyChatMessageDTO);
|
|
|
+ for (KeyWordVo keyWordVo : keyWordVos) {
|
|
|
+ if (keyWordVo.getIfCore() != null && keyWordVo.getIfCore().equals("true")) {
|
|
|
+ String keyword = keyWordVo.getKeyword();
|
|
|
+ List<String> synonyms = this.getSynonym(keyword, difyChatMessageDTO);
|
|
|
+ synonyms.add(keyword);
|
|
|
+ synonyms = this.formQueryKey(synonyms);
|
|
|
+ String re = "(" + StringUtils.join(synonyms, " OR ") + ")";
|
|
|
+ res.add(re);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String query = this.getQueryKey(res);
|
|
|
+ PatentStarListDTO patentStarListDTO = new PatentStarListDTO();
|
|
|
+ patentStarListDTO.setCurrentQuery(query);
|
|
|
+ patentStarListDTO.setDBType("CN");
|
|
|
+ patentStarListDTO.setOrderBy("AD");
|
|
|
+ patentStarListDTO.setOrderByType("DESC");
|
|
|
+ patentStarListDTO.setPageNum(1);
|
|
|
+ patentStarListDTO.setRowCount(10);
|
|
|
+ Map<String, Object> searchApiMap = patentStarApiService.patentStarSearchApi(patentStarListDTO);
|
|
|
+ Integer total=-1;
|
|
|
+ if(searchApiMap!=null) {
|
|
|
+ total = Integer.parseInt(searchApiMap.get("total").toString());
|
|
|
+ }
|
|
|
+ Cell cell2 = row.getCell(2);
|
|
|
+ String value = cell2.getStringCellValue();
|
|
|
+ value = value.trim();
|
|
|
+ List<String> patentNos = Arrays.asList(value.split("\n"));
|
|
|
+ value = value.replaceAll("\n", " OR ");
|
|
|
+ query = query + " AND " + "PN=(" + value + ") OR GN=(" + value + ")";
|
|
|
+ patentStarListDTO.setCurrentQuery(query);
|
|
|
+ patentStarListDTO.setFormed(null);
|
|
|
+ searchApiMap = patentStarApiService.patentStarSearchApi(patentStarListDTO);
|
|
|
+ List<StarPatentVO> starPatentVOS=new ArrayList<>();
|
|
|
+ if(searchApiMap!=null) {
|
|
|
+ starPatentVOS = (List<StarPatentVO>) searchApiMap.get("records");
|
|
|
+ }
|
|
|
+ List<String> result = new ArrayList<>();
|
|
|
+ starPatentVOS.forEach(item -> {
|
|
|
+ if (patentNos.contains(item.getPN())) {
|
|
|
+ result.add(item.getPN());
|
|
|
+ } else if (patentNos.contains(item.getGN())) {
|
|
|
+ result.add(item.getGN());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ starPatentVOS.forEach(item -> {
|
|
|
+ });
|
|
|
+ String reText = "总数:" + total + "\n" + "结果:" + StringUtils.join(result, "\n");
|
|
|
+ Cell cell3 = row.createCell(3);
|
|
|
+ cell3.setCellValue(reText);
|
|
|
+ Cell cell4 = row.createCell(4);
|
|
|
+ String re = "";
|
|
|
+ if (result.size() == 0) {
|
|
|
+ re = "无";
|
|
|
+ } else if (result.size() == patentNos.size()) {
|
|
|
+ re = "完全检索";
|
|
|
+ } else {
|
|
|
+ re = "部分检索";
|
|
|
+ }
|
|
|
+ cell4.setCellValue(re);
|
|
|
+ Cell cell5 = row.createCell(5);
|
|
|
+ cell5.setCellValue(query);
|
|
|
+ }
|
|
|
+ inputStream.close();
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(new File(outPath));
|
|
|
+ workbook.write(fileOutputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getQueryKey(List<String> res) {
|
|
|
+ String query = "TX=" + StringUtils.join(res, " AND TX=");
|
|
|
+ return query;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> formQueryKey(List<String> keys) {
|
|
|
+ for (int i=0;i<keys.size();i++){
|
|
|
+ keys.set(i,"\"" + keys.get(i) + "\"");
|
|
|
+ }
|
|
|
+ return keys;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|