allen 2 жил өмнө
parent
commit
6b575f1825

+ 45 - 13
kgraph-graph/src/main/java/com/kgraph/graph/suport/controller/ExtractResultController.java

@@ -5,6 +5,7 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.List;
 import javax.servlet.http.HttpServletResponse;
 
@@ -23,14 +24,14 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
-import com.kgraph.common.annotation.Log;;
-import com.kgraph.common.core.controller.BaseController;;
-import com.kgraph.common.core.domain.AjaxResult;;
-import com.kgraph.common.enums.BusinessType;;
+import com.kgraph.common.annotation.Log;
+import com.kgraph.common.core.controller.BaseController;
+import com.kgraph.common.core.domain.AjaxResult;
+import com.kgraph.common.enums.BusinessType;
 import com.kgraph.graph.suport.domain.ExtractResult;
 import com.kgraph.graph.suport.service.IExtractResultService;
-import com.kgraph.common.utils.poi.ExcelUtil;;
-import com.kgraph.common.core.page.TableDataInfo;;
+import com.kgraph.common.utils.poi.ExcelUtil;
+import com.kgraph.common.core.page.TableDataInfo;
 
 /**
  * 抽取结果信息Controller
@@ -131,13 +132,7 @@ public class ExtractResultController extends BaseController
         if (!FileUtils.checkAllowDownload(filePath)) {
             throw new RuntimeException(StringUtils.format("文件名称({})非法,不允许下载。 ", filePath));
         }
-        String realFileName = FileUtils.getFileNameByPath(filePath);
-
-//        String parentPath = dictDataService.getValueByTypeAndValue("kg_algorithm", "resultPath");
         String filePathFinal = filePath;
-//        if (StringUtils.isNotEmpty(parentPath)) {
-//            filePathFinal = parentPath + filePathFinal;
-//        }
         Path path = Paths.get(filePathFinal);
         String data = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
         return AjaxResult.success("success", data);
@@ -170,8 +165,45 @@ public class ExtractResultController extends BaseController
         if(StringUtils.isNotEmpty(resultPath)){
             String data = (String) getResult(resultPath).get(AjaxResult.DATA_TAG);
             JSONObject jsonObject = JSONObject.parseObject(data);
-            return success(jsonObject);
+            return success(refactorData(jsonObject));
         }
         return error("没有生成抽取结果");
     }
+    private JSONObject refactorData(JSONObject jsonObject) {
+        JSONArray dataList = new JSONArray();
+        JSONArray sentenceList = jsonObject.getJSONArray("sentenceList");
+        JSONArray knowledgeList = jsonObject.getJSONArray("knowledgeList");
+        int step = 5;
+        int sentSize = sentenceList.size();
+        int pageSize = (sentSize - 1) / step;
+        for (int i = 0; i <= pageSize; i++) {
+            JSONObject newData = new JSONObject();
+            JSONArray subSentenceList = new JSONArray();
+            JSONArray subKnowledgeList = new JSONArray();
+            List sentenceIdList = new ArrayList<>(8);;
+            for (int index = 0; index < step; index++) {
+                if(i * step + index >= sentSize){
+                    break;
+                }
+                JSONObject sentence = sentenceList.getJSONObject(i * step + index);
+                subSentenceList.add(sentence);
+                sentenceIdList.add(sentence.get("id"));
+            }
+            int size = knowledgeList.size();
+            for(int index = 0; index < size; index++){
+                JSONObject knowledge = knowledgeList.getJSONObject(index);
+                if(sentenceIdList.contains(knowledge.get("sentenceId"))){
+                    subKnowledgeList.add(knowledge);
+                }
+            }
+            newData.put("sentenceList",subSentenceList);
+            newData.put("knowledgeList",subKnowledgeList);
+            dataList.add(newData);
+        }
+        JSONObject returnValue = new JSONObject();
+        returnValue.put("dataList",dataList);
+        returnValue.put("total",pageSize+1);
+        returnValue.put("step",step);
+        return returnValue;
+    }
 }

+ 20 - 2
kgraph-graph/src/main/java/com/kgraph/graph/suport/service/impl/ExtractKnowledgeSubTaskServiceImpl.java

@@ -206,10 +206,10 @@ public class ExtractKnowledgeSubTaskServiceImpl implements IExtractKnowledgeSubT
 
             // save resultJson
 //            String resultPath = sysDictDataService.getValueByTypeAndValue("kg_algorithm", "resultPath");
-            String result = extractResult.getResultData();
+            String result = refactorResult(extractResult.getResultData());
 
             try {
-                FileUtils.writeJsonToFile(result,extractResult.getResult());
+                FileUtils.writeJsonToFile(result, extractResult.getResult());
             } catch (Exception e) {
                 logger.error("抽取结果保存失败");
             }
@@ -232,6 +232,24 @@ public class ExtractKnowledgeSubTaskServiceImpl implements IExtractKnowledgeSubT
 
     }
 
+    private String refactorResult(String resultData) {
+        JSONArray jsonArray = JSONArray.parseArray(resultData);
+        JSONObject jsonObject = new JSONObject();
+        JSONArray sentenceList = new JSONArray();
+        JSONArray knowledgeList = new JSONArray();
+        int size = jsonArray.size();
+        if(size>0){
+            for(int i=0; i < size; i++){
+                JSONObject item = jsonArray.getJSONObject(i);
+                sentenceList.addAll(item.getJSONArray("sentenceList"));
+                knowledgeList.addAll(item.getJSONArray("knowledgeList"));
+            }
+        }
+        jsonObject.put("sentenceList",sentenceList);
+        jsonObject.put("knowledgeList",knowledgeList);
+        return jsonObject.toString();
+    }
+
     @Override
     public void updateTaskStatusBySubTaskId(Long subTaskId) {
         extractKnowledgeSubTaskMapper.updateTaskStatusBySubTaskId(subTaskId);