yuchengwei пре 1 месец
родитељ
комит
6a70bfdee6

+ 5 - 3
src/main/java/com/qizhen/healsphere/util/FileCommonUtils.java

@@ -57,8 +57,10 @@ public class FileCommonUtils {
     }
 
     public static void main(String[] args) {
-        String filePath = "E:\\project\\demo\\example.txt";
-        String fileNameWithoutExtension = getFileNameWithoutExtension(filePath);
-        System.out.println("File Name Without Extension: " + fileNameWithoutExtension);
+//        String filePath = "/Users/ycw/work/邵逸夫急诊科审核通过数据/files/《急诊与灾难医学(第4版)》";
+//        String fileNameWithoutExtension = getFileNameWithoutExtension(filePath);
+//        System.out.println("File Name Without Extension: " + fileNameWithoutExtension);
+        String s = "/《急诊与灾难医学(第4版)》/第九章少尿与无尿/第一节概述/三、诊断/三、诊断_split_0.txt";
+        System.out.println(s.replace("/","\\"));
     }
 }

+ 22 - 10
src/test/java/com/qizhen/healsphere/DataExtractTest.java

@@ -44,15 +44,27 @@ public class DataExtractTest {
     //public static final String appId = "9da5da63-29a1-4c59-ad0b-f1f3c737924a";
 
     //public static final String inputDirectoryPath = "E:\\project\\vscode\\《急诊与灾难医学(第4版)》\\第二章急性发热\\"; // 修改为目录路径
-    public static final String inputDirectoryPath = "E:\\project\\vscode\\《急诊与灾难医学(第4版)》\\第五章心悸与心律失常\\"; // 修改为目录路径
+    static List<String> fileNameList = new ArrayList<>();
+    static {
+        fileNameList.add("第十二章环境及理化因素损伤");
+        fileNameList.add("第十三章急性感染");
+        fileNameList.add("第十四章多器官功能障碍综合征");
+        fileNameList.add("第十五章心肺脑复苏");
+    }
+    public static String inputDirectoryPath = "/Users/ycw/work/邵逸夫急诊科审核通过数据/files/《急诊与灾难医学(第4版)》/"; // 修改为目录路径
     public static void main(String[] args) {
-
         //String inputDirectoryPath = "E:\\project\\vscode\\《急诊与灾难医学(第4版)》\\"; // 修改为目录路径
-        File inputDirectory = new File(inputDirectoryPath);
-        // 无子目录时处理txt文件
-        workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
-        sheet = workbook.createSheet();
-        traverse(inputDirectory);
+        String baseInputDirectoryPath = inputDirectoryPath;
+        for (String fileName : fileNameList) {
+            inputDirectoryPath = baseInputDirectoryPath + fileName;
+            File inputDirectory = new File(inputDirectoryPath);
+            // 无子目录时处理txt文件
+            workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
+            sheet = workbook.createSheet();
+            rows = 0; // 重置行号
+            traverse(inputDirectory);
+        }
+
     }
     static HSSFWorkbook workbook;
     static  HSSFSheet sheet = null;
@@ -85,7 +97,7 @@ public class DataExtractTest {
                 if (file.getName().toLowerCase().endsWith(".txt") && file.getName().toLowerCase().contains("_split_")) {
                     //System.out.println("发现文本文件: " + file.getName());
                     String inputFilePath = file.getAbsolutePath();
-                    inputFilePath = inputFilePath.replace("E:\\project\\vscode", "");
+                    inputFilePath = inputFilePath.replace("/Users/ycw/work/邵逸夫急诊科审核通过数据/files", "");
                     StringBuilder sb = new StringBuilder();
                     try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                         String line;
@@ -102,7 +114,7 @@ public class DataExtractTest {
                         HSSFRow row = sheet.createRow(rows++);
                         row.createCell(0).setCellValue(response == null ? "" : JSONObject.parseObject(response).getString("answer"));
                         row.createCell(1).setCellValue(sb.toString());
-                        row.createCell(2).setCellValue(inputFilePath);
+                        row.createCell(2).setCellValue(inputFilePath.replace("/","\\"));
                         save(FileCommonUtils.getFileNameWithoutExtension(inputDirectoryPath));
                     } catch (IOException e) {
                         e.printStackTrace();
@@ -114,7 +126,7 @@ public class DataExtractTest {
 
     private static synchronized void save(String fileName) {
         try {
-            fileName="C:\\Users\\17664\\Desktop\\"+fileName+".xlsx";
+            fileName="/Users/ycw/work/邵逸夫急诊科审核通过数据/"+fileName+".xlsx";
             //文档输出
             FileOutputStream out = new FileOutputStream(new File(fileName));
             workbook.write(out);

Разлика између датотеке није приказан због своје велике величине
+ 207 - 0
src/test/java/com/qizhen/healsphere/DiagbotDisease2Neo4jTest.java


+ 131 - 0
src/test/java/com/qizhen/healsphere/Tree2Neo4jTest.java

@@ -0,0 +1,131 @@
+package com.qizhen.healsphere;
+
+import cn.hutool.core.date.DateUtil;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.qizhen.healsphere.config.Neo4jUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.neo4j.driver.v1.Session;
+import org.neo4j.driver.v1.StatementResult;
+import org.neo4j.driver.v1.Values;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.File;
+import java.util.Date;
+
+@RunWith(SpringRunner.class)
+@ComponentScan(basePackages = {"com.qizhen.healsphere"})
+@SpringBootTest
+public class Tree2Neo4jTest {
+
+    @Autowired
+    private Neo4jUtil neo4jUtil;
+
+    private static final String JSON_FOLDER = "/Users/ycw/dev/json_file";
+
+    @Test
+    public void mergeData() {
+        try {
+            processAllJsonFiles();
+            System.out.println("数据处理完成");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    public void processAllJsonFiles() {
+        File folder = new File(JSON_FOLDER);
+        File[] files = folder.listFiles((dir, name) -> name.endsWith(".json"));
+        if (files != null) {
+            try (Session session = neo4jUtil.getSession()) {
+                for (File file : files) {
+                    processJsonFile(file, session);
+                }
+            }
+        }
+    }
+
+    private void processJsonFile(File jsonFile, Session session) {
+        try {
+            ObjectMapper mapper = new ObjectMapper();
+            TreeNode root = mapper.readValue(jsonFile, TreeNode.class);
+            processNode(root, null, session, 1);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void processNode(TreeNode node, Long parentId, Session session, int level) {
+        System.out.println("=======当前时间:" + DateUtil.formatDateTime(new Date()) + ", 处理节点名称:" + node.getName() + "=======");
+        // 根据层级设置不同的标签和属性
+        String label = determineLabel(level);
+
+        //先查询节点名称是否存在
+        String checkNodeQuery = String.format("MATCH (n:%s {name: $name}) RETURN n", label);
+        StatementResult result = session.run(checkNodeQuery, Values.parameters("name", node.getName()));
+        Long nodeId = null;
+        if (result.hasNext()) {
+            nodeId = result.next().get("n").asNode().id();
+        }else{
+            String createNodeQuery = String.format("MERGE (n:%s {source_id: $id, name: $name,source_type: 'diagbotcloud'}) RETURN id(n)", label);
+            session.run(createNodeQuery, Values.parameters(
+                    "id", node.getId(),
+                    "name", node.getName()
+            ));
+        }
+
+        // 如果有父节点,创建关系
+        if (parentId != null) {
+            String relationshipType = determineRelationshipType(level);
+            if (nodeId == null){
+                String createRelationQuery =
+                        String.format("MATCH (parent {source_id: $parentId}), (child {source_id: $childId}) " +
+                                "MERGE (parent)-[:%s]->(child)", relationshipType);
+                session.run(createRelationQuery, Values.parameters(
+                        "parentId", parentId,
+                        "childId", node.getId()
+                ));
+            }else {
+                String createRelationQuery =
+                        String.format("MATCH (parent {source_id: $parentId}), (child) WHERE id(child)= $childId " +
+                                "MERGE (parent)-[:%s]->(child)", relationshipType);
+                session.run(createRelationQuery, Values.parameters(
+                        "parentId", parentId,
+                        "childId", nodeId
+                ));
+            }
+
+
+        }
+
+        // 递归处理子节点
+        if (node.getNextTree() != null) {
+            for (TreeNode child : node.getNextTree()) {
+                processNode(child, node.getId(), session, level + 1);
+            }
+        }
+    }
+
+    private String determineLabel(int level) {
+        if (level <= 2) {
+            return "药品类别";
+        } else {
+            return "药品";
+        }
+    }
+
+    private String determineRelationshipType(int level) {
+        if (level <= 2) {
+            return "药品类别相关子类";
+        } else {
+            return "药品类别相关子类药品";
+        }
+    }
+
+
+
+}

+ 35 - 0
src/test/java/com/qizhen/healsphere/TreeNode.java

@@ -0,0 +1,35 @@
+package com.qizhen.healsphere;
+
+import java.util.List;
+
+public class TreeNode {
+    private long id;
+    private String name;
+    private List<TreeNode> nextTree;
+
+    public TreeNode() {}
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<TreeNode> getNextTree() {
+        return nextTree;
+    }
+
+    public void setNextTree(List<TreeNode> nextTree) {
+        this.nextTree = nextTree;
+    }
+}