SGTY 6 months ago
parent
commit
5e551a3710

+ 165 - 0
src/test/java/com/qizhen/healsphere/DrugExcelTest.java

@@ -0,0 +1,165 @@
+package com.qizhen.healsphere;
+
+import com.qizhen.healsphere.common.ai.BaidubceUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+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.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.*;
+
+@RunWith(SpringRunner.class)
+@ComponentScan(basePackages = {"com.qizhen.healsphere.model", "com.qizhen.healsphere.repository"})
+@SpringBootTest
+public class DrugExcelTest {
+/*    @Autowired
+    RelationshipService relationshipService;
+    @Autowired
+    EntityService entityService;*/
+    private static int maxCount= 20;
+    private static String drugExcelPath = "C:\\Users\\17664\\Desktop\\药品说明书-部分字段-部分内容.xlsx";
+    static HSSFWorkbook workbook;
+    public static void main(String[] args) throws Exception {
+        workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
+        String accessToken = BaidubceUtil.getAccessToken();
+        InputStream drugFis = new FileInputStream(drugExcelPath);
+        Workbook drugWorkbook = new XSSFWorkbook(drugFis);
+        Sheet drugSheet = drugWorkbook.getSheetAt(2);
+        String propertys = "用法用量";
+        int curCount = 0;
+        String[] split = propertys.split(",");
+        //for (int rowNum = 1; rowNum <= drugSheet.getLastRowNum(); rowNum++) {
+        for(String property:split) {
+            HSSFSheet sheet = workbook.createSheet(property);
+            for (int rowNum = 1; rowNum <= drugSheet.getLastRowNum(); rowNum++) {
+                try {
+                    Row row = drugSheet.getRow(rowNum);
+                    String name = row.getCell(2).getStringCellValue();
+                    String zhaiyao = row.getCell(7).getStringCellValue();
+                    if (StringUtils.isEmpty(name) || StringUtils.isEmpty(zhaiyao)) {
+                        continue;
+                    }
+
+                    String value = getValues(zhaiyao, property, accessToken);
+
+                    HSSFRow writeRow = sheet.createRow(curCount);
+                    writeRow.createCell(0).setCellValue(name == null ? "" : name);
+                    writeRow.createCell(1).setCellValue(value == null ? "" : value);
+                    writeRow.createCell(2).setCellValue(zhaiyao == null ? "" : zhaiyao);
+                    curCount++;
+                    if (maxCount > 0) {
+                        if (curCount >= maxCount) {
+                            break;
+                        }
+                    }
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+            save(property);
+            curCount = 0;
+        }
+    }
+
+    private static String getValues(String zhaiyao, String property, String accessToken) {
+        if (StringUtils.isEmpty(zhaiyao)) {
+            return "";
+        }
+        String format = "你是医药领域的专家。你将在指定的文本中抽取其中“用法用量”。\n" +
+                "\n" +
+                "#要求\n" +
+                "1、“用法用量”输出格式参考:#人群,#用法,#用量,#次数\n" +
+                "\n" +
+                "#示例1\n" +
+                "文本:\n" +
+                "【适应症】 \n" +
+                "主要用于敏感的革兰阳性菌所致的皮肤、软组织感染、胆道感染、呼吸道感染、链球菌咽峡炎、猩红热、白喉、军团菌病、百日咳等,以及淋病、非淋病性尿道炎、痤疮等。 \n" +
+                "【用法用量】\n" +
+                "静脉注射或静脉滴注。先用少量氯化钠注射液或葡萄糖注射液溶解,然后再稀释到需要的浓度。\n" +
+                "成人,一次20万-40万单位(1-2支),一日2-3次。\n" +
+                "小儿,一日20万单位(1支)或酌减,分2-3次给药。\n" +
+                "静脉注射时,浓度不得大于2%即20万单位,将1次用量溶于10-20ml氯化钠注射液或葡萄糖注射液中;缓慢推注(急速静注,有时出现恶心、腹痛、血压下降、休克症状等),注射速度应不少于5分钟,以免产生静脉不适。 \n" +
+                "【不良反应】 \n" +
+                "本品的胃肠道反应发生率较红霉素低,偶见皮疹和瘙痒。\n" +
+                "\n" +
+                "\n" +
+                "输出:[\"成人,静脉滴注,一次20万-40万单位(1-2支),一日2-3次\",\"小儿,静脉滴注,一日20万单位(1支),分2-3次给药\"]\n" +
+                "\n" +
+                "#示例2\n" +
+                "文本:\n" +
+                "【适应症】\n" +
+                "本品联合化疗应用于晚期乳腺癌、非小细胞肺癌患者的治疗。\n" +
+                "【用法用量】\n" +
+                "与化疗联合应用,每次300ml,每天1次,于化疗前3-7天开始给药,再与化疗联合治疗2-3周期,化疗药使用的当天停用本品。Ⅲ期临床中与本品联合的化疗方案和种类详见[临床试验]项中表2。\n" +
+                "用药方法:将尿多酸肽注射液与氯化钠注射液或5%葡萄糖注射液,按1∶1稀释后静脉滴注(建议采用锁骨下静脉滴注或者使用PICC管),滴速以原药100ml/小时为宜。\n" +
+                "【不良反应】\n" +
+                "临床试验中观察到的不良反应主要为胃肠道反应,表现为恶心、呕吐、腹泻、纳差、腹胀等。其他不良反应表现为疼痛、口干、胸部不适、颜面潮红、心悸等,多数为Ⅰ-Ⅱ度,可自行恢复。可给予对症处理。外周静脉给药时可能有静脉刺激症状,产生静脉炎,采用锁骨下静脉滴注时静脉刺激明显减轻。\n" +
+                "本品Ⅲ期临床研究试验组(本品与化疗联合)可评价毒副反应301例,对照组(仅化疗)可评价毒副反应153例。主要不良反应情况见表1。\n" +
+                "\n" +
+                "\n" +
+                "输出:[\"静脉滴注,每次300ml,每天1次\"]\n" +
+                "\n" +
+                "#示例3\n" +
+                "【适应症】\n" +
+                "1、急慢性风湿性、急慢性关节炎、急慢性强直性脊椎炎、骨关节炎。 \n" +
+                "2、肩周炎、滑囊炎、肌腱炎及腱鞘炎。\n" +
+                "3、腰背痛、扭伤、劳损及其他软组织损伤。\n" +
+                "4、急性痛风。 \n" +
+                "5、痛经或附件炎、牙痛和术后疼痛。 \n" +
+                "6、创伤后的疼痛与炎症,如扭伤、肌肉拉伤等。 \n" +
+                "7、耳鼻喉严重的感染性疼痛和炎症(如扁桃体炎、耳炎、鼻窦炎等),应同时使刚抗感染药物。\n" +
+                "【用法用量】\n" +
+                "口服:一日1次,一次0.1g,或遵医嘱。晚餐后用温开水送服,需整片吞服,不要弄碎或咀嚼。\n" +
+                "【不良反应】\n" +
+                "1、可引起头痛及腹痛、便秘、腹泻、胃烧灼感、恶心、消化不良等胃肠道反应。\n" +
+                "\n" +
+                "\n" +
+                "输出:[\"一次0.1g,一日1次\"]\n" +
+                "\n" +
+                "2、没有可抽取的“用法用量”,则返回空json数组。\n" +
+                "\n" +
+                "本次抽取的文本如下:\n\n";
+
+        String zhiling = format + zhaiyao;
+        System.out.println(zhiling);
+        String chatResponse = BaidubceUtil.getChatResponse(zhiling, accessToken);
+        chatResponse = filte(chatResponse);
+        System.out.println(chatResponse);
+        return chatResponse;
+    }
+
+    private static synchronized void save(String fileName) {
+        try {
+            fileName="C:\\Users\\17664\\Desktop\\"+fileName+System.currentTimeMillis()+".xlsx";
+            //文档输出
+            FileOutputStream out = new FileOutputStream(new File(fileName));
+            workbook.write(out);
+            out.close();
+            System.out.println(fileName + "存储完毕");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static String filte(String chatResponse) {
+        if (chatResponse.startsWith("```json")) {
+            chatResponse = chatResponse.substring(7);
+        }
+        if (chatResponse.endsWith("```")) {
+            chatResponse = chatResponse.substring(0, chatResponse.length() - 3);
+        }
+        return chatResponse;
+    }
+
+
+}
+

+ 6 - 7
src/test/java/com/qizhen/healsphere/DrugTest10.java

@@ -139,19 +139,18 @@ public class DrugTest10 {
                 writeRow.createCell(0).setCellValue(name);
                 writeRow.createCell(1).setCellValue(value);
                 writeRow.createCell(2).setCellValue(zhaiyao == null ? "" : zhaiyao);
-                curCount++;
-                if (maxCount > 0) {
-                    if (curCount >= maxCount) {
-                        break;
-                    }
-                }
                 Knowlege knowlege = new Knowlege();
                 knowlege.setEntity(name);
                 knowlege.setProperty(property);
                 knowlege.setValue(value);
 
                 result.add(knowlege);
-
+                curCount++;
+                if (maxCount > 0) {
+                    if (curCount >= maxCount) {
+                        break;
+                    }
+                }
             } catch (Exception e) {
                 e.printStackTrace();
             }

+ 4 - 1
src/test/java/com/qizhen/healsphere/DrugYfylTest.java

@@ -16,6 +16,9 @@ import org.springframework.test.context.junit4.SpringRunner;
 
 import java.io.*;
 
+/**
+ * 用法用量
+ */
 @RunWith(SpringRunner.class)
 @ComponentScan(basePackages = {"com.qizhen.healsphere.model", "com.qizhen.healsphere.repository"})
 @SpringBootTest
@@ -54,8 +57,8 @@ public class DrugYfylTest {
                     writeRow.createCell(0).setCellValue(name == null ? "" : name);
                     writeRow.createCell(1).setCellValue(value == null ? "" : value);
                     writeRow.createCell(2).setCellValue(zhaiyao == null ? "" : zhaiyao);
+                    curCount++;
                     if (maxCount > 0) {
-                        curCount++;
                         if (curCount >= maxCount) {
                             break;
                         }

File diff suppressed because it is too large
+ 5163 - 0
src/test/java/com/qizhen/healsphere/JianyanKfTest.java


+ 265 - 0
src/test/java/com/qizhen/healsphere/JianyanTest10.java

@@ -0,0 +1,265 @@
+package com.qizhen.healsphere;
+
+import com.alibaba.fastjson.JSONArray;
+import com.qizhen.healsphere.common.ai.BaidubceUtil;
+import com.qizhen.healsphere.common.ai.Knowlege;
+import com.qizhen.healsphere.repository.neo4j.entity.BaseEntity;
+import com.qizhen.healsphere.service.EntityService;
+import com.qizhen.healsphere.service.RelationshipService;
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
+import com.qizhen.healsphere.web.vo.RelationshipVO;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+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 org.springframework.util.CollectionUtils;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+@RunWith(SpringRunner.class)
+@ComponentScan(basePackages = {"com.qizhen.healsphere.model", "com.qizhen.healsphere.repository"})
+@SpringBootTest
+public class JianyanTest10 {
+    @Autowired
+    RelationshipService relationshipService;
+    @Autowired
+    EntityService entityService;
+    private static int maxCount= 10;
+    private static String directoryPath  = "C:\\Users\\17664\\Desktop\\邵逸夫医院爬取数据\\检验";
+    static HSSFWorkbook workbook;
+    static String startLabel = "检验";
+    @Test
+    public void writeNeo4j() throws Exception {
+        String propertys = "别名" +
+                ",英文名称" +
+                ",参考区间";
+        workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
+        String accessToken = BaidubceUtil.getAccessToken();
+        String[] split = propertys.split(",");
+        for(String property:split) {
+            HSSFSheet sheet = workbook.createSheet(property);
+            List<Knowlege> knowleges = saveExecl(property, accessToken, sheet);
+
+            for (Knowlege temp:knowleges) {
+                String value = temp.getValue();
+                if(StringUtils.isBlank(value)){
+                    continue;
+                }
+                try{
+                    JSONArray jsonArray = JSONArray.parseArray(value);
+                    BaseEntity startEntity = createNoExists(startLabel, temp.getEntity());
+                    long startId = startEntity.getId();
+                    List<RelationshipVO> relationshipList = new ArrayList<>();
+                    for(int i=0;i<jsonArray.size();i++){
+                        String name = jsonArray.getString(i);
+                        if(StringUtils.isEmpty(name)){
+                            continue;
+                        }
+                        BaseEntity endEntity =  createNoExists(startLabel+property, name);
+                        Long endId = endEntity.getId();
+                        RelationshipVO relationshipVO = new RelationshipVO();
+                        relationshipVO.setStartId(startId);
+                        relationshipVO.setEndId(endId);
+                        relationshipVO.setStartLabel(startLabel);
+                        relationshipVO.setEndLabel(startLabel+property);
+                        relationshipVO.setRelationshipType(startLabel+"相关"+property);
+                        relationshipList.add(relationshipVO);
+                    }
+                    if(!CollectionUtils.isEmpty(relationshipList)) {
+                        System.out.println(relationshipService.createRelationship(relationshipList));
+                    }
+                }catch (Exception e){
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    private BaseEntity createNoExists(String labelName, String name) {
+        BaseEntity nodeByName = entityService.findNodeByName(labelName, name);
+        if(Objects.nonNull(nodeByName)){//节点不存在
+            return nodeByName;
+        }
+        CreateEntityVO createEntity = new CreateEntityVO();
+        createEntity.setName(name);
+        createEntity.setLabel(labelName);
+        return entityService.create(createEntity);
+    }
+
+    public static void main(String[] args) throws Exception {
+        String propertys = /*"别名" +
+                ",英文名称" +
+                ",参考区间" +
+                ",升高的临床意义" +*/
+                "降低的临床意义" +
+                ",危急高值" +
+                ",危急低值" +
+                ",适用性别" +
+                ",是否空腹";
+        workbook = new HSSFWorkbook();//这里也可以设置sheet的Name
+        String accessToken = BaidubceUtil.getAccessToken();
+        String[] split = propertys.split(",");
+        for(String property:split) {
+            HSSFSheet sheet = workbook.createSheet(property);
+            saveExecl(property,accessToken, sheet);
+        }
+    }
+
+    public static String readTxtFile(File txtFile) {
+        StringBuilder content = new StringBuilder();
+        try (BufferedReader reader = new BufferedReader(new FileReader(txtFile))) {
+            String line;
+            // 逐行读取文件内容
+            while ((line = reader.readLine()) != null) {
+                // 输出读取到的每一行内容
+                content.append(line+"\n");
+            }
+        } catch (IOException e) {
+            System.out.println("读取文件时发生错误: " + e.getMessage());
+        }
+        return content.toString();
+    }
+
+    private static List<Knowlege> saveExecl(String property, String accessToken, HSSFSheet sheet) throws Exception {
+        int curCount = 0;
+        List<Knowlege> result = new ArrayList<>();
+        File directory = new File(directoryPath);
+        // 获取目录下的所有文件和子目录
+        File[] files = directory.listFiles();
+
+        // 检查files是否为null,避免NullPointerException
+        if (files != null) {
+            // 循环遍历目录下的每个文件和子目录
+            for (File file : files) {
+                // 检查文件是否是.txt文件
+                if (file.isFile() && file.getName().endsWith(".txt")) {
+                    // 读取.txt文件的内容
+                    String s = readTxtFile(file);
+                    String name = file.getName().split("_")[1].replace(".txt","");
+
+                    String value = getValues(s, property, accessToken);
+
+                    HSSFRow writeRow = sheet.createRow(curCount);
+                    name = (name == null ? "" : name);
+                    value = (value == null ? "" : value);
+                    writeRow.createCell(0).setCellValue(name);
+                    writeRow.createCell(1).setCellValue(value);
+                    writeRow.createCell(2).setCellValue(s == null ? "" : s);
+                    Knowlege knowlege = new Knowlege();
+                    knowlege.setEntity(name);
+                    knowlege.setProperty(property);
+                    knowlege.setValue(value);
+
+                    result.add(knowlege);
+                    curCount++;
+                    if (maxCount > 0) {
+                        if (curCount >= maxCount) {
+                            break;
+                        }
+                    }
+                }
+            }
+        } else {
+            System.out.println("指定的目录不存在或无法访问: " + directoryPath);
+        }
+        save(property);
+        return result;
+    }
+
+    private static String getValues(String zhaiyao, String property, String accessToken) {
+        if (StringUtils.isEmpty(zhaiyao)) {
+            return "";
+        }
+        String format = "你是医学检验领域的专家。你将在指定的文本中抽取其中“" + property + "”。\n" +
+                "\n" +
+                "#要求\n" +
+                "1、抽取的结果将以JSON数组的形式呈现。每个抽取的“" + property + "”高度简洁、高度概括,不要要描述性的文字,文字尽量保持在20个字符以内!\n" +
+                "\n" +
+                "#示例1\n" +
+                "以抽取“分期”为例\n" +
+                "文本:\n" +
+                "肱骨骨折如果是**肱骨头坏死**则有Cruess分期,包括I期、Ⅱ期、Ⅲ期、IV期、V期^[1]^。\n" +
+                "\n" +
+                "如果是肱骨近端骨折则有Neer分型和AO分型^[3]^。\n" +
+                "输出:[\"Cruess分期I期\",\"Cruess分期Ⅱ期\",\"Cruess分期Ⅲ期\",\"Cruess分期IV期\",\"Cruess分期V期\"]\n" +
+                "\n" +
+                "#示例2\n" +
+                "以抽取“英文名称”为例\n" +
+                "文本:\n" +
+                "通用名称:双氯芬酸钠缓释片\n" +
+                "商品名称:迪根\n" +
+                "英文名称:DiclofenacSodium Sustained Release Tablets\n" +
+                "汉语拼音:ShuanglvfensuannaHuanshiPian\n" +
+                "\n" +
+                "输出:[\"DiclofenacSodium Sustained Release Tablet\"]\n\n"+
+                "#示例3\n" +
+                "以抽取“参考区间”为例\n" +
+                "文本:\n" +
+                "\n" +
+                "参考区间:\n" +
+                " 1. \n" +
+                "成年男性HDL-C为1.16~1.42mmol/L(45~55mg/dl);女性为1.29~1.55mmol/L(50~60mg/dl)。正常人HDL-C约占TC的25%~30%。\n" +
+                "\n" +
+                " 2. 我国《中国成人血脂异常防治建议》提出的标准(2007)为: \n" +
+                "\n" +
+                " 理想范围:>1.04mmol/L(>40mg/dl)。 \n" +
+                "\n" +
+                " 升高:≥1.55mmol/L(60mg/dl)。 \n" +
+                "\n" +
+                " 降低:<1.04mmol/L(<40mg/dl)。 \n" +
+                "\n" +
+                " 3. NCEPATPⅢ提出的医学决定水平: \n" +
+                "\n" +
+                " <1.03mmol/L(40mg/dl)为降低,CHD发生风险增高。 \n" +
+                "\n" +
+                " ≥1.55mmol/L(60mg/dl),CHD发生风险降低。\n" +
+                "\n" +
+                "输出:[\"成年男性HDL-C为1.16~1.42mmol/L(45~55mg/dl)\",\"女性为1.29~1.55mmol/L(50~60mg/dl)\",\"正常人HDL-C约占TC的25%~30%\",\"理想范围:>1.04mmol/L(>40mg/dl)\",\"升高:≥1.55mmol/L(60mg/dl)\",\"降低:<1.04mmol/L(<40mg/dl)\",\"≥1.55mmol/L(60mg/dl),CHD发生风险降低\",\"≥1.55mmol/L(60mg/dl),CHD发生风险降低\"]\n\n"+
+
+                "2、没有可抽取的“" + property + "”,则返回空json数组。\n" +
+                "\n" +
+                "本次抽取的文本如下:\n\n";
+
+        String zhiling = format + zhaiyao;
+        System.out.println(zhiling);
+        String chatResponse = BaidubceUtil.getChatResponse(zhiling, accessToken);
+        chatResponse = filte(chatResponse);
+        System.out.println(chatResponse);
+        return chatResponse;
+    }
+
+    private static synchronized void save(String fileName) {
+        try {
+            fileName="C:\\Users\\17664\\Desktop\\"+fileName+System.currentTimeMillis()+".xlsx";
+            //文档输出
+            FileOutputStream out = new FileOutputStream(new File(fileName));
+            workbook.write(out);
+            out.close();
+            System.out.println(fileName + "存储完毕");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static String filte(String chatResponse) {
+        if (chatResponse.startsWith("```json")) {
+            chatResponse = chatResponse.substring(7);
+        }
+        if (chatResponse.endsWith("```")) {
+            chatResponse = chatResponse.substring(0, chatResponse.length() - 3);
+        }
+        return chatResponse;
+    }
+
+
+}
+

+ 1 - 1
src/test/java/com/qizhen/healsphere/MigrateDataTest.java

@@ -40,7 +40,7 @@ public class MigrateDataTest {
 
     @Test
     public void migrateData() {
-        String endLabels = "传播途径,传染源,分期,分类,别称,危险因素,多发季节,常见并发症,并发症,接触者检疫,是否传染病,治疗药物,潜伏期,病因,英文名称,诱因,辅助检查,隔离期,高危人群,高发地区";
+        String endLabels = "鉴别诊断,常见体征,手术治疗";
         String[] split = endLabels.split(",");
         String startLabel = "疾病";
         for(String endLabel:split) {